diff --git a/kubernetes/provider.go b/kubernetes/provider.go index a20c172cab..1d556be0ab 100644 --- a/kubernetes/provider.go +++ b/kubernetes/provider.go @@ -6,6 +6,8 @@ import ( "fmt" "log" "net/http" + "os" + "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -68,11 +70,11 @@ func Provider() *schema.Provider { DefaultFunc: schema.EnvDefaultFunc("KUBE_CLUSTER_CA_CERT_DATA", ""), Description: "PEM-encoded root certificates bundle for TLS authentication.", }, - "config_path": { - Type: schema.TypeString, + "config_paths": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, - DefaultFunc: schema.EnvDefaultFunc("KUBE_CONFIG_PATH", ""), - Description: "Path to the kube config file. Can be set with KUBE_CONFIG_PATH environment variable.", + Description: "A list of paths to kube config file. Can be set with KUBE_CONFIG_PATHS environment variable.", }, "config_context": { Type: schema.TypeString, @@ -260,13 +262,26 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error) overrides := &clientcmd.ConfigOverrides{} loader := &clientcmd.ClientConfigLoadingRules{} - if configPath, ok := d.GetOk("config_path"); ok && configPath.(string) != "" { - path, err := homedir.Expand(configPath.(string)) - if err != nil { - return nil, err + configPaths := []string{} + 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 + // does not yet allow you to set a default for a TypeList + configPaths = strings.Split(v, ":") + } + + if len(configPaths) > 0 { + expandedPaths := []string{} + for _, p := range configPaths { + path, err := homedir.Expand(p) + if err != nil { + return nil, err + } + log.Printf("[DEBUG] Using kubeconfig: %s", path) + expandedPaths = append(expandedPaths, path) } - log.Printf("[DEBUG] Configuration file is: %s", path) - loader.ExplicitPath = path + loader.Precedence = expandedPaths ctxSuffix := "; default context" diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index b437a63c6d..e9687eead0 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -15,7 +15,9 @@ Use the navigation to the left to read about the available resources. ```hcl provider "kubernetes" { - config_path = "~/.kube/config" + config_paths = [ + "~/.kube/config" + ] config_context = "my-context" } @@ -79,12 +81,6 @@ the provider will try to use the service account token from the `/var/run/secret Detection of in-cluster execution is based on the sole availability both of the `KUBERNETES_SERVICE_HOST` and `KUBERNETES_SERVICE_PORT` environment variables, with non empty values. -```hcl -provider "kubernetes" { - load_config_file = "false" -} -``` - If you have any other static configuration setting specified in a config file or static configuration, in-cluster service account token will not be tried. ### Statically defined credentials @@ -93,8 +89,6 @@ Another way is **statically** define TLS certificate credentials: ```hcl provider "kubernetes" { - load_config_file = "false" - host = "https://104.196.242.174" client_certificate = "${file("~/.kube/client-cert.pem")}" @@ -107,8 +101,6 @@ or username and password (HTTP Basic Authorization): ```hcl provider "kubernetes" { - load_config_file = "false" - host = "https://104.196.242.174" username = "username" @@ -132,7 +124,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) Path to the kube config file. Can be sourced from `KUBE_CONFIG`. +* `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`. * `config_context_cluster` - (Optional) Cluster context of the kube config (name of the kubeconfig cluster, `--cluster` flag in `kubectl`). Can be sourced from `KUBE_CTX_CLUSTER`.