|
| 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 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 27 | +) |
| 28 | + |
| 29 | +// NestedclusterImmutableMsg ... |
| 30 | +const NestedclusterImmutableMsg = "Nestedclusters spec field is immutable. Please create a new resource instead. Ref doc: https://cluster-api.sigs.k8s.io/tasks/change-machine-template.html" |
| 31 | + |
| 32 | +func (r *NestedCluster) SetupWebhookWithManager(mgr manager.Manager) error { |
| 33 | + return builder.WebhookManagedBy(mgr). |
| 34 | + For(r). |
| 35 | + Complete() |
| 36 | +} |
| 37 | + |
| 38 | +// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-nestedcluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=nestedclusters,versions=v1alpha4,name=validation.nestedclusters.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 39 | + |
| 40 | +var _ webhook.Validator = &NestedCluster{} |
| 41 | + |
| 42 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. |
| 43 | +func (r *NestedCluster) ValidateCreate() error { |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. |
| 48 | +func (r *NestedCluster) ValidateUpdate(old runtime.Object) error { |
| 49 | + var allErrs field.ErrorList |
| 50 | + oldNestedcluster := old.(*NestedCluster) |
| 51 | + |
| 52 | + if !reflect.DeepEqual(r.Spec, oldNestedcluster.Spec) { |
| 53 | + allErrs = append(allErrs, |
| 54 | + field.Invalid(field.NewPath("spec", "template", "spec"), r, NestedclusterImmutableMsg), |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + if len(allErrs) != 0 { |
| 59 | + return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs) |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. |
| 66 | +func (r *NestedCluster) ValidateDelete() error { |
| 67 | + return nil |
| 68 | +} |
0 commit comments