Added defaulting validation webhook

This commit is contained in:
Jean-Marc ANDRE 2020-12-01 22:41:18 +01:00
parent aee385a67d
commit 22c909bc1c
4 changed files with 87 additions and 8 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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()
}
}

View File

@ -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")