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