-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validating webhook for vm-operator VSphereMachine
Signed-off-by: Gong Zhang <gongz@vmware.com>
- Loading branch information
Showing
13 changed files
with
276 additions
and
9 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,4 @@ | ||
resources: | ||
- manifests.yaml | ||
|
||
namePrefix: supervisor- |
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,54 @@ | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: MutatingWebhookConfiguration | ||
metadata: | ||
name: mutating-webhook-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
- v1beta1 | ||
clientConfig: | ||
service: | ||
name: webhook-service | ||
namespace: system | ||
path: /mutate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-vspheremachine | ||
failurePolicy: Fail | ||
matchPolicy: Equivalent | ||
name: default.vspheremachine.vmware.infrastructure.cluster.x-k8s.io | ||
rules: | ||
- apiGroups: | ||
- vmware.infrastructure.cluster.x-k8s.io | ||
apiVersions: | ||
- v1beta1 | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: | ||
- vspheremachines | ||
sideEffects: None | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: ValidatingWebhookConfiguration | ||
metadata: | ||
name: validating-webhook-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
- v1beta1 | ||
clientConfig: | ||
service: | ||
name: webhook-service | ||
namespace: system | ||
path: /validate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-vspheremachine | ||
failurePolicy: Fail | ||
matchPolicy: Equivalent | ||
name: validation.vspheremachine.vmware.infrastructure.x-k8s.io | ||
rules: | ||
- apiGroups: | ||
- vmware.infrastructure.cluster.x-k8s.io | ||
apiVersions: | ||
- v1beta1 | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: | ||
- vspheremachines | ||
sideEffects: None |
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,105 @@ | ||
/* | ||
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 vmoperator is the package for webhooks of VM Operator resources. | ||
package vmoperator | ||
|
||
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/internal/webhooks" | ||
) | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-vspheremachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachines,versions=v1beta1,name=validation.vspheremachine.vmware.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 | ||
// +kubebuilder:webhook:verbs=create;update,path=/mutate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-vspheremachine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachines,versions=v1beta1,name=default.vspheremachine.vmware.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 | ||
|
||
// VSphereMachineWebhook implements a validation and defaulting webhook for VM Operator VSphereMachine. | ||
type VSphereMachineWebhook struct{} | ||
|
||
var _ webhook.CustomValidator = &VSphereMachineWebhook{} | ||
var _ webhook.CustomDefaulter = &VSphereMachineWebhook{} | ||
|
||
func (webhook *VSphereMachineWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&vmwarev1.VSphereMachine{}). | ||
WithValidator(webhook). | ||
WithDefaulter(webhook). | ||
Complete() | ||
} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type. | ||
func (webhook *VSphereMachineWebhook) Default(_ context.Context, _ runtime.Object) error { | ||
return nil | ||
} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereMachineWebhook) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (webhook *VSphereMachineWebhook) ValidateUpdate(_ context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { | ||
var allErrs field.ErrorList | ||
|
||
newTyped, ok := newRaw.(*vmwarev1.VSphereMachine) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VM Operator VSphereMachine but got a %T", newRaw)) | ||
} | ||
|
||
oldTyped, ok := oldRaw.(*vmwarev1.VSphereMachine) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VM Operator VSphereMachine but got a %T", oldRaw)) | ||
} | ||
|
||
newSpec, oldSpec := newTyped.Spec, oldTyped.Spec | ||
|
||
// In VM operator, following fields are immutable, so CAPV should not allow to update them. | ||
// - ImageName | ||
// - ClassName | ||
// - StorageClass | ||
// - MinHardwareVersion | ||
if newSpec.ImageName != oldSpec.ImageName { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "imageName"), newTyped.Spec, "updating imageName is not allowed")) | ||
} | ||
|
||
if newSpec.ClassName != oldSpec.ClassName { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "className"), newTyped.Spec, "updating className is not allowed")) | ||
} | ||
|
||
if newSpec.StorageClass != oldSpec.StorageClass { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "storageClass"), newTyped.Spec, "updating storageClass is not allowed")) | ||
} | ||
|
||
if newSpec.MinHardwareVersion != oldSpec.MinHardwareVersion { | ||
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "minHardwareVersion"), newTyped.Spec, "updating minHardwareVersion is not allowed")) | ||
} | ||
|
||
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 *VSphereMachineWebhook) 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,86 @@ | ||
/* | ||
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 vmoperator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" | ||
) | ||
|
||
func TestVSphereMachine_ValidateUpdate(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
tests := []struct { | ||
name string | ||
oldVSphereMachine *vmwarev1.VSphereMachine | ||
vsphereMachine *vmwarev1.VSphereMachine | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "updating ImageName cannot be done", | ||
oldVSphereMachine: createVSphereMachine("tkgs-old-imagename", "best-effort-xsmall", "wcpglobalstorageprofile", "vmx-15"), | ||
vsphereMachine: createVSphereMachine("tkgs-new-imagename", "best-effort-xsmall", "wcpglobalstorageprofile", "vmx-15"), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "updating ClassName cannot be done", | ||
oldVSphereMachine: createVSphereMachine("tkgs-imagename", "old-best-effort-xsmall", "wcpglobalstorageprofile", "vmx-15"), | ||
vsphereMachine: createVSphereMachine("tkgs-imagename", "new-best-effort-xsmall", "wcpglobalstorageprofile", "vmx-15"), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "updating StorageClass cannot be done", | ||
oldVSphereMachine: createVSphereMachine("tkgs-imagename", "best-effort-xsmall", "old-wcpglobalstorageprofile", "vmx-15"), | ||
vsphereMachine: createVSphereMachine("tkgs-imagename", "best-effort-xsmall", "new-wcpglobalstorageprofile", "vmx-15"), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "updating MinHardwareVersion cannot be done", | ||
oldVSphereMachine: createVSphereMachine("tkgs-imagename", "best-effort-xsmall", "wcpglobalstorageprofile", "vmx-15"), | ||
vsphereMachine: createVSphereMachine("tkgs-imagename", "best-effort-xsmall", "wcpglobalstorageprofile", "vmx-16"), | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
webhook := &VSphereMachineWebhook{} | ||
_, err := webhook.ValidateUpdate(context.Background(), tc.oldVSphereMachine, tc.vsphereMachine) | ||
if tc.wantErr { | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func createVSphereMachine(imageName, className, storageClass, minHardwareVersion string) *vmwarev1.VSphereMachine { | ||
vSphereMachine := &vmwarev1.VSphereMachine{ | ||
Spec: vmwarev1.VSphereMachineSpec{ | ||
ImageName: imageName, | ||
ClassName: className, | ||
StorageClass: storageClass, | ||
MinHardwareVersion: minHardwareVersion, | ||
}, | ||
} | ||
|
||
return vSphereMachine | ||
} |
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
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
Oops, something went wrong.