From 5eca98f0917cac93b2976ae4fe6039141e93e6de Mon Sep 17 00:00:00 2001 From: Mehedi Hasan Date: Thu, 2 Mar 2023 12:05:34 +0600 Subject: [PATCH] fix sidekick pod create issue Signed-off-by: Mehedi Hasan --- pkg/controllers/apps/sidekick_controller.go | 54 ++++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/pkg/controllers/apps/sidekick_controller.go b/pkg/controllers/apps/sidekick_controller.go index 3a091cf5..1486298f 100644 --- a/pkg/controllers/apps/sidekick_controller.go +++ b/pkg/controllers/apps/sidekick_controller.go @@ -19,9 +19,11 @@ package apps import ( "context" "fmt" + "k8s.io/klog/v2" "regexp" "sort" "strconv" + "time" appsv1alpha1 "kubeops.dev/sidekick/apis/apps/v1alpha1" @@ -69,6 +71,8 @@ type SidekickReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + + fmt.Println("reconlice called with -------------->", req.NamespacedName) logger := log.FromContext(ctx, "sidekick", req.Name, "ns", req.Namespace) ctx = log.IntoContext(ctx, logger) @@ -82,20 +86,32 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } leader, err := r.getLeader(ctx, sidekick) - if errors.IsNotFound(err) || (err == nil && leader.Name != sidekick.Status.Leader.Name) { + if errors.IsNotFound(err) || (err == nil && leader.Name != h.Status.Leader.Name) { var pod corev1.Pod e2 := r.Get(ctx, req.NamespacedName, &pod) if e2 == nil { - r.Delete(ctx, &pod) // TODO + err := r.Delete(ctx, &pod) + klog.Error(err) + if err != nil { + return ctrl.Result{}, err + } sidekick.Status.Leader.Name = "" sidekick.Status.Pod = "" sidekick.Status.Phase = "Current" sidekick.Status.ObservedGeneration = sidekick.GetGeneration() - r.Status().Update(ctx, &sidekick) // TODO - return ctrl.Result{}, nil // client.IgnoreNotFound(err) + err = r.Status().Update(ctx, &sidekick) + if err != nil { + klog.Error(err) + return ctrl.Result{}, err + } // TODO + return ctrl.Result{}, nil // client.IgnoreNotFound(err) } else if err != nil { - return ctrl.Result{}, client.IgnoreNotFound(err) + klog.Error(err) + return ctrl.Result{ + Requeue: true, + RequeueAfter: time.Second * 10, + }, client.IgnoreNotFound(err) } } else if err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) @@ -112,22 +128,31 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c if expectedHash != actualHash || leader.Name != pod.Annotations[keyLeader] || leader.Spec.NodeName != pod.Spec.NodeName { - r.Delete(ctx, &pod) // TODO + err := r.Delete(ctx, &pod) + if err != nil { + return ctrl.Result{}, err + } // TODO sidekick.Status.Leader.Name = "" sidekick.Status.Pod = "" sidekick.Status.Phase = "Current" sidekick.Status.ObservedGeneration = sidekick.GetGeneration() - r.Status().Update(ctx, &sidekick) // TODO - return ctrl.Result{}, nil // client.IgnoreNotFound(err) + err = r.Status().Update(ctx, &sidekick) + if err != nil { + return ctrl.Result{}, err + } // TODO + return ctrl.Result{}, nil // client.IgnoreNotFound(err) } // sidekick.Status.Leader.Name = "" sidekick.Status.Pod = pod.Status.Phase sidekick.Status.Phase = "Current" sidekick.Status.ObservedGeneration = sidekick.GetGeneration() - r.Status().Update(ctx, &sidekick) // TODO - return ctrl.Result{}, nil // client.IgnoreNotFound(err) + err := r.Status().Update(ctx, &sidekick) + if err != nil { + return ctrl.Result{}, err + } // TODO + return ctrl.Result{}, nil // client.IgnoreNotFound(err) } else if !errors.IsNotFound(e2) { return ctrl.Result{}, e2 } @@ -214,8 +239,12 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c sidekick.Status.Pod = pod.Status.Phase sidekick.Status.Phase = "Current" sidekick.Status.ObservedGeneration = sidekick.GetGeneration() - r.Status().Update(ctx, &sidekick) // TODO - return ctrl.Result{}, nil // client.IgnoreNotFound(err) + err = r.Status().Update(ctx, &sidekick) + if err != nil { + return ctrl.Result{}, err + } // TODO + klog.Info("comes here after reconcile") + return ctrl.Result{}, nil // client.IgnoreNotFound(err) } func convContainer(leader *corev1.Pod, c appsv1alpha1.Container) (*corev1.Container, error) { @@ -364,6 +393,7 @@ func (r *SidekickReconciler) getLeader(ctx context.Context, sidekick appsv1alpha // SetupWithManager sets up the controller with the Manager. func (r *SidekickReconciler) SetupWithManager(mgr ctrl.Manager) error { + fmt.Println("------------------------------->setup with manager called---------------------> ") leaderHandler := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request { sidekicks := &appsv1alpha1.SidekickList{} if err := r.List(context.Background(), sidekicks, client.InNamespace(a.GetNamespace())); err != nil {