Skip to content

Commit 9602d64

Browse files
committed
Change config_path -> config_paths
1 parent 3be7d12 commit 9602d64

File tree

3 files changed

+40
-50
lines changed

3 files changed

+40
-50
lines changed

kubernetes/provider.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"log"
88
"net/http"
9+
"os"
10+
"strings"
911

1012
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1113

@@ -68,11 +70,11 @@ func Provider() *schema.Provider {
6870
DefaultFunc: schema.EnvDefaultFunc("KUBE_CLUSTER_CA_CERT_DATA", ""),
6971
Description: "PEM-encoded root certificates bundle for TLS authentication.",
7072
},
71-
"config_path": {
72-
Type: schema.TypeString,
73+
"config_paths": {
74+
Type: schema.TypeList,
75+
Elem: &schema.Schema{Type: schema.TypeString},
7376
Optional: true,
74-
DefaultFunc: schema.EnvDefaultFunc("KUBE_CONFIG_PATH", ""),
75-
Description: "Path to the kube config file. Can be set with KUBE_CONFIG_PATH environment variable.",
77+
Description: "A list of paths to kube config file. Can be set with KUBE_CONFIG_PATHS environment variable.",
7678
},
7779
"config_context": {
7880
Type: schema.TypeString,
@@ -260,13 +262,26 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error)
260262
overrides := &clientcmd.ConfigOverrides{}
261263
loader := &clientcmd.ClientConfigLoadingRules{}
262264

263-
if configPath, ok := d.GetOk("config_path"); ok && configPath.(string) != "" {
264-
path, err := homedir.Expand(configPath.(string))
265-
if err != nil {
266-
return nil, err
265+
configPaths := []string{}
266+
if v, ok := d.Get("config_paths").([]string); ok && len(v) > 0 {
267+
configPaths = v
268+
} else if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" {
269+
// NOTE we have to do this here because the schema
270+
// does not yet allow you to set a default for a TypeList
271+
configPaths = strings.Split(v, ":")
272+
}
273+
274+
if len(configPaths) > 0 {
275+
expandedPaths := []string{}
276+
for _, p := range configPaths {
277+
path, err := homedir.Expand(p)
278+
if err != nil {
279+
return nil, err
280+
}
281+
log.Printf("[DEBUG] Using kubeconfig: %s", path)
282+
expandedPaths = append(expandedPaths, path)
267283
}
268-
log.Printf("[DEBUG] Configuration file is: %s", path)
269-
loader.ExplicitPath = path
284+
loader.Precedence = expandedPaths
270285

271286
ctxSuffix := "; default context"
272287

kubernetes/provider_test.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
87
"os"
98
"strings"
109
"testing"
1110

11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
1213
gversion "github.com/hashicorp/go-version"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -72,7 +73,7 @@ func TestProvider_configure(t *testing.T) {
7273
resetEnv := unsetEnv(t)
7374
defer resetEnv()
7475

75-
os.Setenv("KUBECONFIG", "test-fixtures/kube-config.yaml")
76+
os.Setenv("KUBE_CONFIG_PATHS", "test-fixtures/kube-config.yaml")
7677
os.Setenv("KUBE_CTX", "gcp")
7778

7879
rc := terraform.NewResourceConfigRaw(map[string]interface{}{})
@@ -86,11 +87,8 @@ func TestProvider_configure(t *testing.T) {
8687
func unsetEnv(t *testing.T) func() {
8788
e := getEnv()
8889

89-
if err := os.Unsetenv("KUBECONFIG"); err != nil {
90-
t.Fatalf("Error unsetting env var KUBECONFIG: %s", err)
91-
}
92-
if err := os.Unsetenv("KUBE_CONFIG"); err != nil {
93-
t.Fatalf("Error unsetting env var KUBE_CONFIG: %s", err)
90+
if err := os.Unsetenv("KUBE_CONFIG_PATHS"); err != nil {
91+
t.Fatalf("Error unsetting env var KUBE_CONFIG_PATHS: %s", err)
9492
}
9593
if err := os.Unsetenv("KUBE_CTX"); err != nil {
9694
t.Fatalf("Error unsetting env var KUBE_CTX: %s", err)
@@ -122,19 +120,13 @@ func unsetEnv(t *testing.T) func() {
122120
if err := os.Unsetenv("KUBE_INSECURE"); err != nil {
123121
t.Fatalf("Error unsetting env var KUBE_INSECURE: %s", err)
124122
}
125-
if err := os.Unsetenv("KUBE_LOAD_CONFIG_FILE"); err != nil {
126-
t.Fatalf("Error unsetting env var KUBE_LOAD_CONFIG_FILE: %s", err)
127-
}
128123
if err := os.Unsetenv("KUBE_TOKEN"); err != nil {
129124
t.Fatalf("Error unsetting env var KUBE_TOKEN: %s", err)
130125
}
131126

132127
return func() {
133-
if err := os.Setenv("KUBE_CONFIG", e.Config); err != nil {
134-
t.Fatalf("Error resetting env var KUBE_CONFIG: %s", err)
135-
}
136-
if err := os.Setenv("KUBECONFIG", e.Config); err != nil {
137-
t.Fatalf("Error resetting env var KUBECONFIG: %s", err)
128+
if err := os.Setenv("KUBE_CONFIG_PATHS", strings.Join(e.ConfigPaths, ":")); err != nil {
129+
t.Fatalf("Error resetting env var KUBE_CONFIG_PATHS: %s", err)
138130
}
139131
if err := os.Setenv("KUBE_CTX", e.Ctx); err != nil {
140132
t.Fatalf("Error resetting env var KUBE_CTX: %s", err)
@@ -166,9 +158,6 @@ func unsetEnv(t *testing.T) func() {
166158
if err := os.Setenv("KUBE_INSECURE", e.Insecure); err != nil {
167159
t.Fatalf("Error resetting env var KUBE_INSECURE: %s", err)
168160
}
169-
if err := os.Setenv("KUBE_LOAD_CONFIG_FILE", e.LoadConfigFile); err != nil {
170-
t.Fatalf("Error resetting env var KUBE_LOAD_CONFIG_FILE: %s", err)
171-
}
172161
if err := os.Setenv("KUBE_TOKEN", e.Token); err != nil {
173162
t.Fatalf("Error resetting env var KUBE_TOKEN: %s", err)
174163
}
@@ -187,14 +176,10 @@ func getEnv() *currentEnv {
187176
ClientKeyData: os.Getenv("KUBE_CLIENT_KEY_DATA"),
188177
ClusterCACertData: os.Getenv("KUBE_CLUSTER_CA_CERT_DATA"),
189178
Insecure: os.Getenv("KUBE_INSECURE"),
190-
LoadConfigFile: os.Getenv("KUBE_LOAD_CONFIG_FILE"),
191179
Token: os.Getenv("KUBE_TOKEN"),
192180
}
193-
if cfg := os.Getenv("KUBE_CONFIG"); cfg != "" {
194-
e.Config = cfg
195-
}
196-
if cfg := os.Getenv("KUBECONFIG"); cfg != "" {
197-
e.Config = cfg
181+
if v := os.Getenv("KUBE_CONFIG_PATHS"); v != "" {
182+
e.ConfigPaths = strings.Split(v, ":")
198183
}
199184
return e
200185
}
@@ -203,8 +188,7 @@ func testAccPreCheck(t *testing.T) {
203188
ctx := context.TODO()
204189
hasFileCfg := (os.Getenv("KUBE_CTX_AUTH_INFO") != "" && os.Getenv("KUBE_CTX_CLUSTER") != "") ||
205190
os.Getenv("KUBE_CTX") != "" ||
206-
os.Getenv("KUBECONFIG") != "" ||
207-
os.Getenv("KUBE_CONFIG") != ""
191+
os.Getenv("KUBE_CONFIG_PATHS") != ""
208192
hasUserCredentials := os.Getenv("KUBE_USER") != "" && os.Getenv("KUBE_PASSWORD") != ""
209193
hasClientCert := os.Getenv("KUBE_CLIENT_CERT_DATA") != "" && os.Getenv("KUBE_CLIENT_KEY_DATA") != ""
210194
hasStaticCfg := (os.Getenv("KUBE_HOST") != "" &&
@@ -440,7 +424,7 @@ func clusterVersionLessThan(vs string) bool {
440424
}
441425

442426
type currentEnv struct {
443-
Config string
427+
ConfigPaths []string
444428
Ctx string
445429
CtxAuthInfo string
446430
CtxCluster string
@@ -451,7 +435,6 @@ type currentEnv struct {
451435
ClientKeyData string
452436
ClusterCACertData string
453437
Insecure string
454-
LoadConfigFile string
455438
Token string
456439
}
457440

website/docs/index.html.markdown

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Use the navigation to the left to read about the available resources.
1515

1616
```hcl
1717
provider "kubernetes" {
18-
config_path = "~/.kube/config"
18+
config_paths = [
19+
"~/.kube/config"
20+
]
1921
config_context = "my-context"
2022
}
2123
@@ -79,12 +81,6 @@ the provider will try to use the service account token from the `/var/run/secret
7981
Detection of in-cluster execution is based on the sole availability both of the `KUBERNETES_SERVICE_HOST` and `KUBERNETES_SERVICE_PORT` environment variables,
8082
with non empty values.
8183

82-
```hcl
83-
provider "kubernetes" {
84-
load_config_file = "false"
85-
}
86-
```
87-
8884
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.
8985

9086
### Statically defined credentials
@@ -93,8 +89,6 @@ Another way is **statically** define TLS certificate credentials:
9389

9490
```hcl
9591
provider "kubernetes" {
96-
load_config_file = "false"
97-
9892
host = "https://104.196.242.174"
9993
10094
client_certificate = "${file("~/.kube/client-cert.pem")}"
@@ -107,8 +101,6 @@ or username and password (HTTP Basic Authorization):
107101

108102
```hcl
109103
provider "kubernetes" {
110-
load_config_file = "false"
111-
112104
host = "https://104.196.242.174"
113105
114106
username = "username"
@@ -132,7 +124,7 @@ The following arguments are supported:
132124
* `client_certificate` - (Optional) PEM-encoded client certificate for TLS authentication. Can be sourced from `KUBE_CLIENT_CERT_DATA`.
133125
* `client_key` - (Optional) PEM-encoded client certificate key for TLS authentication. Can be sourced from `KUBE_CLIENT_KEY_DATA`.
134126
* `cluster_ca_certificate` - (Optional) PEM-encoded root certificates bundle for TLS authentication. Can be sourced from `KUBE_CLUSTER_CA_CERT_DATA`.
135-
* `config_path` - (Optional) Path to the kube config file. Can be sourced from `KUBE_CONFIG`.
127+
* `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.
136128
* `config_context` - (Optional) Context to choose from the config file. Can be sourced from `KUBE_CTX`.
137129
* `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`.
138130
* `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`.

0 commit comments

Comments
 (0)