-
Notifications
You must be signed in to change notification settings - Fork 104
/
leaderelection.go
37 lines (31 loc) · 1.14 KB
/
leaderelection.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
31
32
33
34
35
36
37
package util
import (
"context"
configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/library-go/pkg/config/clusterstatus"
"github.com/openshift/library-go/pkg/config/leaderelection"
"k8s.io/client-go/rest"
"k8s.io/klog"
)
// GetLeaderElectionConfig returns leader election configs defaults based on the cluster topology
func GetLeaderElectionConfig(restcfg *rest.Config, enabled bool) configv1.LeaderElection {
// Defaults follow conventions
// https://github.com/openshift/enhancements/blob/master/CONVENTIONS.md#high-availability
defaultLeaderElection := leaderelection.LeaderElectionDefaulting(
configv1.LeaderElection{
Disable: !enabled,
},
"", "",
)
if !enabled {
return defaultLeaderElection
}
if infra, err := clusterstatus.GetClusterInfraStatus(context.TODO(), restcfg); err == nil && infra != nil {
if infra.ControlPlaneTopology == configv1.SingleReplicaTopologyMode {
return leaderelection.LeaderElectionSNOConfig(defaultLeaderElection)
}
} else {
klog.Warningf("unable to get cluster infrastructure status, using HA cluster values for leader election: %v", err)
}
return defaultLeaderElection
}