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

Add message when no contexts are found in kubeconfig #259

Open
wants to merge 7 commits 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
7 changes: 7 additions & 0 deletions cmd/kubectx/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ type InteractiveSwitchOp struct {
func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
// parse kubeconfig just to see if it can be loaded
kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)

if err := kc.Parse(); err != nil {
if cmdutil.IsNotFoundErr(err) {
printer.Warning(stderr, "kubeconfig file not found")
return nil
}
return errors.Wrap(err, "kubeconfig error")
}

ctxs := kc.ContextNames()
if ctxs == nil {
err := printer.Warning(stderr, "No kubectl context found")
return errors.Wrap(err, "kubeconfig error")
}
kc.Close()

cmd := exec.Command("fzf", "--ansi", "--no-preview")
Expand Down
4 changes: 4 additions & 0 deletions cmd/kubectx/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (_ ListOp) Run(stdout, stderr io.Writer) error {
}

ctxs := kc.ContextNames()
if ctxs == nil {
err := printer.Warning(stderr, "No kubectl context found")
return errors.Wrap(err, "kubeconfig error")
}
natsort.Sort(ctxs)

cur := kc.GetCurrentContext()
Expand Down
10 changes: 9 additions & 1 deletion test/kubectx.bats
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ load common
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "" ]]
[[ "$output" = "warning: No kubectl context found" ]]
}

@test "delete several contexts including a non existent one" {
Expand Down Expand Up @@ -242,3 +242,11 @@ load common
run ${COMMAND} -c
[ "$status" -ne 0 ]
}

@test "display msg on empty context" {
use_config config3
run ${COMMAND}
echo "$output"
[ "$status" -eq 0 ]
[[ "$output" = "warning: No kubectl context found" ]]
}
15 changes: 15 additions & 0 deletions test/testdata/config3
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# config with no context

apiVersion: v1
clusters:
- cluster:
server: ""
name: cluster1
contexts:
- context:
current-context: ""
kind: Config
preferences: {}
users:
- name: user1
user: {}