diff --git a/kind/resource_cluster.go b/kind/resource_cluster.go index e5bb33d..e24e85e 100644 --- a/kind/resource_cluster.go +++ b/kind/resource_cluster.go @@ -55,14 +55,16 @@ func resourceCluster() *schema.Resource { Schema: kindConfigFields(), }, }, - "kubeconfig": { + "kubeconfig_path": { Type: schema.TypeString, - Description: `Kubeconfig set after the the cluster is created.`, + Description: `Kubeconfig path set after the the cluster is created or by the user to override defaults.`, + ForceNew: true, + Optional: true, Computed: true, }, - "kubeconfig_path": { + "kubeconfig": { Type: schema.TypeString, - Description: `Kubeconfig path set after the the cluster is created.`, + Description: `Kubeconfig set after the the cluster is created.`, Computed: true, }, "client_certificate": { @@ -95,9 +97,17 @@ func resourceKindClusterCreate(d *schema.ResourceData, meta interface{}) error { nodeImage := d.Get("node_image").(string) config := d.Get("kind_config") waitForReady := d.Get("wait_for_ready").(bool) + kubeconfigPath := d.Get("kubeconfig_path") var copts []cluster.CreateOption + if kubeconfigPath != nil { + path := kubeconfigPath.(string) + if path != "" { + copts = append(copts, cluster.CreateWithKubeconfigPath(path)) + } + } + if config != nil { cfg := config.([]interface{}) if len(cfg) == 1 { // there is always just one kind_config allowed @@ -147,13 +157,16 @@ func resourceKindClusterRead(d *schema.ResourceData, meta interface{}) error { d.SetId("") return err } - exportPath := fmt.Sprintf("%s%s%s-config", currentPath, string(os.PathSeparator), name) - err = provider.ExportKubeConfig(name, exportPath) - if err != nil { - d.SetId("") - return err + + if _, ok := d.GetOkExists("kubeconfig_path"); !ok { + exportPath := fmt.Sprintf("%s%s%s-config", currentPath, string(os.PathSeparator), name) + err = provider.ExportKubeConfig(name, exportPath) + if err != nil { + d.SetId("") + return err + } + d.Set("kubeconfig_path", exportPath) } - d.Set("kubeconfig_path", exportPath) // use the current context in kubeconfig config, err := clientcmd.RESTConfigFromKubeConfig([]byte(kconfig)) @@ -185,6 +198,10 @@ func resourceKindClusterUpdate(d *schema.ResourceData, meta interface{}) error { d.SetPartial("kind_config") } + if d.HasChange("kubeconfig_path") { + d.SetPartial("kubeconfig_path") + } + d.Partial(false) return resourceKindClusterRead(d, meta) } diff --git a/kind/resource_cluster_test.go b/kind/resource_cluster_test.go index 1084461..8300351 100644 --- a/kind/resource_cluster_test.go +++ b/kind/resource_cluster_test.go @@ -45,6 +45,17 @@ func TestAccCluster(t *testing.T) { resource.TestCheckNoResourceAttr(resourceName, "kind_config"), ), }, + { + Config: testAccBasicClusterConfigWithKubeconfigPath(clusterName), + Check: resource.ComposeTestCheckFunc( + testAccCheckClusterCreate(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", clusterName), + resource.TestCheckResourceAttr(resourceName, "kubeconfig_path", "/tmp/kind-provider-test/new_file"), + resource.TestCheckNoResourceAttr(resourceName, "node_image"), + resource.TestCheckResourceAttr(resourceName, "wait_for_ready", "false"), + resource.TestCheckNoResourceAttr(resourceName, "kind_config"), + ), + }, { Config: testAccBasicWaitForReadyClusterConfig(clusterName), Check: resource.ComposeTestCheckFunc( @@ -360,6 +371,16 @@ resource "kind_cluster" "test" { `, name) } +func testAccBasicClusterConfigWithKubeconfigPath(name string) string { + + return fmt.Sprintf(` +resource "kind_cluster" "test" { + name = "%s" + kubeconfig_path = "/tmp/kind-provider-test/new_file" +} +`, name) +} + func testAccNodeImageClusterConfig(name string) string { return fmt.Sprintf(` resource "kind_cluster" "test" {