Skip to content

Commit 60287d8

Browse files
committed
Merge branch 'marlow/gl_48' into 'main'
fix: Mutating webhook should unset spec.nodeName See merge request SchedMD/slinky-dev/slurm-bridge!120
2 parents 6d410c5 + dbe29f4 commit 60287d8

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

internal/admission/admission.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ func (r *PodAdmission) Default(ctx context.Context, pod *corev1.Pod) error {
4646
if !isManaged && pod.Spec.SchedulerName != r.SchedulerName {
4747
return nil
4848
}
49+
50+
// On create, unset spec.nodeName so the pod is scheduled by slurm-bridge.
51+
if req, err := admission.RequestFromContext(ctx); err == nil && req.Operation == "CREATE" {
52+
if pod.Spec.NodeName != "" {
53+
logger.V(1).Info("Unsetting spec.nodeName on create so slurm scheduling will occur", "pod", klog.KObj(pod), "previousNodeName", pod.Spec.NodeName)
54+
pod.Spec.NodeName = ""
55+
}
56+
}
57+
4958
if pod.Spec.SchedulerName == corev1.DefaultSchedulerName {
5059
pod.Spec.SchedulerName = r.SchedulerName
5160
}

internal/admission/admission_test.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ func TestPodAdmission_Namespaces(t *testing.T) {
8989
pod *corev1.Pod
9090
}
9191
tests := []struct {
92-
name string
93-
args args
94-
wantErr bool
95-
sched string
92+
name string
93+
args args
94+
wantErr bool
95+
sched string
96+
wantNodeName string
9697
}{
9798
{
9899
name: "PodWithDefaultNamespace",
@@ -250,6 +251,52 @@ func TestPodAdmission_Namespaces(t *testing.T) {
250251
wantErr: false,
251252
sched: corev1.DefaultSchedulerName,
252253
},
254+
{
255+
name: "PodWithNodeNameOnCreateInManagedNamespace_unsetsNodeName",
256+
args: args{
257+
ctx: contextWithAdmissionOperation("CREATE"),
258+
pod: &corev1.Pod{
259+
ObjectMeta: metav1.ObjectMeta{
260+
Name: "test-pod",
261+
Namespace: namespace,
262+
Labels: map[string]string{"app": "test-app"},
263+
},
264+
Spec: corev1.PodSpec{
265+
SchedulerName: corev1.DefaultSchedulerName,
266+
NodeName: "some-node",
267+
Containers: []corev1.Container{
268+
{Name: "test-container", Image: "test-image"},
269+
},
270+
},
271+
},
272+
},
273+
wantErr: false,
274+
sched: SchedulerName,
275+
wantNodeName: "",
276+
},
277+
{
278+
name: "PodWithNodeNameOnUpdateInManagedNamespace_preservesNodeName",
279+
args: args{
280+
ctx: contextWithAdmissionOperation("UPDATE"),
281+
pod: &corev1.Pod{
282+
ObjectMeta: metav1.ObjectMeta{
283+
Name: "test-pod",
284+
Namespace: namespace,
285+
Labels: map[string]string{"app": "test-app"},
286+
},
287+
Spec: corev1.PodSpec{
288+
SchedulerName: corev1.DefaultSchedulerName,
289+
NodeName: "some-node",
290+
Containers: []corev1.Container{
291+
{Name: "test-container", Image: "test-image"},
292+
},
293+
},
294+
},
295+
},
296+
wantErr: false,
297+
sched: SchedulerName,
298+
wantNodeName: "some-node",
299+
},
253300
}
254301
for _, tt := range tests {
255302
t.Run(tt.name, func(t *testing.T) {
@@ -266,10 +313,20 @@ func TestPodAdmission_Namespaces(t *testing.T) {
266313
if tt.args.pod.Spec.SchedulerName != tt.sched {
267314
t.Errorf("PodAdmission.Default() scheduler = %s, want scheduler %s", tt.args.pod.Spec.SchedulerName, tt.sched)
268315
}
316+
if tt.args.pod.Spec.NodeName != tt.wantNodeName {
317+
t.Errorf("PodAdmission.Default() nodeName = %q, want %q", tt.args.pod.Spec.NodeName, tt.wantNodeName)
318+
}
269319
})
270320
}
271321
}
272322

323+
// contextWithAdmissionOperation returns a context with an admission request for the given operation.
324+
func contextWithAdmissionOperation(op string) context.Context {
325+
req := admission.Request{}
326+
reflect.ValueOf(&req).Elem().FieldByName("Operation").SetString(op)
327+
return admission.NewContextWithRequest(context.TODO(), req)
328+
}
329+
273330
func TestPodAdmission_ValidateCreate(t *testing.T) {
274331
type fields struct {
275332
SchedulerName string

0 commit comments

Comments
 (0)