Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to override rancher provider settings #6606

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
Loading