From 23897153b84106a8a6d10ab530639affe21485c6 Mon Sep 17 00:00:00 2001 From: Bo Liu Date: Mon, 18 Nov 2024 10:20:08 +0800 Subject: [PATCH] fix(pd): use pod in ctx directly and update it after api call (#147) Signed-off-by: liubo02 --- pkg/controllers/pd/tasks/pod.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/pkg/controllers/pd/tasks/pod.go b/pkg/controllers/pd/tasks/pod.go index 19219f0fe8..b0a5450d31 100644 --- a/pkg/controllers/pd/tasks/pod.go +++ b/pkg/controllers/pd/tasks/pod.go @@ -19,7 +19,6 @@ import ( "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/pingcap/tidb-operator/apis/core/v1alpha1" @@ -47,7 +46,7 @@ func (t *TaskPod) Name() string { } func TaskPodSuspend(c client.Client) task.Task[ReconcileContext] { - return task.NameTaskFunc[ReconcileContext]("PodSuspend", func(ctx task.Context[ReconcileContext]) task.Result { + return task.NameTaskFunc("PodSuspend", func(ctx task.Context[ReconcileContext]) task.Result { rtx := ctx.Self() if rtx.Pod == nil { return task.Complete().With("pod has been deleted") @@ -62,28 +61,20 @@ func TaskPodSuspend(c client.Client) task.Task[ReconcileContext] { func (t *TaskPod) Sync(ctx task.Context[ReconcileContext]) task.Result { rtx := ctx.Self() - cur := corev1.Pod{} expected := t.newPod(rtx.Cluster, rtx.PDGroup, rtx.PD) - - if err := t.Client.Get(ctx, client.ObjectKey{ - Name: rtx.PD.Name, - Namespace: rtx.PD.Namespace, - }, &cur); err != nil { - if !errors.IsNotFound(err) { - return task.Fail().With("can't get pod of pd: %v", err) - } - + if rtx.Pod == nil { if err := t.Client.Apply(rtx, expected); err != nil { return task.Fail().With("can't apply pod of pd: %v", err) } + rtx.Pod = expected return task.Complete().With("pod is synced") } - res := k8s.ComparePods(&cur, expected) + res := k8s.ComparePods(rtx.Pod, expected) t.Logger.Info("compare pod", "result", res, "configChanged", rtx.ConfigChanged) if res == k8s.CompareResultRecreate || rtx.ConfigChanged { t.Logger.Info("will recreate the pod") - if err := t.Client.Delete(rtx, &cur); err != nil { + if err := t.Client.Delete(rtx, rtx.Pod); err != nil { return task.Fail().With("can't delete pod of pd: %v", err) } return task.Complete().With("pod is deleting") @@ -92,6 +83,8 @@ func (t *TaskPod) Sync(ctx task.Context[ReconcileContext]) task.Result { if err := t.Client.Apply(rtx, expected); err != nil { return task.Fail().With("can't apply pod of pd: %v", err) } + + rtx.Pod = expected } return task.Complete().With("pod is synced")