From a280a91793a34da21c09a216a8865465ff892009 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 --- Makefile | 2 + pkg/controllers/apps/sidekick_controller.go | 70 ++++++++++++++------- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 08cdc376..94e5d6e4 100644 --- a/Makefile +++ b/Makefile @@ -426,6 +426,8 @@ install: kubectl label ns $(KUBE_NAMESPACE) pod-security.kubernetes.io/enforce=restricted; \ helm upgrade -i sidekick charts/sidekick --wait \ --namespace=$(KUBE_NAMESPACE) --create-namespace \ + --set registryFQDN="" \ + --set image.registry=$(REGISTRY) \ --set image.tag=$(TAG_PROD) \ --set imagePullPolicy=$(IMAGE_PULL_POLICY) \ $(IMAGE_PULL_SECRETS); diff --git a/pkg/controllers/apps/sidekick_controller.go b/pkg/controllers/apps/sidekick_controller.go index b04bcb1c..c008438e 100644 --- a/pkg/controllers/apps/sidekick_controller.go +++ b/pkg/controllers/apps/sidekick_controller.go @@ -22,6 +22,7 @@ import ( "regexp" "sort" "strconv" + "time" appsv1alpha1 "kubeops.dev/sidekick/apis/apps/v1alpha1" @@ -31,6 +32,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" + "k8s.io/klog/v2" "k8s.io/utils/pointer" cu "kmodules.xyz/client-go/client" core_util "kmodules.xyz/client-go/core/v1" @@ -47,8 +49,9 @@ import ( ) const ( - keyHash = "sidekick.appscode.com/hash" - keyLeader = "sidekick.appscode.com/leader" + keyHash = "sidekick.appscode.com/hash" + keyLeader = "sidekick.appscode.com/leader" + SidekickPhaseCurrent = "Current" ) // SidekickReconciler reconciles a Sidekick object @@ -71,6 +74,7 @@ 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) { + klog.Infof(fmt.Sprintf("reconciling %v ", req.NamespacedName)) logger := log.FromContext(ctx, "sidekick", req.Name, "ns", req.Namespace) ctx = log.IntoContext(ctx, logger) @@ -106,25 +110,36 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c var pod corev1.Pod e2 := r.Get(ctx, req.NamespacedName, &pod) if e2 == nil { - r.Delete(ctx, &pod) // TODO + err := r.Delete(ctx, &pod) + if err != nil { + klog.Error(err) + return ctrl.Result{}, err + } sidekick.Status.Leader.Name = "" sidekick.Status.Pod = "" - sidekick.Status.Phase = "Current" + sidekick.Status.Phase = SidekickPhaseCurrent 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 + } + 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) } - var pod corev1.Pod e2 := r.Get(ctx, req.NamespacedName, &pod) if e2 != nil && !errors.IsNotFound(e2) { - return ctrl.Result{}, client.IgnoreNotFound(err) + return ctrl.Result{}, client.IgnoreNotFound(e2) } if e2 == nil { expectedHash := meta.GenerationHash(&sidekick) @@ -132,26 +147,34 @@ 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.Phase = SidekickPhaseCurrent 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.Phase = SidekickPhaseCurrent 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 + } + return ctrl.Result{}, nil // client.IgnoreNotFound(err) } else if !errors.IsNotFound(e2) { return ctrl.Result{}, e2 } - // pod not exists, so create one o1 := metav1.NewControllerRef(&sidekick, appsv1alpha1.SchemeGroupVersion.WithKind("Sidekick")) @@ -236,10 +259,14 @@ func (r *SidekickReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c sidekick.Status.Leader.Name = leader.Name sidekick.Status.Pod = pod.Status.Phase - sidekick.Status.Phase = "Current" + sidekick.Status.Phase = SidekickPhaseCurrent 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) { @@ -268,7 +295,7 @@ func convContainer(leader *corev1.Pod, c appsv1alpha1.Container) (*corev1.Contai TTY: c.TTY, } for _, vm := range c.VolumeMounts { - empty := vm.ReadOnly == false && + empty := !vm.ReadOnly && vm.MountPath == "" && vm.SubPath == "" && vm.MountPropagation == nil && @@ -388,6 +415,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 {