forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(appset): Advanced Templating using templatePatch (argoproj#14893)
* fix(11164): Advanced templating using patchTemplate Signed-off-by: gmuselli <geoffrey.muselli@gmail.com> * small changes Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --------- Signed-off-by: gmuselli <geoffrey.muselli@gmail.com> Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
- Loading branch information
1 parent
b18f488
commit b3f8086
Showing
17 changed files
with
1,275 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/util/strategicpatch" | ||
|
||
"github.com/argoproj/argo-cd/v2/applicationset/utils" | ||
appv1 "github.com/argoproj/argo-cd/v2/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
package controllers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
) | ||
|
||
func Test_ApplyTemplatePatch(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
appTemplate *appv1.Application | ||
templatePatch string | ||
expectedApp *appv1.Application | ||
}{ | ||
{ | ||
name: "patch with JSON", | ||
appTemplate: &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: "namespace", | ||
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"}, | ||
}, | ||
Spec: appv1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &appv1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
}, | ||
Destination: appv1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
}, | ||
}, | ||
templatePatch: `{ | ||
"metadata": { | ||
"annotations": { | ||
"annotation-some-key": "annotation-some-value" | ||
} | ||
}, | ||
"spec": { | ||
"source": { | ||
"helm": { | ||
"valueFiles": [ | ||
"values.test.yaml", | ||
"values.big.yaml" | ||
] | ||
} | ||
}, | ||
"syncPolicy": { | ||
"automated": { | ||
"prune": true | ||
} | ||
} | ||
} | ||
}`, | ||
expectedApp: &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: "namespace", | ||
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"}, | ||
Annotations: map[string]string{ | ||
"annotation-some-key": "annotation-some-value", | ||
}, | ||
}, | ||
Spec: appv1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &appv1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
Helm: &appv1.ApplicationSourceHelm{ | ||
ValueFiles: []string{ | ||
"values.test.yaml", | ||
"values.big.yaml", | ||
}, | ||
}, | ||
}, | ||
Destination: appv1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
SyncPolicy: &appv1.SyncPolicy{ | ||
Automated: &appv1.SyncPolicyAutomated{ | ||
Prune: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "patch with YAML", | ||
appTemplate: &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: "namespace", | ||
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"}, | ||
}, | ||
Spec: appv1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &appv1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
}, | ||
Destination: appv1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
}, | ||
}, | ||
templatePatch: ` | ||
metadata: | ||
annotations: | ||
annotation-some-key: annotation-some-value | ||
spec: | ||
source: | ||
helm: | ||
valueFiles: | ||
- values.test.yaml | ||
- values.big.yaml | ||
syncPolicy: | ||
automated: | ||
prune: true`, | ||
expectedApp: &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: "namespace", | ||
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"}, | ||
Annotations: map[string]string{ | ||
"annotation-some-key": "annotation-some-value", | ||
}, | ||
}, | ||
Spec: appv1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &appv1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
Helm: &appv1.ApplicationSourceHelm{ | ||
ValueFiles: []string{ | ||
"values.test.yaml", | ||
"values.big.yaml", | ||
}, | ||
}, | ||
}, | ||
Destination: appv1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
SyncPolicy: &appv1.SyncPolicy{ | ||
Automated: &appv1.SyncPolicyAutomated{ | ||
Prune: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "project field isn't overwritten", | ||
appTemplate: &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: "namespace", | ||
}, | ||
Spec: appv1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &appv1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
}, | ||
Destination: appv1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
}, | ||
}, | ||
templatePatch: ` | ||
spec: | ||
project: my-project`, | ||
expectedApp: &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: "namespace", | ||
}, | ||
Spec: appv1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &appv1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
}, | ||
Destination: appv1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tcc := tc | ||
t.Run(tcc.name, func(t *testing.T) { | ||
result, err := applyTemplatePatch(tcc.appTemplate, tcc.templatePatch) | ||
require.NoError(t, err) | ||
assert.Equal(t, *tcc.expectedApp, *result) | ||
}) | ||
} | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
app := &appv1.Application{} | ||
|
||
result, err := applyTemplatePatch(app, "hello world") | ||
require.Error(t, err) | ||
require.Nil(t, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.