delete_cluster.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "fmt"
  16. "io"
  17. "github.com/spf13/cobra"
  18. "k8s.io/client-go/tools/clientcmd"
  19. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  20. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  21. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  22. )
  23. var (
  24. deleteClusterExample = templates.Examples(`
  25. # Delete the minikube cluster
  26. kubectl config delete-cluster minikube`)
  27. )
  28. // NewCmdConfigDeleteCluster returns a Command instance for 'config delete-cluster' sub command
  29. func NewCmdConfigDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  30. cmd := &cobra.Command{
  31. Use: "delete-cluster NAME",
  32. DisableFlagsInUseLine: true,
  33. Short: i18n.T("Delete the specified cluster from the kubeconfig"),
  34. Long: "Delete the specified cluster from the kubeconfig",
  35. Example: deleteClusterExample,
  36. Run: func(cmd *cobra.Command, args []string) {
  37. cmdutil.CheckErr(runDeleteCluster(out, configAccess, cmd))
  38. },
  39. }
  40. return cmd
  41. }
  42. func runDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
  43. config, err := configAccess.GetStartingConfig()
  44. if err != nil {
  45. return err
  46. }
  47. args := cmd.Flags().Args()
  48. if len(args) != 1 {
  49. cmd.Help()
  50. return nil
  51. }
  52. configFile := configAccess.GetDefaultFilename()
  53. if configAccess.IsExplicitFile() {
  54. configFile = configAccess.GetExplicitFile()
  55. }
  56. name := args[0]
  57. _, ok := config.Clusters[name]
  58. if !ok {
  59. return fmt.Errorf("cannot delete cluster %s, not in %s", name, configFile)
  60. }
  61. delete(config.Clusters, name)
  62. if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
  63. return err
  64. }
  65. fmt.Fprintf(out, "deleted cluster %s from %s\n", name, configFile)
  66. return nil
  67. }