@@ -14,6 +14,7 @@ import (
1414 "time"
1515 "unicode"
1616
17+ "github.com/kelseyhightower/envconfig"
1718 volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
1819 "github.com/pkg/errors"
1920 "go.opentelemetry.io/otel"
@@ -283,69 +284,68 @@ func initManager(ctx context.Context) (runtime.Options, error) {
283284
284285 options .HealthProbeBindAddress = ":8081"
285286
287+ options .Controller .GroupKindConcurrency = map [string ]int {
288+ "PostgresCluster." + v1beta1 .GroupVersion .Group : 1 ,
289+ "PGUpgrade." + v1beta1 .GroupVersion .Group : 1 ,
290+ "PGAdmin." + v1beta1 .GroupVersion .Group : 1 ,
291+ "PerconaPGCluster." + v2 .GroupVersion .Group : 1 ,
292+ "PerconaPGUpgrade." + v2 .GroupVersion .Group : 1 ,
293+ "PerconaPGBackup." + v2 .GroupVersion .Group : 1 ,
294+ "PerconaPGRestore." + v2 .GroupVersion .Group : 1 ,
295+ }
296+
297+ // K8SPG-915
298+ envs := new (envConfig )
299+ if err := envconfig .Process ("" , envs ); err != nil {
300+ return options , errors .Wrap (err , "parse env vars" )
301+ }
302+
303+ options .LeaseDuration = & envs .LeaseDuration
304+ options .RenewDeadline = & envs .RenewDeadline
305+ options .RetryPeriod = & envs .RetryPeriod
306+ options .PprofBindAddress = envs .PprofBindAddress
307+
308+ options .LeaderElection = envs .LeaderElection
309+ if options .LeaderElection {
310+ options .LeaderElectionID = perconaRuntime .ElectionID
311+ }
312+
286313 // Enable leader elections when configured with a valid Lease.coordination.k8s.io name.
287314 // - https://docs.k8s.io/concepts/architecture/leases
288315 // - https://releases.k8s.io/v1.30.0/pkg/apis/coordination/validation/validation.go#L26
289- if lease := os . Getenv ( "PGO_CONTROLLER_LEASE_NAME" ); len (lease ) > 0 {
316+ if lease := envs . LeaderElectionID ; options . LeaderElection && len (lease ) > 0 {
290317 if errs := validation .IsDNS1123Subdomain (lease ); len (errs ) > 0 {
291318 return options , fmt .Errorf ("value for PGO_CONTROLLER_LEASE_NAME is invalid: %v" , errs )
292319 }
293320
294- options .LeaderElection = true
295321 options .LeaderElectionID = lease
296- options .LeaderElectionNamespace = os .Getenv ("PGO_NAMESPACE" )
297- } else {
298- // K8SPG-761
299- options .LeaderElection = true
300- options .LeaderElectionID = perconaRuntime .ElectionID
322+ options .LeaderElectionNamespace = envs .LeaderElectionNamespace
301323 }
302324
303- // Check PGO_TARGET_NAMESPACE for backwards compatibility with
304- // "singlenamespace" installations
305- singlenamespace := strings .TrimSpace (os .Getenv ("PGO_TARGET_NAMESPACE" ))
306-
307- // Check PGO_TARGET_NAMESPACES for non-cluster-wide, multi-namespace
308- // installations
309- multinamespace := strings .TrimSpace (os .Getenv ("PGO_TARGET_NAMESPACES" ))
310-
311- // Initialize DefaultNamespaces if any target namespaces are set
312- if len (singlenamespace ) > 0 || len (multinamespace ) > 0 {
325+ if len (envs .SingleNamespace ) > 0 || len (envs .MultiNamespaces ) > 0 {
326+ // Initialize DefaultNamespaces if any target namespaces are set
313327 options .Cache .DefaultNamespaces = map [string ]runtime.CacheConfig {}
314- }
315328
316- if len (singlenamespace ) > 0 {
317- options .Cache .DefaultNamespaces [singlenamespace ] = runtime.CacheConfig {}
318- }
319-
320- if len (multinamespace ) > 0 {
321- for _ , namespace := range strings .FieldsFunc (multinamespace , func (c rune ) bool {
322- return c != '-' && ! unicode .IsLetter (c ) && ! unicode .IsNumber (c )
323- }) {
324- options .Cache .DefaultNamespaces [namespace ] = runtime.CacheConfig {}
329+ if len (envs .SingleNamespace ) > 0 {
330+ options .Cache .DefaultNamespaces [envs .SingleNamespace ] = runtime.CacheConfig {}
325331 }
326- }
327332
328- options .Controller .GroupKindConcurrency = map [string ]int {
329- "PostgresCluster." + v1beta1 .GroupVersion .Group : 1 ,
330- "PGUpgrade." + v1beta1 .GroupVersion .Group : 1 ,
331- "PGAdmin." + v1beta1 .GroupVersion .Group : 1 ,
332- "PerconaPGCluster." + v2 .GroupVersion .Group : 1 ,
333- "PerconaPGUpgrade." + v2 .GroupVersion .Group : 1 ,
334- "PerconaPGBackup." + v2 .GroupVersion .Group : 1 ,
335- "PerconaPGRestore." + v2 .GroupVersion .Group : 1 ,
336- }
337-
338- if s := os .Getenv ("PGO_WORKERS" ); s != "" {
339- if i , err := strconv .Atoi (s ); err == nil && i > 0 {
340- for kind := range options .Controller .GroupKindConcurrency {
341- options .Controller .GroupKindConcurrency [kind ] = i
333+ if len (envs .MultiNamespaces ) > 0 {
334+ for _ , namespace := range strings .FieldsFunc (envs .MultiNamespaces , func (c rune ) bool {
335+ return c != '-' && ! unicode .IsLetter (c ) && ! unicode .IsNumber (c )
336+ }) {
337+ options .Cache .DefaultNamespaces [namespace ] = runtime.CacheConfig {}
342338 }
343- } else {
344- log .Error (err , "PGO_WORKERS must be a positive number" )
345339 }
346340 }
347341
348- options .PprofBindAddress = os .Getenv ("PPROF_BIND_ADDRESS" )
342+ if envs .Workers < 0 {
343+ log .Error (nil , "PGO_WORKERS must be a non-negative number; 0 disables the override" )
344+ } else if envs .Workers > 0 {
345+ for kind := range options .Controller .GroupKindConcurrency {
346+ options .Controller .GroupKindConcurrency [kind ] = envs .Workers
347+ }
348+ }
349349
350350 return options , nil
351351}
@@ -519,3 +519,20 @@ func isOpenshift(ctx context.Context, cfg *rest.Config) bool {
519519
520520 return false
521521}
522+
523+ type envConfig struct {
524+ LeaderElection bool `default:"true" envconfig:"PGO_CONTROLLER_LEADER_ELECTION_ENABLED"`
525+ LeaderElectionID string `envconfig:"PGO_CONTROLLER_LEASE_NAME"`
526+ LeaderElectionNamespace string `envconfig:"PGO_NAMESPACE"`
527+
528+ LeaseDuration time.Duration `default:"60s" envconfig:"PGO_CONTROLLER_LEASE_DURATION"`
529+ RenewDeadline time.Duration `default:"40s" envconfig:"PGO_CONTROLLER_RENEW_DEADLINE"`
530+ RetryPeriod time.Duration `default:"10s" envconfig:"PGO_CONTROLLER_RETRY_PERIOD"`
531+
532+ SingleNamespace string `envconfig:"PGO_TARGET_NAMESPACE"`
533+ MultiNamespaces string `envconfig:"PGO_TARGET_NAMESPACES"`
534+
535+ PprofBindAddress string `envconfig:"PPROF_BIND_ADDRESS"`
536+
537+ Workers int `envconfig:"PGO_WORKERS"`
538+ }
0 commit comments