...

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

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

     1  /*
     2  Copyright 2020 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  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"k8s.io/cli-runtime/pkg/genericiooptions"
    25  	"k8s.io/client-go/tools/clientcmd"
    26  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    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  var (
    34  	deleteUserExample = templates.Examples(`
    35  		# Delete the minikube user
    36  		kubectl config delete-user minikube`)
    37  )
    38  
    39  // DeleteUserOptions holds the data needed to run the command
    40  type DeleteUserOptions struct {
    41  	user string
    42  
    43  	configAccess clientcmd.ConfigAccess
    44  	config       *clientcmdapi.Config
    45  	configFile   string
    46  
    47  	genericiooptions.IOStreams
    48  }
    49  
    50  // NewDeleteUserOptions creates the options for the command
    51  func NewDeleteUserOptions(ioStreams genericiooptions.IOStreams, configAccess clientcmd.ConfigAccess) *DeleteUserOptions {
    52  	return &DeleteUserOptions{
    53  		configAccess: configAccess,
    54  		IOStreams:    ioStreams,
    55  	}
    56  }
    57  
    58  // NewCmdConfigDeleteUser returns a Command instance for 'config delete-user' sub command
    59  func NewCmdConfigDeleteUser(streams genericiooptions.IOStreams, configAccess clientcmd.ConfigAccess) *cobra.Command {
    60  	o := NewDeleteUserOptions(streams, configAccess)
    61  
    62  	cmd := &cobra.Command{
    63  		Use:                   "delete-user NAME",
    64  		DisableFlagsInUseLine: true,
    65  		Short:                 i18n.T("Delete the specified user from the kubeconfig"),
    66  		Long:                  i18n.T("Delete the specified user from the kubeconfig."),
    67  		Example:               deleteUserExample,
    68  		ValidArgsFunction:     completion.UserCompletionFunc,
    69  		Run: func(cmd *cobra.Command, args []string) {
    70  			cmdutil.CheckErr(o.Complete(cmd, args))
    71  			cmdutil.CheckErr(o.Validate())
    72  			cmdutil.CheckErr(o.Run())
    73  		},
    74  	}
    75  
    76  	return cmd
    77  }
    78  
    79  // Complete sets up the command to run
    80  func (o *DeleteUserOptions) Complete(cmd *cobra.Command, args []string) error {
    81  	config, err := o.configAccess.GetStartingConfig()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	o.config = config
    86  
    87  	if len(args) != 1 {
    88  		return cmdutil.UsageErrorf(cmd, "user to delete is required")
    89  	}
    90  	o.user = args[0]
    91  
    92  	configFile := o.configAccess.GetDefaultFilename()
    93  	if o.configAccess.IsExplicitFile() {
    94  		configFile = o.configAccess.GetExplicitFile()
    95  	}
    96  	o.configFile = configFile
    97  
    98  	return nil
    99  }
   100  
   101  // Validate ensures the command has enough info to run
   102  func (o *DeleteUserOptions) Validate() error {
   103  	_, ok := o.config.AuthInfos[o.user]
   104  	if !ok {
   105  		return fmt.Errorf("cannot delete user %s, not in %s", o.user, o.configFile)
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  // Run performs the command
   112  func (o *DeleteUserOptions) Run() error {
   113  	delete(o.config.AuthInfos, o.user)
   114  
   115  	if err := clientcmd.ModifyConfig(o.configAccess, *o.config, true); err != nil {
   116  		return err
   117  	}
   118  
   119  	fmt.Fprintf(o.Out, "deleted user %s from %s\n", o.user, o.configFile)
   120  
   121  	return nil
   122  }
   123  

View as plain text