Skip to content

Commit fe545df

Browse files
committed
make login, project, and discovery work against kube with RBAC enabled
1 parent 11d3928 commit fe545df

File tree

6 files changed

+11
-7
lines changed

6 files changed

+11
-7
lines changed

hack/dind-cluster.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ function wait-for-cluster() {
220220
local oc
221221
oc="$(os::build::find-binary oc)"
222222

223+
# wait for healthz to report ok before trying to get nodes
224+
os::util::wait-for-condition "ok" "${oc} get --config=\"${kubeconfig}\" --raw=/healthz" "120"
225+
223226
local msg="${expected_node_count} nodes to report readiness"
224227
local condition="nodes-are-ready ${kubeconfig} ${oc} ${expected_node_count}"
225228
local timeout=120

pkg/cmd/cli/cmd/login/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func whoAmI(clientConfig *restclient.Config) (*api.User, error) {
130130
me, err := client.Users().Get("~")
131131

132132
// if we're talking to kube (or likely talking to kube),
133-
if kerrors.IsNotFound(err) {
133+
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
134134
switch {
135135
case len(clientConfig.BearerToken) > 0:
136136
// the user has already been willing to provide the token on the CLI, so they probably

pkg/cmd/cli/cmd/login/loginoptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (o *LoginOptions) gatherProjectInfo() error {
263263

264264
projectsList, err := oClient.Projects().List(kapi.ListOptions{})
265265
// if we're running on kube (or likely kube), just set it to "default"
266-
if kerrors.IsNotFound(err) {
266+
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
267267
fmt.Fprintf(o.Out, "Using \"default\". You can switch projects with '%s project <projectname>':\n\n", o.CommandName)
268268
o.Project = "default"
269269
return nil

pkg/cmd/cli/cmd/project.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ func (o ProjectOptions) RunProject() error {
281281

282282
func confirmProjectAccess(currentProject string, oClient *client.Client, kClient kclient.Interface) error {
283283
_, projectErr := oClient.Projects().Get(currentProject)
284-
if !kapierrors.IsNotFound(projectErr) {
284+
if !kapierrors.IsNotFound(projectErr) && !kapierrors.IsForbidden(projectErr) {
285285
return projectErr
286286
}
287287

288-
// at this point we know the error is a not found, but we'll test namespaces just in case we're running on kube
288+
// at this point we know the error is a not found or forbidden, but we'll test namespaces just in case we're running on kube
289289
if _, err := kClient.Namespaces().Get(currentProject); err == nil {
290290
return nil
291291
}
@@ -299,7 +299,8 @@ func getProjects(oClient *client.Client, kClient kclient.Interface) ([]api.Proje
299299
if err == nil {
300300
return projects.Items, nil
301301
}
302-
if err != nil && !kapierrors.IsNotFound(err) {
302+
// if this is kube with authorization enabled, this endpoint will be forbidden. OpenShift allows this for everyone.
303+
if err != nil && !(kapierrors.IsNotFound(err) || kapierrors.IsForbidden(err)) {
303304
return nil, err
304305
}
305306

pkg/cmd/cli/config/smart_merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func getUserPartOfNickname(clientCfg *restclient.Config) (string, error) {
6363
return "", err
6464
}
6565
userInfo, err := client.Users().Get("~")
66-
if kerrors.IsNotFound(err) {
66+
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
6767
// if we're talking to kube (or likely talking to kube), take a best guess consistent with login
6868
switch {
6969
case len(clientCfg.BearerToken) > 0:

pkg/cmd/util/clientcmd/negotiate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func negotiateVersion(client *kclient.Client, config *restclient.Config, request
3636
// Get server versions
3737
serverGVs, err := serverAPIVersions(client, "/oapi")
3838
if err != nil {
39-
if errors.IsNotFound(err) {
39+
if errors.IsNotFound(err) || errors.IsForbidden(err) {
4040
glog.V(4).Infof("Server path /oapi was not found, returning the requested group version %v", preferredGV)
4141
return preferredGV, nil
4242
}

0 commit comments

Comments
 (0)