diff --git a/internal/lineagecontrollerwebhook/webhook.go b/internal/lineagecontrollerwebhook/webhook.go index 57aaf2898..13d6d61ce 100644 --- a/internal/lineagecontrollerwebhook/webhook.go +++ b/internal/lineagecontrollerwebhook/webhook.go @@ -112,7 +112,7 @@ func (h *LineageControllerWebhook) Handle(ctx context.Context, req admission.Req // The objectSelector skips objects carrying the managed label, but on UPDATE // it matches if either the old or the new object does. Pods go on, since - // every Pod admission also needs applySchedulingClass. + // a Pod's creation also needs applySchedulingClass. if obj.GetKind() != "Pod" && hasAllLineageLabels(obj) { logger.V(1).Info("object already carries lineage labels, skipping owner walk") return admission.Allowed("lineage labels already present") @@ -136,9 +136,14 @@ func (h *LineageControllerWebhook) Handle(ctx context.Context, req admission.Req h.applyLabels(obj, labels) - if err := h.applySchedulingClass(ctx, obj, owner, req.Namespace); err != nil { - logger.Error(err, "error applying scheduling class") - return admission.Errored(500, fmt.Errorf("error applying scheduling class: %w", err)) + // spec.schedulerName is immutable once the Pod exists, so a class only + // takes effect at creation: setting it on an UPDATE, after the tenant or + // application class changed, would get every update of the Pod refused. + if req.Operation == admissionv1.Create { + if err := h.applySchedulingClass(ctx, obj, owner, req.Namespace); err != nil { + logger.Error(err, "error applying scheduling class") + return admission.Errored(500, fmt.Errorf("error applying scheduling class: %w", err)) + } } mutated, err := json.Marshal(obj) diff --git a/internal/lineagecontrollerwebhook/webhook_test.go b/internal/lineagecontrollerwebhook/webhook_test.go index 6323d9616..5a418bd12 100644 --- a/internal/lineagecontrollerwebhook/webhook_test.go +++ b/internal/lineagecontrollerwebhook/webhook_test.go @@ -184,10 +184,10 @@ func TestHandle_ReadmissionShortCircuit(t *testing.T) { labels: fullLabels, }, { - name: "pod with every lineage label still gets the scheduling fields", + name: "pod with every lineage label still goes through the walk", kind: "Pod", labels: fullLabels, - wantPatches: []string{"add /spec/schedulerName", "replace " + escape(ManagerNameKey)}, + wantPatches: []string{"replace " + escape(ManagerNameKey)}, }, { name: "secret marked unmanaged goes through the walk", @@ -739,3 +739,27 @@ func TestApplySchedulingClass_ApplicationClassDeletedDuringAdmissionKeepsAClass( t.Errorf("expected pod annotation %s=app-class, got %q", schedulerapi.SchedulingClassAnnotation, got) } } + +// spec.schedulerName cannot change once a Pod exists. Setting it on an +// UPDATE, after the tenant or application class changed, would get every +// later update of the Pod refused by the apiserver. +func TestHandle_PodUpdateLeavesSchedulingAlone(t *testing.T) { + w := newHandleTestWebhook(t) + + resp := w.Handle(context.Background(), admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{ + Kind: metav1.GroupVersionKind{Version: "v1", Kind: "Pod"}, + Namespace: "tenant-demo", + Name: "harbor-demo-core-0", + Operation: admissionv1.Update, + Object: runtime.RawExtension{Raw: []byte(podWithNumericFields)}, + OldObject: runtime.RawExtension{Raw: []byte(podWithNumericFields)}, + }}) + if !resp.Allowed { + t.Fatalf("expected allowed response, got %+v", resp.Result) + } + for _, p := range resp.Patches { + if p.Path == "/spec/schedulerName" || strings.Contains(p.Path, "scheduling-class") { + t.Errorf("UPDATE must not touch scheduling, got %s %s", p.Operation, p.Path) + } + } +}