fix(lineage-webhook): leave a running Pod's scheduler alone on update

spec.schedulerName cannot change once a Pod exists, yet the webhook set
it on every Pod admission, updates included. A Pod created before its
tenant got a class, or before its application named one, then had
every later update refused by the apiserver, labels and annotations
written by controllers included. A class can only take effect when the
Pod is created.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
This commit is contained in:
Mattia Eleuteri committed 2026-11-23 08:05:10 +01:00
1 parent d3804ebea9
commit addf7d1258
2 files changed
+35 -6

No files matched your search

+9 -4
View File
@@ -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)
@@ -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)
}
}
}