From 2d88008018ab384cca12707ea576ec40cbbbadd9 Mon Sep 17 00:00:00 2001 From: Dinar Valeev Date: Sun, 10 Mar 2024 16:25:20 +0100 Subject: [PATCH] Allow to override rancher provider settings Currently it is only possible to set provider settings over yaml file. This commit introduces env variables to override URL, token and cluster name. If particular environment variable is set it overrides value supplied in yaml file. Signed-off-by: Dinar Valeev Co-authored-by: Donovan Muller --- .../cloudprovider/rancher/README.md | 6 +++++ .../cloudprovider/rancher/rancher_config.go | 24 +++++++++++++++++ .../rancher/rancher_config_test.go | 27 ++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/cluster-autoscaler/cloudprovider/rancher/README.md b/cluster-autoscaler/cloudprovider/rancher/README.md index 88f384b0b7a0..15114c74461f 100644 --- a/cluster-autoscaler/cloudprovider/rancher/README.md +++ b/cluster-autoscaler/cloudprovider/rancher/README.md @@ -10,6 +10,12 @@ The `cluster-autoscaler` for Rancher needs a configuration file to work by using `--cloud-config` parameter. An up-to-date example can be found in [examples/config.yaml](./examples/config.yaml). +### Configuration via environment variables +In order to override URL, token or clustername use following environment variables: + - RANCHER_URL + - RANCHER_TOKEN + - RANCHER_CLUSTER_NAME + ### Permissions The Rancher server account provided in the `cloud-config` requires the diff --git a/cluster-autoscaler/cloudprovider/rancher/rancher_config.go b/cluster-autoscaler/cloudprovider/rancher/rancher_config.go index 8ac79db5227e..b17f5fdcb6ae 100644 --- a/cluster-autoscaler/cloudprovider/rancher/rancher_config.go +++ b/cluster-autoscaler/cloudprovider/rancher/rancher_config.go @@ -23,6 +23,12 @@ import ( "gopkg.in/yaml.v2" ) +const ( + envUrl = "RANCHER_URL" + envClusterName = "RANCHER_CLUSTER_NAME" + envClusterToken = "RANCHER_TOKEN" +) + type cloudConfig struct { URL string `yaml:"url"` Token string `yaml:"token"` @@ -31,6 +37,22 @@ type cloudConfig struct { ClusterAPIVersion string `yaml:"clusterAPIVersion"` } +func overrideFromEnv(c *cloudConfig) *cloudConfig { + url := os.Getenv(envUrl) + cName := os.Getenv(envClusterName) + token := os.Getenv(envClusterToken) + if url != "" { + c.URL = url + } + if cName != "" { + c.ClusterName = cName + } + if token != "" { + c.Token = token + } + return c +} + func newConfig(file string) (*cloudConfig, error) { b, err := os.ReadFile(file) if err != nil { @@ -42,5 +64,7 @@ func newConfig(file string) (*cloudConfig, error) { return nil, fmt.Errorf("unable to unmarshal config file: %w", err) } + config = overrideFromEnv(config) + return config, nil } diff --git a/cluster-autoscaler/cloudprovider/rancher/rancher_config_test.go b/cluster-autoscaler/cloudprovider/rancher/rancher_config_test.go index 05e9f60882f3..bb5ea9d1bdce 100644 --- a/cluster-autoscaler/cloudprovider/rancher/rancher_config_test.go +++ b/cluster-autoscaler/cloudprovider/rancher/rancher_config_test.go @@ -16,7 +16,10 @@ limitations under the License. package rancher -import "testing" +import ( + "os" + "testing" +) func TestNewConfig(t *testing.T) { cfg, err := newConfig("./examples/config.yaml") @@ -40,3 +43,25 @@ func TestNewConfig(t *testing.T) { t.Fatal("expected cluster namespace to be set") } } + +func TestEnvOverride(t *testing.T) { + expectedUrl := "http://rancher-site.com" + overrideToken := "token:changed" + overrideClusterName := "cluster-changed" + os.Setenv(envUrl, expectedUrl) + os.Setenv(envClusterToken, overrideToken) + os.Setenv(envClusterName, overrideClusterName) + cfg, err := newConfig("./examples/config.yaml") + if err != nil { + t.Fatal(err) + } + if cfg.URL != expectedUrl { + t.Fatal("expected url to be set") + } + if cfg.Token != overrideToken { + t.Fatal("expected token to be set") + } + if cfg.ClusterName != overrideClusterName { + t.Fatal("expected cluster name to be set") + } +}