mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-11-08 12:26:36 +00:00
Move the resourcemodifiers package from internal/ to pkg/ so that external plugins, such as Restore Item Action (RIA) plugins, can import and reuse Velero's built-in resource modifier implementation. Due to Go's internal package visibility restrictions, external plugins cannot import packages located under internal/. Relocating resourcemodifiers to pkg/ allows plugins to evaluate and apply resource modifier rules to in-memory resource objects prior to validation or restore execution, ensuring consistent behavior with the Velero server without duplicate code. Update all internal import paths and add an unreleased changelog entry. Fixes #10668 Signed-off-by: Adam Zhang <adam.zhang@broadcom.com>
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package resourcemodifiers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
jsonpatch "github.com/evanphx/json-patch/v5"
|
|
"github.com/sirupsen/logrus"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
type JSONMergePatch struct {
|
|
PatchData string `json:"patchData,omitempty"`
|
|
}
|
|
|
|
type JSONMergePatcher struct {
|
|
patches []JSONMergePatch
|
|
}
|
|
|
|
func (p *JSONMergePatcher) Patch(u *unstructured.Unstructured, _ logrus.FieldLogger) (*unstructured.Unstructured, error) {
|
|
objBytes, err := u.MarshalJSON()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error in marshaling object %s", err)
|
|
}
|
|
|
|
for _, patch := range p.patches {
|
|
patchBytes, err := yaml.YAMLToJSON([]byte(patch.PatchData))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error in converting YAML to JSON %s", err)
|
|
}
|
|
|
|
objBytes, err = jsonpatch.MergePatch(objBytes, patchBytes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error in applying JSON Patch: %s", err.Error())
|
|
}
|
|
}
|
|
|
|
updated := &unstructured.Unstructured{}
|
|
err = updated.UnmarshalJSON(objBytes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error in unmarshalling modified object %s", err.Error())
|
|
}
|
|
|
|
return updated, nil
|
|
}
|