Skip to content

Commit

Permalink
Allow to override rancher provider settings
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
k0da committed Mar 10, 2024
1 parent 90e2891 commit 875baca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cluster-autoscaler/cloudprovider/rancher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions cluster-autoscaler/cloudprovider/rancher/rancher_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand All @@ -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
}
27 changes: 26 additions & 1 deletion cluster-autoscaler/cloudprovider/rancher/rancher_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
}

0 comments on commit 875baca

Please sign in to comment.