From 40fee41a49d6182fee270f6c97eb1b874f0cf78d Mon Sep 17 00:00:00 2001 From: Christopher Randles Date: Tue, 16 Nov 2021 19:28:11 -0500 Subject: [PATCH] fetch kubeconfig if cluster is already created --- support/kind/kind.go | 66 +++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/support/kind/kind.go b/support/kind/kind.go index bf7460fc..ab9b8594 100644 --- a/support/kind/kind.go +++ b/support/kind/kind.go @@ -46,6 +46,28 @@ func (k *Cluster) WithVersion(ver string) *Cluster { return k } +func (k *Cluster) getKubeconfig() (string, error) { + kubecfg := fmt.Sprintf("%s-kubecfg", k.name) + p := k.e.RunProc(fmt.Sprintf(`kind get kubeconfig --name %s`, k.name)) + if p.Err() != nil { + return "", fmt.Errorf("kind get kubeconfig: %s: %w", p.Result(), p.Err()) + } + + file, err := ioutil.TempFile("", fmt.Sprintf("kind-cluser-%s", kubecfg)) + if err != nil { + return "", fmt.Errorf("kind kubeconfig file: %w", err) + } + defer file.Close() + + k.kubecfgFile = file.Name() + + if n, err := io.Copy(file, strings.NewReader(p.Result())); n == 0 || err != nil { + return "", fmt.Errorf("kind kubecfg file: bytes copied: %d: %w]", n, err) + } + + return file.Name(), nil +} + func (k *Cluster) CreateWithConfig(imageName, kindConfigFile string) (string, error) { log.Println("Creating kind cluster ", k.name) if err := k.findOrInstallKind(k.e); err != nil { @@ -54,7 +76,7 @@ func (k *Cluster) CreateWithConfig(imageName, kindConfigFile string) (string, er if strings.Contains(k.e.Run("kind get clusters"), k.name) { log.Println("Skipping Kind Cluster.Create: cluster already created: ", k.name) - return "", nil + return k.getKubeconfig() } log.Println("launching: kind create cluster --name", k.name, "--image", imageName, "--config", kindConfigFile) @@ -70,25 +92,7 @@ func (k *Cluster) CreateWithConfig(imageName, kindConfigFile string) (string, er log.Println("kind clusters available: ", clusters) // Grab kubeconfig file for cluster. - kubecfg := fmt.Sprintf("%s-kubecfg", k.name) - p = k.e.RunProc(fmt.Sprintf(`kind get kubeconfig --name %s`, k.name)) - if p.Err() != nil { - return "", fmt.Errorf("kind get kubeconfig: %s: %w", p.Result(), p.Err()) - } - - file, err := ioutil.TempFile("", fmt.Sprintf("kind-cluser-%s", kubecfg)) - if err != nil { - return "", fmt.Errorf("kind kubeconfig file: %w", err) - } - defer file.Close() - - k.kubecfgFile = file.Name() - - if n, err := io.Copy(file, strings.NewReader(p.Result())); n == 0 || err != nil { - return "", fmt.Errorf("kind kubecfg file: bytes copied: %d: %w]", n, err) - } - - return file.Name(), nil + return k.getKubeconfig() } func (k *Cluster) Create() (string, error) { @@ -99,7 +103,7 @@ func (k *Cluster) Create() (string, error) { if strings.Contains(k.e.Run("kind get clusters"), k.name) { log.Println("Skipping Kind Cluster.Create: cluster already created: ", k.name) - return "", nil + return k.getKubeconfig() } log.Println("launching: kind create cluster --name", k.name) @@ -115,25 +119,7 @@ func (k *Cluster) Create() (string, error) { log.Println("kind clusters available: ", clusters) // Grab kubeconfig file for cluster. - kubecfg := fmt.Sprintf("%s-kubecfg", k.name) - p = k.e.RunProc(fmt.Sprintf(`kind get kubeconfig --name %s`, k.name)) - if p.Err() != nil { - return "", fmt.Errorf("kind get kubeconfig: %s: %w", p.Result(), p.Err()) - } - - file, err := ioutil.TempFile("", fmt.Sprintf("kind-cluser-%s", kubecfg)) - if err != nil { - return "", fmt.Errorf("kind kubeconfig file: %w", err) - } - defer file.Close() - - k.kubecfgFile = file.Name() - - if n, err := io.Copy(file, strings.NewReader(p.Result())); n == 0 || err != nil { - return "", fmt.Errorf("kind kubecfg file: bytes copied: %d: %w]", n, err) - } - - return file.Name(), nil + return k.getKubeconfig() } // GetKubeconfig returns the path of the kubeconfig file