Skip to content

Commit

Permalink
Merge pull request #6606 from k0da/rancher_env
Browse files Browse the repository at this point in the history
Allow to override rancher provider settings
  • Loading branch information
k8s-ci-robot authored Mar 15, 2024
2 parents ffe9680 + 2d88008 commit 96b6022
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).

### 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
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 96b6022

Please sign in to comment.