From 7cd46bb8b19e15971abd2202de3a3ec8ef584462 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Tue, 31 Dec 2019 10:03:53 -0800 Subject: [PATCH] Emit warnings about deprecated settings [#10] These aren't an error case--the plugin will work just fine--but users should be aware they (the settings) aren't being respected. --- README.md | 2 +- internal/helm/config.go | 19 ++++++++++++++++++- internal/helm/config_test.go | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 592baee..1ad4c9e 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ steps: drone-helm3 is largely backwards-compatible with drone-helm. There are some known differences: * `prefix` must be supplied via the `settings` block, not `environment`. -* Several settings no longer have any effect: +* Several settings no longer have any effect. The plugin will produce warnings if any of these are present: * `purge` -- this is the default behavior in Helm 3 * `recreate_pods` * `tiller_ns` diff --git a/internal/helm/config.go b/internal/helm/config.go index a4d1914..f2086d8 100644 --- a/internal/helm/config.go +++ b/internal/helm/config.go @@ -4,10 +4,15 @@ import ( "fmt" "github.com/kelseyhightower/envconfig" "io" + "os" "regexp" + "strings" ) -var justNumbers = regexp.MustCompile(`^\d+$`) +var ( + justNumbers = regexp.MustCompile(`^\d+$`) + deprecatedVars = []string{"PURGE", "RECREATE_PODS", "TILLER_NS", "UPGRADE", "CANARY_IMAGE", "CLIENT_ONLY", "STABLE_REPO_URL"} +) // The Config struct captures the `settings` and `environment` blocks in the application's drone // config. Configuration in drone's `settings` block arrives as uppercase env vars matching the @@ -73,6 +78,8 @@ func NewConfig(stdout, stderr io.Writer) (*Config, error) { cfg.logDebug() } + cfg.deprecationWarn() + return &cfg, nil } @@ -82,3 +89,13 @@ func (cfg Config) logDebug() { } fmt.Fprintf(cfg.Stderr, "Generated config: %+v\n", cfg) } + +func (cfg *Config) deprecationWarn() { + for _, varname := range deprecatedVars { + _, barePresent := os.LookupEnv(varname) + _, prefixedPresent := os.LookupEnv("PLUGIN_" + varname) + if barePresent || prefixedPresent { + fmt.Fprintf(cfg.Stderr, "Warning: ignoring deprecated '%s' setting\n", strings.ToLower(varname)) + } + } +} diff --git a/internal/helm/config_test.go b/internal/helm/config_test.go index f39dd0c..849fbd5 100644 --- a/internal/helm/config_test.go +++ b/internal/helm/config_test.go @@ -1,6 +1,7 @@ package helm import ( + "fmt" "github.com/stretchr/testify/suite" "os" "strings" @@ -123,6 +124,24 @@ func (suite *ConfigTestSuite) TestNewConfigSetsWriters() { suite.Equal(stderr, cfg.Stderr) } +func (suite *ConfigTestSuite) TestDeprecatedSettingWarnings() { + for _, varname := range deprecatedVars { + suite.setenv(varname, "deprecoat") // environment-block entries should cause warnings + } + + suite.unsetenv("PURGE") + suite.setenv("PLUGIN_PURGE", "true") // settings-block entries should cause warnings + suite.setenv("UPGRADE", "") // entries should cause warnings even when set to empty string + + stderr := &strings.Builder{} + _, err := NewConfig(&strings.Builder{}, stderr) + suite.NoError(err) + + for _, varname := range deprecatedVars { + suite.Contains(stderr.String(), fmt.Sprintf("Warning: ignoring deprecated '%s' setting\n", strings.ToLower(varname))) + } +} + func (suite *ConfigTestSuite) TestLogDebug() { suite.setenv("DEBUG", "true") suite.setenv("HELM_COMMAND", "upgrade")