-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(11164): Advanced Templating using templatePatch
Signed-off-by: gmuselli <geoffrey.muselli@gmail.com>
- Loading branch information
Showing
17 changed files
with
1,254 additions
and
815 deletions.
There are no files selected for viewing
295 changes: 162 additions & 133 deletions
295
applicationset/controllers/applicationset_controller.go
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/util/strategicpatch" | ||
|
||
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
) | ||
|
||
func ApplyPatchTemplate(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 := 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([]byte(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) | ||
} | ||
|
||
return &finalApp, err | ||
} |
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,99 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
"github.com/argoproj/argo-cd/v2/test/e2e/fixture/applicationsets/utils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func validate(t *testing.T, patch string) { | ||
app := &appv1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Application", | ||
APIVersion: "argoproj.io/v1alpha1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cluster-guestbook", | ||
Namespace: utils.ArgoCDExternalNamespace, | ||
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"}, | ||
}, | ||
Spec: argov1alpha1.ApplicationSpec{ | ||
Project: "default", | ||
Source: &argov1alpha1.ApplicationSource{ | ||
RepoURL: "https://github.com/argoproj/argocd-example-apps.git", | ||
TargetRevision: "HEAD", | ||
Path: "guestbook", | ||
}, | ||
Destination: argov1alpha1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "guestbook", | ||
}, | ||
}, | ||
} | ||
|
||
result, err := ApplyPatchTemplate(app, patch) | ||
require.NoError(t, err) | ||
require.Contains(t, result.ObjectMeta.Annotations, "annotation-some-key") | ||
assert.Equal(t, result.ObjectMeta.Annotations["annotation-some-key"], "annotation-some-value") | ||
|
||
assert.Equal(t, result.Spec.SyncPolicy.Automated.Prune, true) | ||
require.Contains(t, result.Spec.Source.Helm.ValueFiles, "values.test.yaml") | ||
require.Contains(t, result.Spec.Source.Helm.ValueFiles, "values.big.yaml") | ||
} | ||
|
||
func TestWithJson(t *testing.T) { | ||
|
||
validate(t, `{ | ||
"metadata": { | ||
"annotations": { | ||
"annotation-some-key": "annotation-some-value" | ||
} | ||
}, | ||
"spec": { | ||
"source": { | ||
"helm": { | ||
"valueFiles": [ | ||
"values.test.yaml", | ||
"values.big.yaml" | ||
] | ||
} | ||
}, | ||
"syncPolicy": { | ||
"automated": { | ||
"prune": true | ||
} | ||
} | ||
} | ||
}`) | ||
|
||
} | ||
|
||
func TestWithYaml(t *testing.T) { | ||
|
||
validate(t, ` | ||
metadata: | ||
annotations: | ||
annotation-some-key: annotation-some-value | ||
spec: | ||
source: | ||
helm: | ||
valueFiles: | ||
- values.test.yaml | ||
- values.big.yaml | ||
syncPolicy: | ||
automated: | ||
prune: true`) | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
app := &appv1.Application{} | ||
|
||
result, err := ApplyPatchTemplate(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
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
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
Oops, something went wrong.