-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanupdeployment.go
More file actions
148 lines (132 loc) · 3.71 KB
/
cleanupdeployment.go
File metadata and controls
148 lines (132 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cmd
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/github"
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/helm"
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/k8s"
ns "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/namespace"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var cleanupDeploymentCmd = &cobra.Command{
Use: "cleanup-deployment",
Short: "Cleanup a renku deployment",
Run: cleanupDeployment,
}
func cleanupDeployment(cmd *cobra.Command, args []string) {
ctx := context.Background()
if namespace == "" {
cli, err := github.NewGitHubCLI("")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
namespace, err = ns.FindCurrentNamespace(ctx, cli)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
clients, err := k8s.GetClientset()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
client, err := k8s.GetDynamicClient()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Ask for confirmation
fmt.Printf("This command will perform the following actions in the namespace '%s':\n", namespace)
fmt.Println(" 1. Delete all sessions")
fmt.Println(" 2. Uninstall all helm releases")
fmt.Println(" 3. Delete all jobs")
fmt.Println(" 4. Delete all PVCs")
fmt.Println(" 5. Forcibly delete all sessions")
if deleteNamespace {
fmt.Printf(" 6. Delete the namespace '%s'\n", namespace)
}
proceed, err := askForConfirmation("Proceed?")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !proceed {
os.Exit(0)
}
// 1. Delete all sessions
fmt.Println("1. Delete all sessions")
err = k8s.DeleteAllSessions(ctx, client, namespace, k8s.DeleteAllSessionsOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 2. Uninstall all helm releases
fmt.Println("2. Uninstall all helm releases")
helm, err := helm.NewHelmCLI("")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = helm.UninstallAllReleases(ctx, namespace)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 3. Delete all jobs
fmt.Println("3. Delete all jobs")
propagation := metav1.DeletePropagationForeground
err = clients.BatchV1().Jobs(namespace).DeleteCollection(ctx, metav1.DeleteOptions{PropagationPolicy: &propagation}, metav1.ListOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 4. Delete all PVCs
fmt.Println("4. Delete all PVCs")
err = clients.CoreV1().PersistentVolumeClaims(namespace).DeleteCollection(ctx, metav1.DeleteOptions{PropagationPolicy: &propagation}, metav1.ListOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 5. Forcibly delete all sessions
fmt.Println("5. Forcibly delete all sessions")
err = k8s.ForciblyDeleteAllSessions(ctx, client, namespace, k8s.DeleteAllSessionsOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 6. Delete the namespace
if deleteNamespace {
fmt.Printf("6. Delete the namespace '%s'\n", namespace)
err = clients.CoreV1().Namespaces().Delete(ctx, namespace, metav1.DeleteOptions{PropagationPolicy: &propagation})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
func init() {
cleanupDeploymentCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "k8s namespace")
cleanupDeploymentCmd.Flags().BoolVar(&deleteNamespace, "delete-namespace", false, "if set, the namespace will be deleted")
}
func askForConfirmation(question string) (response bool, err error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s (yes/no): ", question)
res, err := reader.ReadString('\n')
if err != nil {
return false, err
}
res = strings.ToLower(strings.TrimSpace(res))
switch res {
case "yes":
return true, nil
case "no":
return false, nil
}
return false, fmt.Errorf("Invalid answer, aborting.")
}