forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
whoami.go
35 lines (27 loc) · 1.07 KB
/
whoami.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package project
import (
userapi "github.com/openshift/origin/pkg/user/apis/user"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
restclient "k8s.io/client-go/rest"
)
func WhoAmI(clientConfig *restclient.Config) (*userapi.User, error) {
client, err := userclient.NewForConfig(clientConfig)
me, err := client.User().Users().Get("~", metav1.GetOptions{})
// if we're talking to kube (or likely talking to kube),
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
switch {
case len(clientConfig.BearerToken) > 0:
// the user has already been willing to provide the token on the CLI, so they probably
// don't mind using it again if they switch to and from this user
return &userapi.User{ObjectMeta: metav1.ObjectMeta{Name: clientConfig.BearerToken}}, nil
case len(clientConfig.Username) > 0:
return &userapi.User{ObjectMeta: metav1.ObjectMeta{Name: clientConfig.Username}}, nil
}
}
if err != nil {
return nil, err
}
return me, nil
}