@@ -460,3 +460,96 @@ func TestApplySchedulingClass_MissingClassCRSkipsInjection(t *testing.T) {
460460 t .Errorf ("pod spec.schedulerName should not be set when SchedulingClass CR is missing, got %q" , sched )
461461 }
462462}
463+
464+ // newOwnerApplication returns the unstructured Application a Pod's lineage
465+ // walk resolves to, with the given top-level spec.
466+ func newOwnerApplication (namespace , name string , spec map [string ]interface {}) * unstructured.Unstructured {
467+ u := & unstructured.Unstructured {}
468+ u .SetGroupVersionKind (schema.GroupVersionKind {Group : "apps.cozystack.io" , Version : "v1alpha1" , Kind : "Postgres" })
469+ u .SetNamespace (namespace )
470+ u .SetName (name )
471+ if spec != nil {
472+ u .Object ["spec" ] = spec
473+ }
474+ return u
475+ }
476+
477+ func addSchedulingClassCR (t * testing.T , h * LineageControllerWebhook , name string ) {
478+ t .Helper ()
479+ if _ , err := h .dynClient .Resource (schedulingClassGVR ).Create (context .Background (), newSchedulingClassCR (name ), metav1.CreateOptions {}); err != nil {
480+ t .Fatalf ("creating SchedulingClass %s: %v" , name , err )
481+ }
482+ }
483+
484+ func TestApplySchedulingClass_OwnerApplicationClassOverridesNamespace (t * testing.T ) {
485+ ctx := context .Background ()
486+ h := newWebhookTestEnv (t , "tenant-acme" , testSchedulingClass , true )
487+ addSchedulingClassCR (t , h , "app-class" )
488+ owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{
489+ "replicas" : int64 (2 ),
490+ "schedulingClass" : "app-class" ,
491+ })
492+ pod := newPod ("tenant-acme" , "db-0" )
493+
494+ if err := h .applySchedulingClass (ctx , pod , owner , "tenant-acme" ); err != nil {
495+ t .Fatalf ("applySchedulingClass returned error: %v" , err )
496+ }
497+
498+ if got := pod .GetLabels ()[schedulerapi .SchedulingClassLabel ]; got != "app-class" {
499+ t .Errorf ("expected pod label %s=app-class, got %q" , schedulerapi .SchedulingClassLabel , got )
500+ }
501+ if got := pod .GetAnnotations ()[schedulerapi .SchedulingClassAnnotation ]; got != "app-class" {
502+ t .Errorf ("expected pod annotation %s=app-class, got %q" , schedulerapi .SchedulingClassAnnotation , got )
503+ }
504+ }
505+
506+ func TestApplySchedulingClass_OwnerApplicationWithoutClassFallsBackToNamespace (t * testing.T ) {
507+ for _ , tc := range []struct {
508+ name string
509+ spec map [string ]interface {}
510+ }{
511+ {name : "field absent" , spec : map [string ]interface {}{"replicas" : int64 (2 )}},
512+ {name : "field empty" , spec : map [string ]interface {}{"schedulingClass" : "" }},
513+ {name : "no spec" , spec : nil },
514+ } {
515+ t .Run (tc .name , func (t * testing.T ) {
516+ ctx := context .Background ()
517+ h := newWebhookTestEnv (t , "tenant-acme" , testSchedulingClass , true )
518+ pod := newPod ("tenant-acme" , "db-0" )
519+
520+ if err := h .applySchedulingClass (ctx , pod , newOwnerApplication ("tenant-acme" , "db" , tc .spec ), "tenant-acme" ); err != nil {
521+ t .Fatalf ("applySchedulingClass returned error: %v" , err )
522+ }
523+
524+ if got := pod .GetLabels ()[schedulerapi .SchedulingClassLabel ]; got != testSchedulingClass {
525+ t .Errorf ("expected the namespace class %q, got %q" , testSchedulingClass , got )
526+ }
527+ })
528+ }
529+ }
530+
531+ // A SchedulingClass name may be up to 253 characters, a label value only 63.
532+ // A Pod carrying the longer value would be rejected by the apiserver, so the
533+ // class must still apply through the annotation, without the label.
534+ func TestApplySchedulingClass_ClassNameTooLongForLabelSkipsOnlyLabel (t * testing.T ) {
535+ ctx := context .Background ()
536+ longClass := strings .Repeat ("a" , 64 )
537+ h := newWebhookTestEnv (t , "tenant-acme" , "" , false )
538+ addSchedulingClassCR (t , h , longClass )
539+ owner := newOwnerApplication ("tenant-acme" , "db" , map [string ]interface {}{"schedulingClass" : longClass })
540+ pod := newPod ("tenant-acme" , "db-0" )
541+
542+ if err := h .applySchedulingClass (ctx , pod , owner , "tenant-acme" ); err != nil {
543+ t .Fatalf ("applySchedulingClass returned error: %v" , err )
544+ }
545+
546+ if _ , ok := pod .GetLabels ()[schedulerapi .SchedulingClassLabel ]; ok {
547+ t .Errorf ("pod label should not be set for a class name longer than 63 characters" )
548+ }
549+ if got := pod .GetAnnotations ()[schedulerapi .SchedulingClassAnnotation ]; got != longClass {
550+ t .Errorf ("expected pod annotation %s=%s, got %q" , schedulerapi .SchedulingClassAnnotation , longClass , got )
551+ }
552+ if sched , _ , _ := unstructured .NestedString (pod .Object , "spec" , "schedulerName" ); sched != schedulerapi .SchedulerName {
553+ t .Errorf ("expected pod spec.schedulerName=%s, got %q" , schedulerapi .SchedulerName , sched )
554+ }
555+ }
0 commit comments