|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package lineagecontrollerwebhook |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 13 | + dynamicfake "k8s.io/client-go/dynamic/fake" |
| 14 | + "k8s.io/client-go/kubernetes/scheme" |
| 15 | + clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 16 | + |
| 17 | + schedulerapi "github.com/cozystack/cozystack-scheduler/pkg/apis/v1alpha1" |
| 18 | +) |
| 19 | + |
| 20 | +const testSchedulingClass = "co-region-test" |
| 21 | + |
| 22 | +// newSchedulingClassCR builds an unstructured SchedulingClass CR the fake |
| 23 | +// dynamic client can serve. |
| 24 | +func newSchedulingClassCR(name string) *unstructured.Unstructured { |
| 25 | + u := &unstructured.Unstructured{} |
| 26 | + u.SetGroupVersionKind(schema.GroupVersionKind{ |
| 27 | + Group: schedulerapi.Group, |
| 28 | + Version: schedulerapi.Version, |
| 29 | + Kind: "SchedulingClass", |
| 30 | + }) |
| 31 | + u.SetName(name) |
| 32 | + return u |
| 33 | +} |
| 34 | + |
| 35 | +// newWebhookTestEnv builds a LineageControllerWebhook wired with fakes, |
| 36 | +// pre-populated with a Namespace carrying the scheduling-class label and a |
| 37 | +// SchedulingClass CR by that name. |
| 38 | +func newWebhookTestEnv(t *testing.T, namespace string, classOnNamespace string, classExists bool) *LineageControllerWebhook { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + testScheme := runtime.NewScheme() |
| 42 | + _ = scheme.AddToScheme(testScheme) |
| 43 | + |
| 44 | + ns := &corev1.Namespace{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{ |
| 46 | + Name: namespace, |
| 47 | + }, |
| 48 | + } |
| 49 | + if classOnNamespace != "" { |
| 50 | + ns.Labels = map[string]string{ |
| 51 | + schedulerapi.SchedulingClassLabel: classOnNamespace, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + c := clientfake.NewClientBuilder(). |
| 56 | + WithScheme(testScheme). |
| 57 | + WithObjects(ns). |
| 58 | + Build() |
| 59 | + |
| 60 | + gvr := schema.GroupVersionResource{ |
| 61 | + Group: schedulerapi.Group, |
| 62 | + Version: schedulerapi.Version, |
| 63 | + Resource: schedulerapi.Resource, |
| 64 | + } |
| 65 | + |
| 66 | + dynObjs := []runtime.Object{} |
| 67 | + if classExists { |
| 68 | + dynObjs = append(dynObjs, newSchedulingClassCR(classOnNamespace)) |
| 69 | + } |
| 70 | + dyn := dynamicfake.NewSimpleDynamicClientWithCustomListKinds( |
| 71 | + testScheme, |
| 72 | + map[schema.GroupVersionResource]string{gvr: "SchedulingClassList"}, |
| 73 | + dynObjs..., |
| 74 | + ) |
| 75 | + |
| 76 | + return &LineageControllerWebhook{ |
| 77 | + Client: c, |
| 78 | + Scheme: testScheme, |
| 79 | + dynClient: dyn, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// newPod returns a minimal unstructured Pod for mutation tests. |
| 84 | +func newPod(namespace, name string) *unstructured.Unstructured { |
| 85 | + u := &unstructured.Unstructured{} |
| 86 | + u.SetGroupVersionKind(schema.GroupVersionKind{Version: "v1", Kind: "Pod"}) |
| 87 | + u.SetNamespace(namespace) |
| 88 | + u.SetName(name) |
| 89 | + return u |
| 90 | +} |
| 91 | + |
| 92 | +func TestApplySchedulingClass_SetsLabelAnnotationAndSchedulerName(t *testing.T) { |
| 93 | + ctx := context.Background() |
| 94 | + h := newWebhookTestEnv(t, "tenant-acme", testSchedulingClass, true) |
| 95 | + pod := newPod("tenant-acme", "db-0") |
| 96 | + |
| 97 | + if err := h.applySchedulingClass(ctx, pod, nil, "tenant-acme"); err != nil { |
| 98 | + t.Fatalf("applySchedulingClass returned error: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + gotLabel := pod.GetLabels()[schedulerapi.SchedulingClassLabel] |
| 102 | + if gotLabel != testSchedulingClass { |
| 103 | + t.Errorf("expected pod label %s=%s, got %q", schedulerapi.SchedulingClassLabel, testSchedulingClass, gotLabel) |
| 104 | + } |
| 105 | + |
| 106 | + gotAnnotation := pod.GetAnnotations()[schedulerapi.SchedulingClassAnnotation] |
| 107 | + if gotAnnotation != testSchedulingClass { |
| 108 | + t.Errorf("expected pod annotation %s=%s, got %q", schedulerapi.SchedulingClassAnnotation, testSchedulingClass, gotAnnotation) |
| 109 | + } |
| 110 | + |
| 111 | + gotSched, _, _ := unstructured.NestedString(pod.Object, "spec", "schedulerName") |
| 112 | + if gotSched != schedulerapi.SchedulerName { |
| 113 | + t.Errorf("expected pod spec.schedulerName=%s, got %q", schedulerapi.SchedulerName, gotSched) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestApplySchedulingClass_PreservesExistingLabels(t *testing.T) { |
| 118 | + ctx := context.Background() |
| 119 | + h := newWebhookTestEnv(t, "tenant-acme", testSchedulingClass, true) |
| 120 | + pod := newPod("tenant-acme", "db-0") |
| 121 | + pod.SetLabels(map[string]string{ |
| 122 | + "app.kubernetes.io/name": "postgres", |
| 123 | + "role": "primary", |
| 124 | + }) |
| 125 | + |
| 126 | + if err := h.applySchedulingClass(ctx, pod, nil, "tenant-acme"); err != nil { |
| 127 | + t.Fatalf("applySchedulingClass returned error: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + labels := pod.GetLabels() |
| 131 | + if labels[schedulerapi.SchedulingClassLabel] != testSchedulingClass { |
| 132 | + t.Errorf("scheduling-class label not set") |
| 133 | + } |
| 134 | + if labels["app.kubernetes.io/name"] != "postgres" { |
| 135 | + t.Errorf("existing label app.kubernetes.io/name was clobbered: %q", labels["app.kubernetes.io/name"]) |
| 136 | + } |
| 137 | + if labels["role"] != "primary" { |
| 138 | + t.Errorf("existing label role was clobbered: %q", labels["role"]) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestApplySchedulingClass_NonPodIsNoOp(t *testing.T) { |
| 143 | + ctx := context.Background() |
| 144 | + h := newWebhookTestEnv(t, "tenant-acme", testSchedulingClass, true) |
| 145 | + |
| 146 | + svc := &unstructured.Unstructured{} |
| 147 | + svc.SetGroupVersionKind(schema.GroupVersionKind{Version: "v1", Kind: "Service"}) |
| 148 | + svc.SetNamespace("tenant-acme") |
| 149 | + svc.SetName("svc-0") |
| 150 | + |
| 151 | + if err := h.applySchedulingClass(ctx, svc, nil, "tenant-acme"); err != nil { |
| 152 | + t.Fatalf("applySchedulingClass returned error: %v", err) |
| 153 | + } |
| 154 | + |
| 155 | + if _, ok := svc.GetLabels()[schedulerapi.SchedulingClassLabel]; ok { |
| 156 | + t.Errorf("non-Pod object should not be mutated") |
| 157 | + } |
| 158 | + if _, ok := svc.GetAnnotations()[schedulerapi.SchedulingClassAnnotation]; ok { |
| 159 | + t.Errorf("non-Pod object should not be mutated") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestApplySchedulingClass_NoClassOnNamespaceIsNoOp(t *testing.T) { |
| 164 | + ctx := context.Background() |
| 165 | + h := newWebhookTestEnv(t, "tenant-acme", "", false) |
| 166 | + pod := newPod("tenant-acme", "db-0") |
| 167 | + |
| 168 | + if err := h.applySchedulingClass(ctx, pod, nil, "tenant-acme"); err != nil { |
| 169 | + t.Fatalf("applySchedulingClass returned error: %v", err) |
| 170 | + } |
| 171 | + |
| 172 | + if _, ok := pod.GetLabels()[schedulerapi.SchedulingClassLabel]; ok { |
| 173 | + t.Errorf("pod label should not be set when namespace has no scheduling-class label") |
| 174 | + } |
| 175 | + if _, ok := pod.GetAnnotations()[schedulerapi.SchedulingClassAnnotation]; ok { |
| 176 | + t.Errorf("pod annotation should not be set when namespace has no scheduling-class label") |
| 177 | + } |
| 178 | + if sched, _, _ := unstructured.NestedString(pod.Object, "spec", "schedulerName"); sched != "" { |
| 179 | + t.Errorf("pod spec.schedulerName should not be set when namespace has no scheduling-class label, got %q", sched) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestApplySchedulingClass_MissingClassCRSkipsInjection(t *testing.T) { |
| 184 | + ctx := context.Background() |
| 185 | + // Namespace carries the label, but the SchedulingClass CR is NOT created. |
| 186 | + h := newWebhookTestEnv(t, "tenant-acme", testSchedulingClass, false) |
| 187 | + pod := newPod("tenant-acme", "db-0") |
| 188 | + |
| 189 | + if err := h.applySchedulingClass(ctx, pod, nil, "tenant-acme"); err != nil { |
| 190 | + t.Fatalf("applySchedulingClass returned error: %v", err) |
| 191 | + } |
| 192 | + |
| 193 | + // Defensive behavior: pod must not be left referencing a non-existent |
| 194 | + // scheduler, otherwise it would stay Pending forever. |
| 195 | + if _, ok := pod.GetLabels()[schedulerapi.SchedulingClassLabel]; ok { |
| 196 | + t.Errorf("pod label should not be set when SchedulingClass CR is missing") |
| 197 | + } |
| 198 | + if _, ok := pod.GetAnnotations()[schedulerapi.SchedulingClassAnnotation]; ok { |
| 199 | + t.Errorf("pod annotation should not be set when SchedulingClass CR is missing") |
| 200 | + } |
| 201 | + if sched, _, _ := unstructured.NestedString(pod.Object, "spec", "schedulerName"); sched != "" { |
| 202 | + t.Errorf("pod spec.schedulerName should not be set when SchedulingClass CR is missing, got %q", sched) |
| 203 | + } |
| 204 | +} |
0 commit comments