|
| 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 | + "errors" |
| 21 | + "reflect" |
| 22 | + |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 28 | +) |
| 29 | + |
| 30 | +// log is for logging in this package. |
| 31 | +var dockerclustertemplatelog = logf.Log.WithName("dockerclustertemplate-resource") |
| 32 | + |
| 33 | +func (r *DockerClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 34 | + return ctrl.NewWebhookManagedBy(mgr). |
| 35 | + For(r). |
| 36 | + Complete() |
| 37 | +} |
| 38 | + |
| 39 | +//+kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-dockerclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=v1alpha4,name=validation.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 40 | + |
| 41 | +var _ webhook.Defaulter = &DockerClusterTemplate{} |
| 42 | + |
| 43 | +// Default implements webhook.Defaulter so a webhook will be registered for the type |
| 44 | +func (r *DockerClusterTemplate) Default() { |
| 45 | + dockerclustertemplatelog.Info("default", "name", r.Name) |
| 46 | + r.Spec.Template.Spec.setDefaults() |
| 47 | +} |
| 48 | + |
| 49 | +//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-dockerclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=v1alpha4,name=validation.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1 |
| 50 | + |
| 51 | +var _ webhook.Validator = &DockerClusterTemplate{} |
| 52 | + |
| 53 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type |
| 54 | +func (r *DockerClusterTemplate) ValidateCreate() error { |
| 55 | + dockerclustertemplatelog.Info("validate create", "name", r.Name) |
| 56 | + allErrs := r.Spec.Template.Spec.validate() |
| 57 | + if len(allErrs) > 0 { |
| 58 | + return apierrors.NewInvalid(GroupVersion.WithKind("DockerClusterTemplate").GroupKind(), r.Name, allErrs) |
| 59 | + } |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type |
| 64 | +func (r *DockerClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error { |
| 65 | + dockerclustertemplatelog.Info("validate update", "name", r.Name) |
| 66 | + old := oldRaw.(*DockerClusterTemplate) |
| 67 | + if !reflect.DeepEqual(r.Spec, old.Spec) { |
| 68 | + return errors.New("DockerClusterTemplateSpec is immutable") |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type |
| 74 | +func (r *DockerClusterTemplate) ValidateDelete() error { |
| 75 | + dockerclustertemplatelog.Info("validate delete", "name", r.Name) |
| 76 | + return nil |
| 77 | +} |
0 commit comments