File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed
cluster-autoscaler/cloudprovider/rancher Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,12 @@ The `cluster-autoscaler` for Rancher needs a configuration file to work by
10
10
using ` --cloud-config ` parameter. An up-to-date example can be found in
11
11
[ examples/config.yaml] ( ./examples/config.yaml ) .
12
12
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
+
13
19
### Permissions
14
20
15
21
The Rancher server account provided in the ` cloud-config ` requires the
Original file line number Diff line number Diff line change @@ -23,6 +23,12 @@ import (
23
23
"gopkg.in/yaml.v2"
24
24
)
25
25
26
+ const (
27
+ envUrl = "RANCHER_URL"
28
+ envClusterName = "RANCHER_CLUSTER_NAME"
29
+ envClusterToken = "RANCHER_TOKEN"
30
+ )
31
+
26
32
type cloudConfig struct {
27
33
URL string `yaml:"url"`
28
34
Token string `yaml:"token"`
@@ -31,6 +37,22 @@ type cloudConfig struct {
31
37
ClusterAPIVersion string `yaml:"clusterAPIVersion"`
32
38
}
33
39
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
+
34
56
func newConfig (file string ) (* cloudConfig , error ) {
35
57
b , err := os .ReadFile (file )
36
58
if err != nil {
@@ -42,5 +64,7 @@ func newConfig(file string) (*cloudConfig, error) {
42
64
return nil , fmt .Errorf ("unable to unmarshal config file: %w" , err )
43
65
}
44
66
67
+ config = overrideFromEnv (config )
68
+
45
69
return config , nil
46
70
}
Original file line number Diff line number Diff line change @@ -16,7 +16,10 @@ limitations under the License.
16
16
17
17
package rancher
18
18
19
- import "testing"
19
+ import (
20
+ "os"
21
+ "testing"
22
+ )
20
23
21
24
func TestNewConfig (t * testing.T ) {
22
25
cfg , err := newConfig ("./examples/config.yaml" )
@@ -40,3 +43,25 @@ func TestNewConfig(t *testing.T) {
40
43
t .Fatal ("expected cluster namespace to be set" )
41
44
}
42
45
}
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
+ }
You can’t perform that action at this time.
0 commit comments