Skip to content

Commit

Permalink
feat: add minecraft cluster api
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Mar 4, 2022
1 parent 43a1afa commit af183f4
Show file tree
Hide file tree
Showing 27 changed files with 1,046 additions and 105 deletions.
8 changes: 8 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ resources:
kind: MinecraftServer
path: shulkermc.io/m/v2/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: shulkermc.io
kind: MinecraftCluster
path: shulkermc.io/m/v2/api/v1alpha1
version: v1alpha1
version: "3"
109 changes: 109 additions & 0 deletions api/v1alpha1/minecraftcluster_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.conditions[?(@.type==\"Ready\")].status"
//+kubebuilder:printcolumn:name="Servers",type="number",JSONPath=".status.servers"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+kubebuilder:resource:shortName={"smc"},categories=all

// MinecraftCluster is the Schema for the MinecraftCluster API
type MinecraftCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec MinecraftClusterSpec `json:"spec,omitempty"`
Status MinecraftClusterStatus `json:"status,omitempty"`
}

// Defines the defired state of a MinecraftCluster. Most, to not
// say all, fields configurable in a Minecraft Cluster can be
// configured in this CRD.
type MinecraftClusterSpec struct {
// Short name to use internally to refer to this Minecraft Cluster.
// Objects referring to this Minecraft Cluster will include this
// short name as prefix in their own names.
//+kubebuilder:validation:Required
ShortName string `json:"shortName,omitempty"`
}

type MinecraftClusterStatusCondition string

const (
ClusterReadyCondition MinecraftClusterStatusCondition = "Ready"
)

// MinecraftClusterStatus defines the observed state of MinecraftCluster
type MinecraftClusterStatus struct {
// Conditions represent the latest available observations of a
// MinecraftCluster object.
//+kubebuilder:validation:Required
Conditions []metav1.Condition `json:"conditions"`

// Number of servers inside the server pool.
//+kubebuilder:validation:Required
Servers int `json:"servers"`

// Pool of Minecraft Servers linked to this Minecraft Cluster.
//+kubebuilder:validation:Required
ServerPool []MinecraftClusterStatusServerEntry `json:"serverPool"`
}

type MinecraftClusterStatusServerEntry struct {
// Name of the Minecraft Server.
//+kubebuilder:validation:Required
Name string `json:"name,omitempty"`

// IP of the Minecraft Server.
//+kubebuilder:validation:Required
Address string `json:"address,omitempty"`
}

func (s *MinecraftClusterStatus) SetCondition(condition MinecraftClusterStatusCondition, status metav1.ConditionStatus, reason string, message string) {
meta.SetStatusCondition(&s.Conditions, metav1.Condition{
Type: string(condition),
Status: status,
Reason: reason,
Message: message,
})
}

//+kubebuilder:object:root=true

// MinecraftClusterList contains a list of MinecraftCluster
type MinecraftClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MinecraftCluster `json:"items"`
}

type MinecraftClusterRef struct {
// Name of the Minecraft Cluster.
//+kubebuilder:validation:Required
Name string `json:"name,omitempty"`
}

func init() {
SchemeBuilder.Register(&MinecraftCluster{}, &MinecraftClusterList{})
}
35 changes: 33 additions & 2 deletions api/v1alpha1/minecraftserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Cluster Name",type="string",JSONPath=".spec.minecraftClusterRef.name"
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.conditions[?(@.type==\"Ready\")].status"
//+kubebuilder:printcolumn:name="Address",type="boolean",JSONPath=".status.address"
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
//+kubebuilder:resource:shortName={"smc"},categories=all
//+kubebuilder:resource:shortName={"sms"},categories=all

// MinecraftServer is the Schema for the MinecraftServer API.
type MinecraftServer struct {
metav1.TypeMeta `json:",inline"`
Expand All @@ -38,6 +43,10 @@ type MinecraftServer struct {
// say all, fields configurable in a Minecraft Server can be
// configured in this CRD.
type MinecraftServerSpec struct {
// Reference to a Minecraft Cluster. Adding this will enroll
// this Minecraft Server to be part of a Minecraft Cluster.
ClusterRef *MinecraftClusterRef `json:"minecraftClusterRef"`

//+kubebuilder:validation:Required
Version MinecraftServerVersionSpec `json:"version"`

Expand Down Expand Up @@ -88,7 +97,7 @@ type MinecraftServerSpec struct {
Affinity *corev1.Affinity `json:"affinity,omitempty"`
}

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

const (
Expand Down Expand Up @@ -126,6 +135,7 @@ type MinecraftServerVersionSpec struct {

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

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

//+kubebuilder:validation:Enum=Ready;Addressable
type MinecraftServerStatusCondition string

const (
ServerReadyCondition MinecraftServerStatusCondition = "Ready"
ServerAddressableCondition MinecraftServerStatusCondition = "Addressable"
)

// MinecraftServerStatus defines the observed state of MinecraftServer
type MinecraftServerStatus struct {
// Conditions represent the latest available observations of a
// MinecraftServer object.
//+kubebuilder:validation:Required
Conditions []metav1.Condition `json:"conditions"`

// Address of the Minecraft Server pod.
Address string `json:"address,omitempty"`
}

func (s *MinecraftServerStatus) SetCondition(condition MinecraftServerStatusCondition, status metav1.ConditionStatus, reason string, message string) {
meta.SetStatusCondition(&s.Conditions, metav1.Condition{
Type: string(condition),
Status: status,
Reason: reason,
Message: message,
})
}

//+kubebuilder:object:root=true
Expand Down
148 changes: 142 additions & 6 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit af183f4

Please sign in to comment.