diff --git a/controllers/defaults/placements.go b/controllers/defaults/placements.go index 7c9a76c55a..25d45a0e18 100644 --- a/controllers/defaults/placements.go +++ b/controllers/defaults/placements.go @@ -76,7 +76,7 @@ var ( }, PodAntiAffinity: &corev1.PodAntiAffinity{ PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{ - getWeightedPodAffinityTerm(100, "rook-ceph-mds"), + // left the selector value empty as it will be updated later in the getPlacement() }, }, }, @@ -158,7 +158,7 @@ func getTopologySpreadConstraintsSpec(maxSkew int32, valueLabels []string) corev } func getWeightedPodAffinityTerm(weight int32, selectorValue ...string) corev1.WeightedPodAffinityTerm { - WeightedPodAffinityTerm := corev1.WeightedPodAffinityTerm{ + return corev1.WeightedPodAffinityTerm{ Weight: weight, PodAffinityTerm: corev1.PodAffinityTerm{ LabelSelector: &metav1.LabelSelector{ @@ -173,7 +173,24 @@ func getWeightedPodAffinityTerm(weight int32, selectorValue ...string) corev1.We TopologyKey: corev1.LabelHostname, }, } - return WeightedPodAffinityTerm +} + +func GetMdsWeightedPodAffinityTerm(weight int32, selectorValue ...string) corev1.WeightedPodAffinityTerm { + return corev1.WeightedPodAffinityTerm{ + Weight: weight, + PodAffinityTerm: corev1.PodAffinityTerm{ + LabelSelector: &metav1.LabelSelector{ + MatchExpressions: []metav1.LabelSelectorRequirement{ + { + Key: "rook_file_system", + Operator: metav1.LabelSelectorOpIn, + Values: selectorValue, + }, + }, + }, + TopologyKey: corev1.LabelHostname, + }, + } } func getPodAffinityTerm(selectorValue ...string) corev1.PodAffinityTerm { diff --git a/controllers/storagecluster/placement.go b/controllers/storagecluster/placement.go index 2015192ab6..e251deb716 100644 --- a/controllers/storagecluster/placement.go +++ b/controllers/storagecluster/placement.go @@ -20,6 +20,14 @@ func getPlacement(sc *ocsv1.StorageCluster, component string) rookCephv1.Placeme } else { in := defaults.DaemonPlacements[component] (&in).DeepCopyInto(&placement) + // label rook_file_system is added to the mds pod using rook operator + if component == "mds" { + placement.PodAntiAffinity = &corev1.PodAntiAffinity{ + PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{ + defaults.GetMdsWeightedPodAffinityTerm(100, generateNameForCephFilesystem(sc)), + }, + } + } } // ignore default PodAntiAffinity mon placement when arbiter is enabled diff --git a/controllers/storagecluster/placement_test.go b/controllers/storagecluster/placement_test.go index 827be16cfa..d5737c6f71 100644 --- a/controllers/storagecluster/placement_test.go +++ b/controllers/storagecluster/placement_test.go @@ -375,9 +375,9 @@ func TestGetPlacement(t *testing.T) { LabelSelector: &metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ { - Key: "app", + Key: "rook_file_system", Operator: metav1.LabelSelectorOpIn, - Values: []string{"rook-ceph-mds"}, + Values: []string{"storage-test-cephfilesystem"}, }, }, }, @@ -507,6 +507,21 @@ func TestGetPlacement(t *testing.T) { assert.Equal(t, expectedPlacement, actualPlacement, c.label) expectedPlacement = c.expectedPlacements["mds"] + testPodAffinity := &corev1.PodAntiAffinity{ + PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{ + defaults.GetMdsWeightedPodAffinityTerm(100, generateNameForCephFilesystem(sc)), + }, + } + if expectedPlacement.PodAntiAffinity != nil { + topologyKeys := "" + if len(expectedPlacement.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) != 0 { + topologyKeys = expectedPlacement.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.TopologyKey + } + expectedPlacement.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution = testPodAffinity.PreferredDuringSchedulingIgnoredDuringExecution + if topologyKeys != "" { + expectedPlacement.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0].PodAffinityTerm.TopologyKey = topologyKeys + } + } actualPlacement = getPlacement(sc, "mds") assert.Equal(t, expectedPlacement, actualPlacement, c.label) }