|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + k8sfake "k8s.io/client-go/kubernetes/fake" |
| 11 | + |
| 12 | + "github.com/xraph/ctrlplane/id" |
| 13 | + "github.com/xraph/ctrlplane/provider" |
| 14 | +) |
| 15 | + |
| 16 | +// TestScale_PatchesContainerResources is the regression for the bug where |
| 17 | +// Provider.Scale ignored CPU/memory and only acted on replicas — leaving a |
| 18 | +// provisioned workload's resources immutable. A resize must now patch the |
| 19 | +// pod template's container requests AND limits. |
| 20 | +func TestScale_PatchesContainerResources(t *testing.T) { |
| 21 | + instID := id.New(id.PrefixInstance) |
| 22 | + name := deploymentName(instID) |
| 23 | + |
| 24 | + const ns = "default" |
| 25 | + |
| 26 | + big := provider.ResourceSpec{CPUMillis: 500, MemoryMB: 512} |
| 27 | + dep := &appsv1.Deployment{ |
| 28 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns}, |
| 29 | + Spec: appsv1.DeploymentSpec{ |
| 30 | + Template: corev1.PodTemplateSpec{ |
| 31 | + Spec: corev1.PodSpec{ |
| 32 | + Containers: []corev1.Container{{ |
| 33 | + Name: "twinos", |
| 34 | + Image: "img:1", |
| 35 | + Resources: corev1.ResourceRequirements{ |
| 36 | + Requests: buildResourceList(big), |
| 37 | + Limits: buildResourceList(big), |
| 38 | + }, |
| 39 | + }}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + p := &Provider{ |
| 46 | + cfg: Config{Namespace: ns}, |
| 47 | + client: k8sfake.NewSimpleClientset(dep), |
| 48 | + } |
| 49 | + |
| 50 | + // Shrink to values that fit a small node. |
| 51 | + err := p.Scale(context.Background(), instID, provider.ResourceSpec{CPUMillis: 150, MemoryMB: 256}) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Scale: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + got, err := p.client.AppsV1().Deployments(ns).Get(context.Background(), name, metav1.GetOptions{}) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("get deployment: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + c := got.Spec.Template.Spec.Containers[0] |
| 62 | + |
| 63 | + if cpu := c.Resources.Requests.Cpu().MilliValue(); cpu != 150 { |
| 64 | + t.Errorf("cpu request: want 150m, got %dm", cpu) |
| 65 | + } |
| 66 | + |
| 67 | + if cpu := c.Resources.Limits.Cpu().MilliValue(); cpu != 150 { |
| 68 | + t.Errorf("cpu limit: want 150m, got %dm", cpu) |
| 69 | + } |
| 70 | + |
| 71 | + if memMB := c.Resources.Requests.Memory().Value() / (1024 * 1024); memMB != 256 { |
| 72 | + t.Errorf("mem request: want 256Mi, got %dMi", memMB) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// TestScale_noResourceChange_isNoop verifies that a ScaleRequest carrying |
| 77 | +// neither CPU nor memory nor replicas leaves the workload untouched (and |
| 78 | +// crucially does not attempt the resource patch). |
| 79 | +func TestScale_noResourceChange_isNoop(t *testing.T) { |
| 80 | + instID := id.New(id.PrefixInstance) |
| 81 | + name := deploymentName(instID) |
| 82 | + |
| 83 | + const ns = "default" |
| 84 | + |
| 85 | + orig := provider.ResourceSpec{CPUMillis: 500, MemoryMB: 512} |
| 86 | + dep := &appsv1.Deployment{ |
| 87 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns}, |
| 88 | + Spec: appsv1.DeploymentSpec{ |
| 89 | + Template: corev1.PodTemplateSpec{ |
| 90 | + Spec: corev1.PodSpec{ |
| 91 | + Containers: []corev1.Container{{ |
| 92 | + Name: "twinos", |
| 93 | + Image: "img:1", |
| 94 | + Resources: corev1.ResourceRequirements{Requests: buildResourceList(orig), Limits: buildResourceList(orig)}, |
| 95 | + }}, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + p := &Provider{cfg: Config{Namespace: ns}, client: k8sfake.NewSimpleClientset(dep)} |
| 102 | + |
| 103 | + if err := p.Scale(context.Background(), instID, provider.ResourceSpec{}); err != nil { |
| 104 | + t.Fatalf("Scale (empty spec): %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + got, err := p.client.AppsV1().Deployments(ns).Get(context.Background(), name, metav1.GetOptions{}) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("get deployment: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + if cpu := got.Spec.Template.Spec.Containers[0].Resources.Requests.Cpu().MilliValue(); cpu != 500 { |
| 113 | + t.Errorf("empty scale must not change cpu: got %dm, want 500m", cpu) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestProvision_propagatesRequestLabelsToPods is the regression for pods |
| 118 | +// carrying only ctrlplane labels: a caller's per-instance labels (e.g. |
| 119 | +// twinos.workspace / twinos.component) must reach the Deployment AND its |
| 120 | +// pod template so pods are identifiable/queryable, while the reserved |
| 121 | +// ctrlplane.io/* keys stay authoritative. |
| 122 | +func TestProvision_propagatesRequestLabelsToPods(t *testing.T) { |
| 123 | + instID := id.New(id.PrefixInstance) |
| 124 | + |
| 125 | + const ns = "default" |
| 126 | + |
| 127 | + p := &Provider{ |
| 128 | + cfg: Config{Namespace: ns}, |
| 129 | + client: k8sfake.NewSimpleClientset(), |
| 130 | + } |
| 131 | + |
| 132 | + req := provider.ProvisionRequest{ |
| 133 | + InstanceID: instID, |
| 134 | + TenantID: "ten_abc", |
| 135 | + Kind: provider.KindDeployment, |
| 136 | + Labels: map[string]string{ |
| 137 | + "twinos.workspace": "ws_acme", |
| 138 | + "twinos.component": "twinos", |
| 139 | + // A caller trying to clobber a reserved key must NOT win. |
| 140 | + labelInstanceID: "spoofed", |
| 141 | + }, |
| 142 | + Services: []provider.ServiceSpec{{ |
| 143 | + Name: "twinos", |
| 144 | + Image: "img:1", |
| 145 | + Role: provider.RoleMain, |
| 146 | + }}, |
| 147 | + } |
| 148 | + |
| 149 | + if _, err := p.Provision(context.Background(), req); err != nil { |
| 150 | + t.Fatalf("Provision: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + dep, err := p.client.AppsV1().Deployments(ns).Get(context.Background(), deploymentName(instID), metav1.GetOptions{}) |
| 154 | + if err != nil { |
| 155 | + t.Fatalf("get deployment: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + pod := dep.Spec.Template.Labels |
| 159 | + if pod["twinos.workspace"] != "ws_acme" || pod["twinos.component"] != "twinos" { |
| 160 | + t.Fatalf("caller labels missing from pod template: %v", pod) |
| 161 | + } |
| 162 | + |
| 163 | + if pod[labelInstanceID] != instID.String() { |
| 164 | + t.Fatalf("reserved instance-id label was clobbered: got %q, want %q", pod[labelInstanceID], instID.String()) |
| 165 | + } |
| 166 | +} |
0 commit comments