From df44034fcf70cb3ebe1771ae4f19428bd2cd2d8d Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Tue, 24 Dec 2024 15:40:31 +0100 Subject: [PATCH 1/3] Enable leader election. --- percona/runtime/runtime.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/percona/runtime/runtime.go b/percona/runtime/runtime.go index eb71698349..f8e8181680 100644 --- a/percona/runtime/runtime.go +++ b/percona/runtime/runtime.go @@ -25,7 +25,9 @@ func CreateRuntimeManager(namespaces string, config *rest.Config, disableMetrics Cache: cache.Options{ SyncPeriod: &refreshInterval, }, - Scheme: r.Scheme, + Scheme: r.Scheme, + LeaderElection: true, + LeaderElectionID: "08db3feb.percona.com", } options.BaseContext = func() context.Context { From 0161b4c1eba529008c8981c2f38ae87695764e81 Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Tue, 24 Dec 2024 16:28:38 +0100 Subject: [PATCH 2/3] Make leader election setting configurable. --- cmd/postgres-operator/main.go | 2 +- percona/controller/pgcluster/controller_test.go | 2 +- percona/runtime/runtime.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/postgres-operator/main.go b/cmd/postgres-operator/main.go index 50faf7f2ad..1447b8e8cd 100644 --- a/cmd/postgres-operator/main.go +++ b/cmd/postgres-operator/main.go @@ -27,7 +27,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" //"github.com/percona/percona-postgresql-operator/internal/controller/pgupgrade" - "github.com/percona/percona-postgresql-operator/internal/controller/pgupgrade" "github.com/percona/percona-postgresql-operator/internal/controller/postgrescluster" "github.com/percona/percona-postgresql-operator/internal/controller/runtime" @@ -106,6 +105,7 @@ func main() { namespaces, cfg, false, + false, features, ) assertNoError(err) diff --git a/percona/controller/pgcluster/controller_test.go b/percona/controller/pgcluster/controller_test.go index e6b3770b71..95642a807e 100644 --- a/percona/controller/pgcluster/controller_test.go +++ b/percona/controller/pgcluster/controller_test.go @@ -484,7 +484,7 @@ var _ = Describe("Watching secrets", Ordered, func() { Expect(err).NotTo(HaveOccurred()) Expect(err).To(Not(HaveOccurred())) - mgr, err := runtime.CreateRuntimeManager(namespace.Name, cfg, true, gate) + mgr, err := runtime.CreateRuntimeManager(namespace.Name, cfg, true, true, gate) Expect(err).To(Succeed()) Expect(v2.AddToScheme(mgr.GetScheme())).To(Succeed()) diff --git a/percona/runtime/runtime.go b/percona/runtime/runtime.go index f8e8181680..6c36603bc3 100644 --- a/percona/runtime/runtime.go +++ b/percona/runtime/runtime.go @@ -19,14 +19,14 @@ var refreshInterval = 60 * time.Minute // CreateRuntimeManager does the same thing as `internal/controller/runtime.CreateRuntimeManager`, // excet it configures the manager to watch multiple namespaces. -func CreateRuntimeManager(namespaces string, config *rest.Config, disableMetrics bool, features feature.MutableGate) (manager.Manager, error) { +func CreateRuntimeManager(namespaces string, config *rest.Config, disableMetrics, disableLeaderElection bool, features feature.MutableGate) (manager.Manager, error) { options := manager.Options{ Cache: cache.Options{ SyncPeriod: &refreshInterval, }, Scheme: r.Scheme, - LeaderElection: true, + LeaderElection: disableLeaderElection, LeaderElectionID: "08db3feb.percona.com", } From 32a6418397a3826c500a7d34446f0543c04b6bd8 Mon Sep 17 00:00:00 2001 From: Inel Pandzic Date: Tue, 24 Dec 2024 17:00:45 +0100 Subject: [PATCH 3/3] Fix unit test. --- percona/runtime/runtime.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/percona/runtime/runtime.go b/percona/runtime/runtime.go index 6c36603bc3..33edaa5a24 100644 --- a/percona/runtime/runtime.go +++ b/percona/runtime/runtime.go @@ -30,6 +30,11 @@ func CreateRuntimeManager(namespaces string, config *rest.Config, disableMetrics LeaderElectionID: "08db3feb.percona.com", } + if disableLeaderElection { + options.LeaderElection = false + options.LeaderElectionID = "" + } + options.BaseContext = func() context.Context { ctx := context.Background() return feature.NewContext(ctx, features)