Skip to content

Commit

Permalink
Allow manager options to be set from environment
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Oct 18, 2023
1 parent aa53499 commit 8ceb353
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/scratch-env/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,8 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE=
github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/onsi/gomega v1.28.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_model v0.5.0
github.com/sethvargo/go-envconfig v0.9.0
go.uber.org/goleak v1.2.1
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE=
github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down
116 changes: 116 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/sethvargo/go-envconfig"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -318,6 +319,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
}
// Set default values for options fields
options = setOptionsDefaults(options)
options = setOptionsFromEnv(options)

cluster, err := cluster.New(config, func(clusterOptions *cluster.Options) {
clusterOptions.Scheme = options.Scheme
Expand Down Expand Up @@ -667,3 +669,117 @@ func setOptionsDefaults(options Options) Options {

return options
}

type envOptions struct {
// Override determines whether or not to use environment variables to override
// controller global configuration options.
Override bool `env:"ENV_OVERRIDE"`

LeaderElection *bool `env:"LEADER_ELECTION_ENABLED,noinit"`
LeaderElectionResourceLock *string `env:"LEADER_ELECTION_RESOURCE_LOCK,noinit"`
LeaderElectionNamespace *string `env:"LEADER_ELECTION_NAMESPACE,noinit"`
LeaderElectionID *string `env:"LEADER_ELECTION_ID,noinit"`
LeaderElectionLeaseDuration *time.Duration `env:"LEADER_ELECTION_LEASE_DURATION,noinit"`
LeaderElectionRenewDeadline *time.Duration `env:"LEADER_ELECTION_RENEW_DEADLINE,noinit"`
LeaderElectionRetryPeriod *time.Duration `env:"LEADER_ELECTION_RETRY_PERIOD,noinit"`
LeaderElectionReleaseOnCancel *bool `env:"LEADER_ELECTION_RELEASE_ON_CANCEL,noinit`

Check failure on line 685 in pkg/manager/manager.go

View workflow job for this annotation

GitHub Actions / lint

structtag: struct field tag `env:"LEADER_ELECTION_RELEASE_ON_CANCEL,noinit` not compatible with reflect.StructTag.Get: bad syntax for struct tag value (govet)

MetricsSecureServing *bool `env:"METRICS_SERVER_SECURE_SERVING,noinit"`
MetricsBindAddress *string `env:"METRICS_SERVER_BIND_ADDRESS,noinit"`
MetricsCertDir *string `env:"METRICS_SERVER_CERT_DIR,noinit"`

HealthProbeBindAddress *string `env:"HEALTH_PROBE_BIND_ADDRESS,noinit"`
PprofBindAddress *string `env:"PPROF_BIND_ADDRESS,noinit"`

WebhookPort *int `env:"WEBHOOK_SERVER_PORT,noinit"`
WebhookHost *string `env:"WEBHOOK_SERVER_HOST,noinit"`
WebhookCertDir *string `env:"WEBHOOK_SERVER_CERT_DIR,noinit"`

GracefulShutdownTimeout *metav1.Duration `env:"GRACEFUL_SHUTDOWN_TIMEOUT,noinit"`
}

func setOptionsFromEnv(options Options) Options {
env := &envOptions{}
look := envconfig.PrefixLookuper("CONTROLLER_RUNTIME_", envconfig.OsLookuper())
if err := envconfig.ProcessWith(context.Background(), env, look); err != nil {
// If there was an error processing the environment variables, log, and continue.
options.Logger.Error(err, "error processing environment variables")
return options
}

// If the override flag is not set, return the options as is.
if !env.Override {
return options
}

if env.LeaderElection != nil {
options.LeaderElection = *env.LeaderElection
}
if env.LeaderElectionResourceLock != nil {
options.LeaderElectionResourceLock = *env.LeaderElectionResourceLock
}
if env.LeaderElectionNamespace != nil {
options.LeaderElectionNamespace = *env.LeaderElectionNamespace
}
if env.LeaderElectionID != nil {
options.LeaderElectionID = *env.LeaderElectionID
}
if env.LeaderElectionLeaseDuration != nil {
options.LeaseDuration = env.LeaderElectionLeaseDuration
}
if env.LeaderElectionRenewDeadline != nil {
options.RenewDeadline = env.LeaderElectionRenewDeadline
}
if env.LeaderElectionRetryPeriod != nil {
options.RetryPeriod = env.LeaderElectionRetryPeriod
}
if env.LeaderElectionReleaseOnCancel != nil {
options.LeaderElectionReleaseOnCancel = *env.LeaderElectionReleaseOnCancel
}

if env.MetricsSecureServing != nil {
options.Metrics.SecureServing = *env.MetricsSecureServing
}
if env.MetricsBindAddress != nil {
options.Metrics.BindAddress = *env.MetricsBindAddress
}
if env.MetricsCertDir != nil {
options.Metrics.CertDir = *env.MetricsCertDir
}

if env.HealthProbeBindAddress != nil {
options.HealthProbeBindAddress = *env.HealthProbeBindAddress
}
if env.PprofBindAddress != nil {
options.PprofBindAddress = *env.PprofBindAddress
}

webhookServerOptions := webhook.Options{}
if env.WebhookPort != nil {
webhookServerOptions.Port = *env.WebhookPort
}
if env.WebhookHost != nil {
webhookServerOptions.Host = *env.WebhookHost
}
if env.WebhookCertDir != nil {
webhookServerOptions.CertDir = *env.WebhookCertDir
}

if options.WebhookServer == nil {
options.WebhookServer = webhook.NewServer(webhookServerOptions)
} else if dh, ok := options.WebhookServer.(*webhook.DefaultServer); ok {
current := &dh.Options
if webhookServerOptions.Port != 0 {
current.Port = webhookServerOptions.Port
}
if webhookServerOptions.Host != "" {
current.Host = webhookServerOptions.Host
}
if webhookServerOptions.CertDir != "" {
current.CertDir = webhookServerOptions.CertDir
}
dh.Options = *current
}

return options
}

0 comments on commit 8ceb353

Please sign in to comment.