Skip to content

Commit

Permalink
fix: check lifecycle actions to avoid NPE when calling actions during…
Browse files Browse the repository at this point in the history
… scale-in (#8762)
  • Loading branch information
leon-inf authored Jan 10, 2025
1 parent 0d7336f commit 18f1121
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions controllers/apps/transformer_component_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -700,21 +708,26 @@ 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) {
return nil
}
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
}
Expand Down

0 comments on commit 18f1121

Please sign in to comment.