Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/webhook/admission/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func ValidationResponse(allowed bool, reason string) Response {
return resp
}

// ValidationResponseFromStatus returns a response for admitting a request with provided Status object.
func ValidationResponseFromStatus(allowed bool, status *metav1.Status) Response {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know that this needs to be a public helper method at the moment, especially since it's basically just constructing an object from the listed fields.

resp := Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: allowed,
Result: status,
},
}
return resp
}

// PatchResponseFromRaw takes 2 byte arrays and returns a new response with json patch.
// The original object should be passed in as raw bytes to avoid the roundtripping problem
// described in https://github.com/kubernetes-sigs/kubebuilder/issues/510.
Expand Down
26 changes: 26 additions & 0 deletions pkg/webhook/admission/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net/http"

"k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -67,7 +69,13 @@ func (h *validatingHandler) Handle(ctx context.Context, req Request) Response {
}

err = obj.ValidateCreate()

if err != nil {

isStatusError, status := isStatusError(&err)
if isStatusError {
return ValidationResponseFromStatus(false, status)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
isStatusError, status := isStatusError(&err)
if isStatusError {
return ValidationResponseFromStatus(false, status)
var statusError errors.StatusError
if goerrors.As(&statusError) {
return ...
}

Copy link
Contributor

Choose a reason for hiding this comment

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

(the other nice thing about this is that if the status error is wrapped in such a way that it's intended to be unwrapped, we'll still pull the status)

Copy link
Contributor

Choose a reason for hiding this comment

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

is there any corresponding interface for status-returning errors in the k8s api errors package that we should be asserting on? (basically, is there a pattern for "my custom error returns status information)

}
return Denied(err.Error())
}
}
Expand All @@ -85,7 +93,12 @@ func (h *validatingHandler) Handle(ctx context.Context, req Request) Response {
}

err = obj.ValidateUpdate(oldObj)

if err != nil {
isStatusError, status := isStatusError(&err)
if isStatusError {
return ValidationResponseFromStatus(false, status)
}
return Denied(err.Error())
}
}
Expand All @@ -100,9 +113,22 @@ func (h *validatingHandler) Handle(ctx context.Context, req Request) Response {

err = obj.ValidateDelete()
if err != nil {
isStatusError, status := isStatusError(&err)
if isStatusError {
return ValidationResponseFromStatus(false, status)
}
return Denied(err.Error())
}
}

return Allowed("")
}

func isStatusError(err *error) (bool, *metav1.Status) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this method is a bit weirdly named -- I'd just expect it to return a bool.

The entire thing should be replacable with errors.As (see above)

statusError, isStatusError := (*err).(*errors.StatusError)
if isStatusError {
return true, &statusError.ErrStatus
}

return false, nil
}