rename_context.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2017 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. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  21. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  22. )
  23. // RenameContextOptions contains the options for running the rename-context cli command.
  24. type RenameContextOptions struct {
  25. configAccess clientcmd.ConfigAccess
  26. contextName string
  27. newName string
  28. }
  29. const (
  30. renameContextUse = "rename-context CONTEXT_NAME NEW_NAME"
  31. renameContextShort = "Renames a context from the kubeconfig file."
  32. )
  33. var (
  34. renameContextLong = templates.LongDesc(`
  35. Renames a context from the kubeconfig file.
  36. CONTEXT_NAME is the context name that you wish to change.
  37. NEW_NAME is the new name you wish to set.
  38. Note: In case the context being renamed is the 'current-context', this field will also be updated.`)
  39. renameContextExample = templates.Examples(`
  40. # Rename the context 'old-name' to 'new-name' in your kubeconfig file
  41. kubectl config rename-context old-name new-name`)
  42. )
  43. // NewCmdConfigRenameContext creates a command object for the "rename-context" action
  44. func NewCmdConfigRenameContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  45. options := &RenameContextOptions{configAccess: configAccess}
  46. cmd := &cobra.Command{
  47. Use: renameContextUse,
  48. DisableFlagsInUseLine: true,
  49. Short: renameContextShort,
  50. Long: renameContextLong,
  51. Example: renameContextExample,
  52. Run: func(cmd *cobra.Command, args []string) {
  53. cmdutil.CheckErr(options.Complete(cmd, args, out))
  54. cmdutil.CheckErr(options.Validate())
  55. cmdutil.CheckErr(options.RunRenameContext(out))
  56. },
  57. }
  58. return cmd
  59. }
  60. // Complete assigns RenameContextOptions from the args.
  61. func (o *RenameContextOptions) Complete(cmd *cobra.Command, args []string, out io.Writer) error {
  62. if len(args) != 2 {
  63. return helpErrorf(cmd, "Unexpected args: %v", args)
  64. }
  65. o.contextName = args[0]
  66. o.newName = args[1]
  67. return nil
  68. }
  69. // Validate makes sure that provided values for command-line options are valid
  70. func (o RenameContextOptions) Validate() error {
  71. if len(o.newName) == 0 {
  72. return errors.New("You must specify a new non-empty context name")
  73. }
  74. return nil
  75. }
  76. // RunRenameContext performs the execution for 'config rename-context' sub command
  77. func (o RenameContextOptions) RunRenameContext(out io.Writer) error {
  78. config, err := o.configAccess.GetStartingConfig()
  79. if err != nil {
  80. return err
  81. }
  82. configFile := o.configAccess.GetDefaultFilename()
  83. if o.configAccess.IsExplicitFile() {
  84. configFile = o.configAccess.GetExplicitFile()
  85. }
  86. context, exists := config.Contexts[o.contextName]
  87. if !exists {
  88. return fmt.Errorf("cannot rename the context %q, it's not in %s", o.contextName, configFile)
  89. }
  90. _, newExists := config.Contexts[o.newName]
  91. if newExists {
  92. return fmt.Errorf("cannot rename the context %q, the context %q already exists in %s", o.contextName, o.newName, configFile)
  93. }
  94. config.Contexts[o.newName] = context
  95. delete(config.Contexts, o.contextName)
  96. if config.CurrentContext == o.contextName {
  97. config.CurrentContext = o.newName
  98. }
  99. if err := clientcmd.ModifyConfig(o.configAccess, *config, true); err != nil {
  100. return err
  101. }
  102. fmt.Fprintf(out, "Context %q renamed to %q.\n", o.contextName, o.newName)
  103. return nil
  104. }