Compare commits
2 Commits
cd39ff0236
...
d5b9ffd8a4
| Author | SHA1 | Date | |
|---|---|---|---|
| d5b9ffd8a4 | |||
| ce16a92081 |
@ -76,6 +76,9 @@ type BackupConfigurationSpec struct {
|
||||
// Foo is an example field of BackupConfiguration. Edit BackupConfiguration_types.go to remove/update
|
||||
Repository `json:"repository"`
|
||||
|
||||
// +optional
|
||||
Suspend *bool `json:"suspend,omitempty"`
|
||||
|
||||
// +optional
|
||||
Schedule string `json:"schedule,omitempty"`
|
||||
// +kubebuilder:validation:MinItems=1
|
||||
|
||||
78
api/v1alpha1/backupconfiguration_webhook.go
Normal file
78
api/v1alpha1/backupconfiguration_webhook.go
Normal file
@ -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
|
||||
}
|
||||
@ -105,6 +105,11 @@ func (in *BackupConfigurationList) DeepCopyObject() runtime.Object {
|
||||
func (in *BackupConfigurationSpec) DeepCopyInto(out *BackupConfigurationSpec) {
|
||||
*out = *in
|
||||
out.Repository = in.Repository
|
||||
if in.Suspend != nil {
|
||||
in, out := &in.Suspend, &out.Suspend
|
||||
*out = new(bool)
|
||||
**out = **in
|
||||
}
|
||||
if in.Targets != nil {
|
||||
in, out := &in.Targets, &out.Targets
|
||||
*out = make([]Target, len(*in))
|
||||
|
||||
@ -238,15 +238,29 @@ func (r *BackupConfigurationReconciler) addSidecarContainer(backupConf *formolv1
|
||||
func (r *BackupConfigurationReconciler) addCronJob(backupConf *formolv1alpha1.BackupConfiguration) error {
|
||||
log := r.Log.WithValues("addCronJob", backupConf.Name)
|
||||
|
||||
if err := formolrbac.CreateBackupSessionCreatorRBAC(r.Client, backupConf.Namespace); err != nil {
|
||||
log.Error(err, "unable to create backupsession-creator RBAC")
|
||||
return nil
|
||||
}
|
||||
|
||||
cronjob := &kbatch_beta1.CronJob{}
|
||||
if err := r.Get(context.Background(), client.ObjectKey{
|
||||
Namespace: backupConf.Namespace,
|
||||
Name: "backup-" + backupConf.Name,
|
||||
}, cronjob); err == nil {
|
||||
log.V(0).Info("there is already a cronjob")
|
||||
var changed bool
|
||||
if backupConf.Spec.Schedule != cronjob.Spec.Schedule {
|
||||
log.V(0).Info("cronjob schedule has changed", "old schedule", cronjob.Spec.Schedule, "new schedule", backupConf.Spec.Schedule)
|
||||
cronjob.Spec.Schedule = backupConf.Spec.Schedule
|
||||
changed = true
|
||||
}
|
||||
if backupConf.Spec.Suspend != cronjob.Spec.Suspend {
|
||||
log.V(0).Info("cronjob suspend has changed", "before", cronjob.Spec.Suspend, "new", backupConf.Spec.Suspend)
|
||||
cronjob.Spec.Suspend = backupConf.Spec.Suspend
|
||||
changed = true
|
||||
}
|
||||
if changed == true {
|
||||
if err := r.Update(context.TODO(), cronjob); err != nil {
|
||||
log.Error(err, "unable to update cronjob definition")
|
||||
return err
|
||||
@ -258,17 +272,13 @@ func (r *BackupConfigurationReconciler) addCronJob(backupConf *formolv1alpha1.Ba
|
||||
return err
|
||||
}
|
||||
|
||||
if err := formolrbac.CreateBackupSessionCreatorRBAC(r.Client, backupConf.Namespace); err != nil {
|
||||
log.Error(err, "unable to create backupsession-creator RBAC")
|
||||
return nil
|
||||
}
|
||||
|
||||
cronjob = &kbatch_beta1.CronJob{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "backup-" + backupConf.Name,
|
||||
Namespace: backupConf.Namespace,
|
||||
},
|
||||
Spec: kbatch_beta1.CronJobSpec{
|
||||
Suspend: backupConf.Spec.Suspend,
|
||||
Schedule: backupConf.Spec.Schedule,
|
||||
JobTemplate: kbatch_beta1.JobTemplateSpec{
|
||||
Spec: batchv1.JobSpec{
|
||||
|
||||
4
main.go
4
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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user