|
| 1 | +package installer |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "pb/pkg/common" |
| 10 | + "pb/pkg/helm" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "gopkg.in/yaml.v2" |
| 15 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | +) |
| 18 | + |
| 19 | +func Uninstaller(verbose bool) error { |
| 20 | + // Load configuration from the parseable.yaml file |
| 21 | + configPath := filepath.Join(os.Getenv("HOME"), ".parseable", "parseable.yaml") |
| 22 | + config, err := loadParseableConfig(configPath) |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("failed to load configuration: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + if config == (&ValuesHolder{}) { |
| 28 | + return fmt.Errorf("no existing configuration found in ~/.parseable/parseable.yaml") |
| 29 | + } |
| 30 | + |
| 31 | + // Prompt for Kubernetes context |
| 32 | + _, err = promptK8sContext() |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to prompt for Kubernetes context: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + // Prompt user to confirm namespace |
| 38 | + namespace := config.ParseableSecret.Namespace |
| 39 | + confirm, err := promptUserConfirmation(fmt.Sprintf(common.Yellow+"Do you wish to uninstall Parseable from namespace '%s'?", namespace)) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("failed to get user confirmation: %v", err) |
| 42 | + } |
| 43 | + if !confirm { |
| 44 | + return fmt.Errorf("Uninstall cancelled.") |
| 45 | + } |
| 46 | + |
| 47 | + // Helm application configuration |
| 48 | + helmApp := helm.Helm{ |
| 49 | + ReleaseName: "parseable", |
| 50 | + Namespace: namespace, |
| 51 | + RepoName: "parseable", |
| 52 | + RepoURL: "https://charts.parseable.com", |
| 53 | + ChartName: "parseable", |
| 54 | + Version: "1.6.5", |
| 55 | + } |
| 56 | + |
| 57 | + // Create a spinner |
| 58 | + spinner := createDeploymentSpinner(namespace, "Uninstalling parseable in ") |
| 59 | + |
| 60 | + // Redirect standard output if not in verbose mode |
| 61 | + var oldStdout *os.File |
| 62 | + if !verbose { |
| 63 | + oldStdout = os.Stdout |
| 64 | + _, w, _ := os.Pipe() |
| 65 | + os.Stdout = w |
| 66 | + } |
| 67 | + |
| 68 | + spinner.Start() |
| 69 | + |
| 70 | + // Run Helm uninstall |
| 71 | + _, err = helm.Uninstall(helmApp, verbose) |
| 72 | + spinner.Stop() |
| 73 | + |
| 74 | + // Restore stdout |
| 75 | + if !verbose { |
| 76 | + os.Stdout = oldStdout |
| 77 | + } |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to uninstall Parseable: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + // Namespace cleanup using Kubernetes client |
| 84 | + fmt.Printf(common.Yellow+"Cleaning up namespace '%s'...\n"+common.Reset, namespace) |
| 85 | + cleanupErr := cleanupNamespaceWithClient(namespace) |
| 86 | + if cleanupErr != nil { |
| 87 | + return fmt.Errorf("failed to clean up namespace '%s': %v", namespace, cleanupErr) |
| 88 | + } |
| 89 | + |
| 90 | + // Print success banner |
| 91 | + fmt.Printf(common.Green+"Successfully uninstalled Parseable from namespace '%s'.\n"+common.Reset, namespace) |
| 92 | + |
| 93 | + return nil |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +// promptUserConfirmation prompts the user for a yes/no confirmation |
| 98 | +func promptUserConfirmation(message string) (bool, error) { |
| 99 | + reader := bufio.NewReader(os.Stdin) |
| 100 | + fmt.Printf("%s [y/N]: ", message) |
| 101 | + response, err := reader.ReadString('\n') |
| 102 | + if err != nil { |
| 103 | + return false, err |
| 104 | + } |
| 105 | + response = strings.TrimSpace(strings.ToLower(response)) |
| 106 | + return response == "y" || response == "yes", nil |
| 107 | +} |
| 108 | + |
| 109 | +// loadParseableConfig loads the configuration from the specified file |
| 110 | +func loadParseableConfig(path string) (*ValuesHolder, error) { |
| 111 | + data, err := os.ReadFile(path) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + var config ValuesHolder |
| 116 | + if err := yaml.Unmarshal(data, &config); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + return &config, nil |
| 120 | +} |
| 121 | + |
| 122 | +// cleanupNamespaceWithClient deletes the specified namespace using Kubernetes client-go |
| 123 | +func cleanupNamespaceWithClient(namespace string) error { |
| 124 | + // Load the kubeconfig |
| 125 | + config, err := loadKubeConfig() |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("failed to load kubeconfig: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + // Create the clientset |
| 131 | + clientset, err := kubernetes.NewForConfig(config) |
| 132 | + if err != nil { |
| 133 | + return fmt.Errorf("failed to create Kubernetes client: %v", err) |
| 134 | + } |
| 135 | + |
| 136 | + // Create a context with a timeout for namespace deletion |
| 137 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) |
| 138 | + defer cancel() |
| 139 | + |
| 140 | + // Delete the namespace |
| 141 | + err = clientset.CoreV1().Namespaces().Delete(ctx, namespace, v1.DeleteOptions{}) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("error deleting namespace: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + // Wait for the namespace to be fully removed |
| 147 | + fmt.Printf("Waiting for namespace '%s' to be deleted...\n", namespace) |
| 148 | + for { |
| 149 | + _, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, v1.GetOptions{}) |
| 150 | + if err != nil { |
| 151 | + fmt.Printf("Namespace '%s' successfully deleted.\n", namespace) |
| 152 | + break |
| 153 | + } |
| 154 | + time.Sleep(2 * time.Second) |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments