...

Source file src/k8s.io/kubectl/pkg/cmd/config/get_users.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  	"sort"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/cli-runtime/pkg/genericiooptions"
    26  	"k8s.io/client-go/tools/clientcmd"
    27  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    28  	"k8s.io/kubectl/pkg/util/i18n"
    29  	"k8s.io/kubectl/pkg/util/templates"
    30  )
    31  
    32  var (
    33  	getUsersExample = templates.Examples(`
    34  		# List the users that kubectl knows about
    35  		kubectl config get-users`)
    36  )
    37  
    38  // GetUsersOptions holds the data needed to run the command
    39  type GetUsersOptions struct {
    40  	configAccess clientcmd.ConfigAccess
    41  
    42  	genericiooptions.IOStreams
    43  }
    44  
    45  // NewGetUsersOptions creates the options for the command
    46  func NewGetUsersOptions(ioStreams genericiooptions.IOStreams, configAccess clientcmd.ConfigAccess) *GetUsersOptions {
    47  	return &GetUsersOptions{
    48  		configAccess: configAccess,
    49  		IOStreams:    ioStreams,
    50  	}
    51  }
    52  
    53  // NewCmdConfigGetUsers creates a command object for the "get-users" action, which
    54  // lists all users defined in the kubeconfig.
    55  func NewCmdConfigGetUsers(streams genericiooptions.IOStreams, configAccess clientcmd.ConfigAccess) *cobra.Command {
    56  	o := NewGetUsersOptions(streams, configAccess)
    57  
    58  	cmd := &cobra.Command{
    59  		Use:     "get-users",
    60  		Short:   i18n.T("Display users defined in the kubeconfig"),
    61  		Long:    i18n.T("Display users defined in the kubeconfig."),
    62  		Example: getUsersExample,
    63  		Run: func(cmd *cobra.Command, args []string) {
    64  			cmdutil.CheckErr(o.Run())
    65  		},
    66  	}
    67  
    68  	return cmd
    69  }
    70  
    71  // Run performs the command
    72  func (o *GetUsersOptions) Run() error {
    73  	config, err := o.configAccess.GetStartingConfig()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	users := make([]string, 0, len(config.AuthInfos))
    79  	for user := range config.AuthInfos {
    80  		users = append(users, user)
    81  	}
    82  	sort.Strings(users)
    83  
    84  	fmt.Fprintf(o.Out, "NAME\n")
    85  	for _, user := range users {
    86  		fmt.Fprintf(o.Out, "%s\n", user)
    87  	}
    88  
    89  	return nil
    90  }
    91  

View as plain text