2020-01-07 17:06:17 -08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-02-25 15:21:55 -08:00
|
|
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
2022-04-01 07:26:47 +02:00
|
|
|
commonAnchor "github.com/kyverno/kyverno/pkg/engine/anchor"
|
2022-10-02 20:45:03 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-04-01 07:26:47 +02:00
|
|
|
jsonutils "github.com/kyverno/kyverno/pkg/utils/json"
|
2020-01-07 17:06:17 -08:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ApplyPatches patches given resource with given patches and returns patched document
|
2020-05-26 10:36:56 -07:00
|
|
|
// return original resource if any error occurs
|
2020-01-07 17:06:17 -08:00
|
|
|
func ApplyPatches(resource []byte, patches [][]byte) ([]byte, error) {
|
2022-03-18 12:30:49 +01:00
|
|
|
if len(patches) == 0 {
|
|
|
|
return resource, nil
|
|
|
|
}
|
2022-04-01 07:26:47 +02:00
|
|
|
joinedPatches := jsonutils.JoinPatches(patches...)
|
2020-01-07 17:06:17 -08:00
|
|
|
patch, err := jsonpatch.DecodePatch(joinedPatches)
|
|
|
|
if err != nil {
|
2022-10-02 20:45:03 +01:00
|
|
|
logging.V(4).Info("failed to decode JSON patch", "patch", patch)
|
2020-01-15 18:15:48 -08:00
|
|
|
return resource, err
|
2020-01-07 17:06:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
patchedDocument, err := patch.Apply(resource)
|
|
|
|
if err != nil {
|
2022-10-02 20:45:03 +01:00
|
|
|
logging.V(4).Info("failed to apply JSON patch", "patch", patch)
|
2020-01-07 17:06:17 -08:00
|
|
|
return resource, err
|
|
|
|
}
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2022-10-02 20:45:03 +01:00
|
|
|
logging.V(4).Info("applied JSON patch", "patch", patch)
|
2020-01-07 17:06:17 -08:00
|
|
|
return patchedDocument, err
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// ApplyPatchNew patches given resource with given joined patches
|
2020-01-07 17:06:17 -08:00
|
|
|
func ApplyPatchNew(resource, patch []byte) ([]byte, error) {
|
|
|
|
jsonpatch, err := jsonpatch.DecodePatch(patch)
|
|
|
|
if err != nil {
|
2021-02-25 15:21:55 -08:00
|
|
|
return resource, err
|
2020-01-07 17:06:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
patchedResource, err := jsonpatch.Apply(resource)
|
|
|
|
if err != nil {
|
2021-02-25 15:21:55 -08:00
|
|
|
return resource, err
|
2020-01-07 17:06:17 -08:00
|
|
|
}
|
2020-05-26 10:36:56 -07:00
|
|
|
|
2020-01-07 17:06:17 -08:00
|
|
|
return patchedResource, err
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// ConvertToUnstructured converts the resource to unstructured format
|
2020-01-07 17:06:17 -08:00
|
|
|
func ConvertToUnstructured(data []byte) (*unstructured.Unstructured, error) {
|
|
|
|
resource := &unstructured.Unstructured{}
|
|
|
|
err := resource.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// GetAnchorsFromMap gets the conditional anchor map
|
2020-01-07 17:06:17 -08:00
|
|
|
func GetAnchorsFromMap(anchorsMap map[string]interface{}) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
|
|
|
|
for key, value := range anchorsMap {
|
2020-08-29 06:52:22 +05:30
|
|
|
if commonAnchor.IsConditionAnchor(key) {
|
2020-01-07 17:06:17 -08:00
|
|
|
result[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|