From 857ccf5e2d6a5171c81bb9eb02d713c0c5a4ec36 Mon Sep 17 00:00:00 2001 From: Ricardo Weir Date: Mon, 21 Oct 2024 15:59:42 -0700 Subject: [PATCH] Remove use of client from CLI setup Prior, we were using a kubeconfig client during setup of the platform commands. The client uses the command "vcluster platform token". This would cause an endless loop like the following: 1. you run the "vcluster platform ..." command 2. vcluster makes request to /deployments endpoint, before running the intended command. 3. the k8s client uses vcluster to authenticate the request 4. return to step 2 The request was being made to detect the platform namespace and set it as the default namespace for the backup management command. Now, there is not a default that is set ahead of running the command but if a value was not passed, the CLI will then search for the platform namespace. --- .../cmd/platform/backup/management.go | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/vclusterctl/cmd/platform/backup/management.go b/cmd/vclusterctl/cmd/platform/backup/management.go index cc7b6504b..59295d9d9 100644 --- a/cmd/vclusterctl/cmd/platform/backup/management.go +++ b/cmd/vclusterctl/cmd/platform/backup/management.go @@ -68,10 +68,8 @@ vcluster platform backup management }, } - platformNamespace, _ := clihelper.VClusterPlatformInstallationNamespace(context.Background()) - c.Flags().StringSliceVar(&cmd.Skip, "skip", []string{}, "What resources the backup should skip. Valid options are: users, teams, accesskeys, sharedsecrets, clusters and clusteraccounttemplates") - c.Flags().StringVar(&cmd.Namespace, "namespace", platformNamespace, product.Replace("The namespace vCluster platform was installed into")) + c.Flags().StringVar(&cmd.Namespace, "namespace", "", product.Replace("The namespace vCluster platform was installed into")) c.Flags().StringVar(&cmd.Filename, "filename", "backup.yaml", "The filename to write the backup to") return c } @@ -92,12 +90,15 @@ func (cmd *ManagementCmd) run(cobraCmd *cobra.Command) error { return fmt.Errorf("there is an error loading your current kube config (%w), please make sure you have access to a kubernetes cluster and the command `kubectl get namespaces` is working", err) } - isInstalled, err := clihelper.IsLoftAlreadyInstalled(cobraCmd.Context(), kubeClient, cmd.Namespace) + ctx := cobraCmd.Context() + ns, err := cmd.getNS(ctx) + + isInstalled, err := clihelper.IsLoftAlreadyInstalled(cobraCmd.Context(), kubeClient, ns) if err != nil { return err } else if !isInstalled { answer, err := cmd.Log.Question(&survey.QuestionOptions{ - Question: fmt.Sprintf(product.Replace("Seems like vCluster platform was not installed into namespace %q, do you want to continue?"), cmd.Namespace), + Question: fmt.Sprintf(product.Replace("Seems like vCluster platform was not installed into namespace %q, do you want to continue?"), ns), DefaultValue: "Yes", Options: []string{"Yes", "No"}, }) @@ -106,7 +107,6 @@ func (cmd *ManagementCmd) run(cobraCmd *cobra.Command) error { } } - ctx := cobraCmd.Context() client, err := clientpkg.New(kubeConfig, clientpkg.Options{Scheme: scheme}) if err != nil { return err @@ -133,3 +133,14 @@ func (cmd *ManagementCmd) run(cobraCmd *cobra.Command) error { cmd.Log.Donef("Wrote backup to %s", cmd.Filename) return nil } + +func (cmd *ManagementCmd) getNS(ctx context.Context) (string, error) { + if cmd.Namespace != "" { + return cmd.Namespace, nil + } + platformNamespace, err := clihelper.VClusterPlatformInstallationNamespace(ctx) + if err != nil { + return "", err + } + return platformNamespace, nil +}