1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/patches.go

91 lines
2.5 KiB
Go
Raw Normal View History

2019-05-21 18:27:56 +03:00
package engine
2019-03-15 17:58:16 +02:00
import (
"encoding/json"
"fmt"
2019-03-15 17:58:16 +02:00
jsonpatch "github.com/evanphx/json-patch"
2019-05-21 11:00:09 -07:00
kubepolicy "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
"github.com/nirmata/kyverno/pkg/result"
2019-03-15 17:58:16 +02:00
)
// PatchBytes stands for []byte
2019-03-15 17:58:16 +02:00
type PatchBytes []byte
// ProcessPatches Returns array from separate patches that can be applied to the document
// Returns error ONLY in case when creation of resource should be denied.
2019-06-05 16:44:53 +03:00
func ProcessPatches(rule kubepolicy.Rule, resource []byte) ([]PatchBytes, result.RuleApplicationResult) {
res := result.NewRuleApplicationResult(rule.Name)
if rule.Mutation == nil || len(rule.Mutation.Patches) == 0 {
return nil, res
}
if len(resource) == 0 {
res.AddMessagef("Source document for patching is empty")
res.Reason = result.Failed
return nil, res
2019-03-15 17:58:16 +02:00
}
var allPatches []PatchBytes
2019-05-20 13:02:55 -07:00
patchedDocument := resource
2019-06-05 16:44:53 +03:00
for i, patch := range rule.Mutation.Patches {
patchRaw, err := json.Marshal(patch)
if err != nil {
2019-03-15 17:58:16 +02:00
}
2019-05-20 13:02:55 -07:00
patchedDocument, err = applyPatch(patchedDocument, patchRaw)
if err != nil {
// TODO: continue on error if one of the patches fails, will add the failure event in such case
if patch.Operation == "remove" {
continue
}
message := fmt.Sprintf("Patch failed: patch number = %d, patch Operation = %s, err: %v", i, patch.Operation, err)
res.Messages = append(res.Messages, message)
continue
2019-03-15 17:58:16 +02:00
}
allPatches = append(allPatches, patchRaw)
2019-03-15 17:58:16 +02:00
}
return allPatches, res
2019-03-15 17:58:16 +02:00
}
// JoinPatches joins array of serialized JSON patches to the single JSONPatch array
2019-03-15 17:58:16 +02:00
func JoinPatches(patches []PatchBytes) PatchBytes {
var result PatchBytes
if len(patches) == 0 {
return result
}
result = append(result, []byte("[\n")...)
for index, patch := range patches {
result = append(result, patch...)
if index != len(patches)-1 {
2019-03-15 17:58:16 +02:00
result = append(result, []byte(",\n")...)
}
}
result = append(result, []byte("\n]")...)
return result
}
// applyPatch applies patch for resource, returns patched resource.
func applyPatch(resource []byte, patchRaw []byte) ([]byte, error) {
patchesList := []PatchBytes{patchRaw}
return ApplyPatches(resource, patchesList)
}
// ApplyPatches patches given resource with given patches and returns patched document
func ApplyPatches(resource []byte, patches []PatchBytes) ([]byte, error) {
joinedPatches := JoinPatches(patches)
patch, err := jsonpatch.DecodePatch(joinedPatches)
2019-03-15 17:58:16 +02:00
if err != nil {
return nil, err
2019-03-15 17:58:16 +02:00
}
patchedDocument, err := patch.Apply(resource)
if err != nil {
return resource, err
}
return patchedDocument, err
2019-03-15 17:58:16 +02:00
}