Skip to content

Commit

Permalink
fix(pd): use pod in ctx directly and update it after api call (pingca…
Browse files Browse the repository at this point in the history
…p#147)

Signed-off-by: liubo02 <liubo02@pingcap.com>
  • Loading branch information
liubog2008 authored Nov 18, 2024
1 parent f2ed0b2 commit 2389715
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions pkg/controllers/pd/tasks/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 2389715

Please sign in to comment.