Skip to content

Commit 96b6022

Browse files
authored
Merge pull request #6606 from k0da/rancher_env
Allow to override rancher provider settings
2 parents ffe9680 + 2d88008 commit 96b6022

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

cluster-autoscaler/cloudprovider/rancher/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ The `cluster-autoscaler` for Rancher needs a configuration file to work by
1010
using `--cloud-config` parameter. An up-to-date example can be found in
1111
[examples/config.yaml](./examples/config.yaml).
1212

13+
### Configuration via environment variables
14+
In order to override URL, token or clustername use following environment variables:
15+
- RANCHER_URL
16+
- RANCHER_TOKEN
17+
- RANCHER_CLUSTER_NAME
18+
1319
### Permissions
1420

1521
The Rancher server account provided in the `cloud-config` requires the

cluster-autoscaler/cloudprovider/rancher/rancher_config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import (
2323
"gopkg.in/yaml.v2"
2424
)
2525

26+
const (
27+
envUrl = "RANCHER_URL"
28+
envClusterName = "RANCHER_CLUSTER_NAME"
29+
envClusterToken = "RANCHER_TOKEN"
30+
)
31+
2632
type cloudConfig struct {
2733
URL string `yaml:"url"`
2834
Token string `yaml:"token"`
@@ -31,6 +37,22 @@ type cloudConfig struct {
3137
ClusterAPIVersion string `yaml:"clusterAPIVersion"`
3238
}
3339

40+
func overrideFromEnv(c *cloudConfig) *cloudConfig {
41+
url := os.Getenv(envUrl)
42+
cName := os.Getenv(envClusterName)
43+
token := os.Getenv(envClusterToken)
44+
if url != "" {
45+
c.URL = url
46+
}
47+
if cName != "" {
48+
c.ClusterName = cName
49+
}
50+
if token != "" {
51+
c.Token = token
52+
}
53+
return c
54+
}
55+
3456
func newConfig(file string) (*cloudConfig, error) {
3557
b, err := os.ReadFile(file)
3658
if err != nil {
@@ -42,5 +64,7 @@ func newConfig(file string) (*cloudConfig, error) {
4264
return nil, fmt.Errorf("unable to unmarshal config file: %w", err)
4365
}
4466

67+
config = overrideFromEnv(config)
68+
4569
return config, nil
4670
}

cluster-autoscaler/cloudprovider/rancher/rancher_config_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ limitations under the License.
1616

1717
package rancher
1818

19-
import "testing"
19+
import (
20+
"os"
21+
"testing"
22+
)
2023

2124
func TestNewConfig(t *testing.T) {
2225
cfg, err := newConfig("./examples/config.yaml")
@@ -40,3 +43,25 @@ func TestNewConfig(t *testing.T) {
4043
t.Fatal("expected cluster namespace to be set")
4144
}
4245
}
46+
47+
func TestEnvOverride(t *testing.T) {
48+
expectedUrl := "http://rancher-site.com"
49+
overrideToken := "token:changed"
50+
overrideClusterName := "cluster-changed"
51+
os.Setenv(envUrl, expectedUrl)
52+
os.Setenv(envClusterToken, overrideToken)
53+
os.Setenv(envClusterName, overrideClusterName)
54+
cfg, err := newConfig("./examples/config.yaml")
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
if cfg.URL != expectedUrl {
59+
t.Fatal("expected url to be set")
60+
}
61+
if cfg.Token != overrideToken {
62+
t.Fatal("expected token to be set")
63+
}
64+
if cfg.ClusterName != overrideClusterName {
65+
t.Fatal("expected cluster name to be set")
66+
}
67+
}

0 commit comments

Comments
 (0)