From 6be9263c185c6594ffc2a8d3276cc9f22f818c90 Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 8 Jan 2025 16:40:48 +0800 Subject: [PATCH] fix: check lifecycle actions to avoid NPE when calling actions during scale-in --- .../apps/transformer_component_workload.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/controllers/apps/transformer_component_workload.go b/controllers/apps/transformer_component_workload.go index 8f2f05c1ec1..ce94edfaba4 100644 --- a/controllers/apps/transformer_component_workload.go +++ b/controllers/apps/transformer_component_workload.go @@ -687,7 +687,15 @@ func (r *componentWorkloadOps) leaveMember4ScaleIn(deleteReplicas, joinedReplica } func (r *componentWorkloadOps) leaveMemberForPod(pod *corev1.Pod, pods []*corev1.Pod) error { + var ( + synthesizedComp = r.synthesizeComp + lifecycleActions = synthesizedComp.LifecycleActions + ) + trySwitchover := func(lfa lifecycle.Lifecycle, pod *corev1.Pod) error { + if lifecycleActions.Switchover == nil { + return nil + } err := lfa.Switchover(r.reqCtx.Ctx, r.cli, nil, "") if err != nil { if errors.Is(err, lifecycle.ErrActionNotDefined) { @@ -700,6 +708,9 @@ func (r *componentWorkloadOps) leaveMemberForPod(pod *corev1.Pod, pods []*corev1 } tryMemberLeave := func(lfa lifecycle.Lifecycle, pod *corev1.Pod) error { + if lifecycleActions.MemberLeave == nil { + return nil + } err := lfa.MemberLeave(r.reqCtx.Ctx, r.cli, nil) if err != nil { if errors.Is(err, lifecycle.ErrActionNotDefined) { @@ -707,14 +718,16 @@ func (r *componentWorkloadOps) leaveMemberForPod(pod *corev1.Pod, pods []*corev1 } return err } - r.reqCtx.Log.Info("successfully call leave member action for pod", "pod", pod.Name) return nil } - synthesizedComp := r.synthesizeComp + if lifecycleActions == nil || (lifecycleActions.Switchover == nil && lifecycleActions.MemberLeave == nil) { + return nil + } + lfa, err := lifecycle.New(synthesizedComp.Namespace, synthesizedComp.ClusterName, synthesizedComp.Name, - synthesizedComp.LifecycleActions, synthesizedComp.TemplateVars, pod, pods...) + lifecycleActions, synthesizedComp.TemplateVars, pod, pods...) if err != nil { return err }