1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 17:37:12 +00:00
kyverno/pkg/engine/mutation/patches.go
2019-05-13 18:17:28 -07:00

67 lines
1.7 KiB
Go

package mutation
import (
"encoding/json"
"errors"
jsonpatch "github.com/evanphx/json-patch"
kubepolicy "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
)
type PatchBytes []byte
// Test patches on given document according to given sets.
// Returns array from separate patches that can be applied to the document
// Returns error ONLY in case when creation of resource should be denied.
func ProcessPatches(patches []kubepolicy.Patch, resource []byte) ([]PatchBytes, error) {
if len(resource) == 0 {
return nil, errors.New("Source document for patching is empty")
}
var appliedPatches []PatchBytes
for _, patch := range patches {
patchRaw, err := json.Marshal(patch)
if err != nil {
return nil, err
}
_, err = applyPatch(resource, patchRaw)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patchRaw)
}
return appliedPatches, nil
}
// JoinPatches joins array of serialized JSON patches to the single JSONPatch array
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) {
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) {
patchRaw = append([]byte{'['}, patchRaw...) // push [ forward
patchRaw = append(patchRaw, ']') // push ] back
patch, err := jsonpatch.DecodePatch(patchRaw)
if err != nil {
return nil, err
}
return patch.Apply(resource)
}