-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 [WIP] supervisor: create ClusterModule per MachineDeployment and re-reconcile VirtualMachineResourceSetPolicy to update VirtualMachineSetResourcePolicy #3287
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package vmware is the package for webhooks of vmware resources. | ||
package vmware | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" | ||
"sigs.k8s.io/cluster-api-provider-vsphere/feature" | ||
"sigs.k8s.io/cluster-api-provider-vsphere/internal/webhooks" | ||
) | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-vspherecluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=vmware.infrastructure.cluster.x-k8s.io,resources=vsphereclusters,versions=v1beta1,name=validation.vspherecluster.vmware.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 | ||
|
||
// VSphereClusterWebhook implements a validation and defaulting webhook for VSphereCluster. | ||
type VSphereClusterWebhook struct{} | ||
|
||
var _ webhook.CustomValidator = &VSphereClusterWebhook{} | ||
|
||
func (webhook *VSphereClusterWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&vmwarev1.VSphereCluster{}). | ||
WithValidator(webhook). | ||
Complete() | ||
} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereClusterWebhook) ValidateCreate(_ context.Context, newRaw runtime.Object) (admission.Warnings, error) { | ||
var allErrs field.ErrorList | ||
|
||
newTyped, ok := newRaw.(*vmwarev1.VSphereCluster) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereCluster but got a %T", newRaw)) | ||
} | ||
|
||
newSpec := newTyped.Spec | ||
|
||
if !feature.Gates.Enabled(feature.WorkerAntiAffinity) { | ||
// Cluster mode is not allowed without WorkerAntiAffinity being enabled. | ||
if newSpec.Placement != nil && newSpec.Placement.WorkerAntiAffinity != nil && newSpec.Placement.WorkerAntiAffinity.Mode == vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment { | ||
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "placement", "workerAntiAffinity", "mode"), "cannot be set to Cluster with feature-gate WorkerAntiAffinity being disabled")) | ||
} | ||
} | ||
|
||
return nil, webhooks.AggregateObjErrors(newTyped.GroupVersionKind().GroupKind(), newTyped.Name, allErrs) | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereClusterWebhook) ValidateUpdate(_ context.Context, _ runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { | ||
var allErrs field.ErrorList | ||
|
||
newTyped, ok := newRaw.(*vmwarev1.VSphereCluster) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereCluster but got a %T", newRaw)) | ||
} | ||
|
||
newSpec := newTyped.Spec | ||
|
||
if !feature.Gates.Enabled(feature.WorkerAntiAffinity) { | ||
// Cluster mode is not allowed without WorkerAntiAffinity being enabled. | ||
if newSpec.Placement != nil && newSpec.Placement.WorkerAntiAffinity != nil && newSpec.Placement.WorkerAntiAffinity.Mode == vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment { | ||
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "placement", "workerAntiAffinity", "mode"), "cannot be set to Cluster with feature-gate WorkerAntiAffinity being disabled")) | ||
} | ||
} | ||
|
||
return nil, webhooks.AggregateObjErrors(newTyped.GroupVersionKind().GroupKind(), newTyped.Name, allErrs) | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereClusterWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package vmware | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
utilfeature "k8s.io/component-base/featuregate/testing" | ||
|
||
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" | ||
"sigs.k8s.io/cluster-api-provider-vsphere/feature" | ||
) | ||
|
||
func TestVSphereCluster_ValidateCreate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
vsphereCluster *vmwarev1.VSphereCluster | ||
workerAntiAffinity bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Allow Cluster (WorkerAntiAffinity=false)", | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
workerAntiAffinity: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Allow Cluster (WorkerAntiAffinity=true)", | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
workerAntiAffinity: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Allow None (WorkerAntiAffinity=false)", | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
workerAntiAffinity: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Allow None (WorkerAntiAffinity=true)", | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
workerAntiAffinity: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Deny MachineDeployment (WorkerAntiAffinity=false)", | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment), | ||
workerAntiAffinity: false, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Allow MachineDeployment (WorkerAntiAffinity=true)", | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment), | ||
workerAntiAffinity: true, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.WorkerAntiAffinity, tt.workerAntiAffinity) | ||
|
||
webhook := &VSphereClusterWebhook{} | ||
_, err := webhook.ValidateCreate(context.Background(), tt.vsphereCluster) | ||
if tt.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
} | ||
} | ||
func TestVSphereCluster_ValidateUpdate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
oldVSphereCluster *vmwarev1.VSphereCluster | ||
vsphereCluster *vmwarev1.VSphereCluster | ||
workerAntiAffinity bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "noop (WorkerAntiAffinity=false)", | ||
oldVSphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
workerAntiAffinity: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "noop (WorkerAntiAffinity=true)", | ||
oldVSphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
workerAntiAffinity: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Allow Cluster to None (WorkerAntiAffinity=false)", | ||
oldVSphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeNone), | ||
workerAntiAffinity: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Allow Cluster to None (WorkerAntiAffinity=true)", | ||
oldVSphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeNone), | ||
workerAntiAffinity: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Disallow Cluster to MachineDeployment (WorkerAntiAffinity=false)", | ||
oldVSphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment), | ||
workerAntiAffinity: false, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Allow Cluster to MachineDeployment (WorkerAntiAffinity=true)", | ||
oldVSphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster), | ||
vsphereCluster: createVSphereCluster(vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment), | ||
workerAntiAffinity: true, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.WorkerAntiAffinity, tt.workerAntiAffinity) | ||
|
||
webhook := &VSphereClusterWebhook{} | ||
_, err := webhook.ValidateUpdate(context.Background(), tt.oldVSphereCluster, tt.vsphereCluster) | ||
if tt.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func createVSphereCluster(mode vmwarev1.VSphereClusterWorkerAntiAffinityMode) *vmwarev1.VSphereCluster { | ||
vSphereCluster := &vmwarev1.VSphereCluster{} | ||
if mode != "" { | ||
vSphereCluster.Spec.Placement = &vmwarev1.VSphereClusterPlacement{ | ||
WorkerAntiAffinity: &vmwarev1.VSphereClusterWorkerAntiAffinity{ | ||
Mode: mode, | ||
}, | ||
} | ||
} | ||
return vSphereCluster | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ import ( | |
|
||
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1" | ||
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" | ||
"sigs.k8s.io/cluster-api-provider-vsphere/feature" | ||
capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context" | ||
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/vmware" | ||
infrautilv1 "sigs.k8s.io/cluster-api-provider-vsphere/pkg/util" | ||
|
@@ -432,7 +433,9 @@ func (v *VmopMachineService) reconcileVMOperatorVM(ctx context.Context, supervis | |
// Assign the VM's labels. | ||
vmOperatorVM.Labels = getVMLabels(supervisorMachineCtx, vmOperatorVM.Labels) | ||
|
||
addResourcePolicyAnnotations(supervisorMachineCtx, vmOperatorVM) | ||
if err := addResourcePolicyAnnotations(ctx, v.Client, supervisorMachineCtx, vmOperatorVM); err != nil { | ||
return err | ||
} | ||
|
||
if err := v.addVolumes(ctx, supervisorMachineCtx, vmOperatorVM); err != nil { | ||
return err | ||
|
@@ -537,7 +540,7 @@ func (v *VmopMachineService) getVirtualMachinesInCluster(ctx context.Context, su | |
|
||
// Helper function to add annotations to indicate which tag vm-operator should add as well as which clusterModule VM | ||
// should be associated. | ||
func addResourcePolicyAnnotations(supervisorMachineCtx *vmware.SupervisorMachineContext, vm *vmoprv1.VirtualMachine) { | ||
func addResourcePolicyAnnotations(ctx context.Context, ctrlClient client.Client, supervisorMachineCtx *vmware.SupervisorMachineContext, vm *vmoprv1.VirtualMachine) error { | ||
annotations := vm.ObjectMeta.GetAnnotations() | ||
if annotations == nil { | ||
annotations = make(map[string]string) | ||
|
@@ -548,10 +551,49 @@ func addResourcePolicyAnnotations(supervisorMachineCtx *vmware.SupervisorMachine | |
annotations[ClusterModuleNameAnnotationKey] = ControlPlaneVMClusterModuleGroupName | ||
} else { | ||
annotations[ProviderTagsAnnotationKey] = WorkerVMVMAntiAffinityTagValue | ||
annotations[ClusterModuleNameAnnotationKey] = getMachineDeploymentNameForCluster(supervisorMachineCtx.Cluster) | ||
|
||
// Only set the ClusterModuleGroup annotation if not already set | ||
if _, ok := annotations[ClusterModuleNameAnnotationKey]; !ok { | ||
var clusterModuleGroupName string | ||
// Set ClusterModuleGroupName depending on the configured mode from VSphereCluster.Spec.Placement.WorkerAntiAffinity.Mode | ||
// and the WorkerAntiAffinity feature-gate | ||
switch mode := getWorkerAntiAffinityMode(supervisorMachineCtx.VSphereCluster); mode { | ||
case vmwarev1.VSphereClusterWorkerAntiAffinityModeNone: | ||
// Nothing to set. | ||
case vmwarev1.VSphereClusterWorkerAntiAffinityModeCluster: | ||
if feature.Gates.Enabled(feature.WorkerAntiAffinity) { | ||
// Set the cluster-wide group name. | ||
clusterModuleGroupName = ClusterWorkerVMClusterModuleGroupName | ||
} else { | ||
// Fallback to old name. | ||
clusterModuleGroupName = getFallbackWorkerClusterModuleGroupName(supervisorMachineCtx.Cluster.Name) | ||
} | ||
case vmwarev1.VSphereClusterWorkerAntiAffinityModeMachineDeployment: | ||
if !feature.Gates.Enabled(feature.WorkerAntiAffinity) { | ||
// This should not be possible because MachineDeployment mode is only allowed with the feature enabled. | ||
return errors.New("failed to set ClusterModuleGroup in mode MachineDeployment with WorkerAntiAffinity disabled") | ||
} | ||
// Set the MachineDeployment name as ClusterModuleGroup. | ||
var ok bool | ||
clusterModuleGroupName, ok = supervisorMachineCtx.Machine.Labels[clusterv1.MachineDeploymentNameLabel] | ||
if !ok { | ||
return errors.Errorf("failed to set ClusterModuleGroup because of missing label %s on Machine", clusterv1.MachineDeploymentNameLabel) | ||
} | ||
default: | ||
return errors.Errorf("unknown mode %q configured for WorkerAntiAffinity", mode) | ||
} | ||
|
||
if clusterModuleGroupName != "" { | ||
if err := checkClusterModuleGroup(ctx, ctrlClient, supervisorMachineCtx.Cluster, clusterModuleGroupName); err != nil { | ||
return err | ||
} | ||
annotations[ClusterModuleNameAnnotationKey] = clusterModuleGroupName | ||
} | ||
} | ||
} | ||
|
||
vm.ObjectMeta.SetAnnotations(annotations) | ||
return nil | ||
} | ||
|
||
func volumeName(machine *vmwarev1.VSphereMachine, volume vmwarev1.VSphereMachineVolume) string { | ||
|
@@ -699,8 +741,20 @@ func getTopologyLabels(supervisorMachineCtx *vmware.SupervisorMachineContext) ma | |
return nil | ||
} | ||
|
||
// getMachineDeploymentName returns the MachineDeployment name for a Cluster. | ||
// This is also the name used by VSphereMachineTemplate and KubeadmConfigTemplate. | ||
func getMachineDeploymentNameForCluster(cluster *clusterv1.Cluster) string { | ||
return fmt.Sprintf("%s-workers-0", cluster.Name) | ||
func getMachineDeploymentNamesForCluster(ctx context.Context, ctrlClient client.Client, cluster *clusterv1.Cluster) ([]string, error) { | ||
mdNames := []string{} | ||
labels := map[string]string{clusterv1.ClusterNameLabel: cluster.GetName()} | ||
mdList := &clusterv1.MachineDeploymentList{} | ||
if err := ctrlClient.List( | ||
ctx, mdList, | ||
client.InNamespace(cluster.GetNamespace()), | ||
client.MatchingLabels(labels)); err != nil { | ||
return nil, errors.Wrapf(err, "failed to list MachineDeployment objects") | ||
} | ||
for _, md := range mdList.Items { | ||
if md.DeletionTimestamp.IsZero() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely sure we should drop deleted MD, there could still be machines for them. |
||
mdNames = append(mdNames, md.Name) | ||
} | ||
} | ||
return mdNames, nil | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -59,6 +59,36 @@ func GetVSphereClusterFromVMwareMachine(ctx context.Context, c client.Client, ma | |||||
return vsphereCluster, err | ||||||
} | ||||||
|
||||||
// GetVMwareVSphereClusterFromMachineDeployment gets the vmware.infrastructure.cluster.x-k8s.io.VSphereCluster resource for the given MachineDeployment$. | ||||||
func GetVMwareVSphereClusterFromMachineDeployment(ctx context.Context, c client.Client, machineDeployment *clusterv1.MachineDeployment) (*vmwarev1.VSphereCluster, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
clusterName := machineDeployment.Labels[clusterv1.ClusterNameLabel] | ||||||
if clusterName == "" { | ||||||
return nil, errors.Errorf("error getting VSphereCluster name from MachineDeployment %s/%s", | ||||||
machineDeployment.Namespace, machineDeployment.Name) | ||||||
} | ||||||
namespacedName := apitypes.NamespacedName{ | ||||||
Namespace: machineDeployment.Namespace, | ||||||
Name: clusterName, | ||||||
} | ||||||
cluster := &clusterv1.Cluster{} | ||||||
if err := c.Get(ctx, namespacedName, cluster); err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
if cluster.Spec.InfrastructureRef == nil { | ||||||
return nil, errors.Errorf("error getting VSphereCluster name from MachineDeployment %s/%s: Cluster.spec.infrastructureRef not yet set", | ||||||
machineDeployment.Namespace, machineDeployment.Name) | ||||||
} | ||||||
|
||||||
vsphereClusterKey := apitypes.NamespacedName{ | ||||||
Namespace: machineDeployment.Namespace, | ||||||
Name: cluster.Spec.InfrastructureRef.Name, | ||||||
} | ||||||
vsphereCluster := &vmwarev1.VSphereCluster{} | ||||||
err := c.Get(ctx, vsphereClusterKey, vsphereCluster) | ||||||
return vsphereCluster, err | ||||||
} | ||||||
|
||||||
// GetVSphereClusterFromVSphereMachine gets the infrastructure.cluster.x-k8s.io.VSphereCluster resource for the given VSphereMachine. | ||||||
func GetVSphereClusterFromVSphereMachine(ctx context.Context, c client.Client, machine *infrav1.VSphereMachine) (*infrav1.VSphereCluster, error) { | ||||||
clusterName := machine.Labels[clusterv1.ClusterNameLabel] | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I prefer this approach (using the patch helper), but let's consider also using createOrUpdate for consistency with the rest of the codebase