Skip to content

Set a role=leader label on the Pod who won the leader election #208

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 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions pkg/operator/operator_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"

"github.com/arangodb/arangosync/pkg/retry"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
"github.com/arangodb/kube-arangodb/pkg/util/probe"
)
Expand Down Expand Up @@ -75,6 +77,10 @@ func (o *Operator) runLeaderElection(lockName string, onStart func(stop <-chan s
OnStartedLeading: func(stop <-chan struct{}) {
recordEvent("Leader Election Won", fmt.Sprintf("Pod %s is running as leader", o.Config.PodName))
readyProbe.SetReady()
if err := o.setRoleLabel(log, constants.LabelRoleLeader); err != nil {
log.Error().Msg("Cannot set leader role on Pod. Terminating process")
os.Exit(2)
}
onStart(stop)
},
OnStoppedLeading: func() {
Expand Down Expand Up @@ -123,3 +129,38 @@ func (o *Operator) getLeaderElectionEventTarget(log zerolog.Logger) runtime.Obje
}
return depl
}

// setRoleLabel sets a label with key `role` and given value in the pod metadata.
func (o *Operator) setRoleLabel(log zerolog.Logger, role string) error {
ns := o.Config.Namespace
kubecli := o.Dependencies.KubeCli
pods := kubecli.CoreV1().Pods(ns)
log = log.With().Str("pod-name", o.Config.PodName).Logger()
op := func() error {
pod, err := pods.Get(o.Config.PodName, metav1.GetOptions{})
if k8sutil.IsNotFound(err) {
log.Error().Err(err).Msg("Pod not found, so we cannot set its role label")
return retry.Permanent(maskAny(err))
} else if err != nil {
return maskAny(err)
}
labels := pod.ObjectMeta.GetLabels()
if labels == nil {
labels = make(map[string]string)
}
labels[constants.LabelRole] = role
pod.ObjectMeta.SetLabels(labels)
if _, err := pods.Update(pod); k8sutil.IsConflict(err) {
// Retry it
return maskAny(err)
} else if err != nil {
log.Error().Err(err).Msg("Failed to update Pod wrt 'role' label")
return retry.Permanent(maskAny(err))
}
return nil
}
if err := retry.Retry(op, time.Second*15); err != nil {
return maskAny(err)
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/util/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ const (
FinalizerPVCMemberExists = "pvc.database.arangodb.com/member-exists" // Finalizer added to PVCs, indicating the need to keep is as long as its member exists

AnnotationEnforceAntiAffinity = "database.arangodb.com/enforce-anti-affinity" // Key of annotation added to PVC. Value is a boolean "true" or "false"

LabelRole = "role"
LabelRoleLeader = "leader"
)