@@ -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+
273330func TestPodAdmission_ValidateCreate (t * testing.T ) {
274331 type fields struct {
275332 SchedulerName string
0 commit comments