From a5404b6548ff9f30d6dac826703e7c44a5de9b59 Mon Sep 17 00:00:00 2001 From: "m.nabokikh" Date: Thu, 29 Sep 2022 11:50:33 +0200 Subject: [PATCH] Split the API error in two Signed-off-by: m.nabokikh Kubernetes-commit: e6e670624b5fbe5493f48796f54e60d505727314 --- pkg/cmd/auth/whoami.go | 19 ++++++++++++++----- pkg/cmd/auth/whoami_test.go | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/auth/whoami.go b/pkg/cmd/auth/whoami.go index daca2adea..5ef8c7237 100644 --- a/pkg/cmd/auth/whoami.go +++ b/pkg/cmd/auth/whoami.go @@ -139,19 +139,28 @@ func NewCmdWhoAmI(restClientGetter genericclioptions.RESTClientGetter, streams g return cmd } -var notEnabledErr = fmt.Errorf( - "the selfsubjectreviews API is not enabled in the cluster or you do not have permission to call\n" + - "enable APISelfSubjectReview feature gate and authentication.k8s.io/v1alpha1 API") +var ( + notEnabledErr = fmt.Errorf( + "the selfsubjectreviews API is not enabled in the cluster\n" + + "enable APISelfSubjectReview feature gate and authentication.k8s.io/v1alpha1 API") + + forbiddenErr = fmt.Errorf( + "the selfsubjectreviews API is not enabled in the cluster or you do not have permission to call it") +) // Run prints all user attributes. func (o WhoAmIOptions) Run() error { sar := &authenticationv1alpha1.SelfSubjectReview{} response, err := o.authClient.SelfSubjectReviews().Create(context.TODO(), sar, metav1.CreateOptions{}) if err != nil { - if errors.IsForbidden(err) || errors.IsNotFound(err) { + switch { + case errors.IsForbidden(err): + return forbiddenErr + case errors.IsNotFound(err): return notEnabledErr + default: + return err } - return err } return o.resourcePrinterFunc(response, o.Out) } diff --git a/pkg/cmd/auth/whoami_test.go b/pkg/cmd/auth/whoami_test.go index 461f7e775..7a44f155d 100644 --- a/pkg/cmd/auth/whoami_test.go +++ b/pkg/cmd/auth/whoami_test.go @@ -107,7 +107,7 @@ func TestWhoAmIRun(t *testing.T) { serverErr: errors.NewForbidden( corev1.Resource("selfsubjectreviews"), "foo", fmt.Errorf("error"), ), - expectedError: notEnabledErr, + expectedError: forbiddenErr, expectedBodyStrings: []string{}, }, {