Skip to content

Commit

Permalink
Support config_path and config_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhouston committed Nov 20, 2020
1 parent 5a16a65 commit 7cc1c12
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
18 changes: 16 additions & 2 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func Provider() *schema.Provider {
Optional: true,
Description: "A list of paths to kube config files. Can be set with KUBE_CONFIG_PATHS environment variable.",
},
"config_path": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CONFIG_PATH", ""),
Description: "Path to the kube config file, defaults to ~/.kube/config",
},
"config_context": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -263,7 +269,10 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error)
loader := &clientcmd.ClientConfigLoadingRules{}

configPaths := []string{}
if v, ok := d.Get("config_paths").([]string); ok && len(v) > 0 {

if v, ok := d.Get("config_path").(string); ok && v != "" {
configPaths = []string{v}
} else if v, ok := d.Get("config_paths").([]string); ok && len(v) > 0 {
configPaths = v
} else if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" {
// NOTE we have to do this here because the schema
Expand All @@ -281,7 +290,12 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error)
log.Printf("[DEBUG] Using kubeconfig: %s", path)
expandedPaths = append(expandedPaths, path)
}
loader.Precedence = expandedPaths

if len(expandedPaths) == 1 {
loader.ExplicitPath = expandedPaths[0]
} else {
loader.Precedence = expandedPaths
}

ctxSuffix := "; default context"

Expand Down
2 changes: 1 addition & 1 deletion kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestProvider_configure(t *testing.T) {
resetEnv := unsetEnv(t)
defer resetEnv()

os.Setenv("KUBE_CONFIG_PATHS", "test-fixtures/kube-config.yaml")
os.Setenv("KUBE_CONFIG_PATH", "test-fixtures/kube-config.yaml")
os.Setenv("KUBE_CTX", "gcp")

rc := terraform.NewResourceConfigRaw(map[string]interface{}{})
Expand Down
5 changes: 2 additions & 3 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ Use the navigation to the left to read about the available resources.

```hcl
provider "kubernetes" {
config_paths = [
"~/.kube/config"
]
config_path = "~/.kube/config"
config_context = "my-context"
}
Expand Down Expand Up @@ -124,6 +122,7 @@ The following arguments are supported:
* `client_certificate` - (Optional) PEM-encoded client certificate for TLS authentication. Can be sourced from `KUBE_CLIENT_CERT_DATA`.
* `client_key` - (Optional) PEM-encoded client certificate key for TLS authentication. Can be sourced from `KUBE_CLIENT_KEY_DATA`.
* `cluster_ca_certificate` - (Optional) PEM-encoded root certificates bundle for TLS authentication. Can be sourced from `KUBE_CLUSTER_CA_CERT_DATA`.
* `config_path` - (Optional) A path to a kube config files. Can be sourced from `KUBE_CONFIG_PATH`.
* `config_paths` - (Optional) A list of paths to the kube config files. Can be sourced from `KUBE_CONFIG_PATHS`, which allows `:` to be used to delimit multiple paths.
* `config_context` - (Optional) Context to choose from the config file. Can be sourced from `KUBE_CTX`.
* `config_context_auth_info` - (Optional) Authentication info context of the kube config (name of the kubeconfig user, `--user` flag in `kubectl`). Can be sourced from `KUBE_CTX_AUTH_INFO`.
Expand Down

0 comments on commit 7cc1c12

Please sign in to comment.