delete_cluster.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/kubernetes/pkg/client/unversioned/clientcmd"
  19. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  20. )
  21. func NewCmdConfigDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  22. cmd := &cobra.Command{
  23. Use: "delete-cluster NAME",
  24. Short: "Delete the specified cluster from the kubeconfig",
  25. Run: func(cmd *cobra.Command, args []string) {
  26. err := runDeleteCluster(out, configAccess, cmd)
  27. cmdutil.CheckErr(err)
  28. },
  29. }
  30. return cmd
  31. }
  32. func runDeleteCluster(out io.Writer, configAccess clientcmd.ConfigAccess, cmd *cobra.Command) error {
  33. config, err := configAccess.GetStartingConfig()
  34. if err != nil {
  35. return err
  36. }
  37. args := cmd.Flags().Args()
  38. if len(args) != 1 {
  39. cmd.Help()
  40. return nil
  41. }
  42. configFile := configAccess.GetDefaultFilename()
  43. if configAccess.IsExplicitFile() {
  44. configFile = configAccess.GetExplicitFile()
  45. }
  46. name := args[0]
  47. _, ok := config.Clusters[name]
  48. if !ok {
  49. return fmt.Errorf("cannot delete cluster %s, not in %s", name, configFile)
  50. }
  51. delete(config.Clusters, name)
  52. if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
  53. return err
  54. }
  55. fmt.Fprintf(out, "deleted cluster %s from %s", name, configFile)
  56. return nil
  57. }