/
leader.go
30 lines (28 loc) · 1.01 KB
/
leader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package leader
import (
"github.com/mabels/cloudflared-controller/controller/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
func LeaderSelection(cfc types.CFController, lcb leaderelection.LeaderCallbacks) {
cfc.Log().Info().Str("namespace", cfc.Cfg().Leader.Namespace).Str("name", cfc.Cfg().Leader.Name).Msg("Start Leader Election")
lock := resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: cfc.Cfg().Leader.Name,
Namespace: cfc.Cfg().Leader.Namespace,
},
Client: cfc.Rest().K8s().CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: cfc.Cfg().Identity,
},
}
leaderelection.RunOrDie(cfc.Context(), leaderelection.LeaderElectionConfig{
Lock: &lock,
ReleaseOnCancel: true,
LeaseDuration: cfc.Cfg().Leader.LeaseDuration,
RenewDeadline: cfc.Cfg().Leader.RenewDeadline,
RetryPeriod: cfc.Cfg().Leader.RetryPeriod,
Callbacks: lcb,
})
}