diff --git a/cluster-autoscaler/cloudprovider/rancher/README.md b/cluster-autoscaler/cloudprovider/rancher/README.md index 88f384b0b7a0..0c5a62b3d496 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). +### config override +In order to override URL, token or clustername use following environemnt variables: + - RANCHER_URL + - RANCHER_TOKEN + - RANCHER_CLUSER_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") + } +}