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

Adding ReasonForError usage: #384

Merged
merged 2 commits into from Apr 6, 2019
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
92 changes: 49 additions & 43 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Expand Up @@ -13,7 +13,7 @@

[[constraint]]
name = "k8s.io/apimachinery"
branch = "release-1.8"
branch = "release-1.9"

[[constraint]]
name = "k8s.io/client-go"
Expand Down
38 changes: 30 additions & 8 deletions handlers/secrets.go
Expand Up @@ -54,8 +54,9 @@ func getSecretsHandler(kube typedV1.SecretInterface, w http.ResponseWriter, r *h
selector := fmt.Sprintf("%s=%s", secretLabel, secretLabelValue)
res, err := kube.List(metav1.ListOptions{LabelSelector: selector})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Secrets query error: %v\n", err)
acornies marked this conversation as resolved.
Show resolved Hide resolved
status, reason := processErrorReasons(err)
log.Printf("Secret list error reason: %s, %v\n", reason, err)
w.WriteHeader(status)
return
}

Expand Down Expand Up @@ -86,8 +87,9 @@ func createSecretHandler(kube typedV1.SecretInterface, namespace string, w http.
}
_, err = kube.Create(secret)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Secret create error: %v\n", err)
status, reason := processErrorReasons(err)
log.Printf("Secret create error reason: %s, %v\n", reason, err)
w.WriteHeader(status)
return
}
log.Printf("Secret %s create\n", secret.GetName())
Expand Down Expand Up @@ -115,8 +117,9 @@ func replaceSecretHandler(kube typedV1.SecretInterface, namespace string, w http
secret.StringData = newSecret.StringData
_, err = kube.Update(secret)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Secret update error: %v\n", err)
status, reason := processErrorReasons(err)
log.Printf("Secret update error reason: %s, %v\n", reason, err)
w.WriteHeader(status)
return
}
log.Printf("Secret %s updated", secret.GetName())
Expand All @@ -136,8 +139,9 @@ func deleteSecretHandler(kube typedV1.SecretInterface, namespace string, w http.
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Secret %s delete error: %v\n", secret.GetName(), err)
status, reason := processErrorReasons(err)
log.Printf("Secret delete error reason: %s, %v\n", reason, err)
w.WriteHeader(status)
return
}
log.Printf("Secret %s deleted\n", secret.GetName())
Expand Down Expand Up @@ -311,3 +315,21 @@ func removeVolumeMount(volumeName string, mounts []apiv1.VolumeMount) []apiv1.Vo
func isNotFound(err error) bool {
return k8serrors.IsNotFound(err) || k8serrors.IsGone(err)
}

// processErrorReasons maps k8serrors.ReasonForError to http status codes
func processErrorReasons(err error) (int, metav1.StatusReason) {
switch {
case k8serrors.ReasonForError(err) == metav1.StatusReasonConflict:
return http.StatusConflict, metav1.StatusReasonConflict
case k8serrors.ReasonForError(err) == metav1.StatusReasonInvalid:
return http.StatusUnprocessableEntity, metav1.StatusReasonInvalid
case k8serrors.ReasonForError(err) == metav1.StatusReasonBadRequest:
return http.StatusBadRequest, metav1.StatusReasonBadRequest
case k8serrors.ReasonForError(err) == metav1.StatusReasonForbidden:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my concern with this approach is that we don't give the reason for the error back to the caller.

We should also be able to test this new method that has been broken out with unit tests when we have a final design on how it is going to work.

I am not super keen on logging in the function. I'd rather see you return the Status Code plus a message that we then chose to log in the calling function - thereby applying Single Responsibility Principle (SRP)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

return http.StatusForbidden, metav1.StatusReasonForbidden
case k8serrors.ReasonForError(err) == metav1.StatusReasonTimeout:
return http.StatusRequestTimeout, metav1.StatusReasonTimeout
default:
return http.StatusInternalServerError, metav1.StatusReasonInternalError
}
}
29 changes: 29 additions & 0 deletions handlers/secrets_test.go
@@ -1,12 +1,17 @@
package handlers

import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/openfaas/faas/gateway/requests"
apiv1 "k8s.io/api/core/v1"
v1beta1 "k8s.io/api/extensions/v1beta1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func Test_UpdateSecrets_DoesNotAddVolumeIfRequestSecretsIsNil(t *testing.T) {
Expand Down Expand Up @@ -226,6 +231,30 @@ func Test_UpdateSecrets_RemovesSecretsVolumeIfRequestSecretsIsEmptyOrNil(t *test
validateEmptySecretVolumesAndMounts(t, deployment)
}

func Test_DefaultErrorReason(t *testing.T) {
err := errors.New("A default error mapping")
status, reason := processErrorReasons(err)
if status != http.StatusInternalServerError {
t.Errorf("Unexpected default status code: %d", status)
}
if reason != metav1.StatusReasonInternalError {
t.Errorf("Unexpected default reason: %s", reason)
}
}

func Test_ConflictErrorReason(t *testing.T) {
gv := schema.GroupResource{Group: "testing", Resource: "testing"}
statusError := k8serrors.NewConflict(gv, "conflict_test", errors.New("A test error for confict"))

status, reason := processErrorReasons(statusError)
if status != http.StatusConflict {
t.Errorf("Unexpected default status code: %d", status)
}
if !k8serrors.IsConflict(statusError) {
t.Errorf("Unexpected default reason: %s", reason)
}
}

func validateEmptySecretVolumesAndMounts(t *testing.T, deployment *v1beta1.Deployment) {
numVolumes := len(deployment.Spec.Template.Spec.Volumes)
if numVolumes != 0 {
Expand Down
11 changes: 8 additions & 3 deletions vendor/github.com/PuerkitoBio/purell/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/github.com/PuerkitoBio/purell/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/PuerkitoBio/purell/purell.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.