Moved to kubebuilder 0.8.3

This commit is contained in:
jandre 2021-05-08 22:35:13 +02:00
parent 2ba6e19f8d
commit 771942cb94
3 changed files with 41 additions and 34 deletions

22
go.mod
View File

@ -4,18 +4,18 @@ go 1.14
require ( require (
github.com/desmo999r/formol v0.7.1 github.com/desmo999r/formol v0.7.1
github.com/go-logr/logr v0.1.0 github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.1.0 github.com/go-logr/zapr v0.2.0
github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.12.1 github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.1 github.com/onsi/gomega v1.10.2
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.3.2 github.com/spf13/viper v1.7.0
go.uber.org/zap v1.10.0 go.uber.org/zap v1.15.0
k8s.io/api v0.18.6 k8s.io/api v0.20.2
k8s.io/apimachinery v0.18.6 k8s.io/apimachinery v0.20.2
k8s.io/client-go v0.18.6 k8s.io/client-go v0.20.2
sigs.k8s.io/controller-runtime v0.6.4 sigs.k8s.io/controller-runtime v0.8.3
) )
replace github.com/desmo999r/formol => /home/jandre/devel/golang/formol replace github.com/desmo999r/formol => /home/jandre/devel/golang/formol

View File

@ -9,6 +9,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"time" "time"
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1" formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
@ -24,16 +25,17 @@ type BackupSessionReconciler struct {
Scheme *runtime.Scheme 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) time.Sleep(2 * time.Second)
ctx := context.Background()
log := r.Log.WithValues("backupsession", req.NamespacedName) log := r.Log.WithValues("backupsession", req.NamespacedName)
// your logic here // your logic here
backupSession := &formolv1alpha1.BackupSession{} backupSession := &formolv1alpha1.BackupSession{}
if err := r.Get(ctx, req.NamespacedName, backupSession); err != nil { if err := r.Get(ctx, req.NamespacedName, backupSession); err != nil {
log.Error(err, "unable to get backupsession") log.Error(err, "unable to get backupsession")
return ctrl.Result{}, client.IgnoreNotFound(err) return reconcile.Result{}, client.IgnoreNotFound(err)
} }
backupConf := &formolv1alpha1.BackupConfiguration{} backupConf := &formolv1alpha1.BackupConfiguration{}
if err := r.Get(ctx, client.ObjectKey{ 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, Name: backupSession.Spec.Ref.Name,
}, backupConf); err != nil { }, backupConf); err != nil {
log.Error(err, "unable to get backupConfiguration") 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) deploymentName := os.Getenv(formolv1alpha1.TARGET_NAME)
@ -59,7 +61,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
status.SessionState = formolv1alpha1.Init status.SessionState = formolv1alpha1.Init
if err := r.Status().Update(ctx, backupSession); err != nil { if err := r.Status().Update(ctx, backupSession); err != nil {
log.Error(err, "unable to update backupsession status") log.Error(err, "unable to update backupsession status")
return ctrl.Result{}, err return reconcile.Result{}, err
} }
case formolv1alpha1.Init: case formolv1alpha1.Init:
log.V(0).Info("Start to run the backup initializing steps if any") 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, Namespace: backupConf.Namespace,
}, function); err != nil { }, function); err != nil {
log.Error(err, "unable to get function", "function", step.Name) 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 { 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) 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 { if err := r.Status().Update(ctx, backupSession); err != nil {
log.Error(err, "unable to update backupsession status") log.Error(err, "unable to update backupsession status")
return ctrl.Result{}, err return reconcile.Result{}, err
} }
case formolv1alpha1.Running: case formolv1alpha1.Running:
log.V(0).Info("Running session. Do the backup") 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) log.V(1).Info("current backupSession status", "status", backupSession.Status)
if err := r.Status().Update(ctx, backupSession); err != nil { if err := r.Status().Update(ctx, backupSession); err != nil {
log.Error(err, "unable to update backupsession status") log.Error(err, "unable to update backupsession status")
return ctrl.Result{}, err return reconcile.Result{}, err
} }
case formolv1alpha1.Finalize: case formolv1alpha1.Finalize:
log.V(0).Info("Start to run the backup finalizing steps if any") 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, Namespace: backupConf.Namespace,
}, function); err != nil { }, function); err != nil {
log.Error(err, "unable to get function", "function", step.Name) 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 { 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) 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 { if err := r.Status().Update(ctx, backupSession); err != nil {
log.Error(err, "unable to update backupsession status") log.Error(err, "unable to update backupsession status")
return ctrl.Result{}, err return reconcile.Result{}, err
} }
case formolv1alpha1.Success, formolv1alpha1.Failure: 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 { func (r *BackupSessionReconciler) SetupWithManager(mgr ctrl.Manager) error {

View File

@ -13,6 +13,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strings" "strings"
"time" "time"
) )
@ -23,14 +24,15 @@ type RestoreSessionReconciler struct {
Scheme *runtime.Scheme Scheme *runtime.Scheme
} }
func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { var _ reconcile.Reconciler = &RestoreSessionReconciler{}
ctx := context.Background()
func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
log := r.Log.WithValues("restoresession", req.NamespacedName) log := r.Log.WithValues("restoresession", req.NamespacedName)
restoreSession := &formolv1alpha1.RestoreSession{} restoreSession := &formolv1alpha1.RestoreSession{}
if err := r.Get(ctx, req.NamespacedName, restoreSession); err != nil { if err := r.Get(ctx, req.NamespacedName, restoreSession); err != nil {
log.Error(err, "unable to get restoresession") log.Error(err, "unable to get restoresession")
return ctrl.Result{}, client.IgnoreNotFound(err) return reconcile.Result{}, client.IgnoreNotFound(err)
} }
backupSession := &formolv1alpha1.BackupSession{} backupSession := &formolv1alpha1.BackupSession{}
if err := r.Get(ctx, client.ObjectKey{ 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) log.V(1).Info("generated backupsession", "backupsession", backupSession)
} else { } else {
log.Error(err, "unable to get backupsession", "restoresession", restoreSession.Spec) 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{} backupConf := &formolv1alpha1.BackupConfiguration{}
if err := r.Get(ctx, client.ObjectKey{ 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, Name: backupSession.Spec.Ref.Name,
}, backupConf); err != nil { }, backupConf); err != nil {
log.Error(err, "unable to get backupConfiguration") 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) deploymentName := os.Getenv(formolv1alpha1.TARGET_NAME)
currentTargetStatus := &(restoreSession.Status.Targets[len(restoreSession.Status.Targets)-1]) 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 { if currentTarget.Name == deploymentName {
switch currentTargetStatus.SessionState { switch currentTargetStatus.SessionState {
case formolv1alpha1.Finalize: 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) podName := os.Getenv(formolv1alpha1.POD_NAME)
podNamespace := os.Getenv(formolv1alpha1.POD_NAMESPACE) podNamespace := os.Getenv(formolv1alpha1.POD_NAMESPACE)
pod := &corev1.Pod{} pod := &corev1.Pod{}
@ -73,12 +75,12 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
Name: podName, Name: podName,
}, pod); err != nil { }, pod); err != nil {
log.Error(err, "unable to get pod", "name", podName, "namespace", podNamespace) 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 { for _, containerStatus := range pod.Status.ContainerStatuses {
if !containerStatus.Ready { if !containerStatus.Ready {
log.V(0).Info("Not all the containers in the pod are ready. Reschedule", "name", containerStatus.Name) 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)") 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 result := formolv1alpha1.Success
for i := range currentTarget.Steps { for i := range currentTarget.Steps {
step := currentTarget.Steps[len(currentTarget.Steps)-1-i] step := currentTarget.Steps[len(currentTarget.Steps)-1-i]
log.V(1).Info("current step", "step", step.Name)
backupFunction := &formolv1alpha1.Function{} backupFunction := &formolv1alpha1.Function{}
if err := r.Get(ctx, client.ObjectKey{ if err := r.Get(ctx, client.ObjectKey{
Namespace: backupConf.Namespace, Namespace: backupConf.Namespace,
Name: step.Name, Name: step.Name,
}, backupFunction); err != nil { }, backupFunction); err != nil {
log.Error(err, "unable to get backup function") 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 // 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 // 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 continue
} }
} }
log.V(1).Info("No associated restore function", "step", step.Name)
} }
if len(restoreFunction.Spec.Command) > 1 { if len(restoreFunction.Spec.Command) > 1 {
log.V(0).Info("Running the restore function", "name", restoreFunction.Name, "command", restoreFunction.Spec.Command) 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 // so that we can move to the next step
log.V(0).Info("Finalize is over", "target", currentTarget.Name)
currentTargetStatus.SessionState = result currentTargetStatus.SessionState = result
if err := r.Status().Update(ctx, restoreSession); err != nil { if err := r.Status().Update(ctx, restoreSession); err != nil {
log.Error(err, "unable to update restoresession") 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 { func (r *RestoreSessionReconciler) SetupWithManager(mgr ctrl.Manager) error {