use_context.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "errors"
  16. "fmt"
  17. "io"
  18. "github.com/spf13/cobra"
  19. "k8s.io/client-go/tools/clientcmd"
  20. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  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. var (
  26. useContextExample = templates.Examples(`
  27. # Use the context for the minikube cluster
  28. kubectl config use-context minikube`)
  29. )
  30. type useContextOptions struct {
  31. configAccess clientcmd.ConfigAccess
  32. contextName string
  33. }
  34. // NewCmdConfigUseContext returns a Command instance for 'config use-context' sub command
  35. func NewCmdConfigUseContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  36. options := &useContextOptions{configAccess: configAccess}
  37. cmd := &cobra.Command{
  38. Use: "use-context CONTEXT_NAME",
  39. DisableFlagsInUseLine: true,
  40. Short: i18n.T("Sets the current-context in a kubeconfig file"),
  41. Aliases: []string{"use"},
  42. Long: `Sets the current-context in a kubeconfig file`,
  43. Example: useContextExample,
  44. Run: func(cmd *cobra.Command, args []string) {
  45. cmdutil.CheckErr(options.complete(cmd))
  46. cmdutil.CheckErr(options.run())
  47. fmt.Fprintf(out, "Switched to context %q.\n", options.contextName)
  48. },
  49. }
  50. return cmd
  51. }
  52. func (o useContextOptions) run() error {
  53. config, err := o.configAccess.GetStartingConfig()
  54. if err != nil {
  55. return err
  56. }
  57. err = o.validate(config)
  58. if err != nil {
  59. return err
  60. }
  61. config.CurrentContext = o.contextName
  62. return clientcmd.ModifyConfig(o.configAccess, *config, true)
  63. }
  64. func (o *useContextOptions) complete(cmd *cobra.Command) error {
  65. endingArgs := cmd.Flags().Args()
  66. if len(endingArgs) != 1 {
  67. return helpErrorf(cmd, "Unexpected args: %v", endingArgs)
  68. }
  69. o.contextName = endingArgs[0]
  70. return nil
  71. }
  72. func (o useContextOptions) validate(config *clientcmdapi.Config) error {
  73. if len(o.contextName) == 0 {
  74. return errors.New("empty context names are not allowed")
  75. }
  76. for name := range config.Contexts {
  77. if name == o.contextName {
  78. return nil
  79. }
  80. }
  81. return fmt.Errorf("no context exists with the name: %q", o.contextName)
  82. }