@@ -4,19 +4,25 @@ import (
44 "fmt"
55
66 "github.com/spf13/cobra"
7- "k8s.io/client-go/rest"
8- "k8s.io/client-go/tools/clientcmd/api"
97
8+ "k8s.io/apimachinery/pkg/api/errors"
109 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110 "k8s.io/cli-runtime/pkg/genericclioptions"
11+ "k8s.io/client-go/kubernetes"
12+ "k8s.io/client-go/rest"
13+ "k8s.io/client-go/tools/clientcmd/api"
1214 kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1315 "k8s.io/kubernetes/pkg/kubectl/util/templates"
1416
1517 userv1 "github.com/openshift/api/user/v1"
1618 userv1typedclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
1719)
1820
19- const WhoAmIRecommendedCommandName = "whoami"
21+ const (
22+ WhoAmIRecommendedCommandName = "whoami"
23+ openShiftConfigManagedNamespaceName = "openshift-config-managed"
24+ consolePublicConfigMap = "console-public"
25+ )
2026
2127var whoamiLong = templates .LongDesc (`
2228 Show information about the current session
@@ -29,11 +35,13 @@ type WhoAmIOptions struct {
2935 UserInterface userv1typedclient.UserV1Interface
3036
3137 ClientConfig * rest.Config
38+ KubeClient kubernetes.Interface
3239 RawConfig api.Config
3340
34- ShowToken bool
35- ShowContext bool
36- ShowServer bool
41+ ShowToken bool
42+ ShowContext bool
43+ ShowServer bool
44+ ShowConsoleUrl bool
3745
3846 genericclioptions.IOStreams
3947}
@@ -61,6 +69,7 @@ func NewCmdWhoAmI(name, fullName string, f kcmdutil.Factory, streams genericclio
6169 cmd .Flags ().BoolVarP (& o .ShowToken , "show-token" , "t" , o .ShowToken , "Print the token the current session is using. This will return an error if you are using a different form of authentication." )
6270 cmd .Flags ().BoolVarP (& o .ShowContext , "show-context" , "c" , o .ShowContext , "Print the current user context name" )
6371 cmd .Flags ().BoolVar (& o .ShowServer , "show-server" , o .ShowServer , "If true, print the current server's REST API URL" )
72+ cmd .Flags ().BoolVar (& o .ShowConsoleUrl , "show-console" , o .ShowConsoleUrl , "If true, print the current server's web console URL" )
6473
6574 return cmd
6675}
@@ -76,11 +85,18 @@ func (o WhoAmIOptions) WhoAmI() (*userv1.User, error) {
7685
7786func (o * WhoAmIOptions ) Complete (f kcmdutil.Factory ) error {
7887 var err error
88+
7989 o .ClientConfig , err = f .ToRESTConfig ()
8090 if err != nil {
8191 return err
8292 }
8393
94+ kubeClient , err := kubernetes .NewForConfig (o .ClientConfig )
95+ if err != nil {
96+ return err
97+ }
98+ o .KubeClient = kubeClient
99+
84100 o .RawConfig , err = f .ToRawKubeConfigLoader ().RawConfig ()
85101 return err
86102}
@@ -96,18 +112,41 @@ func (o *WhoAmIOptions) Validate() error {
96112 return nil
97113}
98114
115+ func (o * WhoAmIOptions ) getWebConsoleUrl () (string , error ) {
116+ consolePublicConfig , err := o .KubeClient .CoreV1 ().ConfigMaps (openShiftConfigManagedNamespaceName ).Get (consolePublicConfigMap , metav1.GetOptions {})
117+ // This means the command was run against 3.x server
118+ if errors .IsNotFound (err ) {
119+ return o .ClientConfig .Host , nil
120+ }
121+ if err != nil {
122+ return "" , fmt .Errorf ("unable to determine console location: %v" , err )
123+ }
124+
125+ consoleUrl , exists := consolePublicConfig .Data ["consoleURL" ]
126+ if ! exists {
127+ return "" , fmt .Errorf ("unable to determine console location from the cluster" )
128+ }
129+ return consoleUrl , nil
130+ }
131+
99132func (o * WhoAmIOptions ) Run () error {
100- if o .ShowToken {
133+ switch {
134+ case o .ShowToken :
101135 fmt .Fprintf (o .Out , "%s\n " , o .ClientConfig .BearerToken )
102136 return nil
103- }
104- if o .ShowContext {
137+ case o .ShowContext :
105138 fmt .Fprintf (o .Out , "%s\n " , o .RawConfig .CurrentContext )
106139 return nil
107- }
108- if o .ShowServer {
140+ case o .ShowServer :
109141 fmt .Fprintf (o .Out , "%s\n " , o .ClientConfig .Host )
110142 return nil
143+ case o .ShowConsoleUrl :
144+ consoleUrl , err := o .getWebConsoleUrl ()
145+ if err != nil {
146+ return err
147+ }
148+ fmt .Fprintf (o .Out , "%s\n " , consoleUrl )
149+ return nil
111150 }
112151
113152 var err error
0 commit comments