|
6 | 6 | "fmt"
|
7 | 7 | "log"
|
8 | 8 | "net/http"
|
| 9 | + "os" |
| 10 | + "strings" |
9 | 11 |
|
10 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
11 | 13 |
|
@@ -68,11 +70,11 @@ func Provider() *schema.Provider {
|
68 | 70 | DefaultFunc: schema.EnvDefaultFunc("KUBE_CLUSTER_CA_CERT_DATA", ""),
|
69 | 71 | Description: "PEM-encoded root certificates bundle for TLS authentication.",
|
70 | 72 | },
|
71 |
| - "config_path": { |
72 |
| - Type: schema.TypeString, |
| 73 | + "config_paths": { |
| 74 | + Type: schema.TypeList, |
| 75 | + Elem: &schema.Schema{Type: schema.TypeString}, |
73 | 76 | 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.", |
76 | 78 | },
|
77 | 79 | "config_context": {
|
78 | 80 | Type: schema.TypeString,
|
@@ -260,13 +262,26 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error)
|
260 | 262 | overrides := &clientcmd.ConfigOverrides{}
|
261 | 263 | loader := &clientcmd.ClientConfigLoadingRules{}
|
262 | 264 |
|
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) |
267 | 283 | }
|
268 |
| - log.Printf("[DEBUG] Configuration file is: %s", path) |
269 |
| - loader.ExplicitPath = path |
| 284 | + loader.Precedence = expandedPaths |
270 | 285 |
|
271 | 286 | ctxSuffix := "; default context"
|
272 | 287 |
|
|
0 commit comments