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 af0af77
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 77 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
102 changes: 30 additions & 72 deletions 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 All @@ -87,79 +87,33 @@ func TestProvider_configure(t *testing.T) {
func unsetEnv(t *testing.T) func() {
e := getEnv()

if err := os.Unsetenv("KUBE_CONFIG_PATHS"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CONFIG_PATHS: %s", err)
envVars := map[string]string{
"KUBE_CONFIG_PATH": e.ConfigPath,
"KUBE_CONFIG_PATHS": strings.Join(e.ConfigPaths, ":"),
"KUBE_CTX": e.Ctx,
"KUBE_CTX_AUTH_INFO": e.CtxAuthInfo,
"KUBE_CTX_CLUSTER": e.CtxCluster,
"KUBE_HOST": e.Host,
"KUBE_USER": e.User,
"KUBE_PASSWORD": e.Password,
"KUBE_CLIENT_CERT_DATA": e.ClientCertData,
"KUBE_CLIENT_KEY_DATA": e.ClientKeyData,
"KUBE_CLUSTER_CA_CERT_DATA": e.ClusterCACertData,
"KUBE_INSECURE": e.Insecure,
"KUBE_TOKEN": e.Token,
}
if err := os.Unsetenv("KUBE_CTX"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CTX: %s", err)
}
if err := os.Unsetenv("KUBE_CTX_AUTH_INFO"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CTX_AUTH_INFO: %s", err)
}
if err := os.Unsetenv("KUBE_CTX_CLUSTER"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CTX_CLUSTER: %s", err)
}
if err := os.Unsetenv("KUBE_HOST"); err != nil {
t.Fatalf("Error unsetting env var KUBE_HOST: %s", err)
}
if err := os.Unsetenv("KUBE_USER"); err != nil {
t.Fatalf("Error unsetting env var KUBE_USER: %s", err)
}
if err := os.Unsetenv("KUBE_PASSWORD"); err != nil {
t.Fatalf("Error unsetting env var KUBE_PASSWORD: %s", err)
}
if err := os.Unsetenv("KUBE_CLIENT_CERT_DATA"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CLIENT_CERT_DATA: %s", err)
}
if err := os.Unsetenv("KUBE_CLIENT_KEY_DATA"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CLIENT_KEY_DATA: %s", err)
}
if err := os.Unsetenv("KUBE_CLUSTER_CA_CERT_DATA"); err != nil {
t.Fatalf("Error unsetting env var KUBE_CLUSTER_CA_CERT_DATA: %s", err)
}
if err := os.Unsetenv("KUBE_INSECURE"); err != nil {
t.Fatalf("Error unsetting env var KUBE_INSECURE: %s", err)
}
if err := os.Unsetenv("KUBE_TOKEN"); err != nil {
t.Fatalf("Error unsetting env var KUBE_TOKEN: %s", err)

for k, _ := range envVars {
if err := os.Unsetenv(k); err != nil {
t.Fatalf("Error unsetting env var %s: %s", k, err)
}
}

return func() {
if err := os.Setenv("KUBE_CONFIG_PATHS", strings.Join(e.ConfigPaths, ":")); err != nil {
t.Fatalf("Error resetting env var KUBE_CONFIG_PATHS: %s", err)
}
if err := os.Setenv("KUBE_CTX", e.Ctx); err != nil {
t.Fatalf("Error resetting env var KUBE_CTX: %s", err)
}
if err := os.Setenv("KUBE_CTX_AUTH_INFO", e.CtxAuthInfo); err != nil {
t.Fatalf("Error resetting env var KUBE_CTX_AUTH_INFO: %s", err)
}
if err := os.Setenv("KUBE_CTX_CLUSTER", e.CtxCluster); err != nil {
t.Fatalf("Error resetting env var KUBE_CTX_CLUSTER: %s", err)
}
if err := os.Setenv("KUBE_HOST", e.Host); err != nil {
t.Fatalf("Error resetting env var KUBE_HOST: %s", err)
}
if err := os.Setenv("KUBE_USER", e.User); err != nil {
t.Fatalf("Error resetting env var KUBE_USER: %s", err)
}
if err := os.Setenv("KUBE_PASSWORD", e.Password); err != nil {
t.Fatalf("Error resetting env var KUBE_PASSWORD: %s", err)
}
if err := os.Setenv("KUBE_CLIENT_CERT_DATA", e.ClientCertData); err != nil {
t.Fatalf("Error resetting env var KUBE_CLIENT_CERT_DATA: %s", err)
}
if err := os.Setenv("KUBE_CLIENT_KEY_DATA", e.ClientKeyData); err != nil {
t.Fatalf("Error resetting env var KUBE_CLIENT_KEY_DATA: %s", err)
}
if err := os.Setenv("KUBE_CLUSTER_CA_CERT_DATA", e.ClusterCACertData); err != nil {
t.Fatalf("Error resetting env var KUBE_CLUSTER_CA_CERT_DATA: %s", err)
}
if err := os.Setenv("KUBE_INSECURE", e.Insecure); err != nil {
t.Fatalf("Error resetting env var KUBE_INSECURE: %s", err)
}
if err := os.Setenv("KUBE_TOKEN", e.Token); err != nil {
t.Fatalf("Error resetting env var KUBE_TOKEN: %s", err)
for k, v := range envVars {
if err := os.Setenv(k, v); err != nil {
t.Fatalf("Error resetting env var %s: %s", k, err)
}
}
}
}
Expand All @@ -178,7 +132,10 @@ func getEnv() *currentEnv {
Insecure: os.Getenv("KUBE_INSECURE"),
Token: os.Getenv("KUBE_TOKEN"),
}
if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" {
if v := os.Getenv("KUBE_CONFIG_PATH"); v != "" {
e.ConfigPath = v
}
if v := os.Getenv("KUBE_CONFIG_PATH"); v != "" {
e.ConfigPaths = strings.Split(v, ":")
}
return e
Expand All @@ -188,7 +145,7 @@ func testAccPreCheck(t *testing.T) {
ctx := context.TODO()
hasFileCfg := (os.Getenv("KUBE_CTX_AUTH_INFO") != "" && os.Getenv("KUBE_CTX_CLUSTER") != "") ||
os.Getenv("KUBE_CTX") != "" ||
os.Getenv("KUBE_CONFIG_PATHS") != ""
os.Getenv("KUBE_CONFIG_PATH") != ""
hasUserCredentials := os.Getenv("KUBE_USER") != "" && os.Getenv("KUBE_PASSWORD") != ""
hasClientCert := os.Getenv("KUBE_CLIENT_CERT_DATA") != "" && os.Getenv("KUBE_CLIENT_KEY_DATA") != ""
hasStaticCfg := (os.Getenv("KUBE_HOST") != "" &&
Expand Down Expand Up @@ -424,6 +381,7 @@ func clusterVersionLessThan(vs string) bool {
}

type currentEnv struct {
ConfigPath string
ConfigPaths []string
Ctx string
CtxAuthInfo string
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 af0af77

Please sign in to comment.