...

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

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

     1  /*
     2  Copyright 2014 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  	"reflect"
    24  
    25  	"github.com/spf13/cobra"
    26  	"k8s.io/kubectl/pkg/util/templates"
    27  
    28  	"k8s.io/client-go/tools/clientcmd"
    29  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    30  	"k8s.io/kubectl/pkg/util/i18n"
    31  )
    32  
    33  type unsetOptions struct {
    34  	configAccess clientcmd.ConfigAccess
    35  	propertyName string
    36  }
    37  
    38  var (
    39  	unsetLong = templates.LongDesc(i18n.T(`
    40  	Unset an individual value in a kubeconfig file.
    41  
    42  	PROPERTY_NAME is a dot delimited name where each token represents either an attribute name or a map key.  Map keys may not contain dots.`))
    43  
    44  	unsetExample = templates.Examples(`
    45  		# Unset the current-context
    46  		kubectl config unset current-context
    47  
    48  		# Unset namespace in foo context
    49  		kubectl config unset contexts.foo.namespace`)
    50  )
    51  
    52  // NewCmdConfigUnset returns a Command instance for 'config unset' sub command
    53  func NewCmdConfigUnset(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
    54  	options := &unsetOptions{configAccess: configAccess}
    55  
    56  	cmd := &cobra.Command{
    57  		Use:                   "unset PROPERTY_NAME",
    58  		DisableFlagsInUseLine: true,
    59  		Short:                 i18n.T("Unset an individual value in a kubeconfig file"),
    60  		Long:                  unsetLong,
    61  		Example:               unsetExample,
    62  		Run: func(cmd *cobra.Command, args []string) {
    63  			cmdutil.CheckErr(options.complete(cmd, args))
    64  			cmdutil.CheckErr(options.run(out))
    65  
    66  		},
    67  	}
    68  
    69  	return cmd
    70  }
    71  
    72  func (o unsetOptions) run(out io.Writer) error {
    73  	err := o.validate()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	config, err := o.configAccess.GetStartingConfig()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	steps, err := newNavigationSteps(o.propertyName)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	err = modifyConfig(reflect.ValueOf(config), steps, "", true, true)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if err := clientcmd.ModifyConfig(o.configAccess, *config, false); err != nil {
    93  		return err
    94  	}
    95  	if _, err := fmt.Fprintf(out, "Property %q unset.\n", o.propertyName); err != nil {
    96  		return err
    97  	}
    98  	return nil
    99  }
   100  
   101  func (o *unsetOptions) complete(cmd *cobra.Command, args []string) error {
   102  	if len(args) != 1 {
   103  		return helpErrorf(cmd, "Unexpected args: %v", args)
   104  	}
   105  
   106  	o.propertyName = args[0]
   107  	return nil
   108  }
   109  
   110  func (o unsetOptions) validate() error {
   111  	if len(o.propertyName) == 0 {
   112  		return errors.New("you must specify a property")
   113  	}
   114  
   115  	return nil
   116  }
   117  

View as plain text