mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-09-13 10:49:21 +00:00
♻️ refactor: migrate to Go Kubernetes operator
This commit is contained in:
@@ -50,10 +50,38 @@ func (r *ReverseProxyServerReconciler) Reconcile(ctx context.Context, req ctrl.R
|
||||
if err := apply(ctx, r.Client, &rp, resources.ProxyDeployment(&rp), r.Scheme); err != nil {
|
||||
return r.fail(ctx, &rp, "DeploymentFailed", err)
|
||||
}
|
||||
if err := r.pruneStaleResources(ctx, &rp); err != nil {
|
||||
return r.fail(ctx, &rp, "PruneFailed", err)
|
||||
}
|
||||
|
||||
return ctrl.Result{}, r.updateStatus(ctx, &rp)
|
||||
}
|
||||
|
||||
func (r *ReverseProxyServerReconciler) pruneStaleResources(ctx context.Context, rp *v1alpha1.ReverseProxyServer) error {
|
||||
current := resources.ProxyName(rp.Spec.Type, rp.Name)
|
||||
for _, kind := range []v1alpha1.ProxyKind{v1alpha1.ProxyVelocity, v1alpha1.ProxyBungeeCord} {
|
||||
name := resources.ProxyName(kind, rp.Name)
|
||||
if name == current {
|
||||
continue
|
||||
}
|
||||
for _, obj := range []client.Object{&appsv1.Deployment{}, &corev1.Service{}, &corev1.ConfigMap{}} {
|
||||
key := client.ObjectKey{Name: name, Namespace: rp.Namespace}
|
||||
if err := r.Get(ctx, key, obj); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
if metav1.IsControlledBy(obj, rp) {
|
||||
if err := r.Delete(ctx, obj); err != nil && !apierrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ReverseProxyServerReconciler) backends(ctx context.Context, rp *v1alpha1.ReverseProxyServer) ([]string, error) {
|
||||
selector := labels.Everything()
|
||||
if rp.Spec.BackendSelector != nil {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
const (
|
||||
MinecraftImage = "itzg/minecraft-server"
|
||||
ProxyImage = "itzg/mc-proxy:latest"
|
||||
ContainerPort = 25565
|
||||
DefaultHeapPercent = 80
|
||||
)
|
||||
|
||||
@@ -46,8 +46,8 @@ func ProxyService(rp *v1alpha1.ReverseProxyServer) *corev1.Service {
|
||||
|
||||
func proxyEnv(rp *v1alpha1.ReverseProxyServer) []corev1.EnvVar {
|
||||
env := []corev1.EnvVar{
|
||||
{Name: "EULA", Value: "TRUE"},
|
||||
{Name: "TYPE", Value: string(rp.Spec.Type)},
|
||||
{Name: "NETWORKADDRESS_CACHE_TTL", Value: "30"},
|
||||
{Name: "MINIKURA_EXTERNAL_ADDRESS", Value: rp.Spec.ExternalAddress},
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ func ProxyDeployment(rp *v1alpha1.ReverseProxyServer) *appsv1.Deployment {
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{{
|
||||
Name: "proxy",
|
||||
Image: MinecraftImage,
|
||||
Image: ProxyImage,
|
||||
Ports: []corev1.ContainerPort{{
|
||||
Name: "minecraft",
|
||||
ContainerPort: rp.Spec.ListenPort,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
v1alpha1 "github.com/YuzuZensai/Minikura/operator/api/v1alpha1"
|
||||
)
|
||||
|
||||
func testProxy() *v1alpha1.ReverseProxyServer {
|
||||
return &v1alpha1.ReverseProxyServer{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "edge", Namespace: "minikura"},
|
||||
Spec: v1alpha1.ReverseProxyServerSpec{
|
||||
Type: v1alpha1.ProxyVelocity,
|
||||
ExternalAddress: "play.example.com",
|
||||
ExternalPort: 25565,
|
||||
ListenPort: 25577,
|
||||
Resources: v1alpha1.Resources{MemoryLimitMB: 512},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyDeploymentUsesProxyImage(t *testing.T) {
|
||||
container := ProxyDeployment(testProxy()).Spec.Template.Spec.Containers[0]
|
||||
if container.Image != ProxyImage {
|
||||
t.Errorf("image = %q, want %q", container.Image, ProxyImage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyEnvCarriesRuntimeConfig(t *testing.T) {
|
||||
env := proxyEnv(testProxy())
|
||||
for _, want := range []struct{ key, value string }{
|
||||
{"TYPE", "VELOCITY"},
|
||||
{"NETWORKADDRESS_CACHE_TTL", "30"},
|
||||
{"MINIKURA_EXTERNAL_ADDRESS", "play.example.com"},
|
||||
} {
|
||||
got, ok := envValue(env, want.key)
|
||||
if !ok || got != want.value {
|
||||
t.Errorf("%s = %q, %v; want %q", want.key, got, ok, want.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user