diff --git a/api/v1alpha1/backupconfiguration_webhook.go b/api/v1alpha1/backupconfiguration_webhook.go new file mode 100644 index 0000000..4d73f62 --- /dev/null +++ b/api/v1alpha1/backupconfiguration_webhook.go @@ -0,0 +1,78 @@ +/* + + +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 backupconfigurationlog = logf.Log.WithName("backupconfiguration-resource") + +func (r *BackupConfiguration) 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-v1alpha1-backupconfiguration,mutating=true,failurePolicy=fail,groups=formol.desmojim.fr,resources=backupconfigurations,verbs=create;update,versions=v1alpha1,name=mbackupconfiguration.kb.io + +var _ webhook.Defaulter = &BackupConfiguration{} + +// Default implements webhook.Defaulter so a webhook will be registered for the type +func (r *BackupConfiguration) Default() { + backupconfigurationlog.Info("default", "name", r.Name) + + // TODO(user): fill in your defaulting logic. + if r.Spec.Suspend == nil { + r.Spec.Suspend = new(bool) + } +} + +// 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-v1alpha1-backupconfiguration,mutating=false,failurePolicy=fail,groups=formol.desmojim.fr,resources=backupconfigurations,versions=v1alpha1,name=vbackupconfiguration.kb.io + +var _ webhook.Validator = &BackupConfiguration{} + +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type +func (r *BackupConfiguration) ValidateCreate() error { + backupconfigurationlog.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 *BackupConfiguration) ValidateUpdate(old runtime.Object) error { + backupconfigurationlog.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 *BackupConfiguration) ValidateDelete() error { + backupconfigurationlog.Info("validate delete", "name", r.Name) + + // TODO(user): fill in your validation logic upon object deletion. + return nil +} diff --git a/main.go b/main.go index a907994..c642924 100644 --- a/main.go +++ b/main.go @@ -87,6 +87,10 @@ func main() { setupLog.Error(err, "unable to create webhook", "webhook", "BackupSession") os.Exit(1) } + if err = (&formolv1alpha1.BackupConfiguration{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "BackupConfiguration") + os.Exit(1) + } } // +kubebuilder:scaffold:builder