2019-05-08 10:01:41 -07:00
|
|
|
package mutation
|
2019-03-15 17:58:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-05-15 11:45:16 -07:00
|
|
|
"log"
|
2019-03-15 17:58:16 +02:00
|
|
|
|
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
2019-05-13 21:27:47 +03:00
|
|
|
kubepolicy "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
2019-03-15 17:58:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type PatchBytes []byte
|
|
|
|
|
2019-05-15 11:45:16 -07:00
|
|
|
// ProcessPatches Returns array from separate patches that can be applied to the document
|
2019-03-19 14:16:09 +02:00
|
|
|
// Returns error ONLY in case when creation of resource should be denied.
|
2019-05-13 21:27:47 +03:00
|
|
|
func ProcessPatches(patches []kubepolicy.Patch, resource []byte) ([]PatchBytes, error) {
|
|
|
|
if len(resource) == 0 {
|
2019-03-15 17:58:16 +02:00
|
|
|
return nil, errors.New("Source document for patching is empty")
|
|
|
|
}
|
2019-05-13 21:27:47 +03:00
|
|
|
|
2019-03-15 17:58:16 +02:00
|
|
|
var appliedPatches []PatchBytes
|
2019-05-15 11:45:16 -07:00
|
|
|
for i, patch := range patches {
|
2019-05-13 21:27:47 +03:00
|
|
|
patchRaw, err := json.Marshal(patch)
|
|
|
|
if err != nil {
|
2019-03-15 17:58:16 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
_, err = applyPatch(resource, patchRaw)
|
|
|
|
if err != nil {
|
2019-05-15 11:45:16 -07:00
|
|
|
// TODO: continue on error if one of the patches fails, will add the failure event in such case
|
|
|
|
log.Printf("Patch failed: patch number = %d, patch Operation = %s, err: %v", i, patch.Operation, err)
|
|
|
|
continue
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
2019-05-13 21:27:47 +03:00
|
|
|
|
|
|
|
appliedPatches = append(appliedPatches, patchRaw)
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
|
|
|
return appliedPatches, nil
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03: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...)
|
2019-05-15 18:53:45 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
// ApplyPatch applies patch for resource, returns patched resource.
|
|
|
|
func applyPatch(resource []byte, patchRaw []byte) ([]byte, error) {
|
2019-03-15 17:58:16 +02:00
|
|
|
patchRaw = append([]byte{'['}, patchRaw...) // push [ forward
|
|
|
|
patchRaw = append(patchRaw, ']') // push ] back
|
2019-05-13 21:27:47 +03:00
|
|
|
|
2019-03-15 17:58:16 +02:00
|
|
|
patch, err := jsonpatch.DecodePatch(patchRaw)
|
|
|
|
if err != nil {
|
2019-05-13 21:27:47 +03:00
|
|
|
return nil, err
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
return patch.Apply(resource)
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|