Skip to content

Commit

Permalink
Display better uninstall prompt if flux is managed by a different tool
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <[email protected]>
  • Loading branch information
somtochiama committed Nov 1, 2023
1 parent 2f15ad9 commit 9cd4a72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cmd/flux/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getFluxClusterInfo(ctx context.Context, c client.Client) (fluxClusterInfo,
// promptui.ErrAbort if the user doesn't confirm, or an error encountered.
func confirmFluxInstallOverride(info fluxClusterInfo) error {
// no need to display prompt if installation is managed by Flux
if info.managedBy == "" || info.managedBy == "flux" {
if installManagedByFlux(info.managedBy) {
return nil
}

Expand All @@ -104,3 +104,7 @@ func confirmFluxInstallOverride(info fluxClusterInfo) error {
_, err := prompt.Run()
return err
}

func installManagedByFlux(manager string) bool {
return manager == "" || manager == "flux"
}
30 changes: 21 additions & 9 deletions cmd/flux/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/fluxcd/flux2/v2/internal/utils"
"github.com/fluxcd/flux2/v2/pkg/uninstall"
Expand Down Expand Up @@ -59,24 +60,35 @@ func init() {
}

func uninstallCmdRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
if err != nil {
return err
}

if !uninstallArgs.dryRun && !uninstallArgs.silent {
info, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("cluster info unavailable: %w", err)
}
}

promptLabel := "Are you sure you want to delete Flux and its custom resource definitions"
if !installManagedByFlux(info.managedBy) {
promptLabel = fmt.Sprintf("Flux is managed by %s! Are you sure you want to delete Flux and its CRDs using Flux CLI", info.managedBy)
}
prompt := promptui.Prompt{
Label: "Are you sure you want to delete Flux and its custom resource definitions",
Label: promptLabel,
IsConfirm: true,
}
if _, err := prompt.Run(); err != nil {
return fmt.Errorf("aborting")
}
}

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
if err != nil {
return err
}

logger.Actionf("deleting components in %s namespace", *kubeconfigArgs.Namespace)
uninstall.Components(ctx, logger, kubeClient, *kubeconfigArgs.Namespace, uninstallArgs.dryRun)

Expand Down

0 comments on commit 9cd4a72

Please sign in to comment.