2022-01-04 17:36:33 -08:00
|
|
|
package patch
|
2020-08-05 09:11:23 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-02-25 15:21:55 -08:00
|
|
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
2020-08-05 09:11:23 -07:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// ProcessPatchJSON6902 ...
|
2023-06-07 11:45:11 +02:00
|
|
|
func ProcessPatchJSON6902(logger logr.Logger, patchesJSON6902 []byte, resource resource) (resource, error) {
|
|
|
|
patchedResourceRaw, err := applyPatchesWithOptions(resource, patchesJSON6902)
|
|
|
|
if err != nil {
|
2022-01-04 17:36:33 -08:00
|
|
|
logger.Error(err, "failed to apply JSON Patch")
|
2023-06-07 11:45:11 +02:00
|
|
|
return nil, err
|
2020-08-05 09:11:23 -07:00
|
|
|
}
|
2023-06-07 11:45:11 +02:00
|
|
|
return patchedResourceRaw, nil
|
2020-08-05 09:11:23 -07:00
|
|
|
}
|
|
|
|
|
2021-02-25 15:21:55 -08:00
|
|
|
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)
|
|
|
|
}
|
2021-02-25 18:02:52 -08:00
|
|
|
options := &jsonpatch.ApplyOptions{SupportNegativeIndices: true, AllowMissingPathOnRemove: true, EnsurePathExistsOnAdd: true}
|
2021-02-25 15:21:55 -08:00
|
|
|
patchedResource, err := patches.ApplyWithOptions(resource, options)
|
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
|
|
|
}
|
|
|
|
return patchedResource, nil
|
|
|
|
}
|