@@ -14,6 +14,7 @@ import (
1414
1515 "go.uber.org/zap/zapcore"
1616 _ "k8s.io/client-go/plugin/pkg/client/auth"
17+ "k8s.io/client-go/tools/leaderelection/resourcelock"
1718
1819 appsv1 "k8s.io/api/apps/v1"
1920 corev1 "k8s.io/api/core/v1"
@@ -75,8 +76,9 @@ const (
7576)
7677
7778var (
78- scheme = runtime .NewScheme ()
79- shutDownperiod = time .Second * 5
79+ scheme = runtime .NewScheme ()
80+ shutDownperiod = time .Second * 5
81+ leaderRetryDuration = 5 * time .Second
8082)
8183
8284func init () {
@@ -118,12 +120,15 @@ func main() {
118120 }
119121 }()
120122
121- var err error
122123 var configFile string
123124 flag .StringVar (& configFile , "config" , "" ,
124125 "The controller will load its initial configuration from this file. " +
125126 "Omit this flag to use the default configuration values. " +
126127 "Command-line flags override configuration from this file." )
128+
129+ var leaderRenewSeconds uint
130+ flag .UintVar (& leaderRenewSeconds , "leader-renew-seconds" , 10 , "Leader renewal frequency" )
131+
127132 flag .Parse ()
128133
129134 verbosity := 3 // TODO make this configurable 1-5 (5 is a security risk)
@@ -144,6 +149,8 @@ func main() {
144149 logger := ctrl .Log .WithName ("portal_manager" )
145150 setupLogger := logger .WithName ("setup" )
146151
152+ var err error
153+
147154 currentNS := os .Getenv (podNamespace )
148155 if len (currentNS ) == 0 {
149156 err = errors .New ("current namespace not found" )
@@ -214,9 +221,16 @@ func main() {
214221 }
215222 ctrlConfig .IOTCore = iotConfig
216223
224+ renewDeadline := time .Duration (leaderRenewSeconds ) * time .Second
225+ leaseDuration := time .Duration (int (1.2 * float64 (leaderRenewSeconds ))) * time .Second
226+
217227 options := ctrl.Options {
218- Scheme : scheme ,
219- LeaderElectionID : "storageos-portal-manager-leader" ,
228+ Scheme : scheme ,
229+ LeaderElectionID : "storageos-portal-manager-leader" ,
230+ LeaderElectionResourceLock : resourcelock .LeasesResourceLock ,
231+ RenewDeadline : & renewDeadline ,
232+ LeaseDuration : & leaseDuration ,
233+ RetryPeriod : & leaderRetryDuration ,
220234 }
221235 if configFile != "" {
222236 options , err = options .AndFrom (ctrl .ConfigFile ().AtPath (configFile ).OfKind (& ctrlConfig ))
0 commit comments