Skip to content

Commit 6794ea1

Browse files
committed
feat: add the ability to exclude files when using the git file generator (argoproj#13690)
Signed-off-by: hubmat00 <[email protected]>
1 parent f8f9ae9 commit 6794ea1

File tree

19 files changed

+1442
-1341
lines changed

19 files changed

+1442
-1341
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: ApplicationSet
3+
metadata:
4+
name: guestbook
5+
spec:
6+
goTemplate: true
7+
goTemplateOptions: ["missingkey=error"]
8+
generators:
9+
- git:
10+
repoURL: https://github.com/argoproj/argo-cd.git
11+
revision: HEAD
12+
files:
13+
- path: "applicationset/examples/git-generator-files-discovery/cluster-config/**/config.json"
14+
- path: "applicationset/examples/git-generator-files-discovery/cluster-config/*/dev/config.json"
15+
exclude: true
16+
template:
17+
metadata:
18+
name: '{{.cluster.name}}-guestbook'
19+
spec:
20+
project: default
21+
source:
22+
repoURL: https://github.com/argoproj/argo-cd.git
23+
targetRevision: HEAD
24+
path: "applicationset/examples/git-generator-files-discovery/apps/guestbook"
25+
destination:
26+
server: https://kubernetes.default.svc
27+
#server: '{{.cluster.address}}'
28+
namespace: guestbook
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: argoproj.io/v1alpha1
2+
kind: ApplicationSet
3+
metadata:
4+
name: guestbook
5+
spec:
6+
generators:
7+
- git:
8+
repoURL: https://github.com/argoproj/argo-cd.git
9+
revision: HEAD
10+
files:
11+
- path: "applicationset/examples/git-generator-files-discovery/cluster-config/**/config.json"
12+
- path: "applicationset/examples/git-generator-files-discovery/cluster-config/*/dev/config.json"
13+
exclude: true
14+
template:
15+
metadata:
16+
name: '{{cluster.name}}-guestbook'
17+
spec:
18+
project: default
19+
source:
20+
repoURL: https://github.com/argoproj/argo-cd.git
21+
targetRevision: HEAD
22+
path: "applicationset/examples/git-generator-files-discovery/apps/guestbook"
23+
destination:
24+
server: https://kubernetes.default.svc
25+
#server: '{{cluster.address}}'
26+
namespace: guestbook

applicationset/generators/generator_spec_processor_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,16 @@ func TestInterpolateGenerator(t *testing.T) {
423423
assert.Equal(t, "p1", interpolatedGenerator.Clusters.Selector.MatchLabels["path-zero"])
424424
assert.Equal(t, "p1/p2/app3", interpolatedGenerator.Clusters.Selector.MatchLabels["path-full"])
425425

426-
fileNamePath := argov1alpha1.GitFileGeneratorItem{
426+
fileNamePath := argov1alpha1.GitGeneratorItem{
427427
Path: "{{name}}",
428428
}
429-
fileServerPath := argov1alpha1.GitFileGeneratorItem{
429+
fileServerPath := argov1alpha1.GitGeneratorItem{
430430
Path: "{{server}}",
431431
}
432432

433433
requestedGenerator = &argov1alpha1.ApplicationSetGenerator{
434434
Git: &argov1alpha1.GitGenerator{
435-
Files: append([]argov1alpha1.GitFileGeneratorItem{}, fileNamePath, fileServerPath),
435+
Files: append([]argov1alpha1.GitGeneratorItem{}, fileNamePath, fileServerPath),
436436
Template: argov1alpha1.ApplicationSetTemplate{},
437437
},
438438
}
@@ -477,16 +477,16 @@ func TestInterpolateGenerator_go(t *testing.T) {
477477
assert.Equal(t, "p1", interpolatedGenerator.Clusters.Selector.MatchLabels["path-zero"])
478478
assert.Equal(t, "p1/p2/app3", interpolatedGenerator.Clusters.Selector.MatchLabels["path-full"])
479479

480-
fileNamePath := argov1alpha1.GitFileGeneratorItem{
480+
fileNamePath := argov1alpha1.GitGeneratorItem{
481481
Path: "{{.name}}",
482482
}
483-
fileServerPath := argov1alpha1.GitFileGeneratorItem{
483+
fileServerPath := argov1alpha1.GitGeneratorItem{
484484
Path: "{{.server}}",
485485
}
486486

487487
requestedGenerator = &argov1alpha1.ApplicationSetGenerator{
488488
Git: &argov1alpha1.GitGenerator{
489-
Files: append([]argov1alpha1.GitFileGeneratorItem{}, fileNamePath, fileServerPath),
489+
Files: append([]argov1alpha1.GitGeneratorItem{}, fileNamePath, fileServerPath),
490490
Template: argov1alpha1.ApplicationSetTemplate{},
491491
},
492492
}
@@ -530,7 +530,7 @@ func TestInterpolateGeneratorError(t *testing.T) {
530530
{name: "Error templating", args: args{
531531
requestedGenerator: &argov1alpha1.ApplicationSetGenerator{Git: &argov1alpha1.GitGenerator{
532532
RepoURL: "foo",
533-
Files: []argov1alpha1.GitFileGeneratorItem{{Path: "bar/"}},
533+
Files: []argov1alpha1.GitGeneratorItem{{Path: "bar/"}},
534534
Revision: "main",
535535
Values: map[string]string{
536536
"git_test": "{{ toPrettyJson . }}",

applicationset/generators/git.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package generators
33
import (
44
"context"
55
"fmt"
6+
"github.com/bmatcuk/doublestar/v4"
67
"path"
78
"sort"
89
"strconv"
@@ -120,9 +121,11 @@ func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1al
120121
}
121122
sort.Strings(allPaths)
122123

124+
filteredPaths := g.filterPaths(appSetGenerator.Git.Files, allPaths)
125+
123126
// Generate params from each path, and return
124127
res := []map[string]interface{}{}
125-
for _, path := range allPaths {
128+
for _, path := range filteredPaths {
126129

127130
// A JSON / YAML file path can contain multiple sets of parameters (ie it is an array)
128131
paramsArray, err := g.generateParamsFromGitFile(path, allFiles[path], appSetGenerator.Git.Values, useGoTemplate, goTemplateOptions, appSetGenerator.Git.PathParamPrefix)
@@ -212,7 +215,7 @@ func (g *GitGenerator) generateParamsFromGitFile(filePath string, fileContent []
212215
return res, nil
213216
}
214217

215-
func (g *GitGenerator) filterApps(Directories []argoprojiov1alpha1.GitDirectoryGeneratorItem, allPaths []string) []string {
218+
func (g *GitGenerator) filterApps(Directories []argoprojiov1alpha1.GitGeneratorItem, allPaths []string) []string {
216219
res := []string{}
217220
for _, appPath := range allPaths {
218221
appInclude := false
@@ -240,6 +243,33 @@ func (g *GitGenerator) filterApps(Directories []argoprojiov1alpha1.GitDirectoryG
240243
return res
241244
}
242245

246+
func (g *GitGenerator) filterPaths(items []argoprojiov1alpha1.GitGeneratorItem, allPaths []string) []string {
247+
res := []string{}
248+
for _, itemPath := range allPaths {
249+
include := false
250+
exclude := false
251+
for _, requestedPath := range items {
252+
match, err := doublestar.Match(requestedPath.Path, itemPath)
253+
if err != nil {
254+
log.WithError(err).WithField("requestedPath", requestedPath).
255+
WithField("appPath", itemPath).Error("error while matching appPath to requestedPath")
256+
continue
257+
}
258+
if match && !requestedPath.Exclude {
259+
include = true
260+
}
261+
if match && requestedPath.Exclude {
262+
exclude = true
263+
}
264+
}
265+
// Whenever there is a path with exclude: true it wont be included, even if it is included in a different path pattern
266+
if include && !exclude {
267+
res = append(res, itemPath)
268+
}
269+
}
270+
return res
271+
}
272+
243273
func (g *GitGenerator) generateParamsFromApps(requestedApps []string, appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, useGoTemplate bool, goTemplateOptions []string) ([]map[string]interface{}, error) {
244274
res := make([]map[string]interface{}, len(requestedApps))
245275
for i, a := range requestedApps {

0 commit comments

Comments
 (0)