1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-01-20 18:52:16 +00:00
kyverno/pkg/engine/mutate/patches.go

93 lines
2.8 KiB
Go
Raw Normal View History

package mutate
2019-03-15 17:58:16 +02:00
import (
"encoding/json"
2019-08-23 18:34:23 -07:00
"fmt"
"strings"
"time"
2019-03-15 17:58:16 +02:00
2020-03-17 16:25:34 -07:00
"github.com/go-logr/logr"
2019-11-13 13:41:08 -08:00
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
"github.com/nirmata/kyverno/pkg/engine/response"
"github.com/nirmata/kyverno/pkg/engine/utils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2019-03-15 17:58:16 +02:00
)
// applyPatch applies patch for resource, returns patched resource.
func applyPatch(resource []byte, patchRaw []byte) ([]byte, error) {
patchesList := [][]byte{patchRaw}
return utils.ApplyPatches(resource, patchesList)
2019-08-23 18:34:23 -07:00
}
//ProcessPatches applies the patches on the resource and returns the patched resource
func ProcessPatches(log logr.Logger, ruleName string, mutation kyverno.Mutation, resource unstructured.Unstructured) (resp response.RuleResponse, patchedResource unstructured.Unstructured) {
logger := log.WithValues("rule", ruleName)
2019-08-23 18:34:23 -07:00
startTime := time.Now()
2020-03-17 16:25:34 -07:00
logger.V(4).Info("started JSON patch", "startTime", startTime)
resp.Name = ruleName
resp.Type = utils.Mutation.String()
2019-08-23 18:34:23 -07:00
defer func() {
resp.RuleStats.ProcessingTime = time.Since(startTime)
logger.V(4).Info("applied JSON patch", "processingTime", resp.RuleStats.ProcessingTime.String())
2019-08-23 18:34:23 -07:00
}()
// convert to RAW
resourceRaw, err := resource.MarshalJSON()
if err != nil {
resp.Success = false
2020-03-17 16:25:34 -07:00
logger.Error(err, "failed to marshal resource")
resp.Message = fmt.Sprintf("failed to process JSON patches: %v", err)
return resp, resource
2019-08-23 18:34:23 -07:00
}
var errs []error
var patches [][]byte
for _, patch := range mutation.Patches {
2019-08-23 18:34:23 -07:00
// JSON patch
patchRaw, err := json.Marshal(patch)
if err != nil {
2020-03-17 16:25:34 -07:00
logger.Error(err, "failed to marshal JSON patch")
2019-08-23 18:34:23 -07:00
errs = append(errs, err)
continue
}
2019-08-26 16:10:19 -07:00
patchResource, err := applyPatch(resourceRaw, patchRaw)
2019-08-23 18:34:23 -07:00
// TODO: continue on error if one of the patches fails, will add the failure event in such case
if err != nil && patch.Operation == "remove" {
2020-03-17 16:25:34 -07:00
log.Error(err, "failed to process JSON path or patch is a 'remove' operation")
2019-08-23 18:34:23 -07:00
continue
}
if err != nil {
errs = append(errs, err)
continue
}
resourceRaw = patchResource
patches = append(patches, patchRaw)
}
// error while processing JSON patches
if len(errs) > 0 {
resp.Success = false
resp.Message = fmt.Sprintf("failed to process JSON patches: %v", func() string {
2019-08-23 18:34:23 -07:00
var str []string
for _, err := range errs {
str = append(str, err.Error())
}
return strings.Join(str, ";")
}())
return resp, resource
2019-08-23 18:34:23 -07:00
}
err = patchedResource.UnmarshalJSON(resourceRaw)
if err != nil {
2020-03-17 16:25:34 -07:00
logger.Error(err, "failed to unmmarshal resource")
resp.Success = false
resp.Message = fmt.Sprintf("failed to process JSON patches: %v", err)
return resp, resource
2019-08-23 18:34:23 -07:00
}
// JSON patches processed successfully
resp.Success = true
resp.Message = fmt.Sprintf("successfully process JSON patches")
resp.Patches = patches
return resp, patchedResource
2019-08-23 18:34:23 -07:00
}