Skip to content

leader election bugfix: Delete evicted leader pods #2210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion pkg/leader/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,35 @@ func Become(ctx context.Context, lockName string) error {
log.Info("Became the leader.")
return nil
case apierrors.IsAlreadyExists(err):
log.Info("Not the leader. Waiting.")
existingOwners := existing.GetOwnerReferences()
switch {
case len(existingOwners) != 1:
log.Info("Leader lock configmap must have exactly one owner reference.", "ConfigMap", existing)
case existingOwners[0].Kind != "Pod":
log.Info("Leader lock configmap owner reference must be a pod.", "OwnerReference", existingOwners[0])
default:
leaderPod := &corev1.Pod{}
key = crclient.ObjectKey{Namespace: ns, Name: existingOwners[0].Name}
err = client.Get(ctx, key, leaderPod)
switch {
case apierrors.IsNotFound(err):
log.Info("Leader pod has been deleted, waiting for garbage collection do remove the lock.")
case err != nil:
return err
case isPodEvicted(*leaderPod) && leaderPod.GetDeletionTimestamp() == nil:
log.Info("Operator pod with leader lock has been evicted.", "leader", leaderPod.Name)
log.Info("Deleting evicted leader.")
// Pod may not delete immediately, continue with backoff
err := client.Delete(ctx, leaderPod)
if err != nil {
log.Error(err, "Leader pod could not be deleted.")
}

default:
log.Info("Not the leader. Waiting.")
}
}

select {
case <-time.After(wait.Jitter(backoff, .2)):
if backoff < maxBackoffInterval {
Expand Down Expand Up @@ -143,3 +171,9 @@ func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1
}
return owner, nil
}

func isPodEvicted(pod corev1.Pod) bool {
podFailed := pod.Status.Phase == corev1.PodFailed
podEvicted := pod.Status.Reason == "Evicted"
return podFailed && podEvicted
}