From 4a5e558a344a6d6ea4006e603a0a0d975ff449d4 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Sat, 14 Sep 2024 01:15:04 -0500 Subject: [PATCH] chore(util): Only check if namespace exists when no pods are found --- internal/util/cmd_setup.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/util/cmd_setup.go b/internal/util/cmd_setup.go index 22442155..ba0dcbf8 100644 --- a/internal/util/cmd_setup.go +++ b/internal/util/cmd_setup.go @@ -57,10 +57,6 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) er conf.Context = conf.Client.Context conf.Namespace = conf.Client.Namespace - if _, err := conf.Client.Namespaces().Get(ctx, conf.Namespace, metav1.GetOptions{}); err != nil { - slog.Warn("Namespace may not exist", "error", err) - } - podFlag, err := cmd.Flags().GetString(consts.PodFlag) if err != nil { panic(err) @@ -73,6 +69,7 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) er } pod, err := conf.Client.Pods().Get(ctx, podFlag, metav1.GetOptions{}) if err != nil { + checkNamespaceExists(ctx, conf) return err } pods = []corev1.Pod{*pod} @@ -87,6 +84,7 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) er if len(pods) == 0 { result, err := database.DetectDialect(ctx, conf.Client) if err != nil { + checkNamespaceExists(ctx, conf) return err } if len(result) == 1 || opts.NoSurvey { @@ -116,6 +114,7 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) er } else { conf.Dialect, err = database.DetectDialectFromPod(pods[0]) if err != nil { + checkNamespaceExists(ctx, conf) return err } } @@ -533,3 +532,9 @@ func jobPodNameLabel(conf *config.Global, job *batchv1.Job) (string, string) { } return key, job.Name } + +func checkNamespaceExists(ctx context.Context, conf *config.Global) { + if _, err := conf.Client.Namespaces().Get(ctx, conf.Namespace, metav1.GetOptions{}); err != nil { + slog.Warn("Namespace may not exist", "error", err) + } +}