Skip to content
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

Make build operator leader durations configurable #433

Merged
merged 4 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,19 @@ func main() {
}
defer r.Unset()

c := buildconfig.NewDefaultConfig()
if err := c.SetConfigFromEnv(); err != nil {
buildCfg := buildconfig.NewDefaultConfig()
if err := buildCfg.SetConfigFromEnv(); err != nil {
ctxlog.Error(ctx, err, "")
os.Exit(1)
}

mgr, err := controller.NewManager(ctx, c, cfg, manager.Options{
mgr, err := controller.NewManager(ctx, buildCfg, cfg, manager.Options{
LeaderElection: true,
LeaderElectionID: "build-operator-lock",
LeaderElectionNamespace: "default",
LeaderElectionNamespace: buildCfg.ManagerOptions.LeaderElectionNamespace,
LeaseDuration: buildCfg.ManagerOptions.LeaseDuration,
RenewDeadline: buildCfg.ManagerOptions.RenewDeadline,
RetryPeriod: buildCfg.ManagerOptions.RetryPeriod,
Namespace: "",
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
})
Expand All @@ -120,7 +123,7 @@ func main() {

// Add the Metrics Service
addMetrics(ctx, cfg, namespace)
buildMetrics.InitPrometheus(c)
buildMetrics.InitPrometheus(buildCfg)

ctxlog.Info(ctx, "Starting the Cmd.")

Expand Down
4 changes: 4 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: BUILD_OPERATOR_LEADER_ELECTION_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_NAME
valueFrom:
fieldRef:
Expand Down
20 changes: 20 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
Copyright The Shipwright Contributors

SPDX-License-Identifier: Apache-2.0
-->

# Configuration

The `build-operator` is installed into Kubernetes with reasonable defaults. However, there are some settings that can be overridden using environment variables in [`operator.yaml`](../deploy/operator.yaml).

The following environment variables are available:

| Environment Variable | Description |
| --- | --- |
| `CTX_TIMEOUT` | Override the default context timeout used for all Custom Resource Definition reconciliation operations. |
| `KANIKO_CONTAINER_IMAGE` | Specify the Kaniko container image to be used instead of the default, for example `gcr.io/kaniko-project/executor:v1.0.1`. |
| `BUILD_OPERATOR_LEADER_ELECTION_NAMESPACE` | Set the namespace to be used to store the `build-operator` lock, by default it is in the same namespace as the operator itself. |
| `BUILD_OPERATOR_LEASE_DURATION` | Override the `LeaseDuration`, which is the duration that non-leader candidates will wait to force acquire leadership. |
| `BUILD_OPERATOR_RENEW_DEADLINE` | Override the `RenewDeadline`, which is the duration that the acting master will retry refreshing leadership before giving up. |
| `BUILD_OPERATOR_RETRY_PERIOD` | Override the `RetryPeriod`, which is the duration the LeaderElector clients should wait between tries of actions. |
48 changes: 48 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ const (

// environment variable to enable histogram labels
prometheusHistogramEnabledLabelsEnvVar = "PROMETHEUS_HISTOGRAM_ENABLED_LABELS"

leaderElectionNamespaceDefault = "default"
leaderElectionNamespaceEnvVar = "BUILD_OPERATOR_LEADER_ELECTION_NAMESPACE"

leaseDurationEnvVar = "BUILD_OPERATOR_LEASE_DURATION"
renewDeadlineEnvVar = "BUILD_OPERATOR_RENEW_DEADLINE"
retryPeriodEnvVar = "BUILD_OPERATOR_RETRY_PERIOD"
)

var (
Expand All @@ -46,6 +53,7 @@ type Config struct {
CtxTimeOut time.Duration
KanikoContainerImage string
Prometheus PrometheusConfig
ManagerOptions ManagerOptions
}

// PrometheusConfig contains the specific configuration for the
Expand All @@ -56,6 +64,14 @@ type PrometheusConfig struct {
HistogramEnabledLabels []string
}

// ManagerOptions contains configurable options for the build operator manager
type ManagerOptions struct {
LeaderElectionNamespace string
LeaseDuration *time.Duration
RenewDeadline *time.Duration
RetryPeriod *time.Duration
}

// NewDefaultConfig returns a new Config, with context timeout and default Kaniko image.
func NewDefaultConfig() *Config {
return &Config{
Expand All @@ -66,6 +82,9 @@ func NewDefaultConfig() *Config {
BuildRunEstablishDurationBuckets: metricBuildRunEstablishDurationBuckets,
BuildRunRampUpDurationBuckets: metricBuildRunRampUpDurationBuckets,
},
ManagerOptions: ManagerOptions{
LeaderElectionNamespace: leaderElectionNamespaceDefault,
},
}
}

Expand Down Expand Up @@ -97,6 +116,22 @@ func (c *Config) SetConfigFromEnv() error {

c.Prometheus.HistogramEnabledLabels = strings.Split(os.Getenv(prometheusHistogramEnabledLabelsEnvVar), ",")

if leaderElectionNamespace := os.Getenv(leaderElectionNamespaceEnvVar); leaderElectionNamespace != "" {
c.ManagerOptions.LeaderElectionNamespace = leaderElectionNamespace
}

if err := updateBuildOperatorDurationOption(&c.ManagerOptions.LeaseDuration, leaseDurationEnvVar); err != nil {
return err
}

if err := updateBuildOperatorDurationOption(&c.ManagerOptions.RenewDeadline, renewDeadlineEnvVar); err != nil {
return err
}

if err := updateBuildOperatorDurationOption(&c.ManagerOptions.RetryPeriod, retryPeriodEnvVar); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -126,3 +161,16 @@ func updateBucketsConfig(buckets *[]float64, envVarName string) error {

return nil
}

func updateBuildOperatorDurationOption(d **time.Duration, envVarName string) error {
if value := os.Getenv(envVarName); value != "" {
valueDuration, err := time.ParseDuration(value)
if err != nil {
return err
}

*d = &valueDuration
}

return nil
}
21 changes: 21 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ var _ = Describe("Config", func() {
Expect(config.Prometheus.HistogramEnabledLabels).To(Equal([]string{"namespace", "strategy"}))
})
})

It("should allow for an override of the operator leader election namespace using an environment variable", func() {
var overrides = map[string]string{"BUILD_OPERATOR_LEADER_ELECTION_NAMESPACE": "build-operator"}
configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.ManagerOptions.LeaderElectionNamespace).To(Equal("build-operator"))
})
})

It("should allow for an override of the operator leader election times using environment variables", func() {
var overrides = map[string]string{
"BUILD_OPERATOR_LEASE_DURATION": "42s",
"BUILD_OPERATOR_RENEW_DEADLINE": "32s",
"BUILD_OPERATOR_RETRY_PERIOD": "10s",
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(*config.ManagerOptions.LeaseDuration).To(Equal(time.Duration(42 * time.Second)))
Expect(*config.ManagerOptions.RenewDeadline).To(Equal(time.Duration(32 * time.Second)))
Expect(*config.ManagerOptions.RetryPeriod).To(Equal(time.Duration(10 * time.Second)))
})
})
})
})

Expand Down