mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: crenshaw-dev <[email protected]>
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package template
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
|
|
|
"github.com/argoproj/argo-cd/v3/applicationset/utils"
|
|
appv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
|
|
)
|
|
|
|
func applyTemplatePatch(app *appv1.Application, templatePatch string) (*appv1.Application, error) {
|
|
appString, err := json.Marshal(app)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while marhsalling Application %w", err)
|
|
}
|
|
|
|
convertedTemplatePatch, err := utils.ConvertYAMLToJSON(templatePatch)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while converting template to json %q: %w", convertedTemplatePatch, err)
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(convertedTemplatePatch), &appv1.Application{}); err != nil {
|
|
return nil, fmt.Errorf("invalid templatePatch %q: %w", convertedTemplatePatch, err)
|
|
}
|
|
|
|
data, err := strategicpatch.StrategicMergePatch(appString, []byte(convertedTemplatePatch), appv1.Application{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while applying templatePatch template to json %q: %w", convertedTemplatePatch, err)
|
|
}
|
|
|
|
finalApp := appv1.Application{}
|
|
err = json.Unmarshal(data, &finalApp)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while unmarhsalling patched application: %w", err)
|
|
}
|
|
|
|
// Prevent changes to the `project` field. This helps prevent malicious template patches
|
|
finalApp.Spec.Project = app.Spec.Project
|
|
|
|
return &finalApp, nil
|
|
}
|