formol/controllers/backupconfiguration_controller.go

126 lines
4.4 KiB
Go

/*
Copyright 2023.
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 controllers
import (
"context"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
formolutils "github.com/desmo999r/formol/pkg/utils"
)
// BackupConfigurationReconciler reconciles a BackupConfiguration object
type BackupConfigurationReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
context.Context
}
//+kubebuilder:rbac:groups=formol.desmojim.fr,resources=backupconfigurations,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=formol.desmojim.fr,resources=backupconfigurations/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=formol.desmojim.fr,resources=backupconfigurations/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the BackupConfiguration object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.1/pkg/reconcile
func (r *BackupConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Context = ctx
r.Log = log.FromContext(ctx)
r.Log.V(1).Info("Enter Reconcile with req", "req", req, "reconciler", r)
backupConf := formolv1alpha1.BackupConfiguration{}
err := r.Get(ctx, req.NamespacedName, &backupConf)
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
finalizerName := "finalizer.backupconfiguration.formol.desmojim.fr"
if !backupConf.ObjectMeta.DeletionTimestamp.IsZero() {
r.Log.V(0).Info("backupconf being deleted", "backupconf", backupConf.ObjectMeta.Finalizers)
if formolutils.ContainsString(backupConf.ObjectMeta.Finalizers, finalizerName) {
_ = r.DeleteSidecar(backupConf)
_ = r.DeleteCronJob(backupConf)
backupConf.ObjectMeta.Finalizers = formolutils.RemoveString(backupConf.ObjectMeta.Finalizers, finalizerName)
if err := r.Update(ctx, &backupConf); err != nil {
r.Log.Error(err, "unable to remove finalizer")
return ctrl.Result{}, err
}
}
// We have been deleted. Return here
r.Log.V(0).Info("backupconf deleted", "backupconf", backupConf.Name)
return ctrl.Result{}, nil
}
// Add finalizer
if !formolutils.ContainsString(backupConf.ObjectMeta.Finalizers, finalizerName) {
r.Log.V(0).Info("adding finalizer", "backupconf", backupConf)
backupConf.ObjectMeta.Finalizers = append(backupConf.ObjectMeta.Finalizers, finalizerName)
if err := r.Update(ctx, &backupConf); err != nil {
r.Log.Error(err, "unable to append finalizer")
return ctrl.Result{}, err
}
// backupConf has been updated. Exit here. The reconciler will be called again so we can finish the job.
return ctrl.Result{}, nil
}
if err := r.AddCronJob(backupConf); err != nil {
return ctrl.Result{}, err
} else {
backupConf.Status.ActiveCronJob = true
}
if err := r.AddSidecar(backupConf); err != nil {
r.Log.Error(err, "unable to add sidecar container")
return ctrl.Result{}, err
} else {
backupConf.Status.ActiveSidecar = true
}
if err := r.Status().Update(ctx, &backupConf); err != nil {
r.Log.Error(err, "Unable to update BackupConfiguration status")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *BackupConfigurationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&formolv1alpha1.BackupConfiguration{}).
Complete(r)
}