Skip to content

Commit

Permalink
chore: partial revert
Browse files Browse the repository at this point in the history
Stick to using getrepository and pass through the project values from
`appSet.Spec.Template.Spec.Project`.

Signed-off-by: Blake Pettersson <[email protected]>
  • Loading branch information
blakepettersson committed Dec 21, 2024
1 parent ffd629a commit 05f0764
Show file tree
Hide file tree
Showing 7 changed files with 626 additions and 164 deletions.
12 changes: 6 additions & 6 deletions applicationset/generators/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func (g *GitGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.Applic
var err error
var res []map[string]interface{}
if len(appSetGenerator.Git.Directories) != 0 {
res, err = g.generateParamsForGitDirectories(appSetGenerator, noRevisionCache, verifyCommit, appSet.Spec.GoTemplate, appSet.Spec.GoTemplateOptions)
res, err = g.generateParamsForGitDirectories(appSetGenerator, noRevisionCache, verifyCommit, appSet.Spec.GoTemplate, appSet.Spec.Template.Spec.Project, appSet.Spec.GoTemplateOptions)
} else if len(appSetGenerator.Git.Files) != 0 {
res, err = g.generateParamsForGitFiles(appSetGenerator, noRevisionCache, verifyCommit, appSet.Spec.GoTemplate, appSet.Spec.GoTemplateOptions)
res, err = g.generateParamsForGitFiles(appSetGenerator, noRevisionCache, verifyCommit, appSet.Spec.GoTemplate, appSet.Spec.Template.Spec.Project, appSet.Spec.GoTemplateOptions)
} else {
return nil, EmptyAppSetGeneratorError
}
Expand All @@ -97,9 +97,9 @@ func (g *GitGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.Applic
return res, nil
}

func (g *GitGenerator) generateParamsForGitDirectories(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, noRevisionCache, verifyCommit bool, useGoTemplate bool, goTemplateOptions []string) ([]map[string]interface{}, error) {
func (g *GitGenerator) generateParamsForGitDirectories(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, noRevisionCache, verifyCommit, useGoTemplate bool, project string, goTemplateOptions []string) ([]map[string]interface{}, error) {
// Directories, not files
allPaths, err := g.repos.GetDirectories(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision, "", noRevisionCache, verifyCommit)
allPaths, err := g.repos.GetDirectories(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision, project, noRevisionCache, verifyCommit)
if err != nil {
return nil, fmt.Errorf("error getting directories from repo: %w", err)
}
Expand All @@ -122,11 +122,11 @@ func (g *GitGenerator) generateParamsForGitDirectories(appSetGenerator *argoproj
return res, nil
}

func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, noRevisionCache, verifyCommit bool, useGoTemplate bool, goTemplateOptions []string) ([]map[string]interface{}, error) {
func (g *GitGenerator) generateParamsForGitFiles(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, noRevisionCache, verifyCommit, useGoTemplate bool, project string, goTemplateOptions []string) ([]map[string]interface{}, error) {
// Get all files that match the requested path string, removing duplicates
allFiles := make(map[string][]byte)
for _, requestedPath := range appSetGenerator.Git.Files {
files, err := g.repos.GetFiles(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision, "", requestedPath.Path, noRevisionCache, verifyCommit)
files, err := g.repos.GetFiles(context.TODO(), appSetGenerator.Git.RepoURL, appSetGenerator.Git.Revision, project, requestedPath.Path, noRevisionCache, verifyCommit)
if err != nil {
return nil, err
}
Expand Down
67 changes: 14 additions & 53 deletions applicationset/services/repo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ package services

import (
"context"
"errors"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"strings"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/util/git"
"github.com/argoproj/argo-cd/v2/util/db"
"github.com/argoproj/argo-cd/v2/util/io"
"github.com/argoproj/argo-cd/v2/util/repository"
)

type argoCDService struct {
listRepositories func(ctx context.Context) ([]*v1alpha1.Repository, error)
storecreds git.CredsStore
getRepository func(ctx context.Context, url, project string) (*v1alpha1.Repository, error)
submoduleEnabled bool
newFileGlobbingEnabled bool
getGitFiles func(ctx context.Context, req *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error)
Expand All @@ -32,9 +27,9 @@ type Repos interface {
GetDirectories(ctx context.Context, repoURL, revision, project string, noRevisionCache, verifyCommit bool) ([]string, error)
}

func NewArgoCDService(listRepositories func(ctx context.Context) ([]*v1alpha1.Repository, error), submoduleEnabled bool, repoClientset apiclient.Clientset, newFileGlobbingEnabled bool) (Repos, error) {
func NewArgoCDService(db db.ArgoDB, submoduleEnabled bool, repoClientset apiclient.Clientset, newFileGlobbingEnabled bool) (Repos, error) {
return &argoCDService{
listRepositories: listRepositories,
getRepository: db.GetRepository,
submoduleEnabled: submoduleEnabled,
newFileGlobbingEnabled: newFileGlobbingEnabled,
getGitFiles: func(ctx context.Context, fileRequest *apiclient.GitFilesRequest) (*apiclient.GitFilesResponse, error) {
Expand All @@ -57,14 +52,9 @@ func NewArgoCDService(listRepositories func(ctx context.Context) ([]*v1alpha1.Re
}

func (a *argoCDService) GetFiles(ctx context.Context, repoURL, revision, project, pattern string, noRevisionCache, verifyCommit bool) (map[string][]byte, error) {
repos, err := a.listRepositories(ctx)
if err != nil {
return nil, fmt.Errorf("error in ListRepositories: %w", err)
}

repo, err := getRepo(repos, repoURL, project)
repo, err := a.getRepository(ctx, repoURL, project)
if err != nil {
return nil, fmt.Errorf("error retrieving Git files: %w", err)
return nil, fmt.Errorf("error in GetRepository: %w", err)
}

fileRequest := &apiclient.GitFilesRequest{
Expand All @@ -84,14 +74,9 @@ func (a *argoCDService) GetFiles(ctx context.Context, repoURL, revision, project
}

func (a *argoCDService) GetDirectories(ctx context.Context, repoURL, revision, project string, noRevisionCache, verifyCommit bool) ([]string, error) {
repos, err := a.listRepositories(ctx)
if err != nil {
return nil, fmt.Errorf("error in ListRepositories: %w", err)
}

repo, err := getRepo(repos, repoURL, project)
repo, err := a.getRepository(ctx, repoURL, resolveProjectName(project))
if err != nil {
return nil, fmt.Errorf("error retrieving Git Directories: %w", err)
return nil, fmt.Errorf("error in GetRepository: %w", err)
}

dirRequest := &apiclient.GitDirectoriesRequest{
Expand All @@ -109,34 +94,10 @@ func (a *argoCDService) GetDirectories(ctx context.Context, repoURL, revision, p
return dirResponse.GetPaths(), nil
}

func getRepo(repos []*v1alpha1.Repository, repoURL string, project string) (*v1alpha1.Repository, error) {
repo, err := repository.FilterRepositoryByProjectAndURL(repos, repoURL, project)
if err != nil {
if errors.Is(err, status.Error(codes.PermissionDenied, "permission denied")) {
// No repo found with a matching URL - attempt fallback without any actual credentials
return &v1alpha1.Repository{Repo: repoURL}, nil
} else if project == "" {
for _, r := range repos {
if git.SameURL(r.Repo, repoURL) {
// Prioritize using a repository with an unset project.
if r.Project == "" {
return r, nil
}

if repo == nil {
repo = r
}
}
}

// Try any repo matching the same repoURL
if repo != nil {
return repo, nil
}

// No repo found with a matching URL - attempt fallback without any actual credentials
return &v1alpha1.Repository{Repo: repoURL}, nil
}
func resolveProjectName(project string) string {
if strings.Contains(project, "{{") {
return ""
}
return repo, err

return project
}
Loading

0 comments on commit 05f0764

Please sign in to comment.