|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package webhooks |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 29 | + |
| 30 | + vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1" |
| 31 | +) |
| 32 | + |
| 33 | +// +kubebuilder:webhook:verbs=create;update,path=/validate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-providerserviceaccount,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=vmware.infrastructure.cluster.x-k8s.io,resources=providerserviceaccounts,versions=v1beta1,name=validation.providerserviceaccount.vmware.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 34 | +// +kubebuilder:webhook:verbs=create;update,path=/mutate-vmware-infrastructure-cluster-x-k8s-io-v1beta1-providerserviceaccount,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=vmware.infrastructure.cluster.x-k8s.io,resources=providerserviceaccounts,versions=v1beta1,name=default.providerserviceaccount.vmware.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 35 | + |
| 36 | +// ProviderServiceAccountWebhook implements a validation and defaulting webhook for ProviderServiceAccount. |
| 37 | +type ProviderServiceAccountWebhook struct{} |
| 38 | + |
| 39 | +var _ webhook.CustomValidator = &ProviderServiceAccountWebhook{} |
| 40 | +var _ webhook.CustomDefaulter = &ProviderServiceAccountWebhook{} |
| 41 | + |
| 42 | +func (webhook *ProviderServiceAccountWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 43 | + return ctrl.NewWebhookManagedBy(mgr). |
| 44 | + For(&vmwarev1.ProviderServiceAccount{}). |
| 45 | + WithValidator(webhook). |
| 46 | + WithDefaulter(webhook). |
| 47 | + Complete() |
| 48 | +} |
| 49 | + |
| 50 | +// Default implements webhook.Defaulter so a webhook will be registered for the type. |
| 51 | +func (webhook *ProviderServiceAccountWebhook) Default(_ context.Context, _ runtime.Object) error { |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. |
| 56 | +func (webhook *ProviderServiceAccountWebhook) ValidateCreate(_ context.Context, raw runtime.Object) (admission.Warnings, error) { |
| 57 | + var allErrs field.ErrorList |
| 58 | + |
| 59 | + obj, ok := raw.(*vmwarev1.ProviderServiceAccount) |
| 60 | + if !ok { |
| 61 | + return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a ProviderServiceAccount but got a %T", raw)) |
| 62 | + } |
| 63 | + |
| 64 | + spec := obj.Spec |
| 65 | + if spec.Ref == nil && (spec.ClusterName == nil || *spec.ClusterName == "") { //nolint:staticcheck |
| 66 | + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "should specify Ref or ClusterName")) |
| 67 | + if spec.ClusterName != nil && *spec.ClusterName == "" { |
| 68 | + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ClusterName"), "ClusterName should not be empty")) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return nil, aggregateObjErrors(obj.GroupVersionKind().GroupKind(), obj.Name, allErrs) |
| 73 | +} |
| 74 | + |
| 75 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. |
| 76 | +func (webhook *ProviderServiceAccountWebhook) ValidateUpdate(_ context.Context, _ runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { |
| 77 | + var allErrs field.ErrorList |
| 78 | + |
| 79 | + newTyped, ok := newRaw.(*vmwarev1.ProviderServiceAccount) |
| 80 | + if !ok { |
| 81 | + return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a ProviderServiceAccount but got a %T", newRaw)) |
| 82 | + } |
| 83 | + |
| 84 | + spec := newTyped.Spec |
| 85 | + if spec.Ref == nil && (spec.ClusterName == nil || *spec.ClusterName == "") { //nolint:staticcheck |
| 86 | + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "should specify Ref or ClusterName")) |
| 87 | + if spec.ClusterName != nil && *spec.ClusterName == "" { |
| 88 | + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "ClusterName"), "ClusterName should not be empty")) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return nil, aggregateObjErrors(newTyped.GroupVersionKind().GroupKind(), newTyped.Name, allErrs) |
| 93 | +} |
| 94 | + |
| 95 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. |
| 96 | +func (webhook *ProviderServiceAccountWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { |
| 97 | + return nil, nil |
| 98 | +} |
0 commit comments