...

Source file src/k8s.io/kubectl/pkg/cmd/config/rename_context.go

Documentation: k8s.io/kubectl/pkg/cmd/config

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/client-go/tools/clientcmd"
    27  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    28  	"k8s.io/kubectl/pkg/util/completion"
    29  	"k8s.io/kubectl/pkg/util/i18n"
    30  	"k8s.io/kubectl/pkg/util/templates"
    31  )
    32  
    33  // RenameContextOptions contains the options for running the rename-context cli command.
    34  type RenameContextOptions struct {
    35  	configAccess clientcmd.ConfigAccess
    36  	contextName  string
    37  	newName      string
    38  }
    39  
    40  const (
    41  	renameContextUse = "rename-context CONTEXT_NAME NEW_NAME"
    42  )
    43  
    44  var (
    45  	renameContextShort = i18n.T("Rename a context from the kubeconfig file")
    46  
    47  	renameContextLong = templates.LongDesc(i18n.T(`
    48  		Renames a context from the kubeconfig file.
    49  
    50  		CONTEXT_NAME is the context name that you want to change.
    51  
    52  		NEW_NAME is the new name you want to set.
    53  
    54  		Note: If the context being renamed is the 'current-context', this field will also be updated.`))
    55  
    56  	renameContextExample = templates.Examples(`
    57  		# Rename the context 'old-name' to 'new-name' in your kubeconfig file
    58  		kubectl config rename-context old-name new-name`)
    59  )
    60  
    61  // NewCmdConfigRenameContext creates a command object for the "rename-context" action
    62  func NewCmdConfigRenameContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
    63  	options := &RenameContextOptions{configAccess: configAccess}
    64  
    65  	cmd := &cobra.Command{
    66  		Use:                   renameContextUse,
    67  		DisableFlagsInUseLine: true,
    68  		Short:                 renameContextShort,
    69  		Long:                  renameContextLong,
    70  		Example:               renameContextExample,
    71  		ValidArgsFunction:     completion.ContextCompletionFunc,
    72  		Run: func(cmd *cobra.Command, args []string) {
    73  			cmdutil.CheckErr(options.Complete(cmd, args, out))
    74  			cmdutil.CheckErr(options.Validate())
    75  			cmdutil.CheckErr(options.RunRenameContext(out))
    76  		},
    77  	}
    78  	return cmd
    79  }
    80  
    81  // Complete assigns RenameContextOptions from the args.
    82  func (o *RenameContextOptions) Complete(cmd *cobra.Command, args []string, out io.Writer) error {
    83  	if len(args) != 2 {
    84  		return helpErrorf(cmd, "Unexpected args: %v", args)
    85  	}
    86  
    87  	o.contextName = args[0]
    88  	o.newName = args[1]
    89  	return nil
    90  }
    91  
    92  // Validate makes sure that provided values for command-line options are valid
    93  func (o RenameContextOptions) Validate() error {
    94  	if len(o.newName) == 0 {
    95  		return errors.New("You must specify a new non-empty context name")
    96  	}
    97  	return nil
    98  }
    99  
   100  // RunRenameContext performs the execution for 'config rename-context' sub command
   101  func (o RenameContextOptions) RunRenameContext(out io.Writer) error {
   102  	config, err := o.configAccess.GetStartingConfig()
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	configFile := o.configAccess.GetDefaultFilename()
   108  	if o.configAccess.IsExplicitFile() {
   109  		configFile = o.configAccess.GetExplicitFile()
   110  	}
   111  
   112  	context, exists := config.Contexts[o.contextName]
   113  	if !exists {
   114  		return fmt.Errorf("cannot rename the context %q, it's not in %s", o.contextName, configFile)
   115  	}
   116  
   117  	_, newExists := config.Contexts[o.newName]
   118  	if newExists {
   119  		return fmt.Errorf("cannot rename the context %q, the context %q already exists in %s", o.contextName, o.newName, configFile)
   120  	}
   121  
   122  	config.Contexts[o.newName] = context
   123  	delete(config.Contexts, o.contextName)
   124  
   125  	if config.CurrentContext == o.contextName {
   126  		config.CurrentContext = o.newName
   127  	}
   128  
   129  	if err := clientcmd.ModifyConfig(o.configAccess, *config, true); err != nil {
   130  		return err
   131  	}
   132  
   133  	fmt.Fprintf(out, "Context %q renamed to %q.\n", o.contextName, o.newName)
   134  	return nil
   135  }
   136  

View as plain text