Skip to content

Commit 4443979

Browse files
committed
fixes aff/anti-aff placement constraints for VMOP webhook happiness
1 parent 257fa37 commit 4443979

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

pkg/services/vmoperator/constants.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ limitations under the License.
1818
package vmoperator
1919

2020
const (
21-
kubeTopologyZoneLabelKey = "topology.kubernetes.io/zone"
22-
kubeHostNameLabelKey = "kubernetes.io/hostname"
23-
2421
// ControlPlaneVMClusterModuleGroupName is the name used for the control plane Cluster Module.
2522
ControlPlaneVMClusterModuleGroupName = "control-plane-group"
2623
// ClusterModuleNameAnnotationKey is key for the Cluster Module annotation.

pkg/services/vmoperator/vmopmachine.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func (v *VmopMachineService) ReconcileNormal(ctx context.Context, machineCtx cap
194194
// Set the VM state. Will get reset throughout the reconcile
195195
supervisorMachineCtx.VSphereMachine.Status.VMStatus = vmwarev1.VirtualMachineStatePending
196196

197-
// var vmAffinitySpec *vmoprv1.VirtualMachineAffinitySpec
198197
var affInfo affinityInfo
199198
if feature.Gates.Enabled(feature.NodeAutoPlacement) &&
200199
!infrautilv1.IsControlPlaneMachine(machineCtx.GetVSphereMachine()) {
@@ -233,6 +232,21 @@ func (v *VmopMachineService) ReconcileNormal(ctx context.Context, machineCtx cap
233232
affInfo.failureDomain = ptr.To(zone)
234233
}
235234

235+
// Fetch machine deployments without explicit failureDomain specified
236+
// to use when setting the anti-affinity rules
237+
machineDeployments := &clusterv1.MachineDeploymentList{}
238+
if err := v.Client.List(ctx, machineDeployments,
239+
client.InNamespace(supervisorMachineCtx.Cluster.Namespace),
240+
client.MatchingLabels{clusterv1.ClusterNameLabel: supervisorMachineCtx.Cluster.Name}); err != nil {
241+
return false, err
242+
}
243+
mdNames := []string{}
244+
for _, machineDeployment := range machineDeployments.Items {
245+
if machineDeployment.Spec.Template.Spec.FailureDomain == "" && machineDeployment.Name != nodePool {
246+
mdNames = append(mdNames, machineDeployment.Name)
247+
}
248+
}
249+
236250
affInfo.vmAffinitySpec = &vmoprv1.VirtualMachineAffinitySpec{
237251
VMAffinity: &vmoprv1.VirtualMachineAffinityVMAffinitySpec{
238252
RequiredDuringSchedulingIgnoredDuringExecution: []vmoprv1.VMAffinityTerm{
@@ -242,7 +256,7 @@ func (v *VmopMachineService) ReconcileNormal(ctx context.Context, machineCtx cap
242256
clusterv1.MachineDeploymentNameLabel: nodePool,
243257
},
244258
},
245-
TopologyKey: kubeTopologyZoneLabelKey,
259+
TopologyKey: corev1.LabelTopologyZone,
246260
},
247261
},
248262
},
@@ -253,14 +267,8 @@ func (v *VmopMachineService) ReconcileNormal(ctx context.Context, machineCtx cap
253267
MatchLabels: map[string]string{
254268
clusterv1.MachineDeploymentNameLabel: nodePool,
255269
},
256-
MatchExpressions: []metav1.LabelSelectorRequirement{
257-
{
258-
Key: clusterv1.MachineControlPlaneLabel,
259-
Operator: metav1.LabelSelectorOpDoesNotExist,
260-
},
261-
},
262270
},
263-
TopologyKey: kubeHostNameLabelKey,
271+
TopologyKey: corev1.LabelHostname,
264272
},
265273
},
266274
PreferredDuringSchedulingIgnoredDuringExecution: []vmoprv1.VMAffinityTerm{
@@ -269,16 +277,12 @@ func (v *VmopMachineService) ReconcileNormal(ctx context.Context, machineCtx cap
269277
MatchExpressions: []metav1.LabelSelectorRequirement{
270278
{
271279
Key: clusterv1.MachineDeploymentNameLabel,
272-
Operator: metav1.LabelSelectorOpNotIn,
273-
Values: []string{nodePool},
274-
},
275-
{
276-
Key: clusterv1.MachineControlPlaneLabel,
277-
Operator: metav1.LabelSelectorOpDoesNotExist,
280+
Operator: metav1.LabelSelectorOpIn,
281+
Values: mdNames,
278282
},
279283
},
280284
},
281-
TopologyKey: kubeTopologyZoneLabelKey,
285+
TopologyKey: corev1.LabelTopologyZone,
282286
},
283287
},
284288
},
@@ -845,7 +849,7 @@ func (v *VmopMachineService) addVolumes(ctx context.Context, supervisorMachineCt
845849

846850
if zone := supervisorMachineCtx.VSphereMachine.Spec.FailureDomain; zonal && zone != nil {
847851
topology := []map[string]string{
848-
{kubeTopologyZoneLabelKey: *zone},
852+
{corev1.LabelTopologyZone: *zone},
849853
}
850854
b, err := json.Marshal(topology)
851855
if err != nil {
@@ -930,12 +934,12 @@ func getTopologyLabels(supervisorMachineCtx *vmware.SupervisorMachineContext, fa
930934
// TODO: Make it so that we always set the zone label, might require enquiring the zones present (when unset)
931935
if fd := supervisorMachineCtx.VSphereMachine.Spec.FailureDomain; fd != nil && *fd != "" {
932936
return map[string]string{
933-
kubeTopologyZoneLabelKey: *fd,
937+
corev1.LabelTopologyZone: *fd,
934938
}
935939
}
936940
if failureDomain != nil && *failureDomain != "" {
937941
return map[string]string{
938-
kubeTopologyZoneLabelKey: *failureDomain,
942+
corev1.LabelTopologyZone: *failureDomain,
939943
}
940944
}
941945
return nil

0 commit comments

Comments
 (0)