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

Commit

Permalink
allow to pass kubeconfig (#35)
Browse files Browse the repository at this point in the history
* allow to pass kubeconfig

* add a test
  • Loading branch information
Daniel Roth authored Oct 21, 2020
1 parent d421190 commit 2eab7e9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
37 changes: 27 additions & 10 deletions kind/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions kind/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 2eab7e9

Please sign in to comment.