2019-05-21 18:27:56 +03:00
|
|
|
package engine
|
2019-03-15 17:58:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-06-25 18:16:02 -07:00
|
|
|
"errors"
|
2019-08-09 16:55:43 -07:00
|
|
|
"reflect"
|
2019-03-15 17:58:16 +02:00
|
|
|
|
2019-06-28 17:11:19 -07:00
|
|
|
"github.com/golang/glog"
|
|
|
|
|
2019-03-15 17:58:16 +02:00
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
2019-08-09 16:55:43 -07:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
|
2019-03-15 17:58:16 +02:00
|
|
|
)
|
|
|
|
|
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-08-14 19:00:37 -07:00
|
|
|
func processPatches(rule kyverno.Rule, resource []byte) (allPatches [][]byte, errs []error) {
|
2019-07-01 12:16:12 -07:00
|
|
|
if len(resource) == 0 {
|
2019-06-25 18:16:02 -07:00
|
|
|
errs = append(errs, errors.New("Source document for patching is empty"))
|
|
|
|
return nil, errs
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
2019-08-09 16:55:43 -07:00
|
|
|
if reflect.DeepEqual(rule.Mutation, kyverno.Mutation{}) {
|
2019-07-01 12:16:12 -07:00
|
|
|
errs = append(errs, errors.New("No Mutation rules defined"))
|
|
|
|
return nil, errs
|
|
|
|
}
|
2019-05-20 13:02:55 -07:00
|
|
|
patchedDocument := resource
|
2019-06-25 18:16:02 -07:00
|
|
|
for _, patch := range rule.Mutation.Patches {
|
2019-05-13 21:27:47 +03:00
|
|
|
patchRaw, err := json.Marshal(patch)
|
|
|
|
if err != nil {
|
2019-06-25 18:16:02 -07:00
|
|
|
errs = append(errs, err)
|
|
|
|
continue
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 13:02:55 -07:00
|
|
|
patchedDocument, err = applyPatch(patchedDocument, patchRaw)
|
2019-06-28 17:11:19 -07:00
|
|
|
// TODO: continue on error if one of the patches fails, will add the failure event in such case
|
|
|
|
if patch.Operation == "remove" {
|
|
|
|
glog.Info(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-05-13 21:27:47 +03:00
|
|
|
if err != nil {
|
2019-06-25 18:16:02 -07:00
|
|
|
errs = append(errs, err)
|
2019-05-15 11:45:16 -07:00
|
|
|
continue
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
2019-06-05 13:43:07 +03:00
|
|
|
allPatches = append(allPatches, patchRaw)
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
2019-06-25 18:16:02 -07:00
|
|
|
return allPatches, errs
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 21:27:47 +03:00
|
|
|
// JoinPatches joins array of serialized JSON patches to the single JSONPatch array
|
2019-07-05 15:20:43 -07:00
|
|
|
func JoinPatches(patches [][]byte) []byte {
|
|
|
|
var result []byte
|
2019-03-15 17:58:16 +02:00
|
|
|
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-06-05 13:43:07 +03:00
|
|
|
// applyPatch applies patch for resource, returns patched resource.
|
2019-05-13 21:27:47 +03:00
|
|
|
func applyPatch(resource []byte, patchRaw []byte) ([]byte, error) {
|
2019-07-05 15:20:43 -07:00
|
|
|
patchesList := [][]byte{patchRaw}
|
2019-06-05 13:43:07 +03:00
|
|
|
return ApplyPatches(resource, patchesList)
|
|
|
|
}
|
2019-05-13 21:27:47 +03:00
|
|
|
|
2019-06-05 13:43:07 +03:00
|
|
|
// ApplyPatches patches given resource with given patches and returns patched document
|
2019-07-05 15:20:43 -07:00
|
|
|
func ApplyPatches(resource []byte, patches [][]byte) ([]byte, error) {
|
2019-06-05 13:43:07 +03:00
|
|
|
joinedPatches := JoinPatches(patches)
|
|
|
|
patch, err := jsonpatch.DecodePatch(joinedPatches)
|
2019-03-15 17:58:16 +02:00
|
|
|
if err != nil {
|
2019-05-13 21:27:47 +03:00
|
|
|
return nil, err
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 15:14:01 -07:00
|
|
|
patchedDocument, err := patch.Apply(resource)
|
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
|
|
|
}
|
|
|
|
return patchedDocument, err
|
2019-03-15 17:58:16 +02:00
|
|
|
}
|