-
Notifications
You must be signed in to change notification settings - Fork 421
NO-JIRA: Display whoami information as JSON or YAML #1324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christianh814
wants to merge
5
commits into
openshift:main
Choose a base branch
from
christianh814:whoami-show-groups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
790d0f4
Added the ability to change the output to YAML or JSON
25e26e6
Merge branch 'openshift:master' into whoami-show-groups
christianh814 3ad9afe
Merge branch 'openshift:main' into whoami-show-groups
christianh814 71ba17f
resolved comments
christianh814 4ee7a2c
Merge branch 'openshift:main' into whoami-show-groups
christianh814 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,8 @@ import ( | |
| v1 "k8s.io/api/authentication/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/cli-runtime/pkg/genericiooptions" | ||
| "k8s.io/cli-runtime/pkg/genericclioptions" | ||
| "k8s.io/cli-runtime/pkg/printers" | ||
| "k8s.io/client-go/kubernetes" | ||
| authenticationv1client "k8s.io/client-go/kubernetes/typed/authentication/v1" | ||
| "k8s.io/client-go/rest" | ||
|
|
@@ -47,21 +48,24 @@ type WhoAmIOptions struct { | |
| KubeClient kubernetes.Interface | ||
| RawConfig api.Config | ||
|
|
||
| ShowToken bool | ||
| ShowContext bool | ||
| ShowServer bool | ||
| ShowConsoleUrl bool | ||
| ShowToken bool | ||
| ShowContext bool | ||
| ShowServer bool | ||
| ShowConsoleUrl bool | ||
| PrintFlags *genericclioptions.PrintFlags | ||
| resourcePrinterFunc printers.ResourcePrinterFunc | ||
|
|
||
| genericiooptions.IOStreams | ||
| genericclioptions.IOStreams | ||
| } | ||
|
|
||
| func NewWhoAmIOptions(streams genericiooptions.IOStreams) *WhoAmIOptions { | ||
| func NewWhoAmIOptions(streams genericclioptions.IOStreams) *WhoAmIOptions { | ||
| return &WhoAmIOptions{ | ||
| IOStreams: streams, | ||
| PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(""), | ||
| IOStreams: streams, | ||
| } | ||
| } | ||
|
|
||
| func NewCmdWhoAmI(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { | ||
| func NewCmdWhoAmI(f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { | ||
| o := NewWhoAmIOptions(streams) | ||
|
|
||
| cmd := &cobra.Command{ | ||
|
|
@@ -80,6 +84,7 @@ func NewCmdWhoAmI(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra | |
| cmd.Flags().BoolVarP(&o.ShowContext, "show-context", "c", o.ShowContext, "Print the current user context name") | ||
| cmd.Flags().BoolVar(&o.ShowServer, "show-server", o.ShowServer, "If true, print the current server's REST API URL") | ||
| cmd.Flags().BoolVar(&o.ShowConsoleUrl, "show-console", o.ShowConsoleUrl, "If true, print the current server's web console URL") | ||
| o.PrintFlags.AddFlags(cmd) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
@@ -122,10 +127,28 @@ func (o *WhoAmIOptions) Complete(f kcmdutil.Factory) error { | |
| o.KubeClient = kubeClient | ||
|
|
||
| o.RawConfig, err = f.ToRawKubeConfigLoader().RawConfig() | ||
| return err | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Setup printer function | ||
| if o.PrintFlags.OutputFlagSpecified() { | ||
| printer, err := o.PrintFlags.ToPrinter() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| o.resourcePrinterFunc = printer.PrintObj | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (o *WhoAmIOptions) Validate() error { | ||
| // Check if output flag is used with other show flags | ||
| if o.PrintFlags.OutputFlagSpecified() && (o.ShowToken || o.ShowContext || o.ShowServer || o.ShowConsoleUrl) { | ||
| return fmt.Errorf("--output cannot be used with --show-token, --show-context, --show-server, or --show-console") | ||
| } | ||
|
|
||
| if o.ShowToken && len(o.ClientConfig.BearerToken) == 0 { | ||
| return fmt.Errorf("no token is currently in use for this session") | ||
| } | ||
|
|
@@ -154,6 +177,17 @@ func (o *WhoAmIOptions) getWebConsoleUrl() (string, error) { | |
| } | ||
|
|
||
| func (o *WhoAmIOptions) Run() error { | ||
| var err error | ||
| o.UserInterface, err = userv1typedclient.NewForConfig(o.ClientConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| o.AuthV1Client, err = authenticationv1client.NewForConfig(o.ClientConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| switch { | ||
| case o.ShowToken: | ||
| fmt.Fprintf(o.Out, "%s\n", o.ClientConfig.BearerToken) | ||
|
|
@@ -171,19 +205,18 @@ func (o *WhoAmIOptions) Run() error { | |
| } | ||
| fmt.Fprintf(o.Out, "%s\n", consoleUrl) | ||
| return nil | ||
| case o.resourcePrinterFunc != nil: | ||
| // If output format is specified, get the user info and print it in the requested format | ||
| me, err := o.WhoAmI() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if err != nil { | ||
| return err | ||
| } | ||
| // Set GroupVersionKind so it's properly displayed | ||
| me.SetGroupVersionKind(userv1.GroupVersion.WithKind("User")) | ||
| return o.resourcePrinterFunc(me, o.Out) | ||
| } | ||
|
|
||
| var err error | ||
| o.UserInterface, err = userv1typedclient.NewForConfig(o.ClientConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| o.AuthV1Client, err = authenticationv1client.NewForConfig(o.ClientConfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Default behavior: just print the username | ||
| _, err = o.WhoAmI() | ||
| return err | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is deprecated.
genericiooptionsneeds to be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But isn't
genericclioptionsused for PrintFlags? I see I can usegenericiooptionsfor IOStream. Both are used right? BecauserollbackandstartBuildalso use this approachThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
genericclioptionshas been deprecated in favor ofgenericiooptions. So theoretically you can usegenericclioptions. However, it is always preferable to use the recommended one (i.e.genericiooptions), since the other one will be removed entirely at some point.