Skip to content

Commit 7051e00

Browse files
committed
Set operator Pod ready when leader change is detected
1 parent c914e4c commit 7051e00

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pkg/operator/operator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ func NewOperator(config Config, deps Dependencies) (*Operator, error) {
105105
// Run the operator
106106
func (o *Operator) Run() {
107107
if o.Config.EnableDeployment {
108-
go o.runLeaderElection("arango-deployment-operator", o.onStartDeployment)
108+
go o.runLeaderElection("arango-deployment-operator", o.onStartDeployment, o.Dependencies.DeploymentProbe)
109109
}
110110
if o.Config.EnableDeploymentReplication {
111-
go o.runLeaderElection("arango-deployment-replication-operator", o.onStartDeploymentReplication)
111+
go o.runLeaderElection("arango-deployment-replication-operator", o.onStartDeploymentReplication, o.Dependencies.DeploymentReplicationProbe)
112112
}
113113
if o.Config.EnableStorage {
114-
go o.runLeaderElection("arango-storage-operator", o.onStartStorage)
114+
go o.runLeaderElection("arango-storage-operator", o.onStartStorage, o.Dependencies.StorageProbe)
115115
}
116116
// Wait until process terminates
117117
<-context.TODO().Done()

pkg/operator/operator_leader.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ import (
3535
"k8s.io/client-go/tools/leaderelection/resourcelock"
3636

3737
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
38+
"github.com/arangodb/kube-arangodb/pkg/util/probe"
3839
)
3940

4041
// runLeaderElection performs a leader election on a lock with given name in
4142
// the namespace that the operator is deployed in.
4243
// When the leader election is won, the given callback is called.
4344
// When the leader election is lost (even after it was won once), the process is killed.
44-
func (o *Operator) runLeaderElection(lockName string, onStart func(stop <-chan struct{})) {
45+
// The given ready probe is set, as soon as this process became the leader, or a new leader
46+
// is detected.
47+
func (o *Operator) runLeaderElection(lockName string, onStart func(stop <-chan struct{}), readyProbe *probe.ReadyProbe) {
4548
namespace := o.Config.Namespace
4649
kubecli := o.Dependencies.KubeCli
4750
log := o.log.With().Str("lock-name", lockName).Logger()
@@ -71,13 +74,18 @@ func (o *Operator) runLeaderElection(lockName string, onStart func(stop <-chan s
7174
Callbacks: leaderelection.LeaderCallbacks{
7275
OnStartedLeading: func(stop <-chan struct{}) {
7376
recordEvent("Leader Election Won", fmt.Sprintf("Pod %s is running as leader", o.Config.PodName))
77+
readyProbe.SetReady()
7478
onStart(stop)
7579
},
7680
OnStoppedLeading: func() {
7781
recordEvent("Stop Leading", fmt.Sprintf("Pod %s is stopping to run as leader", o.Config.PodName))
7882
log.Info().Msg("Stop leading. Terminating process")
7983
os.Exit(1)
8084
},
85+
OnNewLeader: func(identity string) {
86+
log.Info().Str("identity", identity).Msg("New leader detected")
87+
readyProbe.SetReady()
88+
},
8189
},
8290
})
8391
}

0 commit comments

Comments
 (0)