|
| 1 | +/* |
| 2 | +Copyright 2021 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 v1alpha4 |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/runtime" |
| 23 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 24 | + ctrl "sigs.k8s.io/controller-runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 26 | +) |
| 27 | + |
| 28 | +const openStackClusterTemplateImmutableMsg = "OpenStackClusterTemplate spec.template.spec field is immutable. Please create new resource instead." |
| 29 | + |
| 30 | +func (r *OpenStackClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 31 | + return ctrl.NewWebhookManagedBy(mgr). |
| 32 | + For(r). |
| 33 | + Complete() |
| 34 | +} |
| 35 | + |
| 36 | +// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackclustertemplates,versions=v1alpha4,name=default.openstackclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 37 | +// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackclustertemplates,versions=v1alpha4,name=validation.openstackclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 38 | + |
| 39 | +var ( |
| 40 | + _ webhook.Defaulter = &OpenStackClusterTemplate{} |
| 41 | + _ webhook.Validator = &OpenStackClusterTemplate{} |
| 42 | +) |
| 43 | + |
| 44 | +// Default implements webhook.Defaulter so a webhook will be registered for the type. |
| 45 | +func (r *OpenStackClusterTemplate) Default() { |
| 46 | + if r.Spec.Template.Spec.IdentityRef != nil && r.Spec.Template.Spec.IdentityRef.Kind == "" { |
| 47 | + r.Spec.Template.Spec.IdentityRef.Kind = defaultIdentityRefKind |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. |
| 52 | +func (r *OpenStackClusterTemplate) ValidateCreate() error { |
| 53 | + var allErrs field.ErrorList |
| 54 | + |
| 55 | + if r.Spec.Template.Spec.IdentityRef != nil && r.Spec.Template.Spec.IdentityRef.Kind != defaultIdentityRefKind { |
| 56 | + allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "identityRef", "kind"), "must be a Secret")) |
| 57 | + } |
| 58 | + |
| 59 | + return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs) |
| 60 | +} |
| 61 | + |
| 62 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. |
| 63 | +func (r *OpenStackClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error { |
| 64 | + var allErrs field.ErrorList |
| 65 | + |
| 66 | + old := oldRaw.(*OpenStackClusterTemplate) |
| 67 | + if !reflect.DeepEqual(r.Spec.Template.Spec, old.Spec.Template.Spec) { |
| 68 | + allErrs = append(allErrs, |
| 69 | + field.Invalid(field.NewPath("OpenStackClusterTemplate", "spec", "template", "spec"), r, openStackClusterTemplateImmutableMsg), |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs) |
| 74 | +} |
| 75 | + |
| 76 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. |
| 77 | +func (r *OpenStackClusterTemplate) ValidateDelete() error { |
| 78 | + return nil |
| 79 | +} |
0 commit comments