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

Refactor config environment variable override code #427

Merged
merged 2 commits into from
Oct 6, 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
46 changes: 21 additions & 25 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,28 @@ func NewDefaultConfig() *Config {

// SetConfigFromEnv updates the configuration managed by environment variables.
func (c *Config) SetConfigFromEnv() error {
timeout := os.Getenv(contextTimeoutEnvVar)
if timeout != "" {
if timeout := os.Getenv(contextTimeoutEnvVar); timeout != "" {
i, err := strconv.Atoi(timeout)
if err != nil {
return err
}
c.CtxTimeOut = time.Duration(i) * time.Second
}

kanikoImage := os.Getenv(kanikoImageEnvVar)
if kanikoImage != "" {
if kanikoImage := os.Getenv(kanikoImageEnvVar); kanikoImage != "" {
c.KanikoContainerImage = kanikoImage
}

buildRunCompletionDurationBucketsEnvVarValue := os.Getenv(metricBuildRunCompletionDurationBucketsEnvVar)
if buildRunCompletionDurationBucketsEnvVarValue != "" {
buildRunCompletionDurationBuckets, err := stringToFloat64Array(strings.Split(buildRunCompletionDurationBucketsEnvVarValue, ","))
if err != nil {
return err
}
c.Prometheus.BuildRunCompletionDurationBuckets = buildRunCompletionDurationBuckets
if err := updateBucketsConfig(&c.Prometheus.BuildRunCompletionDurationBuckets, metricBuildRunCompletionDurationBucketsEnvVar); err != nil {
return err
}

buildRunEstablishDurationBucketsEnvVarValue := os.Getenv(metricBuildRunEstablishDurationBucketsEnvVar)
if buildRunEstablishDurationBucketsEnvVarValue != "" {
buildRunEstablishDurationBuckets, err := stringToFloat64Array(strings.Split(buildRunEstablishDurationBucketsEnvVarValue, ","))
if err != nil {
return err
}
c.Prometheus.BuildRunEstablishDurationBuckets = buildRunEstablishDurationBuckets
if err := updateBucketsConfig(&c.Prometheus.BuildRunEstablishDurationBuckets, metricBuildRunEstablishDurationBucketsEnvVar); err != nil {
return err
}

buildRunRampUpDurationBucketsEnvVarValue := os.Getenv(metricBuildRunRampUpDurationBucketsEnvVar)
if buildRunRampUpDurationBucketsEnvVarValue != "" {
buildRunRampUpDurationBuckets, err := stringToFloat64Array(strings.Split(buildRunRampUpDurationBucketsEnvVarValue, ","))
if err != nil {
return err
}
c.Prometheus.BuildRunRampUpDurationBuckets = buildRunRampUpDurationBuckets
if err := updateBucketsConfig(&c.Prometheus.BuildRunRampUpDurationBuckets, metricBuildRunRampUpDurationBucketsEnvVar); err != nil {
return err
}

c.Prometheus.HistogramEnabledLabels = strings.Split(os.Getenv(prometheusHistogramEnabledLabelsEnvVar), ",")
Expand All @@ -130,3 +113,16 @@ func stringToFloat64Array(strings []string) ([]float64, error) {

return floats, nil
}

func updateBucketsConfig(buckets *[]float64, envVarName string) error {
if values, found := os.LookupEnv(envVarName); found {
floats, err := stringToFloat64Array(strings.Split(values, ","))
if err != nil {
return err
}

*buckets = floats
}

return nil
}
17 changes: 17 additions & 0 deletions pkg/config/config_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package config_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
91 changes: 91 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package config_test

import (
"os"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "github.com/shipwright-io/build/pkg/config"
)

var _ = Describe("Config", func() {
Context("obtaining the configuration for build", func() {
It("should create a default configuration with reasonable values", func() {
config := NewDefaultConfig()
Expect(config).ToNot(BeNil())
})

It("should allow for an override of the context timeout using an environment variable", func() {
var overrides = map[string]string{"CTX_TIMEOUT": "600"}
configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.CtxTimeOut).To(Equal(600 * time.Second))
})
})

It("should allow for an override of the default Kaniko project image using an environment variable", func() {
var overrides = map[string]string{"KANIKO_CONTAINER_IMAGE": "gcr.io/kaniko-project/executor:v1.0.1"}
configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.KanikoContainerImage).To(Equal("gcr.io/kaniko-project/executor:v1.0.1"))
})
})

It("should allow for an override of the Prometheus buckets settings using an environment variable", func() {
var overrides = map[string]string{
"PROMETHEUS_BR_COMP_DUR_BUCKETS": "1,2,3,4",
"PROMETHEUS_BR_EST_DUR_BUCKETS": "10,20,30,40",
"PROMETHEUS_BR_RAMPUP_DUR_BUCKETS": "1,2,3,5,8,12,20",
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.Prometheus.BuildRunCompletionDurationBuckets).To(Equal([]float64{1, 2, 3, 4}))
Expect(config.Prometheus.BuildRunEstablishDurationBuckets).To(Equal([]float64{10, 20, 30, 40}))
Expect(config.Prometheus.BuildRunRampUpDurationBuckets).To(Equal([]float64{1, 2, 3, 5, 8, 12, 20}))
})
})

It("should allow for an override of the Prometheus enabled labels using an environment variable", func() {
var overrides = map[string]string{"PROMETHEUS_HISTOGRAM_ENABLED_LABELS": "namespace,strategy"}
configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.Prometheus.HistogramEnabledLabels).To(Equal([]string{"namespace", "strategy"}))
})
})
})
})

func configWithEnvVariableOverrides(settings map[string]string, f func(config *Config)) {
var backup = make(map[string]*string, len(settings))
for k, v := range settings {
value, ok := os.LookupEnv(k)
switch ok {
case true:
backup[k] = &value

case false:
backup[k] = nil
}

os.Setenv(k, v)
}

config := NewDefaultConfig()
Expect(config).ToNot(BeNil())

err := config.SetConfigFromEnv()
Expect(err).ToNot(HaveOccurred())

f(config)

for k, v := range backup {
if v != nil {
os.Setenv(k, *v)
} else {
os.Unsetenv(k)
}
}
}