Skip to content

Commit

Permalink
refactor(shulker-crds): migrate to fabric8 kubernetes api
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Mar 11, 2022
1 parent c159ad5 commit cc649b0
Show file tree
Hide file tree
Showing 89 changed files with 220 additions and 12,715 deletions.
12 changes: 9 additions & 3 deletions api/v1alpha1/minecraftcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:subresource:pool
//+kubebuilder:printcolumn:name="Ready",type="boolean",JSONPath=".status.conditions[?(@.type==\"Ready\")].status"
//+kubebuilder:printcolumn:name="Proxies",type="number",JSONPath=".status.proxies"
//+kubebuilder:printcolumn:name="Servers",type="number",JSONPath=".status.servers"
Expand All @@ -36,6 +37,7 @@ type MinecraftCluster struct {

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

// Defines the defired state of a MinecraftCluster. Most, to not
Expand Down Expand Up @@ -66,12 +68,16 @@ type MinecraftClusterStatus struct {

// Number of servers inside the server pool.
Servers int32 `json:"servers"`
}

// Pool of Minecraft Servers linked to this Minecraft Cluster.
ServerPool []MinecraftClusterStatusServerEntry `json:"serverPool"`
// MinecraftClusterPool contains the list of components linked to a
// MinecraftCluster
type MinecraftClusterPool struct {
// List of servers linked to this Minecraft Cluster.
Servers []MinecraftClusterPoolServerEntry `json:"servers,omitempty"`
}

type MinecraftClusterStatusServerEntry struct {
type MinecraftClusterPoolServerEntry struct {
// Name of the Minecraft Server.
//+kubebuilder:validation:Required
Name string `json:"name,omitempty"`
Expand Down
56 changes: 36 additions & 20 deletions api/v1alpha1/zz_generated.deepcopy.go

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

30 changes: 17 additions & 13 deletions config/crd/bases/shulkermc.io_minecraftclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ spec:
type: string
metadata:
type: object
pool:
description: MinecraftClusterPool contains the list of components linked
to a MinecraftCluster
properties:
servers:
description: List of servers linked to this Minecraft Cluster.
items:
properties:
address:
description: IP of the Minecraft Server.
type: string
name:
description: Name of the Minecraft Server.
type: string
type: object
type: array
type: object
spec:
description: Defines the defired state of a MinecraftCluster. Most, to
not say all, fields configurable in a Minecraft Cluster can be configured
Expand Down Expand Up @@ -136,26 +153,13 @@ spec:
description: Number of proxies.
format: int32
type: integer
serverPool:
description: Pool of Minecraft Servers linked to this Minecraft Cluster.
items:
properties:
address:
description: IP of the Minecraft Server.
type: string
name:
description: Name of the Minecraft Server.
type: string
type: object
type: array
servers:
description: Number of servers inside the server pool.
format: int32
type: integer
required:
- conditions
- proxies
- serverPool
- servers
type: object
type: object
Expand Down
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ rules:
- minecraftclusters/finalizers
verbs:
- update
- apiGroups:
- shulkermc.io
resources:
- minecraftclusters/pool
verbs:
- get
- patch
- update
- apiGroups:
- shulkermc.io
resources:
Expand Down
8 changes: 5 additions & 3 deletions controllers/minecraftcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type MinecraftClusterReconciler struct {
//+kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update
//+kubebuilder:rbac:groups=shulkermc.io,resources=minecraftclusters,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=shulkermc.io,resources=minecraftclusters/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=shulkermc.io,resources=minecraftclusters/pool,verbs=get;update;patch
//+kubebuilder:rbac:groups=shulkermc.io,resources=minecraftclusters/finalizers,verbs=update
//+kubebuilder:rbac:groups=shulkermc.io,resources=minecraftservers,verbs=get;list;watch

Expand Down Expand Up @@ -93,20 +94,21 @@ func (r *MinecraftClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, err
}

serverPool := []shulkermciov1alpha1.MinecraftClusterStatusServerEntry{}
serverPool := []shulkermciov1alpha1.MinecraftClusterPoolServerEntry{}
for _, server := range serverList.Items {
if meta.IsStatusConditionTrue(server.Status.Conditions, string(shulkermciov1alpha1.ServerAddressableCondition)) {
serverPool = append(serverPool, shulkermciov1alpha1.MinecraftClusterStatusServerEntry{
serverPool = append(serverPool, shulkermciov1alpha1.MinecraftClusterPoolServerEntry{
Name: server.Name,
Address: server.Status.Address,
})
}
}

minecraftCluster.Status.Servers = int32(len(serverPool))
minecraftCluster.Status.ServerPool = serverPool
minecraftCluster.Status.SetCondition(shulkermciov1alpha1.ClusterReadyCondition, metav1.ConditionTrue, "Ready", "Cluster is ready")

minecraftCluster.Pool.Servers = serverPool

err = r.Status().Update(ctx, minecraftCluster)
return ctrl.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/cluster/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (b *MinecraftClusterResourceBuilder) ResourceBuilders() ([]common.ResourceB
}

func (b *MinecraftClusterResourceBuilder) getRoleName() string {
return fmt.Sprintf("%s-cluster-watch", b.Instance.Name)
return fmt.Sprintf("%s-cluster-watch-pool", b.Instance.Name)
}

func (b *MinecraftClusterResourceBuilder) getLabels() map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/cluster/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (b *MinecraftClusterRoleBuilder) Update(object client.Object) error {
},
{
APIGroups: []string{"shulkermc.io"},
Resources: []string{"minecraftclusters", "minecraftclusters/status"},
Resources: []string{"minecraftclusters", "minecraftclusters/status", "minecraftclusters/pool"},
Verbs: []string{"get", "watch"},
ResourceNames: []string{b.Instance.Name},
},
Expand Down
2 changes: 1 addition & 1 deletion support/shulker-crds/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ archivesBaseName = "ShulkerCrds"
version = '0.0.1'

dependencies {
compileOnly group: 'io.kubernetes', name: 'client-java', version: '14.0.1'
implementation group: 'io.fabric8', name: 'kubernetes-client', version: '5.12.1'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.shulkermc.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Version("v1alpha1")
@Group("shulkermc.io")
public class MinecraftCluster extends CustomResource<MinecraftClusterSpec, MinecraftClusterStatus> implements Namespaced {
@JsonProperty("pool")
protected MinecraftClusterPool pool;

public void setPool(MinecraftClusterPool pool) {
this.pool = pool;
}

public MinecraftClusterPool getPool() {
return this.pool;
}

@Override
public boolean equals(Object o) {
if (!super.equals(o)) return false;
MinecraftCluster that = (MinecraftCluster) o;
return this.pool.equals(that.pool);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + this.pool.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.shulkermc.models;

import io.fabric8.kubernetes.client.CustomResourceList;

public class MinecraftClusterList extends CustomResourceList<MinecraftCluster> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.shulkermc.models;

import java.util.List;

public class MinecraftClusterPool {
private List<ServerEntry> servers;

public void setServers(List<ServerEntry> servers) {
this.servers = servers;
}

public List<ServerEntry> getServers() {
return this.servers;
}

public static class ServerEntry {
private String name;
private String address;

public void setName(String name) {
this.name = name;
}

public void setAddress(String address) {
this.address = address;
}

public String getName() {
return this.name;
}

public String getAddress() {
return this.address;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.shulkermc.models;

public class MinecraftClusterSpec {
private String mavenSecretName;

public void setMavenSecretName(String mavenSecretName) {
this.mavenSecretName = mavenSecretName;
}

public String getMavenSecretName() {
return this.mavenSecretName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.shulkermc.models;

public class MinecraftClusterStatus {
private int proxies;
private int servers;

public void setProxies(int proxies) {
this.proxies = proxies;
}

public void setServers(int servers) {
this.servers = servers;
}

public int getProxies() {
return this.proxies;
}

public int getServers() {
return this.servers;
}
}
Loading

0 comments on commit cc649b0

Please sign in to comment.