Skip to content

Commit

Permalink
allow switching project even to users outside self-provisioner role (…
Browse files Browse the repository at this point in the history
…revert)

This reverts commit f7d2755.
  • Loading branch information
stlaz committed Jun 24, 2020
1 parent 7762806 commit 9401cfb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 37 deletions.
30 changes: 5 additions & 25 deletions pkg/cli/login/loginoptions.go
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -267,29 +266,6 @@ func (o *LoginOptions) gatherProjectInfo() error {
return err
}

canRequest, err := loginutil.CanRequestProjects(o.Config, o.DefaultNamespace)
if err != nil {
return err
}
// check if ProjectRequestMessage is set
if !canRequest {
msg := ""
res, err := projectClient.RESTClient().Get().Resource("projectrequests").DoRaw(context.TODO())
if err != nil && res != nil {
// if err != nil, try to extract ProjectRequestMessage from status if set
status := metav1.Status{}
err := json.Unmarshal(res, &status)
if err != nil {
return err
}
if len(status.Details.Causes) != 0 && status.Details.Causes[0].Message != "" {
msg = status.Details.Causes[0].Message
}
// now return redirected error from !canRequest with the extracted ProjectRequestMessage if set
fmt.Fprintf(o.Out, errors.NoProjectsExistMessage(canRequest, msg, o.CommandName))
return nil
}
}
projectsList, err := projectClient.Projects().List(context.TODO(), metav1.ListOptions{})
// if we're running on kube (or likely kube), just set it to "default"
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
Expand Down Expand Up @@ -318,7 +294,11 @@ func (o *LoginOptions) gatherProjectInfo() error {

switch len(projectsItems) {
case 0:
msg := errors.NoProjectsExistMessage(canRequest, "", o.CommandName)
canRequest, err := loginutil.CanRequestProjects(o.Config, o.DefaultNamespace)
if err != nil {
return err
}
msg := errors.NoProjectsExistMessage(canRequest, o.CommandName)
fmt.Fprintf(o.Out, msg)
o.Project = ""

Expand Down
12 changes: 5 additions & 7 deletions pkg/cli/project/project.go
Expand Up @@ -169,16 +169,14 @@ func (o ProjectOptions) Run() error {
currentProject = currentContext.Namespace
}

client, kubeclient, err := o.ClientFn()
if err != nil {
return err
}
if err := client.RESTClient().Get().Resource("projectrequests").Do(context.TODO()).Into(&metav1.Status{}); err != nil {
return err
}
// No argument provided, we will just print info
if len(o.ProjectName) == 0 {
if len(currentProject) > 0 {
client, kubeclient, err := o.ClientFn()
if err != nil {
return err
}

switch err := ConfirmProjectAccess(currentProject, client, kubeclient); {
case kapierrors.IsForbidden(err), kapierrors.IsNotFound(err):
return fmt.Errorf("you do not have rights to view project %q specified in your config or the project doesn't exist", currentProject)
Expand Down
2 changes: 1 addition & 1 deletion pkg/helpers/describe/projectstatus.go
Expand Up @@ -205,7 +205,7 @@ func (d *ProjectStatusDescriber) Describe(namespace, name string) (string, error
// the user has not created any projects, and is therefore using a
// default namespace that they cannot list projects from.
if kapierrors.IsForbidden(err) && len(d.RequestedNamespace) == 0 && len(d.CurrentNamespace) == 0 {
return loginerrors.NoProjectsExistMessage(d.CanRequestProjects, "", d.CommandBaseName), nil
return loginerrors.NoProjectsExistMessage(d.CanRequestProjects, d.CommandBaseName), nil
}
if !kapierrors.IsNotFound(err) {
return "", err
Expand Down
5 changes: 1 addition & 4 deletions pkg/helpers/errors/login.go
Expand Up @@ -52,11 +52,8 @@ func kubeConfigSolution(isExplicitFile bool) string {
}

// NoProjectsExistMessage returns a message indicating that no projects have been created by the current user.
func NoProjectsExistMessage(canRequestProjects bool, projReqMsg, commandName string) string {
func NoProjectsExistMessage(canRequestProjects bool, commandName string) string {
if !canRequestProjects {
if len(projReqMsg) != 0 {
return fmt.Sprintf("%s\n", projReqMsg)
}
return fmt.Sprintf("You don't have any projects. Contact your system administrator to request a project.\n")
}
return fmt.Sprintf(`You don't have any projects. You can try to create a new project, by running
Expand Down

0 comments on commit 9401cfb

Please sign in to comment.