Skip to content
This repository was archived by the owner on Jul 15, 2024. It is now read-only.

Commit 336f602

Browse files
author
Adam Johnson
committed
feat: add the ability to exclude files when using the git file generator (#468)
Signed-off-by: Adam Johnson <[email protected]>
1 parent 8220c8a commit 336f602

File tree

12 files changed

+126
-83
lines changed

12 files changed

+126
-83
lines changed

api/v1alpha1/applicationset_types.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,19 @@ type DuckTypeGenerator struct {
272272
}
273273

274274
type GitGenerator struct {
275-
RepoURL string `json:"repoURL"`
276-
Directories []GitDirectoryGeneratorItem `json:"directories,omitempty"`
277-
Files []GitFileGeneratorItem `json:"files,omitempty"`
278-
Revision string `json:"revision"`
279-
RequeueAfterSeconds *int64 `json:"requeueAfterSeconds,omitempty"`
280-
Template ApplicationSetTemplate `json:"template,omitempty"`
275+
RepoURL string `json:"repoURL"`
276+
Directories []GitGeneratorItem `json:"directories,omitempty"`
277+
Files []GitGeneratorItem `json:"files,omitempty"`
278+
Revision string `json:"revision"`
279+
RequeueAfterSeconds *int64 `json:"requeueAfterSeconds,omitempty"`
280+
Template ApplicationSetTemplate `json:"template,omitempty"`
281281
}
282282

283-
type GitDirectoryGeneratorItem struct {
283+
type GitGeneratorItem struct {
284284
Path string `json:"path"`
285285
Exclude bool `json:"exclude,omitempty"`
286286
}
287287

288-
type GitFileGeneratorItem struct {
289-
Path string `json:"path"`
290-
}
291-
292288
// SCMProviderGenerator defines a generator that scrapes a SCMaaS API to find candidate repos.
293289
type SCMProviderGenerator struct {
294290
// Which provider to use and config for it.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 18 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/argoproj/gitops-engine v0.5.1
88
github.com/argoproj/pkg v0.11.1-0.20211203175135-36c59d8fafe0
99
github.com/go-logr/logr v0.4.0
10+
github.com/gobwas/glob v0.2.3
1011
github.com/google/go-github/v35 v35.0.0
1112
github.com/imdario/mergo v0.3.12
1213
github.com/jeremywohl/flatten v1.0.1

manifests/crds/argoproj.io_applicationsets.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,8 @@ spec:
651651
files:
652652
items:
653653
properties:
654+
exclude:
655+
type: boolean
654656
path:
655657
type: string
656658
required:
@@ -1831,6 +1833,8 @@ spec:
18311833
files:
18321834
items:
18331835
properties:
1836+
exclude:
1837+
type: boolean
18341838
path:
18351839
type: string
18361840
required:
@@ -3917,6 +3921,8 @@ spec:
39173921
files:
39183922
items:
39193923
properties:
3924+
exclude:
3925+
type: boolean
39203926
path:
39213927
type: string
39223928
required:

manifests/install-with-argo-cd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,8 @@ spec:
24682468
files:
24692469
items:
24702470
properties:
2471+
exclude:
2472+
type: boolean
24712473
path:
24722474
type: string
24732475
required:
@@ -3648,6 +3650,8 @@ spec:
36483650
files:
36493651
items:
36503652
properties:
3653+
exclude:
3654+
type: boolean
36513655
path:
36523656
type: string
36533657
required:
@@ -5734,6 +5738,8 @@ spec:
57345738
files:
57355739
items:
57365740
properties:
5741+
exclude:
5742+
type: boolean
57375743
path:
57385744
type: string
57395745
required:

manifests/install.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ spec:
650650
files:
651651
items:
652652
properties:
653+
exclude:
654+
type: boolean
653655
path:
654656
type: string
655657
required:
@@ -1830,6 +1832,8 @@ spec:
18301832
files:
18311833
items:
18321834
properties:
1835+
exclude:
1836+
type: boolean
18331837
path:
18341838
type: string
18351839
required:
@@ -3916,6 +3920,8 @@ spec:
39163920
files:
39173921
items:
39183922
properties:
3923+
exclude:
3924+
type: boolean
39193925
path:
39203926
type: string
39213927
required:

pkg/generators/git.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
argoprojiov1alpha1 "github.com/argoproj/applicationset/api/v1alpha1"
1313
"github.com/argoproj/applicationset/pkg/services"
14+
"github.com/gobwas/glob"
1415
"github.com/jeremywohl/flatten"
1516
log "github.com/sirupsen/logrus"
1617
"sigs.k8s.io/yaml"
@@ -85,7 +86,7 @@ func (g *GitGenerator) generateParamsForGitDirectories(appSetGenerator *argoproj
8586
"revision": appSetGenerator.Git.Revision,
8687
}).Info("applications result from the repo service")
8788

88-
requestedApps := g.filterApps(appSetGenerator.Git.Directories, allPaths)
89+
requestedApps := g.filterPaths("directories", appSetGenerator.Git.Directories, allPaths)
8990

9091
res := g.generateParamsFromApps(requestedApps, appSetGenerator)
9192

@@ -114,9 +115,11 @@ func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1al
114115
}
115116
sort.Strings(allPaths)
116117

118+
filteredPaths := g.filterPaths("files", appSetGenerator.Git.Files, allPaths)
119+
117120
// Generate params from each path, and return
118121
res := []map[string]string{}
119-
for _, path := range allPaths {
122+
for _, path := range filteredPaths {
120123

121124
// A JSON / YAML file path can contain multiple sets of parameters (ie it is an array)
122125
paramsArray, err := g.generateParamsFromGitFile(path, allFiles[path])
@@ -174,29 +177,36 @@ func (g *GitGenerator) generateParamsFromGitFile(filePath string, fileContent []
174177

175178
}
176179

177-
func (g *GitGenerator) filterApps(Directories []argoprojiov1alpha1.GitDirectoryGeneratorItem, allPaths []string) []string {
180+
func (g *GitGenerator) filterPaths(pathType string, items []argoprojiov1alpha1.GitGeneratorItem, allPaths []string) []string {
178181
res := []string{}
179-
for _, appPath := range allPaths {
180-
appInclude := false
181-
appExclude := false
182-
// Iterating over each appPath and check whether directories object has requestedPath that matches the appPath
183-
for _, requestedPath := range Directories {
184-
match, err := path.Match(requestedPath.Path, appPath)
185-
if err != nil {
186-
log.WithError(err).WithField("requestedPath", requestedPath).
187-
WithField("appPath", appPath).Error("error while matching appPath to requestedPath")
188-
continue
182+
for _, itemPath := range allPaths {
183+
include := false
184+
exclude := false
185+
for _, requestedPath := range items {
186+
var match bool
187+
var err error
188+
if pathType == "files" {
189+
pathGlob := glob.MustCompile(requestedPath.Path)
190+
match = pathGlob.Match(itemPath)
191+
} else {
192+
match, err = path.Match(requestedPath.Path, itemPath)
193+
if err != nil {
194+
log.WithError(err).WithField("requestedPath", requestedPath).
195+
WithField("appPath", itemPath).Error("error while matching appPath to requestedPath")
196+
continue
197+
}
189198
}
199+
190200
if match && !requestedPath.Exclude {
191-
appInclude = true
201+
include = true
192202
}
193203
if match && requestedPath.Exclude {
194-
appExclude = true
204+
exclude = true
195205
}
196206
}
197207
// Whenever there is a path with exclude: true it wont be included, even if it is included in a different path pattern
198-
if appInclude && !appExclude {
199-
res = append(res, appPath)
208+
if include && !exclude {
209+
res = append(res, itemPath)
200210
}
201211
}
202212
return res

0 commit comments

Comments
 (0)