1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/engine/mutate/patch/patchJSON6902.go
Charles-Edouard Brétéché a345e15511
refactor: remove json patches from engine response (#7449)
* refactor: remove json patches from engine response

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* remove filtering

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-06-07 17:45:11 +08:00

31 lines
921 B
Go

package patch
import (
"fmt"
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/go-logr/logr"
)
// ProcessPatchJSON6902 ...
func ProcessPatchJSON6902(logger logr.Logger, patchesJSON6902 []byte, resource resource) (resource, error) {
patchedResourceRaw, err := applyPatchesWithOptions(resource, patchesJSON6902)
if err != nil {
logger.Error(err, "failed to apply JSON Patch")
return nil, err
}
return patchedResourceRaw, nil
}
func applyPatchesWithOptions(resource, patch []byte) ([]byte, error) {
patches, err := jsonpatch.DecodePatch(patch)
if err != nil {
return resource, fmt.Errorf("failed to decode patches: %v", err)
}
options := &jsonpatch.ApplyOptions{SupportNegativeIndices: true, AllowMissingPathOnRemove: true, EnsurePathExistsOnAdd: true}
patchedResource, err := patches.ApplyWithOptions(resource, options)
if err != nil {
return resource, err
}
return patchedResource, nil
}