-
Notifications
You must be signed in to change notification settings - Fork 69
K8SPG-915: add leader election options to operator.yaml
#1433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pooknull
wants to merge
11
commits into
main
Choose a base branch
from
K8SPG-915
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−83
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3c498ff
K8SPG-915: add leader election options to `operator.yaml`
pooknull 304a5a6
fix unit-tests
pooknull 8ac5ebc
Merge remote-tracking branch 'origin/main' into K8SPG-915
pooknull bf18afe
Merge branch 'main' into K8SPG-915
mayankshah1607 5ab1f96
add `PGO_CONTROLLER_LEADER_ELECTION_ENABLED` env var
pooknull 518e892
move env var logic to `env.go`
pooknull 81f6fe0
Merge branch 'main' into K8SPG-915
pooknull ec54157
copilot suggestions
pooknull 8cc91bf
use `kelseyhightower/envconfig`
pooknull 8e4ece2
Merge branch 'main' into K8SPG-915
pooknull 403100a
small fix
pooknull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import ( | |
| "time" | ||
| "unicode" | ||
|
|
||
| "github.com/kelseyhightower/envconfig" | ||
| "github.com/pkg/errors" | ||
| "go.opentelemetry.io/otel" | ||
| uzap "go.uber.org/zap" | ||
|
|
@@ -280,69 +281,68 @@ func initManager(ctx context.Context) (runtime.Options, error) { | |
|
|
||
| options.HealthProbeBindAddress = ":8081" | ||
|
|
||
| options.Controller.GroupKindConcurrency = map[string]int{ | ||
| "PostgresCluster." + v1beta1.GroupVersion.Group: 1, | ||
| "PGUpgrade." + v1beta1.GroupVersion.Group: 1, | ||
| "PGAdmin." + v1beta1.GroupVersion.Group: 1, | ||
| "PerconaPGCluster." + v2.GroupVersion.Group: 1, | ||
| "PerconaPGUpgrade." + v2.GroupVersion.Group: 1, | ||
| "PerconaPGBackup." + v2.GroupVersion.Group: 1, | ||
| "PerconaPGRestore." + v2.GroupVersion.Group: 1, | ||
| } | ||
|
|
||
| // K8SPG-915 | ||
| envs := new(envConfig) | ||
| if err := envconfig.Process("", envs); err != nil { | ||
| return options, errors.Wrap(err, "parse env vars") | ||
| } | ||
|
|
||
| options.LeaseDuration = &envs.LeaseDuration | ||
| options.RenewDeadline = &envs.RenewDeadline | ||
| options.RetryPeriod = &envs.RetryPeriod | ||
| options.PprofBindAddress = envs.PprofBindAddress | ||
|
|
||
| options.LeaderElection = envs.LeaderElection | ||
| if options.LeaderElection { | ||
| options.LeaderElectionID = perconaRuntime.ElectionID | ||
| } | ||
|
|
||
| // Enable leader elections when configured with a valid Lease.coordination.k8s.io name. | ||
| // - https://docs.k8s.io/concepts/architecture/leases | ||
| // - https://releases.k8s.io/v1.30.0/pkg/apis/coordination/validation/validation.go#L26 | ||
| if lease := os.Getenv("PGO_CONTROLLER_LEASE_NAME"); len(lease) > 0 { | ||
| if lease := envs.LeaderElectionID; options.LeaderElection && len(lease) > 0 { | ||
| if errs := validation.IsDNS1123Subdomain(lease); len(errs) > 0 { | ||
| return options, fmt.Errorf("value for PGO_CONTROLLER_LEASE_NAME is invalid: %v", errs) | ||
| } | ||
|
|
||
| options.LeaderElection = true | ||
| options.LeaderElectionID = lease | ||
| options.LeaderElectionNamespace = os.Getenv("PGO_NAMESPACE") | ||
| } else { | ||
| // K8SPG-761 | ||
| options.LeaderElection = true | ||
| options.LeaderElectionID = perconaRuntime.ElectionID | ||
| options.LeaderElectionNamespace = envs.LeaderElectionNamespace | ||
| } | ||
|
|
||
| // Check PGO_TARGET_NAMESPACE for backwards compatibility with | ||
| // "singlenamespace" installations | ||
| singlenamespace := strings.TrimSpace(os.Getenv("PGO_TARGET_NAMESPACE")) | ||
|
|
||
| // Check PGO_TARGET_NAMESPACES for non-cluster-wide, multi-namespace | ||
| // installations | ||
| multinamespace := strings.TrimSpace(os.Getenv("PGO_TARGET_NAMESPACES")) | ||
|
|
||
| // Initialize DefaultNamespaces if any target namespaces are set | ||
| if len(singlenamespace) > 0 || len(multinamespace) > 0 { | ||
| if len(envs.SingleNamespace) > 0 || len(envs.MultiNamespaces) > 0 { | ||
| // Initialize DefaultNamespaces if any target namespaces are set | ||
| options.Cache.DefaultNamespaces = map[string]runtime.CacheConfig{} | ||
| } | ||
|
|
||
| if len(singlenamespace) > 0 { | ||
| options.Cache.DefaultNamespaces[singlenamespace] = runtime.CacheConfig{} | ||
| } | ||
|
|
||
| if len(multinamespace) > 0 { | ||
| for _, namespace := range strings.FieldsFunc(multinamespace, func(c rune) bool { | ||
| return c != '-' && !unicode.IsLetter(c) && !unicode.IsNumber(c) | ||
| }) { | ||
| options.Cache.DefaultNamespaces[namespace] = runtime.CacheConfig{} | ||
| if len(envs.SingleNamespace) > 0 { | ||
| options.Cache.DefaultNamespaces[envs.SingleNamespace] = runtime.CacheConfig{} | ||
| } | ||
| } | ||
|
|
||
| options.Controller.GroupKindConcurrency = map[string]int{ | ||
| "PostgresCluster." + v1beta1.GroupVersion.Group: 1, | ||
| "PGUpgrade." + v1beta1.GroupVersion.Group: 1, | ||
| "PGAdmin." + v1beta1.GroupVersion.Group: 1, | ||
| "PerconaPGCluster." + v2.GroupVersion.Group: 1, | ||
| "PerconaPGUpgrade." + v2.GroupVersion.Group: 1, | ||
| "PerconaPGBackup." + v2.GroupVersion.Group: 1, | ||
| "PerconaPGRestore." + v2.GroupVersion.Group: 1, | ||
| } | ||
|
|
||
| if s := os.Getenv("PGO_WORKERS"); s != "" { | ||
| if i, err := strconv.Atoi(s); err == nil && i > 0 { | ||
| for kind := range options.Controller.GroupKindConcurrency { | ||
| options.Controller.GroupKindConcurrency[kind] = i | ||
| if len(envs.MultiNamespaces) > 0 { | ||
| for _, namespace := range strings.FieldsFunc(envs.MultiNamespaces, func(c rune) bool { | ||
| return c != '-' && !unicode.IsLetter(c) && !unicode.IsNumber(c) | ||
| }) { | ||
| options.Cache.DefaultNamespaces[namespace] = runtime.CacheConfig{} | ||
| } | ||
| } else { | ||
| log.Error(err, "PGO_WORKERS must be a positive number") | ||
| } | ||
| } | ||
|
|
||
| options.PprofBindAddress = os.Getenv("PPROF_BIND_ADDRESS") | ||
| if envs.Workers < 0 { | ||
| log.Error(nil, "PGO_WORKERS must be a non-negative number; 0 disables the override") | ||
| } else if envs.Workers > 0 { | ||
| for kind := range options.Controller.GroupKindConcurrency { | ||
| options.Controller.GroupKindConcurrency[kind] = envs.Workers | ||
| } | ||
| } | ||
|
|
||
| return options, nil | ||
| } | ||
|
|
@@ -516,3 +516,20 @@ func isOpenshift(ctx context.Context, cfg *rest.Config) bool { | |
|
|
||
| return false | ||
| } | ||
|
|
||
| type envConfig struct { | ||
| LeaderElection bool `default:"true" envconfig:"PGO_CONTROLLER_LEADER_ELECTION_ENABLED"` | ||
| LeaderElectionID string `envconfig:"PGO_CONTROLLER_LEASE_NAME"` | ||
| LeaderElectionNamespace string `envconfig:"PGO_NAMESPACE"` | ||
|
|
||
|
Comment on lines
+520
to
+524
|
||
| LeaseDuration time.Duration `default:"60s" envconfig:"PGO_CONTROLLER_LEASE_DURATION"` | ||
| RenewDeadline time.Duration `default:"40s" envconfig:"PGO_CONTROLLER_RENEW_DEADLINE"` | ||
| RetryPeriod time.Duration `default:"10s" envconfig:"PGO_CONTROLLER_RETRY_PERIOD"` | ||
|
|
||
| SingleNamespace string `envconfig:"PGO_TARGET_NAMESPACE"` | ||
| MultiNamespaces string `envconfig:"PGO_TARGET_NAMESPACES"` | ||
|
|
||
| PprofBindAddress string `envconfig:"PPROF_BIND_ADDRESS"` | ||
|
|
||
| Workers int `envconfig:"PGO_WORKERS"` | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,6 @@ | |||||||||
| package main | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "context" | ||||||||||
| "reflect" | ||||||||||
| "testing" | ||||||||||
| "time" | ||||||||||
|
|
@@ -15,7 +14,7 @@ import ( | |||||||||
| ) | ||||||||||
|
|
||||||||||
| func TestInitManager(t *testing.T) { | ||||||||||
| ctx := context.Background() | ||||||||||
| ctx := t.Context() | ||||||||||
| t.Run("Defaults", func(t *testing.T) { | ||||||||||
| options, err := initManager(ctx) | ||||||||||
| assert.NilError(t, err) | ||||||||||
|
|
@@ -39,13 +38,19 @@ func TestInitManager(t *testing.T) { | |||||||||
|
|
||||||||||
| assert.Assert(t, options.Cache.DefaultNamespaces == nil) | ||||||||||
| assert.Assert(t, options.LeaderElection == true) | ||||||||||
| assert.Assert(t, options.LeaseDuration.Seconds() == 60) | ||||||||||
| assert.Assert(t, options.RenewDeadline.Seconds() == 40) | ||||||||||
| assert.Assert(t, options.RetryPeriod.Seconds() == 10) | ||||||||||
pooknull marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| { | ||||||||||
| options.Cache.SyncPeriod = nil | ||||||||||
| options.Controller.GroupKindConcurrency = nil | ||||||||||
| options.HealthProbeBindAddress = "" | ||||||||||
| options.LeaderElection = false | ||||||||||
| options.LeaderElectionID = "" | ||||||||||
| options.LeaseDuration = nil | ||||||||||
| options.RenewDeadline = nil | ||||||||||
| options.RetryPeriod = nil | ||||||||||
|
|
||||||||||
| assert.Assert(t, reflect.ValueOf(options).IsZero(), | ||||||||||
| "expected remaining fields to be unset:\n%+v", options) | ||||||||||
|
|
@@ -62,6 +67,13 @@ func TestInitManager(t *testing.T) { | |||||||||
| assert.ErrorContains(t, err, "PGO_CONTROLLER_LEASE_NAME") | ||||||||||
| assert.ErrorContains(t, err, "invalid") | ||||||||||
|
|
||||||||||
| assert.Assert(t, options.LeaderElection == true) | ||||||||||
| assert.Equal(t, options.LeaderElectionNamespace, "") | ||||||||||
|
|
||||||||||
| t.Setenv("PGO_CONTROLLER_LEADER_ELECTION_ENABLED", "false") | ||||||||||
| options, err = initManager(ctx) | ||||||||||
| assert.NilError(t, err) | ||||||||||
|
|
||||||||||
| assert.Assert(t, options.LeaderElection == false) | ||||||||||
| assert.Equal(t, options.LeaderElectionNamespace, "") | ||||||||||
| }) | ||||||||||
|
|
@@ -106,7 +118,11 @@ func TestInitManager(t *testing.T) { | |||||||||
| t.Setenv("PGO_WORKERS", v) | ||||||||||
|
|
||||||||||
| options, err := initManager(ctx) | ||||||||||
| assert.NilError(t, err) | ||||||||||
| if v == "3.14" { | ||||||||||
| assert.ErrorContains(t, err, "parse env vars: envconfig.Process: assigning PGO_WORKERS to Workers: converting '3.14' to type int. details: strconv.ParseInt: parsing \"3.14\": invalid syntax") | ||||||||||
|
||||||||||
| assert.ErrorContains(t, err, "parse env vars: envconfig.Process: assigning PGO_WORKERS to Workers: converting '3.14' to type int. details: strconv.ParseInt: parsing \"3.14\": invalid syntax") | |
| assert.ErrorContains(t, err, "parse env vars") | |
| assert.ErrorContains(t, err, "PGO_WORKERS") | |
| assert.ErrorContains(t, err, "invalid syntax") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
envconfig.Processnow makesinitManagerfail whenPGO_WORKERSis set to a non-integer value (e.g.,3.14), whereas previously invalid values were logged and ignored.PGO_WORKERSseparately (string -> int) and keep the previous log-and-ignore behavior for invalid values, while still using envconfig for the new leader-election durations.