Skip to content

Commit 299994b

Browse files
feat(scheduler): set scheduling-class label on mutated pods
The lineage-controller-webhook now sets a scheduler.cozystack.io/scheduling-class label on mutated pods (in addition to the existing annotation of the same name). This unlocks SchedulingClass authors writing podAffinity rules whose labelSelector targets pods across different applications that share the same class. A natural pattern becomes: define a class whose name is the affinity-group identifier, attach it to several applications, and have the scheduler co-locate them at the chosen topology key (region, zone, hostname). Behavior preserved when the SchedulingClass CR is missing: pods are left untouched (no scheduler injection, no label) so they do not stay Pending on a non-existent scheduler. Tests cover label/annotation/schedulerName injection, existing label preservation, non-Pod no-op, no-class no-op, and missing-CR no-op. Signed-off-by: mattia-eleuteri <[email protected]>
1 parent f91841f commit 299994b

2 files changed

Lines changed: 219 additions & 2 deletions

File tree

‎internal/lineagecontrollerwebhook/webhook.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,14 @@ func (h *LineageControllerWebhook) applyLabels(o *unstructured.Unstructured, lab
201201
o.SetLabels(existing)
202202
}
203203

204-
// applySchedulingClass injects schedulerName and scheduling-class annotation
205-
// into Pods whose namespace carries the scheduler.cozystack.io/scheduling-class label.
204+
// applySchedulingClass injects schedulerName, scheduling-class annotation, and
205+
// scheduling-class label into Pods whose namespace carries the
206+
// scheduler.cozystack.io/scheduling-class label.
207+
//
208+
// The label (in addition to the annotation) lets SchedulingClass authors write
209+
// podAffinity rules whose labelSelector matches pods across applications that
210+
// share the same class.
211+
//
206212
// If the referenced SchedulingClass CR does not exist (e.g. the scheduler
207213
// package is not installed), the injection is silently skipped so that pods
208214
// are not left Pending.
@@ -254,6 +260,13 @@ func (h *LineageControllerWebhook) applySchedulingClass(ctx context.Context, obj
254260
annotations[schedulerapi.SchedulingClassAnnotation] = schedulingClass
255261
obj.SetAnnotations(annotations)
256262

263+
podLabels := obj.GetLabels()
264+
if podLabels == nil {
265+
podLabels = make(map[string]string)
266+
}
267+
podLabels[schedulerapi.SchedulingClassLabel] = schedulingClass
268+
obj.SetLabels(podLabels)
269+
257270
return nil
258271
}
259272

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

Comments
 (0)