From 771942cb94644700a8ee48f1372df9c246a0a4e0 Mon Sep 17 00:00:00 2001 From: Jean-Marc Andre Date: Sat, 8 May 2021 22:35:13 +0200 Subject: [PATCH] Moved to kubebuilder 0.8.3 --- go.mod | 22 +++++++-------- pkg/controllers/backupsession_controller.go | 24 ++++++++-------- pkg/controllers/restoresession_controller.go | 29 ++++++++++++-------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index f35c682..dd846cf 100644 --- a/go.mod +++ b/go.mod @@ -4,18 +4,18 @@ go 1.14 require ( github.com/desmo999r/formol v0.7.1 - github.com/go-logr/logr v0.1.0 - github.com/go-logr/zapr v0.1.0 + github.com/go-logr/logr v0.3.0 + github.com/go-logr/zapr v0.2.0 github.com/mitchellh/go-homedir v1.1.0 - github.com/onsi/ginkgo v1.12.1 - github.com/onsi/gomega v1.10.1 - github.com/spf13/cobra v0.0.5 - github.com/spf13/viper v1.3.2 - go.uber.org/zap v1.10.0 - k8s.io/api v0.18.6 - k8s.io/apimachinery v0.18.6 - k8s.io/client-go v0.18.6 - sigs.k8s.io/controller-runtime v0.6.4 + github.com/onsi/ginkgo v1.14.1 + github.com/onsi/gomega v1.10.2 + github.com/spf13/cobra v1.1.1 + github.com/spf13/viper v1.7.0 + go.uber.org/zap v1.15.0 + k8s.io/api v0.20.2 + k8s.io/apimachinery v0.20.2 + k8s.io/client-go v0.20.2 + sigs.k8s.io/controller-runtime v0.8.3 ) replace github.com/desmo999r/formol => /home/jandre/devel/golang/formol diff --git a/pkg/controllers/backupsession_controller.go b/pkg/controllers/backupsession_controller.go index 79696ad..22aba2a 100644 --- a/pkg/controllers/backupsession_controller.go +++ b/pkg/controllers/backupsession_controller.go @@ -9,6 +9,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" "time" formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1" @@ -24,16 +25,17 @@ type BackupSessionReconciler struct { Scheme *runtime.Scheme } -func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { +var _ reconcile.Reconciler = &BackupSessionReconciler{} + +func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { time.Sleep(2 * time.Second) - ctx := context.Background() log := r.Log.WithValues("backupsession", req.NamespacedName) // your logic here backupSession := &formolv1alpha1.BackupSession{} if err := r.Get(ctx, req.NamespacedName, backupSession); err != nil { log.Error(err, "unable to get backupsession") - return ctrl.Result{}, client.IgnoreNotFound(err) + return reconcile.Result{}, client.IgnoreNotFound(err) } backupConf := &formolv1alpha1.BackupConfiguration{} if err := r.Get(ctx, client.ObjectKey{ @@ -41,7 +43,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro Name: backupSession.Spec.Ref.Name, }, backupConf); err != nil { log.Error(err, "unable to get backupConfiguration") - return ctrl.Result{}, client.IgnoreNotFound(err) + return reconcile.Result{}, client.IgnoreNotFound(err) } deploymentName := os.Getenv(formolv1alpha1.TARGET_NAME) @@ -59,7 +61,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro status.SessionState = formolv1alpha1.Init if err := r.Status().Update(ctx, backupSession); err != nil { log.Error(err, "unable to update backupsession status") - return ctrl.Result{}, err + return reconcile.Result{}, err } case formolv1alpha1.Init: log.V(0).Info("Start to run the backup initializing steps if any") @@ -74,7 +76,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro Namespace: backupConf.Namespace, }, function); err != nil { log.Error(err, "unable to get function", "function", step.Name) - return ctrl.Result{}, err + return reconcile.Result{}, err } if err := formolcliutils.RunChroot(function.Spec.Command[0], function.Spec.Command[1:]...); err != nil { log.Error(err, "unable to run function command", "command", function.Spec.Command) @@ -86,7 +88,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro if err := r.Status().Update(ctx, backupSession); err != nil { log.Error(err, "unable to update backupsession status") - return ctrl.Result{}, err + return reconcile.Result{}, err } case formolv1alpha1.Running: log.V(0).Info("Running session. Do the backup") @@ -105,7 +107,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro log.V(1).Info("current backupSession status", "status", backupSession.Status) if err := r.Status().Update(ctx, backupSession); err != nil { log.Error(err, "unable to update backupsession status") - return ctrl.Result{}, err + return reconcile.Result{}, err } case formolv1alpha1.Finalize: log.V(0).Info("Start to run the backup finalizing steps if any") @@ -118,7 +120,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro Namespace: backupConf.Namespace, }, function); err != nil { log.Error(err, "unable to get function", "function", step.Name) - return ctrl.Result{}, err + return reconcile.Result{}, err } if err := formolcliutils.RunChroot(function.Spec.Command[0], function.Spec.Command[1:]...); err != nil { log.Error(err, "unable to run function command", "command", function.Spec.Command) @@ -131,7 +133,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro if err := r.Status().Update(ctx, backupSession); err != nil { log.Error(err, "unable to update backupsession status") - return ctrl.Result{}, err + return reconcile.Result{}, err } case formolv1alpha1.Success, formolv1alpha1.Failure: @@ -141,7 +143,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro } } } - return ctrl.Result{}, nil + return reconcile.Result{}, nil } func (r *BackupSessionReconciler) SetupWithManager(mgr ctrl.Manager) error { diff --git a/pkg/controllers/restoresession_controller.go b/pkg/controllers/restoresession_controller.go index a576ee0..02726c5 100644 --- a/pkg/controllers/restoresession_controller.go +++ b/pkg/controllers/restoresession_controller.go @@ -13,6 +13,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" "strings" "time" ) @@ -23,14 +24,15 @@ type RestoreSessionReconciler struct { Scheme *runtime.Scheme } -func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { - ctx := context.Background() +var _ reconcile.Reconciler = &RestoreSessionReconciler{} + +func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { log := r.Log.WithValues("restoresession", req.NamespacedName) restoreSession := &formolv1alpha1.RestoreSession{} if err := r.Get(ctx, req.NamespacedName, restoreSession); err != nil { log.Error(err, "unable to get restoresession") - return ctrl.Result{}, client.IgnoreNotFound(err) + return reconcile.Result{}, client.IgnoreNotFound(err) } backupSession := &formolv1alpha1.BackupSession{} if err := r.Get(ctx, client.ObjectKey{ @@ -45,16 +47,16 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err log.V(1).Info("generated backupsession", "backupsession", backupSession) } else { log.Error(err, "unable to get backupsession", "restoresession", restoreSession.Spec) - return ctrl.Result{}, client.IgnoreNotFound(err) + return reconcile.Result{}, client.IgnoreNotFound(err) } } backupConf := &formolv1alpha1.BackupConfiguration{} if err := r.Get(ctx, client.ObjectKey{ - Namespace: backupSession.Spec.Ref.Namespace, + Namespace: restoreSession.Namespace, // we use the BackupConfiguration in RestoreSession namespace. Name: backupSession.Spec.Ref.Name, }, backupConf); err != nil { log.Error(err, "unable to get backupConfiguration") - return ctrl.Result{}, client.IgnoreNotFound(err) + return reconcile.Result{}, client.IgnoreNotFound(err) } deploymentName := os.Getenv(formolv1alpha1.TARGET_NAME) currentTargetStatus := &(restoreSession.Status.Targets[len(restoreSession.Status.Targets)-1]) @@ -64,7 +66,7 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err if currentTarget.Name == deploymentName { switch currentTargetStatus.SessionState { case formolv1alpha1.Finalize: - log.V(0).Info("It's for us!") + log.V(0).Info("It's for us!", "target", currentTarget.Name) podName := os.Getenv(formolv1alpha1.POD_NAME) podNamespace := os.Getenv(formolv1alpha1.POD_NAMESPACE) pod := &corev1.Pod{} @@ -73,12 +75,12 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err Name: podName, }, pod); err != nil { log.Error(err, "unable to get pod", "name", podName, "namespace", podNamespace) - return ctrl.Result{}, err + return reconcile.Result{}, err } for _, containerStatus := range pod.Status.ContainerStatuses { if !containerStatus.Ready { log.V(0).Info("Not all the containers in the pod are ready. Reschedule", "name", containerStatus.Name) - return ctrl.Result{RequeueAfter: 10 * time.Second}, nil + return reconcile.Result{RequeueAfter: 10 * time.Second}, nil } } log.V(0).Info("All the containers in the pod are ready. Time to run the restore steps (in reverse order)") @@ -86,13 +88,14 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err result := formolv1alpha1.Success for i := range currentTarget.Steps { step := currentTarget.Steps[len(currentTarget.Steps)-1-i] + log.V(1).Info("current step", "step", step.Name) backupFunction := &formolv1alpha1.Function{} if err := r.Get(ctx, client.ObjectKey{ Namespace: backupConf.Namespace, Name: step.Name, }, backupFunction); err != nil { log.Error(err, "unable to get backup function") - return ctrl.Result{}, err + return reconcile.Result{}, err } // We got the backup function corresponding to the step from the BackupConfiguration // Now let's try to get the restore function is there is one @@ -117,6 +120,7 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err continue } } + log.V(1).Info("No associated restore function", "step", step.Name) } if len(restoreFunction.Spec.Command) > 1 { log.V(0).Info("Running the restore function", "name", restoreFunction.Name, "command", restoreFunction.Spec.Command) @@ -129,8 +133,9 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err } } } - // We are done with the restore of this target. We flag it as success of failure + // We are done with the restore of this target. We flag it as success or failure // so that we can move to the next step + log.V(0).Info("Finalize is over", "target", currentTarget.Name) currentTargetStatus.SessionState = result if err := r.Status().Update(ctx, restoreSession); err != nil { log.Error(err, "unable to update restoresession") @@ -139,7 +144,7 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err } } - return ctrl.Result{}, nil + return reconcile.Result{}, nil } func (r *RestoreSessionReconciler) SetupWithManager(mgr ctrl.Manager) error {