Skip to content

Commit 21dab6c

Browse files
ehudyonasipsschwei
andauthored
#97: Added soft abort to installation if knative exists (#379)
* #97: Added soft abort to installation if knative exists * Added missing soft abort * Fixes for recreate cluster * Update pkg/minikube/minikube.go Co-authored-by: Paul Schweigert <[email protected]> * Added recreate cluster function to minikube Co-authored-by: Paul Schweigert <[email protected]>
1 parent e215686 commit 21dab6c

File tree

2 files changed

+110
-50
lines changed

2 files changed

+110
-50
lines changed

pkg/kind/kind.go

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var clusterName string
3131
var kindVersion = 0.16
3232
var container_reg_name = "kind-registry"
3333
var container_reg_port = "5001"
34+
var installKnative = true
3435

3536
// SetUp creates a local Kind cluster and installs all the relevant Knative components
3637
func SetUp(name, kVersion string, installServing, installEventing bool) error {
@@ -62,22 +63,22 @@ func SetUp(name, kVersion string, installServing, installEventing bool) error {
6263
if err := createKindCluster(); err != nil {
6364
return fmt.Errorf("creating cluster: %w", err)
6465
}
65-
66-
if installServing {
67-
if err := install.Serving(); err != nil {
68-
return fmt.Errorf("install serving: %w", err)
69-
}
70-
if err := install.Kourier(); err != nil {
71-
return fmt.Errorf("install kourier: %w", err)
72-
}
73-
if err := install.KourierKind(); err != nil {
74-
return fmt.Errorf("configure kourier: %w", err)
66+
if installKnative {
67+
if installServing {
68+
if err := install.Serving(); err != nil {
69+
return fmt.Errorf("install serving: %w", err)
70+
}
71+
if err := install.Kourier(); err != nil {
72+
return fmt.Errorf("install kourier: %w", err)
73+
}
74+
if err := install.KourierKind(); err != nil {
75+
return fmt.Errorf("configure kourier: %w", err)
76+
}
7577
}
76-
}
77-
78-
if installEventing {
79-
if err := install.Eventing(); err != nil {
80-
return fmt.Errorf("install eventing: %w", err)
78+
if installEventing {
79+
if err := install.Eventing(); err != nil {
80+
return fmt.Errorf("install eventing: %w", err)
81+
}
8182
}
8283
}
8384

@@ -203,23 +204,31 @@ func checkForExistingCluster() error {
203204
fmt.Print("Knative Cluster kind-" + clusterName + " already installed.\nDelete and recreate [y/N]: ")
204205
fmt.Scanf("%s", &resp)
205206
if resp == "y" || resp == "Y" {
206-
fmt.Println("\n Deleting cluster...")
207-
deleteCluster := exec.Command("kind", "delete", "cluster", "--name", clusterName)
208-
if err := deleteCluster.Run(); err != nil {
209-
return fmt.Errorf("delete cluster: %w", err)
210-
}
211-
deleteContainerRegistry := deleteContainerRegistry()
212-
if err := deleteContainerRegistry.Run(); err != nil {
213-
return fmt.Errorf("delete container registry: %w", err)
214-
}
215-
if err := createNewCluster(); err != nil {
207+
if err := recreateCluster(); err != nil {
216208
return fmt.Errorf("new cluster: %w", err)
217209
}
218-
if err := connectLocalRegistry(); err != nil {
219-
return fmt.Errorf("local-registry: %w", err)
220-
}
221210
} else {
222211
fmt.Println("\n Installation skipped")
212+
checkKnativeNamespace := exec.Command("kubectl", "get", "namespaces")
213+
output, err := checkKnativeNamespace.CombinedOutput()
214+
namespaces := string(output)
215+
if err != nil {
216+
fmt.Println(string(output))
217+
return fmt.Errorf("check existing cluster: %w", err)
218+
}
219+
if strings.Contains(namespaces, "knative") {
220+
fmt.Print("Knative installation already exists.\nDelete and recreate the cluster [y/N]: ")
221+
fmt.Scanf("%s", &resp)
222+
if resp == "y" || resp == "Y" {
223+
if err := recreateCluster(); err != nil {
224+
return fmt.Errorf("new cluster: %w", err)
225+
}
226+
} else {
227+
fmt.Println("Skipping installation")
228+
installKnative = false
229+
return nil
230+
}
231+
}
223232
return nil
224233
}
225234
} else {
@@ -234,6 +243,29 @@ func checkForExistingCluster() error {
234243
return nil
235244
}
236245

246+
// recreateCluster recreates a Kind cluster
247+
func recreateCluster() error {
248+
fmt.Println("\n Deleting cluster...")
249+
deleteCluster := exec.Command("kind", "delete", "cluster", "--name", clusterName)
250+
if err := deleteCluster.Run(); err != nil {
251+
return fmt.Errorf("delete cluster: %w", err)
252+
}
253+
deleteContainerRegistry := deleteContainerRegistry()
254+
if err := deleteContainerRegistry.Run(); err != nil {
255+
return fmt.Errorf("delete container registry: %w", err)
256+
}
257+
if err := createNewCluster(); err != nil {
258+
return fmt.Errorf("new cluster: %w", err)
259+
}
260+
if err := createLocalRegistry(); err != nil {
261+
return fmt.Errorf("%w", err)
262+
}
263+
if err := connectLocalRegistry(); err != nil {
264+
return fmt.Errorf("local-registry: %w", err)
265+
}
266+
return nil
267+
}
268+
237269
// createNewCluster creates a new Kind cluster
238270
func createNewCluster() error {
239271

pkg/minikube/minikube.go

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var clusterVersionOverride bool
3232
var minikubeVersion = 1.28
3333
var cpus = "3"
3434
var memory = "3072"
35+
var installKnative = true
3536

3637
// SetUp creates a local Minikube cluster and installs all the relevant Knative components
3738
func SetUp(name, kVersion string, installServing, installEventing bool) error {
@@ -66,22 +67,22 @@ func SetUp(name, kVersion string, installServing, installEventing bool) error {
6667
fmt.Println("The tunnel command must be running in a terminal window any time when using the knative quickstart environment.")
6768
fmt.Println("\nPress the Enter key to continue")
6869
fmt.Scanln()
69-
70-
if installServing {
71-
if err := install.Serving(); err != nil {
72-
return fmt.Errorf("install serving: %w", err)
73-
}
74-
if err := install.Kourier(); err != nil {
75-
return fmt.Errorf("install kourier: %w", err)
70+
if installKnative {
71+
if installServing {
72+
if err := install.Serving(); err != nil {
73+
return fmt.Errorf("install serving: %w", err)
74+
}
75+
if err := install.Kourier(); err != nil {
76+
return fmt.Errorf("install kourier: %w", err)
77+
}
78+
if err := install.KourierMinikube(); err != nil {
79+
return fmt.Errorf("configure kourier: %w", err)
80+
}
7681
}
77-
if err := install.KourierMinikube(); err != nil {
78-
return fmt.Errorf("configure kourier: %w", err)
79-
}
80-
}
81-
82-
if installEventing {
83-
if err := install.Eventing(); err != nil {
84-
return fmt.Errorf("install eventing: %w", err)
82+
if installEventing {
83+
if err := install.Eventing(); err != nil {
84+
return fmt.Errorf("install eventing: %w", err)
85+
}
8586
}
8687
}
8788

@@ -154,15 +155,30 @@ func checkForExistingCluster() error {
154155
fmt.Scanf("%s", &resp)
155156
if strings.ToLower(resp) != "y" {
156157
fmt.Println("Installation skipped")
158+
checkKnativeNamespace := exec.Command("kubectl", "get", "namespaces")
159+
output, err := checkKnativeNamespace.CombinedOutput()
160+
namespaces := string(output)
161+
if err != nil {
162+
fmt.Println(string(output))
163+
return fmt.Errorf("check existing cluster: %w", err)
164+
}
165+
if strings.Contains(namespaces, "knative") {
166+
fmt.Print("Knative installation already exists.\nDelete and recreate the cluster [y/N]: ")
167+
fmt.Scanf("%s", &resp)
168+
if strings.ToLower(resp) != "y" {
169+
fmt.Println("Skipping installation")
170+
installKnative = false
171+
return nil
172+
} else {
173+
if err := recreateCluster(); err != nil {
174+
return fmt.Errorf("failed recreating cluster: %w", err)
175+
}
176+
}
177+
}
157178
return nil
158179
}
159-
fmt.Println("deleting cluster...")
160-
deleteCluster := exec.Command("minikube", "delete", "--profile", "--insecure-registry", clusterName)
161-
if err := deleteCluster.Run(); err != nil {
162-
return fmt.Errorf("delete cluster: %w", err)
163-
}
164-
if err := createNewCluster(); err != nil {
165-
return fmt.Errorf("new cluster: %w", err)
180+
if err := recreateCluster(); err != nil {
181+
return fmt.Errorf("failed recreating cluster: %w", err)
166182
}
167183
return nil
168184
}
@@ -243,3 +259,15 @@ func getMinikubeConfig(k string) (string, bool) {
243259
}
244260
return strings.TrimRight(string(v), "\n"), ok
245261
}
262+
263+
func recreateCluster() error {
264+
fmt.Println("deleting cluster...")
265+
deleteCluster := exec.Command("minikube", "delete", "--profile", clusterName)
266+
if err := deleteCluster.Run(); err != nil {
267+
return fmt.Errorf("delete cluster: %w", err)
268+
}
269+
if err := createNewCluster(); err != nil {
270+
return fmt.Errorf("new cluster: %w", err)
271+
}
272+
return nil
273+
}

0 commit comments

Comments
 (0)