Skip to content

Commit 79eb403

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
resources: add support for setting affinity for pods in statefulsets
This patch includes the logic to add the anti-affinity rule that you typically want for ctdb clustered smbds at the end of the rules pulled from the common config. Signed-off-by: John Mulligan <[email protected]>
1 parent d1acef7 commit 79eb403

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

internal/resources/statefulsets.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func buildStatefulSet(
3131
size := planner.ClusterSize()
3232
podSpec := buildClusteredPodSpec(planner, dataPVCName, statePVCName)
3333
if planner.NodeSpread() {
34-
podSpec.Affinity = buildOneSmbdPerNodeAffinity(labels, serviceLabel)
34+
podSpec.Affinity = buildOneSmbdPerNodeAffinity(planner, labels, serviceLabel)
35+
} else {
36+
podSpec.Affinity = affinityForSmbPod(planner)
3537
}
3638
statefulSet := &appsv1.StatefulSet{
3739
ObjectMeta: metav1.ObjectMeta{
@@ -56,11 +58,15 @@ func buildStatefulSet(
5658
return statefulSet
5759
}
5860

59-
func buildOneSmbdPerNodeAffinity(labels map[string]string, key string) *corev1.Affinity {
61+
func buildOneSmbdPerNodeAffinity(
62+
planner *pln.Planner,
63+
labels map[string]string,
64+
key string) *corev1.Affinity {
65+
// ---
6066
topoKey := "kubernetes.io/hostname"
6167
value := labels[key]
6268

63-
aaTerms := []corev1.PodAffinityTerm{{
69+
onePodPerNode := corev1.PodAffinityTerm{
6470
TopologyKey: topoKey,
6571
LabelSelector: &metav1.LabelSelector{
6672
MatchExpressions: []metav1.LabelSelectorRequirement{{
@@ -71,11 +77,17 @@ func buildOneSmbdPerNodeAffinity(labels map[string]string, key string) *corev1.A
7177
},
7278
}},
7379
},
74-
}}
75-
return &corev1.Affinity{
76-
PodAntiAffinity: &corev1.PodAntiAffinity{
77-
RequiredDuringSchedulingIgnoredDuringExecution: aaTerms,
78-
// ^ what a mouthful!
79-
},
8080
}
81+
82+
affinity := affinityForSmbPod(planner)
83+
if affinity == nil {
84+
affinity = &corev1.Affinity{
85+
PodAntiAffinity: &corev1.PodAntiAffinity{},
86+
}
87+
}
88+
affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = append(
89+
affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution,
90+
onePodPerNode)
91+
// ^ what a mouthful!
92+
return affinity
8193
}

0 commit comments

Comments
 (0)