From 9cd4a7215fe4dd01c467d68e7a8f34030abf2f29 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Tue, 31 Oct 2023 14:37:32 +0100 Subject: [PATCH] Display better uninstall prompt if flux is managed by a different tool Signed-off-by: Somtochi Onyekwere --- cmd/flux/cluster_info.go | 6 +++++- cmd/flux/uninstall.go | 30 +++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cmd/flux/cluster_info.go b/cmd/flux/cluster_info.go index 0640a02573..daf122eeca 100644 --- a/cmd/flux/cluster_info.go +++ b/cmd/flux/cluster_info.go @@ -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 } @@ -104,3 +104,7 @@ func confirmFluxInstallOverride(info fluxClusterInfo) error { _, err := prompt.Run() return err } + +func installManagedByFlux(manager string) bool { + return manager == "" || manager == "flux" +} diff --git a/cmd/flux/uninstall.go b/cmd/flux/uninstall.go index bee7ad4836..0128a83312 100644 --- a/cmd/flux/uninstall.go +++ b/cmd/flux/uninstall.go @@ -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" @@ -59,9 +60,28 @@ 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 { @@ -69,14 +89,6 @@ 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 - } - logger.Actionf("deleting components in %s namespace", *kubeconfigArgs.Namespace) uninstall.Components(ctx, logger, kubeClient, *kubeconfigArgs.Namespace, uninstallArgs.dryRun)