Skip to content

Commit 4696a77

Browse files
committed
unify leader election entrypoint across components that need it
1 parent 45781bd commit 4696a77

File tree

4 files changed

+53
-48
lines changed

4 files changed

+53
-48
lines changed

cmd/common/helpers.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/openshift/library-go/pkg/config/clusterstatus"
2020
"github.com/openshift/library-go/pkg/config/leaderelection"
2121
"k8s.io/client-go/rest"
22+
k8sleaderelection "k8s.io/client-go/tools/leaderelection"
2223
)
2324

2425
// CreateResourceLock returns an interface for the resource lock.
@@ -58,6 +59,38 @@ func CreateResourceLock(cb *clients.Builder, componentNamespace, componentName s
5859
return lock
5960
}
6061

62+
type RunOpts struct {
63+
Namespace string
64+
ComponentName string
65+
Builder *clients.Builder
66+
OnStart func(context.Context)
67+
OnStop func()
68+
}
69+
70+
func DoLeaderElectionAndRunOrDie(ctx context.Context, o *RunOpts) {
71+
onStop := func() {
72+
if o.OnStop != nil {
73+
o.OnStop()
74+
}
75+
76+
klog.Infof("Stopped leading, terminating %s", o.ComponentName)
77+
}
78+
79+
cfg := GetLeaderElectionConfig(o.Builder.GetBuilderConfig())
80+
81+
k8sleaderelection.RunOrDie(ctx, k8sleaderelection.LeaderElectionConfig{
82+
Lock: CreateResourceLock(o.Builder, o.Namespace, o.ComponentName),
83+
ReleaseOnCancel: true,
84+
LeaseDuration: cfg.LeaseDuration.Duration,
85+
RenewDeadline: cfg.RenewDeadline.Duration,
86+
RetryPeriod: cfg.RetryPeriod.Duration,
87+
Callbacks: k8sleaderelection.LeaderCallbacks{
88+
OnStartedLeading: o.OnStart,
89+
OnStoppedLeading: onStop,
90+
},
91+
})
92+
}
93+
6194
// GetLeaderElectionConfig returns leader election configs defaults based on the cluster topology
6295
func GetLeaderElectionConfig(restcfg *rest.Config) configv1.LeaderElection {
6396

cmd/machine-config-controller/start.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/openshift/machine-config-operator/pkg/version"
2222
"github.com/spf13/cobra"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/client-go/tools/leaderelection"
2524
"k8s.io/klog/v2"
2625
)
2726

@@ -156,20 +155,11 @@ func runStartCmd(_ *cobra.Command, _ []string) {
156155
<-ctx.Done()
157156
}
158157

159-
leaderElectionCfg := common.GetLeaderElectionConfig(cb.GetBuilderConfig())
160-
161-
leaderelection.RunOrDie(runContext, leaderelection.LeaderElectionConfig{
162-
Lock: common.CreateResourceLock(cb, startOpts.resourceLockNamespace, componentName),
163-
ReleaseOnCancel: true,
164-
LeaseDuration: leaderElectionCfg.LeaseDuration.Duration,
165-
RenewDeadline: leaderElectionCfg.RenewDeadline.Duration,
166-
RetryPeriod: leaderElectionCfg.RetryPeriod.Duration,
167-
Callbacks: leaderelection.LeaderCallbacks{
168-
OnStartedLeading: run,
169-
OnStoppedLeading: func() {
170-
klog.Infof("Stopped leading. Terminating.")
171-
},
172-
},
158+
common.DoLeaderElectionAndRunOrDie(ctx, &common.RunOpts{
159+
Namespace: startOpts.resourceLockNamespace,
160+
ComponentName: componentName,
161+
Builder: cb,
162+
OnStart: run,
173163
})
174164
}
175165

cmd/machine-config-operator/start.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/openshift/machine-config-operator/pkg/operator"
1212
"github.com/openshift/machine-config-operator/pkg/version"
1313
"github.com/spf13/cobra"
14-
"k8s.io/client-go/tools/leaderelection"
1514
"k8s.io/klog/v2"
1615
)
1716

@@ -132,19 +131,10 @@ func runStartCmd(_ *cobra.Command, _ []string) {
132131
<-ctx.Done()
133132
}
134133

135-
leaderElectionCfg := common.GetLeaderElectionConfig(cb.GetBuilderConfig())
136-
137-
leaderelection.RunOrDie(runContext, leaderelection.LeaderElectionConfig{
138-
Lock: common.CreateResourceLock(cb, ctrlcommon.MCONamespace, componentName),
139-
ReleaseOnCancel: true,
140-
LeaseDuration: leaderElectionCfg.LeaseDuration.Duration,
141-
RenewDeadline: leaderElectionCfg.RenewDeadline.Duration,
142-
RetryPeriod: leaderElectionCfg.RetryPeriod.Duration,
143-
Callbacks: leaderelection.LeaderCallbacks{
144-
OnStartedLeading: run,
145-
OnStoppedLeading: func() {
146-
klog.Info("Stopped leading. Terminating.")
147-
},
148-
},
134+
common.DoLeaderElectionAndRunOrDie(runContext, &common.RunOpts{
135+
Namespace: ctrlcommon.MCONamespace,
136+
ComponentName: componentName,
137+
Builder: cb,
138+
OnStart: run,
149139
})
150140
}

cmd/machine-os-builder/start.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/openshift/machine-config-operator/internal/clients"
99
"github.com/openshift/machine-config-operator/pkg/controller/build"
1010
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
11-
"k8s.io/client-go/tools/leaderelection"
1211

1312
"github.com/openshift/machine-config-operator/pkg/version"
1413
"github.com/spf13/cobra"
@@ -71,23 +70,16 @@ func runStartCmd(_ *cobra.Command, _ []string) {
7170
ctrl.Run(ctx, 3)
7271
}
7372

74-
leaderElectionCfg := common.GetLeaderElectionConfig(cb.GetBuilderConfig())
75-
76-
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
77-
Lock: common.CreateResourceLock(cb, ctrlcommon.MCONamespace, componentName),
78-
ReleaseOnCancel: true,
79-
LeaseDuration: leaderElectionCfg.LeaseDuration.Duration,
80-
RenewDeadline: leaderElectionCfg.RenewDeadline.Duration,
81-
RetryPeriod: leaderElectionCfg.RetryPeriod.Duration,
82-
Callbacks: leaderelection.LeaderCallbacks{
83-
OnStartedLeading: run,
84-
OnStoppedLeading: func() {
85-
// Block this function until the controller shutdown is complete to
86-
// ensure that the controller both has enough time to finish its
87-
// cleanup as well as to terminate its lease.
88-
<-shutdownChan
89-
klog.Infof("Stopped leading; machine-os-builder terminating.")
90-
},
73+
common.DoLeaderElectionAndRunOrDie(ctx, &common.RunOpts{
74+
Namespace: ctrlcommon.MCONamespace,
75+
ComponentName: componentName,
76+
Builder: cb,
77+
OnStart: run,
78+
OnStop: func() {
79+
// Block this function until the controller shutdown is complete to
80+
// ensure that the controller both has enough time to finish its
81+
// cleanup as well as to terminate its lease.
82+
<-shutdownChan
9183
},
9284
})
9385
}

0 commit comments

Comments
 (0)