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

340 lines
9.6 KiB
Go
Raw Normal View History

2019-05-21 18:27:56 +03:00
package engine
import (
"encoding/json"
"errors"
"fmt"
2019-05-22 18:28:38 +01:00
"reflect"
"strconv"
"github.com/golang/glog"
jsonpatch "github.com/evanphx/json-patch"
2019-06-05 16:44:53 +03:00
kubepolicy "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
2019-05-21 18:27:56 +03:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ProcessOverlay handles validating admission request
2019-06-05 17:43:59 -07:00
// Checks the target resources for rules defined in the policy
func ProcessOverlay(rule kubepolicy.Rule, rawResource []byte, gvk metav1.GroupVersionKind) ([][]byte, error) {
2019-06-05 16:44:53 +03:00
2019-05-21 18:27:56 +03:00
var resource interface{}
var appliedPatches [][]byte
err := json.Unmarshal(rawResource, &resource)
if err != nil {
return nil, err
}
patches, err := mutateResourceWithOverlay(resource, *rule.Mutation.Overlay)
if err != nil {
return nil, err
2019-05-21 18:27:56 +03:00
}
appliedPatches = append(appliedPatches, patches...)
2019-05-21 18:27:56 +03:00
return appliedPatches, err
2019-05-21 18:27:56 +03:00
}
// mutateResourceWithOverlay is a start of overlaying process
func mutateResourceWithOverlay(resource, pattern interface{}) ([][]byte, error) {
2019-06-12 12:21:52 +03:00
// It assumes that mutation is started from root, so "/" is passed
return applyOverlay(resource, pattern, "/")
}
// applyOverlay detects type of current item and goes down through overlay and resource trees applying overlay
func applyOverlay(resource, overlay interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
2019-05-22 18:28:38 +01:00
// resource item exists but has different type - replace
// all subtree within this path by overlay
if reflect.TypeOf(resource) != reflect.TypeOf(overlay) {
patch, err := replaceSubtree(overlay, path)
if err != nil {
return nil, err
}
2019-05-22 18:28:38 +01:00
appliedPatches = append(appliedPatches, patch)
}
return applyOverlayForSameTypes(resource, overlay, path)
}
// applyOverlayForSameTypes is applyOverlay for cases when TypeOf(resource) == TypeOf(overlay)
func applyOverlayForSameTypes(resource, overlay interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
// detect the type of resource and overlay and select corresponding handler
2019-05-21 18:27:56 +03:00
switch typedOverlay := overlay.(type) {
// map
2019-05-21 18:27:56 +03:00
case map[string]interface{}:
typedResource := resource.(map[string]interface{})
patches, err := applyOverlayToMap(typedResource, typedOverlay, path)
if err != nil {
return nil, err
2019-05-21 18:27:56 +03:00
}
appliedPatches = append(appliedPatches, patches...)
// array
2019-05-21 18:27:56 +03:00
case []interface{}:
typedResource := resource.([]interface{})
patches, err := applyOverlayToArray(typedResource, typedOverlay, path)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patches...)
// elementary types
2019-05-27 18:21:23 +03:00
case string, float64, int64, bool:
patch, err := replaceSubtree(overlay, path)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patch)
default:
return nil, fmt.Errorf("Overlay has unsupported type: %T", overlay)
2019-05-21 18:27:56 +03:00
}
return appliedPatches, nil
2019-05-21 18:27:56 +03:00
}
// for each overlay and resource map elements applies overlay
func applyOverlayToMap(resourceMap, overlayMap map[string]interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
for key, value := range overlayMap {
// skip anchor element because it has condition, not
// the value that must replace resource value
2019-06-13 17:20:00 +03:00
if isConditionAnchor(key) {
continue
}
2019-06-14 17:19:32 +03:00
noAnchorKey := removeAnchor(key)
currentPath := path + noAnchorKey + "/"
resourcePart, ok := resourceMap[noAnchorKey]
2019-06-14 17:19:32 +03:00
if ok && !isAddingAnchor(key) {
// Key exists - go down through the overlay and resource trees
patches, err := applyOverlay(resourcePart, value, currentPath)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patches...)
2019-06-14 17:19:32 +03:00
}
if !ok {
// Key does not exist - insert entire overlay subtree
patch, err := insertSubtree(value, currentPath)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patch)
}
}
return appliedPatches, nil
}
// for each overlay and resource array elements applies overlay
func applyOverlayToArray(resource, overlay []interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
if 0 == len(overlay) {
return nil, errors.New("Empty array detected in the overlay")
}
if 0 == len(resource) {
2019-06-12 12:21:52 +03:00
// If array resource is empty, insert part from overlay
patch, err := insertSubtree(overlay, path)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patch)
return appliedPatches, nil
}
if reflect.TypeOf(resource[0]) != reflect.TypeOf(overlay[0]) {
return nil, fmt.Errorf("Overlay array and resource array have elements of different types: %T and %T", overlay[0], resource[0])
}
return applyOverlayToArrayOfSameTypes(resource, overlay, path)
}
2019-06-12 12:21:52 +03:00
// applyOverlayToArrayOfSameTypes applies overlay to array elements if they (resource and overlay elements) have same type
func applyOverlayToArrayOfSameTypes(resource, overlay []interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
2019-05-21 18:27:56 +03:00
switch overlay[0].(type) {
case map[string]interface{}:
return applyOverlayToArrayOfMaps(resource, overlay, path)
2019-05-21 18:27:56 +03:00
default:
lastElementIdx := len(resource)
// Add elements to the end
for i, value := range overlay {
currentPath := path + strconv.Itoa(lastElementIdx+i) + "/"
2019-06-12 12:21:52 +03:00
// currentPath example: /spec/template/spec/containers/3/
patch, err := insertSubtree(value, currentPath)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patch)
2019-05-21 18:27:56 +03:00
}
}
return appliedPatches, nil
}
// Array of maps needs special handling as far as it can have anchors.
func applyOverlayToArrayOfMaps(resource, overlay []interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
lastElementIdx := len(resource)
for i, overlayElement := range overlay {
typedOverlay := overlayElement.(map[string]interface{})
anchors := getAnchorsFromMap(typedOverlay)
if len(anchors) > 0 {
// If we have anchors - choose corresponding resource element and mutate it
patches, err := applyOverlayWithAnchors(resource, overlayElement, anchors, path)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patches...)
} else if hasNestedAnchors(overlayElement) {
// If we have anchors on the lower level - continue traversing overlay and resource trees
for j, resourceElement := range resource {
currentPath := path + strconv.Itoa(j) + "/"
2019-06-12 12:21:52 +03:00
// currentPath example: /spec/template/spec/containers/3/
patches, err := applyOverlay(resourceElement, overlayElement, currentPath)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patches...)
}
} else {
// Overlay subtree has no anchors - insert new element
currentPath := path + strconv.Itoa(lastElementIdx+i) + "/"
2019-06-12 12:21:52 +03:00
// currentPath example: /spec/template/spec/containers/3/
patch, err := insertSubtree(overlayElement, currentPath)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patch)
2019-05-22 18:28:38 +01:00
}
2019-05-21 18:27:56 +03:00
}
return appliedPatches, nil
}
func applyOverlayWithAnchors(resource []interface{}, overlay interface{}, anchors map[string]interface{}, path string) ([][]byte, error) {
var appliedPatches [][]byte
for i, resourceElement := range resource {
typedResource := resourceElement.(map[string]interface{})
currentPath := path + strconv.Itoa(i) + "/"
2019-06-12 12:21:52 +03:00
// currentPath example: /spec/template/spec/containers/3/
if !skipArrayObject(typedResource, anchors) {
patches, err := applyOverlay(resourceElement, overlay, currentPath)
if err != nil {
return nil, err
}
appliedPatches = append(appliedPatches, patches...)
}
}
return appliedPatches, nil
2019-05-21 18:27:56 +03:00
}
func insertSubtree(overlay interface{}, path string) ([]byte, error) {
return processSubtree(overlay, path, "add")
}
func replaceSubtree(overlay interface{}, path string) ([]byte, error) {
return processSubtree(overlay, path, "replace")
}
func processSubtree(overlay interface{}, path string, op string) ([]byte, error) {
if len(path) > 1 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
if path == "" {
path = "/"
}
value := prepareJSONValue(overlay)
patchStr := fmt.Sprintf(`{ "op": "%s", "path": "%s", "value": %s }`, op, path, value)
2019-05-21 18:27:56 +03:00
// check the patch
_, err := jsonpatch.DecodePatch([]byte("[" + patchStr + "]"))
if err != nil {
glog.V(3).Info(err)
return nil, fmt.Errorf("Failed to make '%s' patch from an overlay '%s' for path %s", op, value, path)
}
return []byte(patchStr), nil
2019-05-21 18:27:56 +03:00
}
// converts overlay to JSON string to be inserted into the JSON Patch
func prepareJSONValue(overlay interface{}) string {
jsonOverlay, err := json.Marshal(overlay)
if err != nil || hasOnlyAnchors(overlay) {
glog.V(3).Info(err)
return ""
}
return string(jsonOverlay)
}
// Anchor has pattern value, so resource shouldn't be mutated with it
2019-06-12 12:21:52 +03:00
// If entire subtree has only anchor keys - we should skip inserting it
func hasOnlyAnchors(overlay interface{}) bool {
switch typed := overlay.(type) {
case map[string]interface{}:
2019-06-05 17:43:59 -07:00
if anchors := getAnchorsFromMap(typed); len(anchors) == len(typed) {
return true
}
for _, value := range typed {
if !hasOnlyAnchors(value) {
return false
}
}
case []interface{}:
for _, value := range typed {
if !hasOnlyAnchors(value) {
return false
}
}
default:
return false
}
return true
}
// Checks if subtree has anchors
func hasNestedAnchors(overlay interface{}) bool {
switch typed := overlay.(type) {
case map[string]interface{}:
2019-06-05 17:43:59 -07:00
if anchors := getAnchorsFromMap(typed); len(anchors) > 0 {
return true
}
for _, value := range typed {
if hasNestedAnchors(value) {
return true
}
}
2019-05-23 14:51:41 +03:00
return false
case []interface{}:
for _, value := range typed {
if hasNestedAnchors(value) {
return true
}
}
return false
default:
return false
}
2019-05-21 18:27:56 +03:00
}