@@ -30,8 +30,6 @@ import (
3030 corev1 "k8s.io/api/core/v1"
3131 "k8s.io/apimachinery/pkg/api/resource"
3232 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33- "k8s.io/apimachinery/pkg/util/intstr"
34- "k8s.io/utils/ptr"
3533
3634 k8swgmanifests "github.com/k8stopologyawareschedwg/deployer/pkg/manifests"
3735
@@ -635,122 +633,40 @@ func TestDeploymentTLSSettingsRepeated(t *testing.T) {
635633 }
636634}
637635
638- func TestDeploymentAffinityWithStrategyNoLabels (t * testing.T ) {
636+ func TestDeploymentTopologySpreadConstraintsNoLabels (t * testing.T ) {
639637 dp := dpMinimal .DeepCopy ()
640- err := DeploymentAffinityWithStrategy ( dp , nropv1. NUMAResourcesSchedulerSpec {})
641- if err == nil {
642- t .Fatalf ("expected error but received nil" )
638+ dp . Spec . Template . Labels = nil
639+ if err := DeploymentTopologySpreadConstraints ( dp ); err == nil {
640+ t .Fatalf ("expected error but got nil" )
643641 }
644- }
645642
646- func TestDeploymentAffinityWithStrategyExplicitNonNilReplicasCount (t * testing.T ) {
647- testcases := []struct {
648- name string
649- nonNilReplicasCount * int32
650- }{
651- {
652- name : "replicas count is positive int" ,
653- nonNilReplicasCount : ptr .To (int32 (2 )),
654- },
655- {
656- name : "replicas count is zero" ,
657- nonNilReplicasCount : ptr .To (int32 (0 )),
658- },
659- }
660- for _ , tc := range testcases {
661- t .Run (tc .name , func (t * testing.T ) {
662- dp := dpMinimal .DeepCopy ()
663- dp .Spec .Template .ObjectMeta .Labels = map [string ]string {"app" : "scheduler" }
664- initialNodeAffinity := & corev1.NodeAffinity {
665- RequiredDuringSchedulingIgnoredDuringExecution : & corev1.NodeSelector {
666- NodeSelectorTerms : []corev1.NodeSelectorTerm {
667- {
668- MatchExpressions : []corev1.NodeSelectorRequirement {
669- {
670- Key : "node-role.kubernetes.io/worker" ,
671- Operator : corev1 .NodeSelectorOpExists ,
672- Values : []string {"" },
673- },
674- },
675- },
676- },
677- },
678- }
679- dp .Spec .Template .Spec .Affinity = & corev1.Affinity {
680- NodeAffinity : initialNodeAffinity ,
681- PodAntiAffinity : & corev1.PodAntiAffinity {
682- RequiredDuringSchedulingIgnoredDuringExecution : []corev1.PodAffinityTerm {
683- {
684- LabelSelector : & metav1.LabelSelector {
685- MatchLabels : map [string ]string {"app" : "scheduler" },
686- },
687- },
688- },
689- },
690- }
691- err := DeploymentAffinityWithStrategy (dp , nropv1.NUMAResourcesSchedulerSpec {Replicas : tc .nonNilReplicasCount })
692- if err != nil {
693- t .Fatalf ("unexpected error: %v" , err )
694- }
695- // check that the node affinity is preserved
696- if diff := cmp .Diff (initialNodeAffinity , dp .Spec .Template .Spec .Affinity .NodeAffinity ); diff != "" {
697- t .Errorf ("should preserve existing NodeAffinity: affinity mismatch (-expected +got):\n %s" , diff )
698- }
699- if dp .Spec .Template .Spec .Affinity .PodAntiAffinity != nil {
700- t .Fatalf ("expected podAntiAffinity to be reset but got %v" , dp .Spec .Template .Spec .Affinity .PodAntiAffinity )
701- }
702- if dp .Spec .Strategy != (appsv1.DeploymentStrategy {}) {
703- t .Fatalf ("expected strategy to be reset but got %v" , dp .Spec .Strategy )
704- }
705- })
643+ dp .Spec .Template .Labels = map [string ]string {}
644+ if err := DeploymentTopologySpreadConstraints (dp ); err == nil {
645+ t .Fatalf ("expected error but got nil" )
706646 }
707647}
708648
709- func TestDeploymentAffinityWithStrategyWithOverride (t * testing.T ) {
710- labels := map [string ]string {"app" : "scheduler" }
711- expectedStrategy := appsv1.DeploymentStrategy {
712- Type : appsv1 .RollingUpdateDeploymentStrategyType ,
713- RollingUpdate : & appsv1.RollingUpdateDeployment {
714- MaxUnavailable : ptr .To (intstr .FromInt (1 )),
715- MaxSurge : ptr .To (intstr .FromInt (0 )),
716- },
717- }
718- expectedPodAntiAffinity := & corev1.PodAntiAffinity {
719- RequiredDuringSchedulingIgnoredDuringExecution : []corev1.PodAffinityTerm {
720- {
721- LabelSelector : & metav1.LabelSelector {
722- MatchLabels : labels ,
723- },
724- TopologyKey : "kubernetes.io/hostname" ,
725- },
726- },
727- }
649+ func TestDeploymentTopologySpreadConstraints (t * testing.T ) {
728650 dp := dpMinimal .DeepCopy ()
729- dp .Spec .Template .ObjectMeta .Labels = labels
730-
731- err := DeploymentAffinityWithStrategy (dp , nropv1.NUMAResourcesSchedulerSpec {})
732- if err != nil {
733- t .Fatalf ("unexpected error: %v" , err )
734- }
735-
736- if diff := cmp .Diff (expectedPodAntiAffinity , dp .Spec .Template .Spec .Affinity .PodAntiAffinity ); diff != "" {
737- t .Errorf ("affinity mismatch (-expected +got):\n %s" , diff )
738- }
739- if diff := cmp .Diff (expectedStrategy , dp .Spec .Strategy ); diff != "" {
740- t .Errorf ("strategy mismatch (-expected +got):\n %s" , diff )
651+ dp .Spec .Template .Labels = map [string ]string {
652+ "app" : "numaresources-scheduler" ,
741653 }
742-
743- // check reset works
744- err = DeploymentAffinityWithStrategy (dp , nropv1.NUMAResourcesSchedulerSpec {Replicas : ptr .To (int32 (2 ))})
745- if err != nil {
654+ if err := DeploymentTopologySpreadConstraints (dp ); err != nil {
746655 t .Fatalf ("unexpected error: %v" , err )
747656 }
748-
749- if dp .Spec .Template .Spec .Affinity != nil && dp .Spec .Template .Spec .Affinity .PodAntiAffinity != nil {
750- t .Fatalf ("Override failed: expected no podAntiAffinity but got %v" , dp .Spec .Template .Spec .Affinity .PodAntiAffinity )
657+ expectedConstraints := []corev1.TopologySpreadConstraint {
658+ {
659+ LabelSelector : & metav1.LabelSelector {
660+ MatchLabels : dp .Spec .Template .Labels ,
661+ },
662+ MaxSkew : 1 ,
663+ TopologyKey : "kubernetes.io/hostname" ,
664+ WhenUnsatisfiable : corev1 .DoNotSchedule ,
665+ MatchLabelKeys : []string {"pod-template-hash" },
666+ },
751667 }
752- if dp .Spec .Strategy != (appsv1. DeploymentStrategy {}) {
753- t .Fatalf ( "expected strategy to be reset but got %v " , dp . Spec . Strategy )
668+ if diff := cmp . Diff ( dp .Spec .Template . Spec . TopologySpreadConstraints , expectedConstraints ); diff != "" {
669+ t .Errorf ( "constraints mismatch \n %s " , diff )
754670 }
755671}
756672
0 commit comments