Skip to content

Commit 6ab48cb

Browse files
committed
Add support for leaderelection, fixes #152
1 parent ba128e0 commit 6ab48cb

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

cmd/mysql-operator/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ func main() {
7070
}
7171

7272
// Create a new Cmd to provide shared dependencies and start components
73-
mgr, err := manager.New(cfg, manager.Options{})
73+
mgr, err := manager.New(cfg, manager.Options{
74+
LeaderElection: true,
75+
LeaderElectionNamespace: opt.LeaderElectionNamespace,
76+
LeaderElectionID: opt.LeaderElectionID,
77+
})
7478
if err != nil {
7579
log.Error(err, "unable to create a new manager")
7680
os.Exit(1)

hack/charts/mysql-operator/templates/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ spec:
3636
name: {{ template "mysql-operator.orchestrator.fullname" . }}
3737
key: TOPOLOGY_PASSWORD
3838
args:
39+
- --leader-election-namespace={{ .Release.Namespace }}
3940
- --orchestrator-uri=http://{{ template "mysql-operator.orchestrator.fullname" . }}.{{ .Release.Namespace }}/api
4041
{{- if .Values.sidecarImage }}
4142
- --helper-image={{ .Values.sidecarImage }}

pkg/options/options.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/spf13/pflag"
26-
"k8s.io/api/core/v1"
26+
corev1 "k8s.io/api/core/v1"
2727

2828
"github.com/presslabs/mysql-operator/pkg/util"
2929
)
@@ -49,7 +49,7 @@ type Options struct {
4949
MetricsExporterImage string
5050

5151
ImagePullSecretName string
52-
ImagePullPolicy v1.PullPolicy
52+
ImagePullPolicy corev1.PullPolicy
5353

5454
OrchestratorURI string
5555
OrchestratorTopologyPassword string
@@ -58,9 +58,12 @@ type Options struct {
5858
JobCompleteSuccessGraceTime time.Duration
5959

6060
HTTPServeAddr string
61+
62+
LeaderElectionNamespace string
63+
LeaderElectionID string
6164
}
6265

63-
type pullpolicy v1.PullPolicy
66+
type pullpolicy corev1.PullPolicy
6467

6568
func (pp *pullpolicy) String() string {
6669
return string(*pp)
@@ -76,7 +79,7 @@ func (pp *pullpolicy) Type() string {
7679
}
7780

7881
// nolint: unparam
79-
func newPullPolicyValue(defaultValue v1.PullPolicy, v *v1.PullPolicy) *pullpolicy {
82+
func newPullPolicyValue(defaultValue corev1.PullPolicy, v *corev1.PullPolicy) *pullpolicy {
8083
*v = defaultValue
8184
return (*pullpolicy)(v)
8285
}
@@ -88,12 +91,15 @@ const (
8891
defaultMysqlImage = "percona:5.7-stretch"
8992
defaultExporterImage = "prom/mysqld-exporter:latest"
9093

91-
defaultImagePullPolicy = v1.PullIfNotPresent
94+
defaultImagePullPolicy = corev1.PullIfNotPresent
9295

9396
defaultOrchestratorTopologyUser = ""
9497
defaultOrchestratorTopologyPassword = ""
9598

9699
defaultHTTPServerAddr = ":80"
100+
101+
defaultLeaderElectionNamespace = "default"
102+
defaultLeaderElectionID = ""
97103
)
98104

99105
var (
@@ -126,6 +132,11 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
126132

127133
fs.StringVar(&o.HTTPServeAddr, "http-serve-addr", defaultHTTPServerAddr,
128134
"The address for http server.")
135+
136+
fs.StringVar(&o.LeaderElectionNamespace, "leader-election-namespace", defaultLeaderElectionNamespace,
137+
"The leader election namespace.")
138+
fs.StringVar(&o.LeaderElectionID, "leader-election-id", defaultLeaderElectionID,
139+
"The leader election id.")
129140
}
130141

131142
var instance *Options

0 commit comments

Comments
 (0)