-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add WorkerAntiAffinity feature gate and implement validating webhook
- Loading branch information
Showing
4 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
return &vmwarev1.VSphereCluster{ | ||
Spec: vmwarev1.VSphereClusterSpec{ | ||
Placement: vmwarev1.VSphereClusterPlacement{ | ||
WorkerAntiAffinity: vmwarev1.VSphereClusterWorkerAntiAffinity{ | ||
Mode: mode, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |