Skip to content

Commit

Permalink
Merge pull request #146 from tomasaschan/make-existence-check-on-name…
Browse files Browse the repository at this point in the history
…space-switch-optional

Add flag to opt out of existence check when switching namespaces
  • Loading branch information
danielfoehrKn authored Oct 31, 2024
2 parents b0f508e + 894a1cd commit a6f3400
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 a6f3400

Please sign in to comment.