mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
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:
commit
bf766f48a6
@ -63,7 +63,7 @@ func NewCmdConfig(pathOptions *clientcmd.PathOptions, out, errOut io.Writer) *co
|
|||||||
cmd.AddCommand(NewCmdConfigGetContexts(out, pathOptions))
|
cmd.AddCommand(NewCmdConfigGetContexts(out, pathOptions))
|
||||||
cmd.AddCommand(NewCmdConfigGetClusters(out, pathOptions))
|
cmd.AddCommand(NewCmdConfigGetClusters(out, pathOptions))
|
||||||
cmd.AddCommand(NewCmdConfigDeleteCluster(out, pathOptions))
|
cmd.AddCommand(NewCmdConfigDeleteCluster(out, pathOptions))
|
||||||
cmd.AddCommand(NewCmdConfigDeleteContext(out, pathOptions))
|
cmd.AddCommand(NewCmdConfigDeleteContext(out, errOut, pathOptions))
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -33,14 +33,14 @@ var (
|
|||||||
kubectl config delete-context minikube`)
|
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{
|
cmd := &cobra.Command{
|
||||||
Use: "delete-context NAME",
|
Use: "delete-context NAME",
|
||||||
Short: i18n.T("Delete the specified context from the kubeconfig"),
|
Short: i18n.T("Delete the specified context from the kubeconfig"),
|
||||||
Long: "Delete the specified context from the kubeconfig",
|
Long: "Delete the specified context from the kubeconfig",
|
||||||
Example: delete_context_example,
|
Example: delete_context_example,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
err := runDeleteContext(out, configAccess, cmd)
|
err := runDeleteContext(out, errOut, configAccess, cmd)
|
||||||
cmdutil.CheckErr(err)
|
cmdutil.CheckErr(err)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ func NewCmdConfigDeleteContext(out io.Writer, configAccess clientcmd.ConfigAcces
|
|||||||
return cmd
|
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()
|
config, err := configAccess.GetStartingConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
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)
|
delete(config.Contexts, name)
|
||||||
|
|
||||||
if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
|
if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
|
||||||
|
@ -65,7 +65,8 @@ func (test deleteContextTest) run(t *testing.T) {
|
|||||||
pathOptions.EnvVar = ""
|
pathOptions.EnvVar = ""
|
||||||
|
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
cmd := NewCmdConfigDeleteContext(buf, pathOptions)
|
errBuf := bytes.NewBuffer([]byte{})
|
||||||
|
cmd := NewCmdConfigDeleteContext(buf, errBuf, pathOptions)
|
||||||
cmd.SetArgs([]string{test.contextToDelete})
|
cmd.SetArgs([]string{test.contextToDelete})
|
||||||
if err := cmd.Execute(); err != nil {
|
if err := cmd.Execute(); err != nil {
|
||||||
t.Fatalf("unexpected error executing command: %v", err)
|
t.Fatalf("unexpected error executing command: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user