config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2014 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. "path"
  17. "strconv"
  18. "github.com/spf13/cobra"
  19. "k8s.io/cli-runtime/pkg/genericclioptions"
  20. "k8s.io/client-go/tools/clientcmd"
  21. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  22. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  23. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  24. )
  25. // NewCmdConfig creates a command object for the "config" action, and adds all child commands to it.
  26. func NewCmdConfig(f cmdutil.Factory, pathOptions *clientcmd.PathOptions, streams genericclioptions.IOStreams) *cobra.Command {
  27. if len(pathOptions.ExplicitFileFlag) == 0 {
  28. pathOptions.ExplicitFileFlag = clientcmd.RecommendedConfigPathFlag
  29. }
  30. cmd := &cobra.Command{
  31. Use: "config SUBCOMMAND",
  32. DisableFlagsInUseLine: true,
  33. Short: i18n.T("Modify kubeconfig files"),
  34. Long: templates.LongDesc(`
  35. Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"
  36. The loading order follows these rules:
  37. 1. If the --` + pathOptions.ExplicitFileFlag + ` flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
  38. 2. If $` + pathOptions.EnvVar + ` environment variable is set, then it is used as a list of paths (normal path delimiting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
  39. 3. Otherwise, ` + path.Join("${HOME}", pathOptions.GlobalFileSubpath) + ` is used and no merging takes place.`),
  40. Run: cmdutil.DefaultSubCommandRun(streams.ErrOut),
  41. }
  42. // file paths are common to all sub commands
  43. cmd.PersistentFlags().StringVar(&pathOptions.LoadingRules.ExplicitPath, pathOptions.ExplicitFileFlag, pathOptions.LoadingRules.ExplicitPath, "use a particular kubeconfig file")
  44. // TODO(juanvallejo): update all subcommands to work with genericclioptions.IOStreams
  45. cmd.AddCommand(NewCmdConfigView(f, streams, pathOptions))
  46. cmd.AddCommand(NewCmdConfigSetCluster(streams.Out, pathOptions))
  47. cmd.AddCommand(NewCmdConfigSetAuthInfo(streams.Out, pathOptions))
  48. cmd.AddCommand(NewCmdConfigSetContext(streams.Out, pathOptions))
  49. cmd.AddCommand(NewCmdConfigSet(streams.Out, pathOptions))
  50. cmd.AddCommand(NewCmdConfigUnset(streams.Out, pathOptions))
  51. cmd.AddCommand(NewCmdConfigCurrentContext(streams.Out, pathOptions))
  52. cmd.AddCommand(NewCmdConfigUseContext(streams.Out, pathOptions))
  53. cmd.AddCommand(NewCmdConfigGetContexts(streams, pathOptions))
  54. cmd.AddCommand(NewCmdConfigGetClusters(streams.Out, pathOptions))
  55. cmd.AddCommand(NewCmdConfigDeleteCluster(streams.Out, pathOptions))
  56. cmd.AddCommand(NewCmdConfigDeleteContext(streams.Out, streams.ErrOut, pathOptions))
  57. cmd.AddCommand(NewCmdConfigRenameContext(streams.Out, pathOptions))
  58. return cmd
  59. }
  60. func toBool(propertyValue string) (bool, error) {
  61. boolValue := false
  62. if len(propertyValue) != 0 {
  63. var err error
  64. boolValue, err = strconv.ParseBool(propertyValue)
  65. if err != nil {
  66. return false, err
  67. }
  68. }
  69. return boolValue, nil
  70. }
  71. func helpErrorf(cmd *cobra.Command, format string, args ...interface{}) error {
  72. cmd.Help()
  73. msg := fmt.Sprintf(format, args...)
  74. return fmt.Errorf("%s", msg)
  75. }