Skip to content

Commit 6c30e5a

Browse files
committed
test: unit test to verify if anti affinity is set
1 parent 3463908 commit 6c30e5a

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

charts/cluster-api-runtime-extensions-nutanix/addons/registry/cncf-distribution/values-template.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ affinity:
4646
podAffinityTerm:
4747
labelSelector:
4848
matchLabels:
49-
caren.nutanix.com/cluster-uuid: {{ .ClusterUUID }}
49+
caren.nutanix.com/cluster-uuid: {{ .ClusterUUID }}
50+
topologyKey: kubernetes.io/hostname

test/e2e/quick_start_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ var _ = Describe("Quick start", func() {
315315
ClusterProxy: proxy,
316316
},
317317
)
318+
319+
EnsureAntiAffnityForRegistryAddon(
320+
ctx,
321+
EnsureAntiAffnityForRegistryAddonInput{
322+
Registry: addonsConfig.Registry,
323+
WorkloadCluster: workloadCluster,
324+
ClusterProxy: proxy,
325+
},
326+
)
318327
},
319328
}
320329
})

test/e2e/registry.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

test/e2e/self_hosted_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ var _ = Describe("Self-hosted", Serial, func() {
177177
ClusterProxy: proxy,
178178
},
179179
)
180+
181+
EnsureAntiAffnityForRegistryAddon(
182+
ctx,
183+
EnsureAntiAffnityForRegistryAddonInput{
184+
Registry: addonsConfig.Registry,
185+
WorkloadCluster: workloadCluster,
186+
ClusterProxy: proxy,
187+
},
188+
)
180189
},
181190
}
182191
},

test/e2e/statefulset_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type WaitForStatefulSetAvailableInput struct {
2121
StatefulSet *appsv1.StatefulSet
2222
}
2323

24-
// WaitForStatefulSetsAvailable waits until the Deployment has observedGeneration equal to generation and
24+
// WaitForStatefulSetsAvailable waits until the Statefulset has observedGeneration equal to generation and
2525
// status.Available = True, that signals that all the desired replicas are in place.
2626
func WaitForStatefulSetsAvailable(
2727
ctx context.Context, input WaitForStatefulSetAvailableInput, intervals ...interface{},

0 commit comments

Comments
 (0)