Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubens: add force option for ns switch #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions cmd/kubens/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,30 @@ func parseArgs(argv []string) Op {
}
return ListOp{}
}

if len(argv) == 1 {
var isForceSwitch bool
isForceSwitch = false
ArgsLen := len(argv)
if ArgsLen == 1 || ArgsLen == 2 {
v := argv[0]
if v == "--help" || v == "-h" {
return HelpOp{}
}
if v == "--current" || v == "-c" {
return CurrentOp{}
}
if strings.HasPrefix(v, "-") && v != "-" {
if v == "--force" || v == "-f" {
isForceSwitch = true
}
if strings.HasPrefix(v, "-") && v != "-" && !isForceSwitch {
return UnsupportedOp{Err: fmt.Errorf("unsupported option '%s'", v)}
}
return SwitchOp{Target: argv[0]}
if isForceSwitch {
if ArgsLen == 1 {
return UnsupportedOp{Err: fmt.Errorf("force option needs namespace name")}
}
return SwitchOp{Target: argv[1], IsForce: isForceSwitch}
}
return SwitchOp{Target: argv[0], IsForce: isForceSwitch}
}
return UnsupportedOp{Err: fmt.Errorf("too many arguments")}
}
10 changes: 8 additions & 2 deletions cmd/kubens/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ func Test_parseArgs_new(t *testing.T) {
want: CurrentOp{}},
{name: "switch by name",
args: []string{"foo"},
want: SwitchOp{Target: "foo"}},
want: SwitchOp{Target: "foo", IsForce: false}},
{name: "switch by swap",
args: []string{"-"},
want: SwitchOp{Target: "-"}},
want: SwitchOp{Target: "-", IsForce: false}},
{name: "switch by name force",
args: []string{"-f", "foo"},
want: SwitchOp{Target: "foo", IsForce: true}},
{name: "switch by name force long form",
args: []string{"--force", "foo"},
want: SwitchOp{Target: "foo", IsForce: true}},
{name: "unrecognized flag",
args: []string{"-x"},
want: UnsupportedOp{Err: fmt.Errorf("unsupported option '-x'")}},
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubens/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
if choice == "" {
return errors.New("you did not choose any of the options")
}
name, err := switchNamespace(kc, choice)
name, err := switchNamespace(kc, choice, false)
if err != nil {
return errors.Wrap(err, "failed to switch namespace")
}
Expand Down
21 changes: 12 additions & 9 deletions cmd/kubens/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type SwitchOp struct {
Target string // '-' for back and forth, or NAME
Target string // '-' for back and forth, or NAME
IsForce bool
}

func (s SwitchOp) Run(_, stderr io.Writer) error {
Expand All @@ -21,15 +22,15 @@ func (s SwitchOp) Run(_, stderr io.Writer) error {
return errors.Wrap(err, "kubeconfig error")
}

toNS, err := switchNamespace(kc, s.Target)
toNS, err := switchNamespace(kc, s.Target, s.IsForce)
if err != nil {
return err
}
err = printer.Success(stderr, "Active namespace is %q", toNS)
return err
}

func switchNamespace(kc *kubeconfig.Kubeconfig, ns string) (string, error) {
func switchNamespace(kc *kubeconfig.Kubeconfig, ns string, isForce bool) (string, error) {
ctx := kc.GetCurrentContext()
if ctx == "" {
return "", errors.New("current-context is not set")
Expand All @@ -52,12 +53,14 @@ func switchNamespace(kc *kubeconfig.Kubeconfig, ns string) (string, error) {
ns = prev
}

ok, err := namespaceExists(kc, ns)
if err != nil {
return "", errors.Wrap(err, "failed to query if namespace exists (is cluster accessible?)")
}
if !ok {
return "", errors.Errorf("no namespace exists with name %q", ns)
if !isForce {
ok, err := namespaceExists(kc, ns)
if err != nil {
return "", errors.Wrap(err, "failed to query if namespace exists (is cluster accessible?)")
}
if !ok {
return "", errors.Errorf("no namespace exists with name %q", ns)
}
}

if err := kc.SetNamespace(ctx, ns); err != nil {
Expand Down