From 3be7d12a245238040d876ab564bdaea07e57477d Mon Sep 17 00:00:00 2001 From: John Houston Date: Mon, 2 Nov 2020 15:44:42 -0500 Subject: [PATCH] Remove load_config_file and support for KUBECONFIG environment variable --- kubernetes/provider.go | 83 ++++++++++++++------------------ website/docs/index.html.markdown | 4 +- 2 files changed, 37 insertions(+), 50 deletions(-) diff --git a/kubernetes/provider.go b/kubernetes/provider.go index 72fae0baf2..a20c172cab 100644 --- a/kubernetes/provider.go +++ b/kubernetes/provider.go @@ -4,10 +4,11 @@ import ( "bytes" "context" "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "log" "net/http" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/mitchellh/go-homedir" @@ -68,15 +69,10 @@ func Provider() *schema.Provider { Description: "PEM-encoded root certificates bundle for TLS authentication.", }, "config_path": { - Type: schema.TypeString, - Optional: true, - DefaultFunc: schema.MultiEnvDefaultFunc( - []string{ - "KUBE_CONFIG", - "KUBECONFIG", - }, - "~/.kube/config"), - Description: "Path to the kube config file, defaults to ~/.kube/config", + 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.", }, "config_context": { Type: schema.TypeString, @@ -101,12 +97,6 @@ func Provider() *schema.Provider { DefaultFunc: schema.EnvDefaultFunc("KUBE_TOKEN", ""), Description: "Token to authenticate an service account", }, - "load_config_file": { - Type: schema.TypeBool, - Optional: true, - DefaultFunc: schema.EnvDefaultFunc("KUBE_LOAD_CONFIG_FILE", true), - Description: "Load local kubeconfig.", - }, "exec": { Type: schema.TypeList, Optional: true, @@ -270,40 +260,37 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error) overrides := &clientcmd.ConfigOverrides{} loader := &clientcmd.ClientConfigLoadingRules{} - if d.Get("load_config_file").(bool) { - log.Printf("[DEBUG] Trying to load configuration from file") - if configPath, ok := d.GetOk("config_path"); ok && configPath.(string) != "" { - path, err := homedir.Expand(configPath.(string)) - if err != nil { - return nil, err + if configPath, ok := d.GetOk("config_path"); ok && configPath.(string) != "" { + path, err := homedir.Expand(configPath.(string)) + if err != nil { + return nil, err + } + log.Printf("[DEBUG] Configuration file is: %s", path) + loader.ExplicitPath = path + + ctxSuffix := "; default context" + + kubectx, ctxOk := d.GetOk("config_context") + authInfo, authInfoOk := d.GetOk("config_context_auth_info") + cluster, clusterOk := d.GetOk("config_context_cluster") + if ctxOk || authInfoOk || clusterOk { + ctxSuffix = "; overriden context" + if ctxOk { + overrides.CurrentContext = kubectx.(string) + ctxSuffix += fmt.Sprintf("; config ctx: %s", overrides.CurrentContext) + log.Printf("[DEBUG] Using custom current context: %q", overrides.CurrentContext) + } + + overrides.Context = clientcmdapi.Context{} + if authInfoOk { + overrides.Context.AuthInfo = authInfo.(string) + ctxSuffix += fmt.Sprintf("; auth_info: %s", overrides.Context.AuthInfo) } - log.Printf("[DEBUG] Configuration file is: %s", path) - loader.ExplicitPath = path - - ctxSuffix := "; default context" - - kubectx, ctxOk := d.GetOk("config_context") - authInfo, authInfoOk := d.GetOk("config_context_auth_info") - cluster, clusterOk := d.GetOk("config_context_cluster") - if ctxOk || authInfoOk || clusterOk { - ctxSuffix = "; overriden context" - if ctxOk { - overrides.CurrentContext = kubectx.(string) - ctxSuffix += fmt.Sprintf("; config ctx: %s", overrides.CurrentContext) - log.Printf("[DEBUG] Using custom current context: %q", overrides.CurrentContext) - } - - overrides.Context = clientcmdapi.Context{} - if authInfoOk { - overrides.Context.AuthInfo = authInfo.(string) - ctxSuffix += fmt.Sprintf("; auth_info: %s", overrides.Context.AuthInfo) - } - if clusterOk { - overrides.Context.Cluster = cluster.(string) - ctxSuffix += fmt.Sprintf("; cluster: %s", overrides.Context.Cluster) - } - log.Printf("[DEBUG] Using overidden context: %#v", overrides.Context) + if clusterOk { + overrides.Context.Cluster = cluster.(string) + ctxSuffix += fmt.Sprintf("; cluster: %s", overrides.Context.Cluster) } + log.Printf("[DEBUG] Using overidden context: %#v", overrides.Context) } } diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 6fa0490e1e..b437a63c6d 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -15,6 +15,7 @@ Use the navigation to the left to read about the available resources. ```hcl provider "kubernetes" { + config_path = "~/.kube/config" config_context = "my-context" } @@ -131,12 +132,11 @@ 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` or `KUBECONFIG`. Defaults to `~/.kube/config`. +* `config_path` - (Optional) Path to the kube config file. Can be sourced from `KUBE_CONFIG`. * `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`. * `token` - (Optional) Token of your service account. Can be sourced from `KUBE_TOKEN`. -* `load_config_file` - (Optional) By default the local config (~/.kube/config) is loaded when you use this provider. This option at false disables this behaviour which is desired when statically specifying the configuration or relying on in-cluster config. Can be sourced from `KUBE_LOAD_CONFIG_FILE`. * `exec` - (Optional) Configuration block to use an [exec-based credential plugin] (https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins), e.g. call an external command to receive user credentials. * `api_version` - (Required) API version to use when decoding the ExecCredentials resource, e.g. `client.authentication.k8s.io/v1beta1`. * `command` - (Required) Command to execute.