Skip to content

Commit fd4263a

Browse files
committed
feat: filtering logic for exluded paths (git file generator) must stay backwards-compatible with the default greedy git file generator gloobing (argoproj#13690)
1 parent a726535 commit fd4263a

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

applicationset/generators/git.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,17 @@ func (g *GitGenerator) generateParamsForGitDirectories(appSetGenerator *argoproj
101101

102102
func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, useGoTemplate bool, goTemplateOptions []string) ([]map[string]interface{}, error) {
103103

104-
// Get all files that match the requested path string, removing duplicates
104+
// Get all files that match the requested path string but are not configured as excludes, removing duplicates
105105
allFiles := make(map[string][]byte)
106106
for _, requestedPath := range appSetGenerator.Git.Files {
107-
files, err := g.repos.GetFiles(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision, requestedPath.Path)
108-
if err != nil {
109-
return nil, err
110-
}
111-
for filePath, content := range files {
112-
allFiles[filePath] = content
107+
if !requestedPath.Exclude {
108+
files, err := g.repos.GetFiles(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision, requestedPath.Path)
109+
if err != nil {
110+
return nil, err
111+
}
112+
for filePath, content := range files {
113+
allFiles[filePath] = content
114+
}
113115
}
114116
}
115117

@@ -121,7 +123,7 @@ func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1al
121123
}
122124
sort.Strings(allPaths)
123125

124-
filteredPaths := g.filterPaths(appSetGenerator.Git.Files, allPaths)
126+
filteredPaths := g.filterFilePaths(appSetGenerator.Git.Files, allPaths)
125127

126128
// Generate params from each path, and return
127129
res := []map[string]interface{}{}
@@ -243,24 +245,27 @@ func (g *GitGenerator) filterApps(Directories []argoprojiov1alpha1.GitGeneratorI
243245
return res
244246
}
245247

246-
func (g *GitGenerator) filterPaths(items []argoprojiov1alpha1.GitGeneratorItem, allPaths []string) []string {
248+
func (g *GitGenerator) filterFilePaths(Files []argoprojiov1alpha1.GitGeneratorItem, allPaths []string) []string {
247249
res := []string{}
248250
for _, itemPath := range allPaths {
249251
include := false
250252
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 {
253+
for _, requestedPath := range Files {
254+
// exec doublestar.Match only on excluded requestedPaths to stay backwards-compatible with the default
255+
// greedy git file generator globbing
256+
if requestedPath.Exclude {
257+
match, err := doublestar.Match(requestedPath.Path, itemPath)
258+
if err != nil {
259+
log.WithError(err).WithField("requestedPath", requestedPath).
260+
WithField("appPath", itemPath).Error("error while matching appPath to requestedPath")
261+
continue
262+
}
263+
if match {
264+
exclude = true
265+
}
266+
} else {
259267
include = true
260268
}
261-
if match && requestedPath.Exclude {
262-
exclude = true
263-
}
264269
}
265270
// Whenever there is a path with exclude: true it wont be included, even if it is included in a different path pattern
266271
if include && !exclude {

applicationset/generators/git_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,51 @@ cluster:
10871087
},
10881088
expectedError: nil,
10891089
},
1090+
{
1091+
name: "test compatibility with greedy git file generator globbing and exclude filter",
1092+
files: []argoprojiov1alpha1.GitGeneratorItem{{Path: "some-path/*.yaml"}},
1093+
repoFileContents: map[string][]byte{
1094+
"some-path/values.yaml": []byte(`
1095+
cluster:
1096+
1097+
name: production
1098+
address: https://kubernetes.default.svc
1099+
`),
1100+
"some-path/staging/values.yaml": []byte(`
1101+
cluster:
1102+
1103+
name: staging
1104+
address: https://kubernetes.default.svc
1105+
`),
1106+
},
1107+
repoPathsError: nil,
1108+
expected: []map[string]interface{}{
1109+
{
1110+
"cluster.owner": "[email protected]",
1111+
"cluster.name": "production",
1112+
"cluster.address": "https://kubernetes.default.svc",
1113+
"path": "some-path",
1114+
"path.basename": "some-path",
1115+
"path[0]": "some-path",
1116+
"path.basenameNormalized": "some-path",
1117+
"path.filename": "values.yaml",
1118+
"path.filenameNormalized": "values.yaml",
1119+
},
1120+
{
1121+
"cluster.owner": "[email protected]",
1122+
"cluster.name": "staging",
1123+
"cluster.address": "https://kubernetes.default.svc",
1124+
"path": "some-path/staging",
1125+
"path.basename": "staging",
1126+
"path[0]": "some-path",
1127+
"path[1]": "staging",
1128+
"path.basenameNormalized": "staging",
1129+
"path.filename": "values.yaml",
1130+
"path.filenameNormalized": "values.yaml",
1131+
},
1132+
},
1133+
expectedError: nil,
1134+
},
10901135
}
10911136

10921137
for _, testCase := range cases {

docs/operator-manual/applicationset/Generators-Git.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ The filename can always be accessed using `{{path.filename}}`.
339339

340340
### Exclude files
341341

342-
The Git file generator also supports an `exclude` option in order to exclude files in the repository from being scanned by the ApplicationSet controller:
342+
The Git file generator also supports an `exclude` option in order to exclude files/folders in the repository from being scanned by the ApplicationSet controller:
343343

344344
```yaml
345345
apiVersion: argoproj.io/v1alpha1
@@ -373,7 +373,7 @@ spec:
373373
```
374374
(*The full example can be found [here](https://github.com/argoproj/argo-cd/tree/master/applicationset/examples/git-generator-files-discovery/excludes).*)
375375

376-
This example excludes the config.json file in the `dev` directory from the list of files scanned for this `ApplictionSet` resource.
376+
This example excludes the config.json file in the `dev` directory from the list of files scanned for this `ApplicationSet` resource.
377377

378378
File exclude paths are matched using [doublestar.Match](https://github.com/bmatcuk/doublestar/blob/master/match.go#L8)
379379

0 commit comments

Comments
 (0)