diff --git a/pkg/config/config.go b/pkg/config/config.go index cfbf8b01a7..ce69a6002f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -71,8 +71,7 @@ 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 @@ -80,36 +79,20 @@ func (c *Config) SetConfigFromEnv() error { 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), ",") @@ -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 +} diff --git a/pkg/config/config_suite_test.go b/pkg/config/config_suite_test.go new file mode 100644 index 0000000000..ec09a31a5a --- /dev/null +++ b/pkg/config/config_suite_test.go @@ -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") +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 0000000000..db189af2f0 --- /dev/null +++ b/pkg/config/config_test.go @@ -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) + } + } +}