Skip to content

Commit b6b9d55

Browse files
authored
Merge pull request #95 from crandles/get-cluster
match on exact cluster names
2 parents fee1391 + bd4c3cd commit b6b9d55

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

support/kind/kind.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,43 @@ func (k *Cluster) getKubeconfig() (string, error) {
7878
return file.Name(), nil
7979
}
8080

81-
func (k *Cluster) CreateWithConfig(imageName, kindConfigFile string) (string, error) {
82-
log.V(4).Info("Creating kind cluster ", k.name)
83-
if err := k.findOrInstallKind(k.e); err != nil {
84-
return "", err
85-
}
86-
87-
if strings.Contains(k.e.Run("kind get clusters"), k.name) {
88-
log.V(4).Info("Skipping Kind Cluster.Create: cluster already created: ", k.name)
89-
return k.getKubeconfig()
90-
}
91-
92-
log.V(4).Info("Launching: kind create cluster --name", k.name, "--image", imageName, "--config", kindConfigFile)
93-
p := k.e.RunProc(fmt.Sprintf(`kind create cluster --name %s --image %s --config %s`, k.name, imageName, kindConfigFile))
94-
if p.Err() != nil {
95-
return "", fmt.Errorf("failed to create kind cluster: %s : %s", p.Err(), p.Result())
96-
}
97-
81+
func (k *Cluster) clusterExists(name string) (string, bool) {
9882
clusters := k.e.Run("kind get clusters")
99-
if !strings.Contains(clusters, k.name) {
100-
return "", fmt.Errorf("kind Cluster.Create: cluster %v still not in 'cluster list' after creation: %v", k.name, clusters)
83+
for _, c := range strings.Split(clusters, "\n") {
84+
if c == name {
85+
return clusters, true
86+
}
10187
}
102-
log.V(4).Info("kind clusters available: ", clusters)
88+
return clusters, false
89+
}
10390

104-
// Grab kubeconfig file for cluster.
105-
return k.getKubeconfig()
91+
func (k *Cluster) CreateWithConfig(imageName, kindConfigFile string) (string, error) {
92+
return k.Create("--image", imageName, "--config", kindConfigFile)
10693
}
10794

108-
func (k *Cluster) Create() (string, error) {
95+
func (k *Cluster) Create(args ...string) (string, error) {
10996
log.V(4).Info("Creating kind cluster ", k.name)
11097
if err := k.findOrInstallKind(k.e); err != nil {
11198
return "", err
11299
}
113100

114-
if strings.Contains(k.e.Run("kind get clusters"), k.name) {
101+
if _, ok := k.clusterExists(k.name); ok {
115102
log.V(4).Info("Skipping Kind Cluster.Create: cluster already created: ", k.name)
116103
return k.getKubeconfig()
117104
}
118105

119-
log.V(4).Info("Launching: kind create cluster --name", k.name)
120-
p := k.e.RunProc(fmt.Sprintf(`kind create cluster --name %s`, k.name))
106+
command := fmt.Sprintf(`kind create cluster --name %s`, k.name)
107+
if len(args) > 0 {
108+
command = fmt.Sprintf("%s %s", command, strings.Join(args, " "))
109+
}
110+
log.V(4).Info("Launching:", command)
111+
p := k.e.RunProc(command)
121112
if p.Err() != nil {
122113
return "", fmt.Errorf("failed to create kind cluster: %s : %s", p.Err(), p.Result())
123114
}
124115

125-
clusters := k.e.Run("kind get clusters")
126-
if !strings.Contains(clusters, k.name) {
116+
clusters, ok := k.clusterExists(k.name)
117+
if !ok {
127118
return "", fmt.Errorf("kind Cluster.Create: cluster %v still not in 'cluster list' after creation: %v", k.name, clusters)
128119
}
129120
log.V(4).Info("kind clusters available: ", clusters)

0 commit comments

Comments
 (0)