Skip to content
This repository was archived by the owner on Apr 1, 2022. It is now read-only.

Commit 2eab7e9

Browse files
author
Daniel Roth
authored
allow to pass kubeconfig (#35)
* allow to pass kubeconfig * add a test
1 parent d421190 commit 2eab7e9

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

kind/resource_cluster.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ func resourceCluster() *schema.Resource {
5555
Schema: kindConfigFields(),
5656
},
5757
},
58-
"kubeconfig": {
58+
"kubeconfig_path": {
5959
Type: schema.TypeString,
60-
Description: `Kubeconfig set after the the cluster is created.`,
60+
Description: `Kubeconfig path set after the the cluster is created or by the user to override defaults.`,
61+
ForceNew: true,
62+
Optional: true,
6163
Computed: true,
6264
},
63-
"kubeconfig_path": {
65+
"kubeconfig": {
6466
Type: schema.TypeString,
65-
Description: `Kubeconfig path set after the the cluster is created.`,
67+
Description: `Kubeconfig set after the the cluster is created.`,
6668
Computed: true,
6769
},
6870
"client_certificate": {
@@ -95,9 +97,17 @@ func resourceKindClusterCreate(d *schema.ResourceData, meta interface{}) error {
9597
nodeImage := d.Get("node_image").(string)
9698
config := d.Get("kind_config")
9799
waitForReady := d.Get("wait_for_ready").(bool)
100+
kubeconfigPath := d.Get("kubeconfig_path")
98101

99102
var copts []cluster.CreateOption
100103

104+
if kubeconfigPath != nil {
105+
path := kubeconfigPath.(string)
106+
if path != "" {
107+
copts = append(copts, cluster.CreateWithKubeconfigPath(path))
108+
}
109+
}
110+
101111
if config != nil {
102112
cfg := config.([]interface{})
103113
if len(cfg) == 1 { // there is always just one kind_config allowed
@@ -147,13 +157,16 @@ func resourceKindClusterRead(d *schema.ResourceData, meta interface{}) error {
147157
d.SetId("")
148158
return err
149159
}
150-
exportPath := fmt.Sprintf("%s%s%s-config", currentPath, string(os.PathSeparator), name)
151-
err = provider.ExportKubeConfig(name, exportPath)
152-
if err != nil {
153-
d.SetId("")
154-
return err
160+
161+
if _, ok := d.GetOkExists("kubeconfig_path"); !ok {
162+
exportPath := fmt.Sprintf("%s%s%s-config", currentPath, string(os.PathSeparator), name)
163+
err = provider.ExportKubeConfig(name, exportPath)
164+
if err != nil {
165+
d.SetId("")
166+
return err
167+
}
168+
d.Set("kubeconfig_path", exportPath)
155169
}
156-
d.Set("kubeconfig_path", exportPath)
157170

158171
// use the current context in kubeconfig
159172
config, err := clientcmd.RESTConfigFromKubeConfig([]byte(kconfig))
@@ -185,6 +198,10 @@ func resourceKindClusterUpdate(d *schema.ResourceData, meta interface{}) error {
185198
d.SetPartial("kind_config")
186199
}
187200

201+
if d.HasChange("kubeconfig_path") {
202+
d.SetPartial("kubeconfig_path")
203+
}
204+
188205
d.Partial(false)
189206
return resourceKindClusterRead(d, meta)
190207
}

kind/resource_cluster_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ func TestAccCluster(t *testing.T) {
4545
resource.TestCheckNoResourceAttr(resourceName, "kind_config"),
4646
),
4747
},
48+
{
49+
Config: testAccBasicClusterConfigWithKubeconfigPath(clusterName),
50+
Check: resource.ComposeTestCheckFunc(
51+
testAccCheckClusterCreate(resourceName),
52+
resource.TestCheckResourceAttr(resourceName, "name", clusterName),
53+
resource.TestCheckResourceAttr(resourceName, "kubeconfig_path", "/tmp/kind-provider-test/new_file"),
54+
resource.TestCheckNoResourceAttr(resourceName, "node_image"),
55+
resource.TestCheckResourceAttr(resourceName, "wait_for_ready", "false"),
56+
resource.TestCheckNoResourceAttr(resourceName, "kind_config"),
57+
),
58+
},
4859
{
4960
Config: testAccBasicWaitForReadyClusterConfig(clusterName),
5061
Check: resource.ComposeTestCheckFunc(
@@ -360,6 +371,16 @@ resource "kind_cluster" "test" {
360371
`, name)
361372
}
362373

374+
func testAccBasicClusterConfigWithKubeconfigPath(name string) string {
375+
376+
return fmt.Sprintf(`
377+
resource "kind_cluster" "test" {
378+
name = "%s"
379+
kubeconfig_path = "/tmp/kind-provider-test/new_file"
380+
}
381+
`, name)
382+
}
383+
363384
func testAccNodeImageClusterConfig(name string) string {
364385
return fmt.Sprintf(`
365386
resource "kind_cluster" "test" {

0 commit comments

Comments
 (0)