@@ -19,6 +19,7 @@ import (
1919 "k8s.io/apimachinery/pkg/runtime/schema"
2020 dynamicfake "k8s.io/client-go/dynamic/fake"
2121 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
22+ "sigs.k8s.io/controller-runtime/pkg/client"
2223 "sigs.k8s.io/controller-runtime/pkg/client/fake"
2324 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2425)
@@ -481,10 +482,23 @@ func addSchedulingClassCR(t *testing.T, h *LineageControllerWebhook, name string
481482 }
482483}
483484
485+ func allowSchedulingClasses (t * testing.T , h * LineageControllerWebhook , namespace , allowed string ) {
486+ t .Helper ()
487+ ns := & corev1.Namespace {}
488+ if err := h .Get (context .Background (), client.ObjectKey {Name : namespace }, ns ); err != nil {
489+ t .Fatalf ("getting namespace %s: %v" , namespace , err )
490+ }
491+ ns .Annotations = map [string ]string {AllowedSchedulingClassesAnnotation : allowed }
492+ if err := h .Update (context .Background (), ns ); err != nil {
493+ t .Fatalf ("updating namespace %s: %v" , namespace , err )
494+ }
495+ }
496+
484497func TestApplySchedulingClass_OwnerApplicationClassOverridesNamespace (t * testing.T ) {
485498 ctx := context .Background ()
486499 h := newWebhookTestEnv (t , "tenant-acme" , testSchedulingClass , true )
487500 addSchedulingClassCR (t , h , "app-class" )
501+ allowSchedulingClasses (t , h , "tenant-acme" , "other-class, app-class" )
488502 owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{
489503 "replicas" : int64 (2 ),
490504 "schedulingClass" : "app-class" ,
@@ -536,6 +550,7 @@ func TestApplySchedulingClass_ClassNameTooLongForLabelSkipsOnlyLabel(t *testing.
536550 longClass := strings .Repeat ("a" , 64 )
537551 h := newWebhookTestEnv (t , "tenant-acme" , "" , false )
538552 addSchedulingClassCR (t , h , longClass )
553+ allowSchedulingClasses (t , h , "tenant-acme" , longClass )
539554 owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{"schedulingClass" : longClass })
540555 pod := newPod ("tenant-acme" , "db-0" )
541556
@@ -553,3 +568,84 @@ func TestApplySchedulingClass_ClassNameTooLongForLabelSkipsOnlyLabel(t *testing.
553568 t .Errorf ("expected pod spec.schedulerName=%s, got %q" , schedulerapi .SchedulerName , sched )
554569 }
555570}
571+
572+ // The tenant class is the administrator's placement constraint. An application
573+ // class that the namespace does not allow, or that does not exist, must leave
574+ // it in force instead of dropping the injection altogether.
575+ func TestApplySchedulingClass_RejectedOwnerApplicationClassKeepsTenantClass (t * testing.T ) {
576+ for _ , tc := range []struct {
577+ name string
578+ appClass string
579+ allowed string
580+ existing []string
581+ }{
582+ {name : "class does not exist" , appClass : "does-not-exist" , allowed : "does-not-exist" },
583+ {name : "class not in the allowed list" , appClass : "app-class" , allowed : "other-class" , existing : []string {"app-class" }},
584+ {name : "no allowed list" , appClass : "app-class" , existing : []string {"app-class" }},
585+ } {
586+ t .Run (tc .name , func (t * testing.T ) {
587+ ctx := context .Background ()
588+ h := newWebhookTestEnv (t , "tenant-acme" , testSchedulingClass , true )
589+ for _ , name := range tc .existing {
590+ addSchedulingClassCR (t , h , name )
591+ }
592+ if tc .allowed != "" {
593+ allowSchedulingClasses (t , h , "tenant-acme" , tc .allowed )
594+ }
595+ owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{"schedulingClass" : tc .appClass })
596+ pod := newPod ("tenant-acme" , "db-0" )
597+
598+ if err := h .applySchedulingClass (ctx , pod , owner , "tenant-acme" ); err != nil {
599+ t .Fatalf ("applySchedulingClass returned error: %v" , err )
600+ }
601+
602+ if got := pod .GetLabels ()[schedulerapi .SchedulingClassLabel ]; got != testSchedulingClass {
603+ t .Errorf ("expected pod label %s=%s, got %q" , schedulerapi .SchedulingClassLabel , testSchedulingClass , got )
604+ }
605+ if got := pod .GetAnnotations ()[schedulerapi .SchedulingClassAnnotation ]; got != testSchedulingClass {
606+ t .Errorf ("expected pod annotation %s=%s, got %q" , schedulerapi .SchedulingClassAnnotation , testSchedulingClass , got )
607+ }
608+ if sched , _ , _ := unstructured .NestedString (pod .Object , "spec" , "schedulerName" ); sched != schedulerapi .SchedulerName {
609+ t .Errorf ("expected pod spec.schedulerName=%s, got %q" , schedulerapi .SchedulerName , sched )
610+ }
611+ })
612+ }
613+ }
614+
615+ // Naming the tenant's own class is the same as leaving the field empty, so it
616+ // needs no entry in the allowed list.
617+ func TestApplySchedulingClass_OwnerApplicationNamingTenantClassNeedsNoAllowedList (t * testing.T ) {
618+ ctx := context .Background ()
619+ h := newWebhookTestEnv (t , "tenant-acme" , testSchedulingClass , true )
620+ owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{"schedulingClass" : testSchedulingClass })
621+ pod := newPod ("tenant-acme" , "db-0" )
622+
623+ if err := h .applySchedulingClass (ctx , pod , owner , "tenant-acme" ); err != nil {
624+ t .Fatalf ("applySchedulingClass returned error: %v" , err )
625+ }
626+
627+ if got := pod .GetLabels ()[schedulerapi .SchedulingClassLabel ]; got != testSchedulingClass {
628+ t .Errorf ("expected pod label %s=%s, got %q" , schedulerapi .SchedulingClassLabel , testSchedulingClass , got )
629+ }
630+ }
631+
632+ // Without a tenant class there is nothing to fall back to: a rejected
633+ // application class leaves the pod on the default scheduler.
634+ func TestApplySchedulingClass_RejectedOwnerApplicationClassWithoutTenantClassIsNoOp (t * testing.T ) {
635+ ctx := context .Background ()
636+ h := newWebhookTestEnv (t , "tenant-acme" , "" , false )
637+ addSchedulingClassCR (t , h , "app-class" )
638+ owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{"schedulingClass" : "app-class" })
639+ pod := newPod ("tenant-acme" , "db-0" )
640+
641+ if err := h .applySchedulingClass (ctx , pod , owner , "tenant-acme" ); err != nil {
642+ t .Fatalf ("applySchedulingClass returned error: %v" , err )
643+ }
644+
645+ if _ , ok := pod .GetAnnotations ()[schedulerapi .SchedulingClassAnnotation ]; ok {
646+ t .Errorf ("pod annotation should not be set for a class the namespace does not allow" )
647+ }
648+ if sched , _ , _ := unstructured .NestedString (pod .Object , "spec" , "schedulerName" ); sched != "" {
649+ t .Errorf ("pod spec.schedulerName should not be set, got %q" , sched )
650+ }
651+ }
0 commit comments