Commit bf766f48 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #42538 from adohe/context_warning

Automatic merge from submit-queue (batch tested with PRs 44119, 42538, 43802, 42336, 43396) print warning when delete current context mirror update to kubectl. fix #42012 @smarterclayton ptal. **Release Note**: ``` print warning when delete current context ```
parents a635aedb 8ebc6e91
......@@ -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
}
......
......@@ -33,14 +33,14 @@ 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)
},
}
......@@ -48,7 +48,7 @@ func NewCmdConfigDeleteContext(out io.Writer, configAccess clientcmd.ConfigAcces
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
......@@ -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 {
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment