From 22c909bc1cdd3ac93cc6f49f6857544c906924fd Mon Sep 17 00:00:00 2001 From: Jean-Marc ANDRE Date: Tue, 1 Dec 2020 22:41:18 +0100 Subject: [PATCH] Added defaulting validation webhook --- api/v1alpha1/backupsession_types.go | 9 ++-- api/v1alpha1/backupsession_webhook.go | 76 +++++++++++++++++++++++++++ api/v1alpha1/zz_generated.deepcopy.go | 6 +-- main.go | 4 ++ 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 api/v1alpha1/backupsession_webhook.go diff --git a/api/v1alpha1/backupsession_types.go b/api/v1alpha1/backupsession_types.go index 8f54ca6..8bedefe 100644 --- a/api/v1alpha1/backupsession_types.go +++ b/api/v1alpha1/backupsession_types.go @@ -26,10 +26,9 @@ import ( type BackupSessionState string const ( - New BackupSessionState = "New" - Starting BackupSessionState = "Starting" - Running BackupSessionState = "Running" - Done BackupSessionState = "Done" + New BackupSessionState = "New" + Running BackupSessionState = "Running" + Done BackupSessionState = "Done" ) type Ref struct { @@ -56,7 +55,7 @@ type BackupSessionStatus struct { // +optional StartTime *metav1.Time `json:"startTime,omitempty"` // +optional - EndTime *metav1.Time `json:"endTime,omitempty"` + Duration *metav1.Time `json:"duration,omitempty"` } // +kubebuilder:object:root=true diff --git a/api/v1alpha1/backupsession_webhook.go b/api/v1alpha1/backupsession_webhook.go new file mode 100644 index 0000000..5c1d016 --- /dev/null +++ b/api/v1alpha1/backupsession_webhook.go @@ -0,0 +1,76 @@ +/* + + +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 v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/webhook" +) + +// log is for logging in this package. +var backupsessionlog = logf.Log.WithName("backupsession-resource") + +func (r *BackupSession) SetupWebhookWithManager(mgr ctrl.Manager) error { + return ctrl.NewWebhookManagedBy(mgr). + For(r). + Complete() +} + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! + +// +kubebuilder:webhook:path=/mutate-formol-desmojim-fr-desmojim-fr-v1alpha1-backupsession,mutating=true,failurePolicy=fail,groups=formol.desmojim.fr.desmojim.fr,resources=backupsessions,verbs=create;update,versions=v1alpha1,name=mbackupsession.kb.io + +var _ webhook.Defaulter = &BackupSession{} + +// Default implements webhook.Defaulter so a webhook will be registered for the type +func (r *BackupSession) Default() { + backupsessionlog.Info("default", "name", r.Name) + + // TODO(user): fill in your defaulting logic. + r.Status.BackupSessionState = New +} + +// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. +// +kubebuilder:webhook:verbs=create;update,path=/validate-formol-desmojim-fr-desmojim-fr-v1alpha1-backupsession,mutating=false,failurePolicy=fail,groups=formol.desmojim.fr.desmojim.fr,resources=backupsessions,versions=v1alpha1,name=vbackupsession.kb.io + +var _ webhook.Validator = &BackupSession{} + +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type +func (r *BackupSession) ValidateCreate() error { + backupsessionlog.Info("validate create", "name", r.Name) + + // TODO(user): fill in your validation logic upon object creation. + return nil +} + +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type +func (r *BackupSession) ValidateUpdate(old runtime.Object) error { + backupsessionlog.Info("validate update", "name", r.Name) + + // TODO(user): fill in your validation logic upon object update. + return nil +} + +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type +func (r *BackupSession) ValidateDelete() error { + backupsessionlog.Info("validate delete", "name", r.Name) + + // TODO(user): fill in your validation logic upon object deletion. + return nil +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 9605aa1..98cbc65 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -22,7 +22,7 @@ package v1alpha1 import ( "k8s.io/api/core/v1" - runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -240,8 +240,8 @@ func (in *BackupSessionStatus) DeepCopyInto(out *BackupSessionStatus) { in, out := &in.StartTime, &out.StartTime *out = (*in).DeepCopy() } - if in.EndTime != nil { - in, out := &in.EndTime, &out.EndTime + if in.Duration != nil { + in, out := &in.Duration, &out.Duration *out = (*in).DeepCopy() } } diff --git a/main.go b/main.go index da18377..0cd418a 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,10 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "BackupSession") os.Exit(1) } + if err = (&formolv1alpha1.BackupSession{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "BackupSession") + os.Exit(1) + } // +kubebuilder:scaffold:builder setupLog.Info("starting manager")