Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drasi Uninstall command #2

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func init() {
NewListCommand(),
NewWaitCommand(),
NewNamespaceCommand(),
NewUninstallCommand(),
)

RootCommand.PersistentFlags().StringP("namespace", "n", "drasi-system", "Kubernetes namespace to install Drasi into")
Expand Down
74 changes: 74 additions & 0 deletions cli/cmd/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"fmt"
"log"
"strings"

"drasi.io/cli/service"
"github.com/spf13/cobra"
)

func NewUninstallCommand() *cobra.Command {
var uninstallCommand = &cobra.Command{
Use: "uninstall ",
Short: "Uninstall Drasi",
Long: `Uninstall Drasi from the current namespace`,
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := readConfig()
var err error
var currentNamespace string
if currentNamespace, err = cmd.Flags().GetString("namespace"); err != nil {
return err
}
if !cmd.Flags().Changed("namespace") {
currentNamespace = cfg.DrasiNamespace
}

fmt.Println("Uninstalling Drasi")
fmt.Println("Deleting namespace: ", currentNamespace)

// Ask for confirmation if the user didn't pass the -y flag
if !cmd.Flags().Changed("yes") {
fmt.Printf("Are you sure you want to uninstall Drasi from the namespace %s? (yes/no): ", currentNamespace)
if !askForConfirmation(currentNamespace) {
fmt.Println("Uninstall cancelled")
return nil
}
}

err = service.UninstallDrasi(currentNamespace)
if err != nil {
return err
}

fmt.Println("Drasi uninstalled successfully")

return nil
},
}

uninstallCommand.Flags().BoolP("yes", "y", false, "uninstall -y")

return uninstallCommand
}

func askForConfirmation(namespace string) bool {
var response string

_, err := fmt.Scanln(&response)
if err != nil {
log.Fatal(err)
}

switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
fmt.Println("I'm sorry but I didn't get what you meant, please type (y)es or (n)o and then press enter:")
return askForConfirmation(namespace)
}
}
56 changes: 56 additions & 0 deletions cli/service/uninstaller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package service

import (
"context"
"fmt"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

func UninstallDrasi(namespace string) error {
configLoadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}

config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(configLoadingRules, configOverrides)

restConfig, err := config.ClientConfig()

if err != nil {
return err
}

clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}

err = clientset.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{})
Dismissed Show dismissed Hide dismissed
if err != nil {
return err
}

// Pods are not deleted immediately; instead, they will remain in a Terminating state for a while
// Need to verify that all resources have been deleted; if not, wait for them to be deleted
nsDeleted := false
for !nsDeleted {
list, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
Dismissed Show dismissed Hide dismissed
if err != nil {
return err
}
for _, ns := range list.Items {
// check if the namespace is still there
if ns.Name == namespace {
fmt.Println("Namespace is still present. Waiting for it to be deleted")
// wait for 10 seconds
time.Sleep(10 * time.Second)
} else {
nsDeleted = true
}
}
}

return nil
}
Loading