-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validating and mutating webhook config for supervisor mode
- Refactor supervisor config folder and generate webhook config and manifest - Enable update validating for vm-operator VSphereMachine to block in-place update of following fields. - ImageName - ClassName - StorageClass - MinHardwareVersion Signed-off-by: Gong Zhang <[email protected]>
- Loading branch information
Showing
26 changed files
with
345 additions
and
28 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,10 @@ | ||
resources: | ||
- manifests.yaml | ||
|
||
namePrefix: supervisor- | ||
|
||
configurations: | ||
- kustomizeconfig.yaml | ||
|
||
patchesStrategicMerge: | ||
- webhookcainjection_patch.yaml |
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,20 @@ | ||
nameReference: | ||
- kind: Service | ||
version: v1 | ||
fieldSpecs: | ||
- kind: ValidatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/name | ||
- kind: MutatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/name | ||
|
||
namespace: | ||
- kind: ValidatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/namespace | ||
create: true | ||
- kind: MutatingWebhookConfiguration | ||
group: admissionregistration.k8s.io | ||
path: webhooks/clientConfig/service/namespace | ||
create: true |
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.cluster.infrastructure.cluster.x-k8s.io | ||
rules: | ||
- apiGroups: | ||
- vmware.infrastructure.cluster.x-k8s.io | ||
apiVersions: | ||
- v1beta1 | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: | ||
- vspheremachines | ||
sideEffects: None |
File renamed without changes.
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 |
---|---|---|
|
@@ -4,3 +4,6 @@ resources: | |
|
||
configurations: | ||
- kustomizeconfig.yaml | ||
|
||
patchesStrategicMerge: | ||
- webhookcainjection_patch.yaml |
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,15 @@ | ||
# This patch add annotation to admission webhook config and | ||
# the variables $(NAMESPACE) and $(CERTIFICATENAME) will be substituted by kustomize. | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: ValidatingWebhookConfiguration | ||
metadata: | ||
name: validating-webhook-configuration | ||
annotations: | ||
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: MutatingWebhookConfiguration | ||
metadata: | ||
name: mutating-webhook-configuration | ||
annotations: | ||
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) |
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
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 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/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.cluster.infrastructure.cluster.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 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 VSphereMachine but got a %T", newRaw)) | ||
} | ||
|
||
oldTyped, ok := oldRaw.(*vmwarev1.VSphereMachine) | ||
if !ok { | ||
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a 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.Forbidden(field.NewPath("spec", "imageName"), "cannot be modified")) | ||
} | ||
|
||
if newSpec.ClassName != oldSpec.ClassName { | ||
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "className"), "cannot be modified")) | ||
} | ||
|
||
if newSpec.StorageClass != oldSpec.StorageClass { | ||
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "storageClass"), "cannot be modified")) | ||
} | ||
|
||
if newSpec.MinHardwareVersion != oldSpec.MinHardwareVersion { | ||
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "minHardwareVersion"), "cannot be modified")) | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.