1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +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"
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/engine/response"
"github.com/kyverno/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)
resp.RuleStats.RuleExecutionTimestamp = startTime.Unix()
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.Status = response.RuleStatusFail
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
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.Status = response.RuleStatusFail
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 {
Dynamic webhooks (#2425) * support k8s 1.22, update admissionregistration.k8s.io/v1beta1 to admissionregistration.k8s.io/v1 Signed-off-by: ShutingZhao <shutting06@gmail.com> * - add failurePolicy to policy spec; - fix typo Signed-off-by: ShutingZhao <shutting06@gmail.com> * - add schema validation for failurePolicy; - add a printer column Signed-off-by: ShutingZhao <shutting06@gmail.com> * set default failure policy to fail if not defined Signed-off-by: ShutingZhao <shutting06@gmail.com> * resolve conflicts Signed-off-by: ShutingZhao <shutting06@gmail.com> * fix missing type for printerColumn Signed-off-by: ShutingZhao <shutting06@gmail.com> * refactor policy controller Signed-off-by: ShutingZhao <shutting06@gmail.com> * add webhook config manager Signed-off-by: ShutingZhao <shutting06@gmail.com> * - build webhook objects per policy update; - add fail webhook to default webhook configurations Signed-off-by: ShutingZhao <shutting06@gmail.com> * fix panic on policy update Signed-off-by: ShutingZhao <shutting06@gmail.com> * build default webhook: match empty if autoUpdateWebhooks is enabled, otherwise match all Signed-off-by: ShutingZhao <shutting06@gmail.com> * - set default webhook configs rule to empty; - handle policy deletion Signed-off-by: ShutingZhao <shutting06@gmail.com> * reset webhook config if policies with a specific failurePolicy are cleaned up Signed-off-by: ShutingZhao <shutting06@gmail.com> * handle wildcard pocliy Signed-off-by: ShutingZhao <shutting06@gmail.com> * update default webhook timeout to 10s Signed-off-by: ShutingZhao <shutting06@gmail.com> * cleanups Signed-off-by: ShutingZhao <shutting06@gmail.com> * added webhook informer to re-create it immediately if missing Signed-off-by: ShutingZhao <shutting06@gmail.com> * update tag webhookTimeoutSeconds description Signed-off-by: ShutingZhao <shutting06@gmail.com> * fix e2e tests Signed-off-by: ShutingZhao <shutting06@gmail.com> * fix linter issue Signed-off-by: ShutingZhao <shutting06@gmail.com> * correct metric endpoint Signed-off-by: ShutingZhao <shutting06@gmail.com> * add pol.generate.kind to webhooks Signed-off-by: ShutingZhao <shutting06@gmail.com>
2021-10-05 00:15:09 -07:00
logger.Error(err, "failed to unmarshal resource")
resp.Status = response.RuleStatusFail
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.Status = response.RuleStatusPass
resp.Message = string("successfully process JSON patches")
resp.Patches = patches
return resp, patchedResource
2019-08-23 18:34:23 -07:00
}