Skip to content

Commit af183f4

Browse files
committed
feat: add minecraft cluster api
1 parent 43a1afa commit af183f4

27 files changed

+1046
-105
lines changed

PROJECT

+8
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ resources:
1212
kind: MinecraftServer
1313
path: shulkermc.io/m/v2/api/v1alpha1
1414
version: v1alpha1
15+
- api:
16+
crdVersion: v1
17+
namespaced: true
18+
controller: true
19+
domain: shulkermc.io
20+
kind: MinecraftCluster
21+
path: shulkermc.io/m/v2/api/v1alpha1
22+
version: v1alpha1
1523
version: "3"
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
meta "k8s.io/apimachinery/pkg/api/meta"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
//+kubebuilder:object:root=true
25+
//+kubebuilder:subresource:status
26+
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.conditions[?(@.type==\"Ready\")].status"
27+
//+kubebuilder:printcolumn:name="Servers",type="number",JSONPath=".status.servers"
28+
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
29+
//+kubebuilder:resource:shortName={"smc"},categories=all
30+
31+
// MinecraftCluster is the Schema for the MinecraftCluster API
32+
type MinecraftCluster struct {
33+
metav1.TypeMeta `json:",inline"`
34+
metav1.ObjectMeta `json:"metadata,omitempty"`
35+
36+
Spec MinecraftClusterSpec `json:"spec,omitempty"`
37+
Status MinecraftClusterStatus `json:"status,omitempty"`
38+
}
39+
40+
// Defines the defired state of a MinecraftCluster. Most, to not
41+
// say all, fields configurable in a Minecraft Cluster can be
42+
// configured in this CRD.
43+
type MinecraftClusterSpec struct {
44+
// Short name to use internally to refer to this Minecraft Cluster.
45+
// Objects referring to this Minecraft Cluster will include this
46+
// short name as prefix in their own names.
47+
//+kubebuilder:validation:Required
48+
ShortName string `json:"shortName,omitempty"`
49+
}
50+
51+
type MinecraftClusterStatusCondition string
52+
53+
const (
54+
ClusterReadyCondition MinecraftClusterStatusCondition = "Ready"
55+
)
56+
57+
// MinecraftClusterStatus defines the observed state of MinecraftCluster
58+
type MinecraftClusterStatus struct {
59+
// Conditions represent the latest available observations of a
60+
// MinecraftCluster object.
61+
//+kubebuilder:validation:Required
62+
Conditions []metav1.Condition `json:"conditions"`
63+
64+
// Number of servers inside the server pool.
65+
//+kubebuilder:validation:Required
66+
Servers int `json:"servers"`
67+
68+
// Pool of Minecraft Servers linked to this Minecraft Cluster.
69+
//+kubebuilder:validation:Required
70+
ServerPool []MinecraftClusterStatusServerEntry `json:"serverPool"`
71+
}
72+
73+
type MinecraftClusterStatusServerEntry struct {
74+
// Name of the Minecraft Server.
75+
//+kubebuilder:validation:Required
76+
Name string `json:"name,omitempty"`
77+
78+
// IP of the Minecraft Server.
79+
//+kubebuilder:validation:Required
80+
Address string `json:"address,omitempty"`
81+
}
82+
83+
func (s *MinecraftClusterStatus) SetCondition(condition MinecraftClusterStatusCondition, status metav1.ConditionStatus, reason string, message string) {
84+
meta.SetStatusCondition(&s.Conditions, metav1.Condition{
85+
Type: string(condition),
86+
Status: status,
87+
Reason: reason,
88+
Message: message,
89+
})
90+
}
91+
92+
//+kubebuilder:object:root=true
93+
94+
// MinecraftClusterList contains a list of MinecraftCluster
95+
type MinecraftClusterList struct {
96+
metav1.TypeMeta `json:",inline"`
97+
metav1.ListMeta `json:"metadata,omitempty"`
98+
Items []MinecraftCluster `json:"items"`
99+
}
100+
101+
type MinecraftClusterRef struct {
102+
// Name of the Minecraft Cluster.
103+
//+kubebuilder:validation:Required
104+
Name string `json:"name,omitempty"`
105+
}
106+
107+
func init() {
108+
SchemeBuilder.Register(&MinecraftCluster{}, &MinecraftClusterList{})
109+
}

api/v1alpha1/minecraftserver_types.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ package v1alpha1
1818

1919
import (
2020
corev1 "k8s.io/api/core/v1"
21+
meta "k8s.io/apimachinery/pkg/api/meta"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
)
2324

2425
//+kubebuilder:object:root=true
2526
//+kubebuilder:subresource:status
27+
//+kubebuilder:printcolumn:name="Cluster Name",type="string",JSONPath=".spec.minecraftClusterRef.name"
28+
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.conditions[?(@.type==\"Ready\")].status"
29+
//+kubebuilder:printcolumn:name="Address",type="boolean",JSONPath=".status.address"
2630
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
27-
//+kubebuilder:resource:shortName={"smc"},categories=all
31+
//+kubebuilder:resource:shortName={"sms"},categories=all
32+
2833
// MinecraftServer is the Schema for the MinecraftServer API.
2934
type MinecraftServer struct {
3035
metav1.TypeMeta `json:",inline"`
@@ -38,6 +43,10 @@ type MinecraftServer struct {
3843
// say all, fields configurable in a Minecraft Server can be
3944
// configured in this CRD.
4045
type MinecraftServerSpec struct {
46+
// Reference to a Minecraft Cluster. Adding this will enroll
47+
// this Minecraft Server to be part of a Minecraft Cluster.
48+
ClusterRef *MinecraftClusterRef `json:"minecraftClusterRef"`
49+
4150
//+kubebuilder:validation:Required
4251
Version MinecraftServerVersionSpec `json:"version"`
4352

@@ -88,7 +97,7 @@ type MinecraftServerSpec struct {
8897
Affinity *corev1.Affinity `json:"affinity,omitempty"`
8998
}
9099

91-
// +kubebuilder:validation:Enum=Vanilla;Forge;Fabric;Spigot;Paper;Airplane;Pufferfish;Purpur;Magma;Mohist;Catserver;Canyon;SpongeVanilla;Limbo;Crucible
100+
//+kubebuilder:validation:Enum=Vanilla;Forge;Fabric;Spigot;Paper;Airplane;Pufferfish;Purpur;Magma;Mohist;Catserver;Canyon;SpongeVanilla;Limbo;Crucible
92101
type MinecraftServerVersionChannel string
93102

94103
const (
@@ -126,6 +135,7 @@ type MinecraftServerVersionSpec struct {
126135

127136
type MinecraftServerWorldSpec struct {
128137
// URL to a downloable world.
138+
//+kubebuilder:validation:Required
129139
Url string `json:"url,omitempty"`
130140

131141
// Whether to allow the Minecraft Server to generate a Nether world
@@ -196,11 +206,32 @@ type MinecraftServerPodProbeSpec struct {
196206
InitialDelaySeconds int32 `json:"initialDelaySeconds,omitempty"`
197207
}
198208

209+
//+kubebuilder:validation:Enum=Ready;Addressable
210+
type MinecraftServerStatusCondition string
211+
212+
const (
213+
ServerReadyCondition MinecraftServerStatusCondition = "Ready"
214+
ServerAddressableCondition MinecraftServerStatusCondition = "Addressable"
215+
)
216+
199217
// MinecraftServerStatus defines the observed state of MinecraftServer
200218
type MinecraftServerStatus struct {
201219
// Conditions represent the latest available observations of a
202220
// MinecraftServer object.
221+
//+kubebuilder:validation:Required
203222
Conditions []metav1.Condition `json:"conditions"`
223+
224+
// Address of the Minecraft Server pod.
225+
Address string `json:"address,omitempty"`
226+
}
227+
228+
func (s *MinecraftServerStatus) SetCondition(condition MinecraftServerStatusCondition, status metav1.ConditionStatus, reason string, message string) {
229+
meta.SetStatusCondition(&s.Conditions, metav1.Condition{
230+
Type: string(condition),
231+
Status: status,
232+
Reason: reason,
233+
Message: message,
234+
})
204235
}
205236

206237
//+kubebuilder:object:root=true

api/v1alpha1/zz_generated.deepcopy.go

+142-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)