Skip to content
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

Bug 1802580: return ProjectRequestMessage if it is set with login,project cmds #406

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions pkg/cli/login/loginoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -266,6 +267,29 @@ 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())
sallyom marked this conversation as resolved.
Show resolved Hide resolved
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
}
sallyom marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -294,11 +318,7 @@ func (o *LoginOptions) gatherProjectInfo() error {

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

Expand Down
12 changes: 7 additions & 5 deletions pkg/cli/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ 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):
return fmt.Errorf("you do not have rights to view project %q", currentProject)
Expand Down
2 changes: 1 addition & 1 deletion pkg/helpers/describe/projectstatus.go
Original file line number Diff line number Diff line change
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: 4 additions & 1 deletion pkg/helpers/errors/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ func kubeConfigSolution(isExplicitFile bool) string {
}

// NoProjectsExistMessage returns a message indicating that no projects have been created by the current user.
func NoProjectsExistMessage(canRequestProjects bool, commandName string) string {
func NoProjectsExistMessage(canRequestProjects bool, projReqMsg, 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