Skip to content

Commit

Permalink
Merge pull request #74 from crandles/allow-reuse
Browse files Browse the repository at this point in the history
Allow re-use of kind clusters
  • Loading branch information
k8s-ci-robot authored Nov 24, 2021
2 parents 9169c87 + 40fee41 commit 9917308
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions support/kind/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 9917308

Please sign in to comment.