Skip to content

Commit

Permalink
Add flag to opt out of existence check when switching namespaces
Browse files Browse the repository at this point in the history
We have a tool that chooses a cluster and namespace based on runtime data, and
by the time we have decided what cluster and namespace to connect to, we already
know that the namespace exists in the cluster.

We want to extend this tool to support multiple switching mechanisms (rather than
just `kubectl config use-context` and `kubectl config set-context`), and but the
existence check for the namespace is prohibitively slow on some of our clusters.
To avoid paying that penalty, this change adds a flag to `switch ns` which allows
the user to opt out of the existence check.

```
λ switch ns does-not-exist
namespace "does-not-exist" not foundexit status 1

λ switch ns does-not-exist --check-existence=false
```
  • Loading branch information
tomasaschan committed Oct 28, 2024
1 parent a6981cf commit 3787809
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
8 changes: 5 additions & 3 deletions cmd/switcher/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -49,14 +50,15 @@ 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,
}
)

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)
}
26 changes: 14 additions & 12 deletions pkg/subcommands/ns/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3787809

Please sign in to comment.