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
```
This commit is contained in:
Kubernetes Submit Queue 2017-04-07 14:09:36 -07:00 committed by GitHub
commit bf766f48a6
3 changed files with 10 additions and 5 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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)