diff --git a/cmd/switcher/namespace.go b/cmd/switcher/namespace.go index 95beaa26..b3739af6 100644 --- a/cmd/switcher/namespace.go +++ b/cmd/switcher/namespace.go @@ -19,7 +19,8 @@ import ( ) var ( - namespaceCommand = &cobra.Command{ + checkExistence bool = true + namespaceCommand = &cobra.Command{ Use: "namespace", Aliases: []string{"ns"}, Short: "Change the current namespace", @@ -33,7 +34,7 @@ var ( }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 1 && len(args[0]) > 0 { - return ns.SwitchToNamespace(args[0], getKubeconfigPathFromFlag()) + return ns.SwitchToNamespace(args[0], getKubeconfigPathFromFlag(), checkExistence) } return ns.SwitchNamespace(getKubeconfigPathFromFlag(), stateDirectory, noIndex) @@ -49,7 +50,7 @@ var ( return nil, cobra.ShellCompDirectiveNoFileComp }, RunE: func(cmd *cobra.Command, args []string) error { - return ns.SwitchToNamespace("default", getKubeconfigPathFromFlag()) + return ns.SwitchToNamespace("default", getKubeconfigPathFromFlag(), false) }, SilenceErrors: true, } @@ -57,6 +58,7 @@ var ( func init() { setCommonFlags(namespaceCommand) + namespaceCommand.Flags().BoolVar(&checkExistence, "check-existence", true, "Check if the namespace exists before switching to it (default true)") rootCommand.AddCommand(namespaceCommand) rootCommand.AddCommand(unsetNamespaceCommand) } diff --git a/pkg/subcommands/ns/ns.go b/pkg/subcommands/ns/ns.go index b23c73fe..a93a4465 100644 --- a/pkg/subcommands/ns/ns.go +++ b/pkg/subcommands/ns/ns.go @@ -54,26 +54,28 @@ var ( ) // SwitchToNamespace takes a target namespace and - given that the namespace exists - sets it on the current kubeconfig file -func SwitchToNamespace(targetNamespace, kubeconfigPathFromFlag string) error { +func SwitchToNamespace(targetNamespace, kubeconfigPathFromFlag string, checkExistence bool) error { kubeconfigPath, err := getKubeconfigPath(kubeconfigPathFromFlag) if err != nil { return err } - c, err := getClient(kubeconfigPath) - if err != nil { - return fmt.Errorf("failed to retrieve current namespaces: %v", err) - } + if checkExistence { + c, err := getClient(kubeconfigPath) + if err != nil { + return fmt.Errorf("failed to retrieve current namespaces: %v", err) + } - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() - ns := corev1.Namespace{} - if err := c.Get(ctx, client.ObjectKey{Name: targetNamespace}, &ns); err != nil { - if apierrors.IsNotFound(err) { - return fmt.Errorf("namespace %q not found", targetNamespace) + ns := corev1.Namespace{} + if err := c.Get(ctx, client.ObjectKey{Name: targetNamespace}, &ns); err != nil { + if apierrors.IsNotFound(err) { + return fmt.Errorf("namespace %q not found", targetNamespace) + } + return fmt.Errorf("failed to find namespace %q: %v", targetNamespace, err) } - return fmt.Errorf("failed to find namespace %q: %v", targetNamespace, err) } kubeconfig, err := kubeconfigutil.NewKubeconfigForPath(kubeconfigPath)