@@ -101,3 +101,57 @@ func EnsureClusterCAForRegistryAddon(
101101 const caCrtKey = "ca.crt"
102102 Expect (rootCASecret .Data [caCrtKey ]).To (Equal (rootCASecret .Data [caCrtKey ]))
103103}
104+
105+ type EnsureAntiAffnityForRegistryAddonInput struct {
106+ Registry * v1alpha1.RegistryAddon
107+ WorkloadCluster * clusterv1.Cluster
108+ ClusterProxy framework.ClusterProxy
109+ }
110+
111+ func EnsureAntiAffnityForRegistryAddon (
112+ ctx context.Context ,
113+ input EnsureAntiAffnityForRegistryAddonInput ,
114+ ) {
115+ if input .Registry == nil {
116+ return
117+ }
118+ Log ("Ensuring anti-affinity for registry addon in workload cluster" )
119+ workloadClusterClient := input .ClusterProxy .GetWorkloadCluster (
120+ ctx , input .WorkloadCluster .Namespace , input .WorkloadCluster .Name ,
121+ ).GetClient ()
122+
123+ sts := & appsv1.StatefulSet {
124+ ObjectMeta : metav1.ObjectMeta {
125+ Name : "cncf-distribution-registry-docker-registry" ,
126+ Namespace : "registry-system" ,
127+ },
128+ }
129+ err := workloadClusterClient .Get (ctx , ctrlclient .ObjectKeyFromObject (sts ), sts )
130+ Expect (err ).NotTo (HaveOccurred ())
131+ Expect (sts .Spec .Template .Spec .Affinity ).ToNot (BeNil ())
132+ Expect (sts .Spec .Template .Spec .Affinity .PodAntiAffinity ).ToNot (BeNil ())
133+ podAntiAffinity := sts .Spec .Template .Spec .Affinity .PodAntiAffinity
134+ Expect (
135+ podAntiAffinity .PreferredDuringSchedulingIgnoredDuringExecution ,
136+ ).ToNot (BeEmpty ())
137+ Expect (
138+ podAntiAffinity .PreferredDuringSchedulingIgnoredDuringExecution [0 ].Weight ,
139+ ).To (Equal (int32 (100 )))
140+ podAffinityTerm := podAntiAffinity .PreferredDuringSchedulingIgnoredDuringExecution [0 ].PodAffinityTerm
141+ Expect (podAffinityTerm ).ToNot (BeNil ())
142+ Expect (podAffinityTerm .TopologyKey ).To (Equal ("kubernetes.io/hostname" ))
143+ Expect (podAffinityTerm .LabelSelector ).ToNot (BeNil ())
144+ affinityLabels := podAffinityTerm .LabelSelector .MatchLabels
145+ Expect (
146+ affinityLabels [v1alpha1 .ClusterUUIDAnnotationKey ],
147+ ).To (Equal (input .WorkloadCluster .Annotations [v1alpha1 .ClusterUUIDAnnotationKey ]))
148+
149+ // test node affinity
150+ nodeAffinity := sts .Spec .Template .Spec .Affinity .NodeAffinity
151+ Expect (nodeAffinity ).ToNot (BeNil ())
152+ Expect (nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution ).ToNot (BeNil ())
153+ nodeSelectorTerm := nodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution .NodeSelectorTerms [0 ]
154+ Expect (nodeSelectorTerm ).ToNot (BeNil ())
155+ Expect (nodeSelectorTerm .MatchExpressions ).ToNot (BeEmpty ())
156+ Expect (nodeSelectorTerm .MatchExpressions [0 ].Key ).To (Equal ("node-role.kubernetes.io/control-plane" ))
157+ }
0 commit comments