1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

mutation failure to not block resource creation

This commit is contained in:
Shuting Zhao 2020-01-15 21:46:58 -08:00
parent 8bba25bc7c
commit 7e59e8e484
4 changed files with 21 additions and 21 deletions

View file

@ -374,7 +374,6 @@ func processSubtree(overlay interface{}, path string, op string) ([]byte, error)
// check the patch // check the patch
_, err := jsonpatch.DecodePatch([]byte("[" + patchStr + "]")) _, err := jsonpatch.DecodePatch([]byte("[" + patchStr + "]"))
if err != nil { if err != nil {
glog.V(3).Info(err)
return nil, fmt.Errorf("Failed to make '%s' patch from an overlay '%s' for path %s, err: %v", op, value, path, err) return nil, fmt.Errorf("Failed to make '%s' patch from an overlay '%s' for path %s, err: %v", op, value, path, err)
} }

View file

@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
) )
// isResponseSuccesful return true if all responses are successful
func isResponseSuccesful(engineReponses []response.EngineResponse) bool { func isResponseSuccesful(engineReponses []response.EngineResponse) bool {
for _, er := range engineReponses { for _, er := range engineReponses {
if !er.IsSuccesful() { if !er.IsSuccesful() {

View file

@ -15,7 +15,8 @@ import (
) )
// HandleMutation handles mutating webhook admission request // HandleMutation handles mutating webhook admission request
func (ws *WebhookServer) HandleMutation(request *v1beta1.AdmissionRequest, resource unstructured.Unstructured, policies []kyverno.ClusterPolicy, roles, clusterRoles []string) (bool, []byte, string) { // return generated patches
func (ws *WebhookServer) HandleMutation(request *v1beta1.AdmissionRequest, resource unstructured.Unstructured, policies []kyverno.ClusterPolicy, roles, clusterRoles []string) []byte {
glog.V(4).Infof("Receive request in mutating webhook: Kind=%s, Namespace=%s Name=%s UID=%s patchOperation=%s", glog.V(4).Infof("Receive request in mutating webhook: Kind=%s, Namespace=%s Name=%s UID=%s patchOperation=%s",
request.Kind.Kind, request.Namespace, request.Name, request.UID, request.Operation) request.Kind.Kind, request.Namespace, request.Name, request.UID, request.Operation)
@ -108,13 +109,20 @@ func (ws *WebhookServer) HandleMutation(request *v1beta1.AdmissionRequest, resou
events := generateEvents(engineResponses, (request.Operation == v1beta1.Update)) events := generateEvents(engineResponses, (request.Operation == v1beta1.Update))
ws.eventGen.Add(events...) ws.eventGen.Add(events...)
if isResponseSuccesful(engineResponses) { sendStat(false)
sendStat(false)
patch := engineutils.JoinPatches(patches) // debug info
return true, patch, "" if len(patches) != 0 {
glog.V(3).Infof("Patches generated for %s/%s/%s, operation=%v:\n %v",
resource.GetKind(), resource.GetNamespace(), resource.GetName(), request.Operation, string(engineutils.JoinPatches(patches)))
} }
sendStat(true) // if any of the policies fails, print out the error
glog.Errorf("Failed to mutate the resource, %s\n", getErrorMsg(engineResponses)) if !isResponseSuccesful(engineResponses) {
return false, nil, getErrorMsg(engineResponses) glog.Errorf("Failed to mutate the resource, report as violation: %s\n", getErrorMsg(engineResponses))
}
// patches holds all the successful patches
// if no patch is created, it returns nil
return engineutils.JoinPatches(patches)
} }

View file

@ -235,23 +235,15 @@ func (ws *WebhookServer) handleAdmissionRequest(request *v1beta1.AdmissionReques
} }
// MUTATION // MUTATION
ok, patches, msg := ws.HandleMutation(request, resource, policies, roles, clusterRoles) // mutation failure should not block the resource creation
if !ok { // any mutation failure is reported as the violation
glog.V(4).Infof("Deny admission request: %v/%s/%s", request.Kind, request.Namespace, request.Name) patches := ws.HandleMutation(request, resource, policies, roles, clusterRoles)
return &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Status: "Failure",
Message: msg,
},
}
}
// patch the resource with patches before handling validation rules // patch the resource with patches before handling validation rules
patchedResource := processResourceWithPatches(patches, request.Object.Raw) patchedResource := processResourceWithPatches(patches, request.Object.Raw)
// VALIDATION // VALIDATION
ok, msg = ws.HandleValidation(request, policies, patchedResource, roles, clusterRoles) ok, msg := ws.HandleValidation(request, policies, patchedResource, roles, clusterRoles)
if !ok { if !ok {
glog.V(4).Infof("Deny admission request: %v/%s/%s", request.Kind, request.Namespace, request.Name) glog.V(4).Infof("Deny admission request: %v/%s/%s", request.Kind, request.Namespace, request.Name)
return &v1beta1.AdmissionResponse{ return &v1beta1.AdmissionResponse{