...
1
16
17 package config
18
19 import (
20 "fmt"
21 "io"
22
23 "github.com/spf13/cobra"
24 "k8s.io/client-go/tools/clientcmd"
25 cmdutil "k8s.io/kubectl/pkg/cmd/util"
26 "k8s.io/kubectl/pkg/util/completion"
27 "k8s.io/kubectl/pkg/util/i18n"
28 "k8s.io/kubectl/pkg/util/templates"
29 )
30
31 var (
32 deleteClusterExample = templates.Examples(`
33 # Delete the minikube cluster
34 kubectl config delete-cluster minikube`)
35 )
36
37
38 func NewCmdConfigDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
39 cmd := &cobra.Command{
40 Use: "delete-cluster NAME",
41 DisableFlagsInUseLine: true,
42 Short: i18n.T("Delete the specified cluster from the kubeconfig"),
43 Long: i18n.T("Delete the specified cluster from the kubeconfig."),
44 Example: deleteClusterExample,
45 ValidArgsFunction: completion.ClusterCompletionFunc,
46 Run: func(cmd *cobra.Command, args []string) {
47 cmdutil.CheckErr(runDeleteCluster(out, configAccess, cmd))
48 },
49 }
50
51 return cmd
52 }
53
54 func runDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
55 config, err := configAccess.GetStartingConfig()
56 if err != nil {
57 return err
58 }
59
60 args := cmd.Flags().Args()
61 if len(args) != 1 {
62 cmd.Help()
63 return nil
64 }
65
66 configFile := configAccess.GetDefaultFilename()
67 if configAccess.IsExplicitFile() {
68 configFile = configAccess.GetExplicitFile()
69 }
70
71 name := args[0]
72 _, ok := config.Clusters[name]
73 if !ok {
74 return fmt.Errorf("cannot delete cluster %s, not in %s", name, configFile)
75 }
76
77 delete(config.Clusters, name)
78
79 if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
80 return err
81 }
82
83 fmt.Fprintf(out, "deleted cluster %s from %s\n", name, configFile)
84
85 return nil
86 }
87
View as plain text