-
Notifications
You must be signed in to change notification settings - Fork 7k
feat: Matrix generator where a generator can reference items of another one #9080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
crenshaw-dev
merged 32 commits into
argoproj:master
from
KojoRising:feat/add-intra-generator-param-support
Jun 6, 2022
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
d4da216
Upgraded Go to 1.18, & have changes related to matrix generator's chi…
jkulkarn 38a2b56
Added import for applicationSet in matrix.go
jkulkarn 5c7ae5a
Added import for applicationSet in matrix.go
jkulkarn f09d41e
Added logging + small fixes for ArgoCD PR
jkulkarn 491d301
Added testing for GetRelevantGenerators
jkulkarn 535a391
Added a test for interpolating Generators
jkulkarn 4dfa276
Added a 2nd test for interpolating Generators
jkulkarn 424f0c9
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising 4edb6fe
Updated Generators-Matrix.md documentation to include an example + re…
jkulkarn ad633e2
Small wording fix.
jkulkarn 62c0426
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising a719638
Small change to generator_spec_processor.go
jkulkarn 78705ce
Merge remote-tracking branch 'origin/feat/add-intra-generator-param-s…
jkulkarn a6a915b
Fixing Test case
jkulkarn 6001908
Small changes for matrix + generator_spec_processor.go
jkulkarn 295a6ed
Fixed (I believe) the issue that @Lobstrosity mentioned.
jkulkarn 4bed874
Refactored code to accept map[string]string instead of []map[string]s…
jkulkarn 54deaed
Fixing test cases
jkulkarn cc8d8bc
Merge branch 'master' into feat/add-intra-generator-param-support
jkulkarn 5785aab
Fixing lint error.
jkulkarn e59253c
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising 6bf05d1
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising db5e8af
Using test-case suggestion from @rumstead.
jkulkarn 0dae059
Changing up naming from testing.
jkulkarn ec0854a
Merge branch 'master' into feat/add-intra-generator-param-support
jkulkarn 2ec3e94
Updated go.sum
jkulkarn cd3602d
Cleaning up for linter.
jkulkarn 848f957
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising b297f20
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising b2d336e
Update Generators-Matrix.md
KojoRising c4c4654
Added changes as asked by @crenshaw-dev. These include
jkulkarn 598eff0
Merge branch 'master' into feat/add-intra-generator-param-support
KojoRising File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
180 changes: 180 additions & 0 deletions
180
applicationset/generators/generator_spec_processor_test.go
This file contains hidden or 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,180 @@ | ||
| package generators | ||
|
|
||
| import ( | ||
| "context" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| corev1 "k8s.io/api/core/v1" | ||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| kubefake "k8s.io/client-go/kubernetes/fake" | ||
| crtclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "testing" | ||
|
|
||
| argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" | ||
| ) | ||
|
|
||
| func getMockClusterGenerator() Generator { | ||
| clusters := []crtclient.Object{ | ||
| &corev1.Secret{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| Kind: "Secret", | ||
| APIVersion: "v1", | ||
| }, | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "staging-01", | ||
| Namespace: "namespace", | ||
| Labels: map[string]string{ | ||
| "argocd.argoproj.io/secret-type": "cluster", | ||
| "environment": "staging", | ||
| "org": "foo", | ||
| }, | ||
| Annotations: map[string]string{ | ||
| "foo.argoproj.io": "staging", | ||
| }, | ||
| }, | ||
| Data: map[string][]byte{ | ||
| "config": []byte("{}"), | ||
| "name": []byte("staging-01"), | ||
| "server": []byte("https://staging-01.example.com"), | ||
| }, | ||
| Type: corev1.SecretType("Opaque"), | ||
| }, | ||
| &corev1.Secret{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| Kind: "Secret", | ||
| APIVersion: "v1", | ||
| }, | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "production-01", | ||
| Namespace: "namespace", | ||
| Labels: map[string]string{ | ||
| "argocd.argoproj.io/secret-type": "cluster", | ||
| "environment": "production", | ||
| "org": "bar", | ||
| }, | ||
| Annotations: map[string]string{ | ||
| "foo.argoproj.io": "production", | ||
| }, | ||
| }, | ||
| Data: map[string][]byte{ | ||
| "config": []byte("{}"), | ||
| "name": []byte("production_01/west"), | ||
| "server": []byte("https://production-01.example.com"), | ||
| }, | ||
| Type: corev1.SecretType("Opaque"), | ||
| }, | ||
| } | ||
| runtimeClusters := []runtime.Object{} | ||
| appClientset := kubefake.NewSimpleClientset(runtimeClusters...) | ||
|
|
||
| fakeClient := fake.NewClientBuilder().WithObjects(clusters...).Build() | ||
| return NewClusterGenerator(fakeClient, context.Background(), appClientset, "namespace") | ||
| } | ||
|
|
||
| func getMockGitGenerator() Generator { | ||
| argoCDServiceMock := argoCDServiceMock{mock: &mock.Mock{}} | ||
| argoCDServiceMock.mock.On("GetDirectories", mock.Anything, mock.Anything, mock.Anything).Return([]string{"app1", "app2", "app_3", "p1/app4"}, nil) | ||
| var gitGenerator = NewGitGenerator(argoCDServiceMock) | ||
| return gitGenerator | ||
| } | ||
|
|
||
| func TestGetRelevantGenerators(t *testing.T) { | ||
|
|
||
| testGenerators := map[string]Generator{ | ||
| "Clusters": getMockClusterGenerator(), | ||
| "Git": getMockGitGenerator(), | ||
| } | ||
|
|
||
| testGenerators["Matrix"] = NewMatrixGenerator(testGenerators) | ||
| testGenerators["Merge"] = NewMergeGenerator(testGenerators) | ||
| testGenerators["List"] = NewListGenerator() | ||
|
|
||
| requestedGenerator := &argoprojiov1alpha1.ApplicationSetGenerator{ | ||
| List: &argoprojiov1alpha1.ListGenerator{ | ||
| Elements: []apiextensionsv1.JSON{{Raw: []byte(`{"cluster": "cluster","url": "url","values":{"foo":"bar"}}`)}}, | ||
| }} | ||
|
|
||
| relevantGenerators := GetRelevantGenerators(requestedGenerator, testGenerators) | ||
| assert.Len(t, relevantGenerators, 1) | ||
| assert.IsType(t, &ListGenerator{}, relevantGenerators[0]) | ||
|
|
||
| requestedGenerator = &argoprojiov1alpha1.ApplicationSetGenerator{ | ||
| Clusters: &argoprojiov1alpha1.ClusterGenerator{ | ||
| Selector: metav1.LabelSelector{}, | ||
| Template: argoprojiov1alpha1.ApplicationSetTemplate{}, | ||
| Values: nil, | ||
| }, | ||
| } | ||
|
|
||
| relevantGenerators = GetRelevantGenerators(requestedGenerator, testGenerators) | ||
| assert.Len(t, relevantGenerators, 1) | ||
| assert.IsType(t, &ClusterGenerator{}, relevantGenerators[0]) | ||
|
|
||
| requestedGenerator = &argoprojiov1alpha1.ApplicationSetGenerator{ | ||
| Git: &argoprojiov1alpha1.GitGenerator{ | ||
| RepoURL: "", | ||
| Directories: nil, | ||
| Files: nil, | ||
| Revision: "", | ||
| RequeueAfterSeconds: nil, | ||
| Template: argoprojiov1alpha1.ApplicationSetTemplate{}, | ||
| }, | ||
| } | ||
|
|
||
| relevantGenerators = GetRelevantGenerators(requestedGenerator, testGenerators) | ||
| assert.Len(t, relevantGenerators, 1) | ||
| assert.IsType(t, &GitGenerator{}, relevantGenerators[0]) | ||
| } | ||
|
|
||
| func TestInterpolateGenerator(t *testing.T) { | ||
| requestedGenerator := &argoprojiov1alpha1.ApplicationSetGenerator{ | ||
| Clusters: &argoprojiov1alpha1.ClusterGenerator{ | ||
| Selector: metav1.LabelSelector{ | ||
| MatchLabels: map[string]string{ | ||
| "argocd.argoproj.io/secret-type": "cluster", | ||
| "path-basename": "{{path.basename}}", | ||
| "path-zero": "{{path[0]}}", | ||
| "path-full": "{{path}}", | ||
| }}, | ||
| }, | ||
| } | ||
| gitGeneratorParams := map[string]string{ | ||
| "path": "p1/p2/app3", "path.basename": "app3", "path[0]": "p1", "path[1]": "p2", "path.basenameNormalized": "app3", | ||
| } | ||
| interpolatedGenerator, err := interpolateGenerator(requestedGenerator, gitGeneratorParams) | ||
| if err != nil { | ||
| log.WithError(err).WithField("requestedGenerator", requestedGenerator).Error("error interpolating Generator") | ||
| return | ||
| } | ||
| assert.Equal(t, "app3", interpolatedGenerator.Clusters.Selector.MatchLabels["path-basename"]) | ||
| assert.Equal(t, "p1", interpolatedGenerator.Clusters.Selector.MatchLabels["path-zero"]) | ||
| assert.Equal(t, "p1/p2/app3", interpolatedGenerator.Clusters.Selector.MatchLabels["path-full"]) | ||
|
|
||
| fileNamePath := argoprojiov1alpha1.GitFileGeneratorItem{ | ||
| Path: "{{name}}", | ||
| } | ||
| fileServerPath := argoprojiov1alpha1.GitFileGeneratorItem{ | ||
| Path: "{{server}}", | ||
| } | ||
|
|
||
| requestedGenerator = &argoprojiov1alpha1.ApplicationSetGenerator{ | ||
| Git: &argoprojiov1alpha1.GitGenerator{ | ||
| Files: append([]argoprojiov1alpha1.GitFileGeneratorItem{}, fileNamePath, fileServerPath), | ||
| Template: argoprojiov1alpha1.ApplicationSetTemplate{}, | ||
| }, | ||
| } | ||
| clusterGeneratorParams := map[string]string{ | ||
| "name": "production_01/west", "server": "https://production-01.example.com", | ||
| } | ||
| interpolatedGenerator, err = interpolateGenerator(requestedGenerator, clusterGeneratorParams) | ||
| if err != nil { | ||
| log.WithError(err).WithField("requestedGenerator", requestedGenerator).Error("error interpolating Generator") | ||
| return | ||
| } | ||
| assert.Equal(t, "production_01/west", interpolatedGenerator.Git.Files[0].Path) | ||
| assert.Equal(t, "https://production-01.example.com", interpolatedGenerator.Git.Files[1].Path) | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.