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

print warning when delete current context #42538

Merged
merged 1 commit into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewCmdConfig(pathOptions *clientcmd.PathOptions, out, errOut io.Writer) *co
cmd.AddCommand(NewCmdConfigGetContexts(out, pathOptions))
cmd.AddCommand(NewCmdConfigGetClusters(out, pathOptions))
cmd.AddCommand(NewCmdConfigDeleteCluster(out, pathOptions))
cmd.AddCommand(NewCmdConfigDeleteContext(out, pathOptions))
cmd.AddCommand(NewCmdConfigDeleteContext(out, errOut, pathOptions))

return cmd
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/kubectl/cmd/config/delete_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ var (
kubectl config delete-context minikube`)
)

func NewCmdConfigDeleteContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
func NewCmdConfigDeleteContext(out, errOut io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
cmd := &cobra.Command{
Use: "delete-context NAME",
Short: i18n.T("Delete the specified context from the kubeconfig"),
Long: "Delete the specified context from the kubeconfig",
Example: delete_context_example,
Run: func(cmd *cobra.Command, args []string) {
err := runDeleteContext(out, configAccess, cmd)
err := runDeleteContext(out, errOut, configAccess, cmd)
cmdutil.CheckErr(err)
},
}

return cmd
}

func runDeleteContext(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
func runDeleteContext(out, errOut io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
config, err := configAccess.GetStartingConfig()
if err != nil {
return err
Expand All @@ -71,6 +71,10 @@ func runDeleteContext(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *c
return fmt.Errorf("cannot delete context %s, not in %s", name, configFile)
}

if config.CurrentContext == name {
fmt.Fprint(errOut, "warning: this removed your active context, use \"kubectl config use-context\" to select a different one\n")
}

delete(config.Contexts, name)

if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/cmd/config/delete_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func (test deleteContextTest) run(t *testing.T) {
pathOptions.EnvVar = ""

buf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigDeleteContext(buf, pathOptions)
errBuf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigDeleteContext(buf, errBuf, pathOptions)
cmd.SetArgs([]string{test.contextToDelete})
if err := cmd.Execute(); err != nil {
t.Fatalf("unexpected error executing command: %v", err)
Expand Down