This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add code * run controller-gen * fmt * test run * add tests * fmt * rename * fix error log * fix build * call repo service directly * add tests * refactor after testing * refactor * fmt * add support to filter paths * PR fixes * add config maps for argocd-cm
- Loading branch information
1 parent
47c3908
commit 11c59c0
Showing
12 changed files
with
534 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ rules: | |
- '' | ||
resources: | ||
- secrets | ||
- configmaps | ||
verbs: | ||
- get | ||
- list | ||
|
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 |
---|---|---|
|
@@ -46,6 +46,7 @@ rules: | |
- '' | ||
resources: | ||
- secrets | ||
- configmaps | ||
verbs: | ||
- get | ||
- list | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,102 @@ | ||
package generators | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1" | ||
"github.com/argoproj-labs/applicationset/pkg/services" | ||
"github.com/argoproj-labs/applicationset/pkg/utils" | ||
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" | ||
log "github.com/sirupsen/logrus" | ||
"path" | ||
) | ||
|
||
var _ Generator = (*GitGenerator)(nil) | ||
|
||
type GitGenerator struct { | ||
repos services.Apps | ||
} | ||
|
||
func NewGitGenerator() Generator { | ||
g := &GitGenerator{} | ||
func NewGitGenerator(repos services.Apps) Generator { | ||
g := &GitGenerator{ | ||
repos: repos, | ||
} | ||
return g | ||
} | ||
|
||
func (g *GitGenerator) GenerateApplications( | ||
appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, | ||
appSet *argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) { | ||
if appSet == nil { | ||
func (g *GitGenerator) GenerateApplications(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, appSet *argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) { | ||
if appSetGenerator == nil || appSet == nil { | ||
return nil, fmt.Errorf("ApplicationSet is empty") | ||
} | ||
|
||
return nil, nil | ||
if appSetGenerator.Git == nil { | ||
return nil, fmt.Errorf("git variable empty") | ||
} | ||
|
||
allApps, err := g.repos.GetApps(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"allAps": allApps, | ||
"total": len(allApps), | ||
"repoURL": appSetGenerator.Git.RepoURL, | ||
"revision": appSetGenerator.Git.Revision, | ||
}).Info("applications result from the repo service") | ||
|
||
requestedApps := g.filter(appSetGenerator.Git.Directories, allApps) | ||
|
||
res := g.generateApplications(requestedApps, appSet) | ||
|
||
return res, nil | ||
} | ||
|
||
func (g *GitGenerator) filter(Directories []argoprojiov1alpha1.GitDirectoryGeneratorItem, allApps []string) []string { | ||
res := []string{} | ||
for _, requestedPath := range Directories { | ||
for _, appPath := range allApps { | ||
match, err := path.Match(requestedPath.Path, appPath) | ||
if err != nil { | ||
log.WithError(err).WithField("requestedPath", requestedPath). | ||
WithField("appPath", appPath).Error("error while matching appPath to requestedPath") | ||
continue | ||
} | ||
if match { | ||
res = append(res, appPath) | ||
} | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func (g *GitGenerator) generateApplications(requestedApps []string, appSet *argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application) { | ||
|
||
res := make([]argov1alpha1.Application, len(requestedApps)) | ||
for i, a := range requestedApps { | ||
app, err := g.generateApplication(appSet, a) | ||
if err != nil { | ||
log.WithError(err).WithField("app", a).Error("error while generating app") | ||
continue | ||
} | ||
res[i] = *app | ||
} | ||
|
||
return res | ||
} | ||
|
||
|
||
func (g *GitGenerator) generateApplication(appSet *argoprojiov1alpha1.ApplicationSet, appPath string) (*argov1alpha1.Application, error) { | ||
var tmplApplication argov1alpha1.Application | ||
tmplApplication.Namespace = appSet.Spec.Template.Namespace | ||
tmplApplication.Name = appSet.Spec.Template.Name | ||
tmplApplication.Spec = appSet.Spec.Template.Spec | ||
|
||
params := make(map[string]string, 2) | ||
params["path"] = appPath | ||
params["path.basename"] = path.Base(appPath) | ||
|
||
tmpApplication, err := utils.RenderTemplateParams(&tmplApplication, params) | ||
|
||
return tmpApplication, err | ||
} |
Oops, something went wrong.