Skip to content

Commit

Permalink
protect against more nil maps
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Dec 8, 2023
1 parent 5d7bee6 commit 0cc8484
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
5 changes: 1 addition & 4 deletions pkg/affected_apps/argocd_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ func getKustomizeApps(vcsToArgoMap config.VcsToArgoMap, repo *repo.Repo, repoPat
log.Debug().Msgf("creating fs for %s", repoPath)
fs := os.DirFS(repoPath)
log.Debug().Msg("following kustomize apps")
kustomizeAppFiles, err := vcsToArgoMap.WalkKustomizeApps(repo, fs)
if err != nil {
log.Warn().Err(err).Msgf("failed to follow kustomize files")
}
kustomizeAppFiles := vcsToArgoMap.WalkKustomizeApps(repo, fs)

logCounts(kustomizeAppFiles)
return kustomizeAppFiles
Expand Down
9 changes: 5 additions & 4 deletions pkg/affected_apps/argocd_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
func TestCreateNewMatcherWithNilVcsMap(t *testing.T) {
// setup
var (
vcsMap config.VcsToArgoMap
repo repo2.Repo
path string
repo repo2.Repo
path string

vcsMap = config.NewVcsToArgoMap()
)

// run test
matcher, err := NewArgocdMatcher(vcsMap, &repo, path)
require.NoError(t, err)

// verify results
require.Nil(t, matcher.appsDirectory)
require.NotNil(t, matcher.appsDirectory)
}

func TestFindAffectedAppsWithNilAppsDirectory(t *testing.T) {
Expand Down
15 changes: 1 addition & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,8 @@ func (v2a *VcsToArgoMap) AddApp(app v1alpha1.Application) {
return
}

rawRepoUrl := app.Spec.Source.RepoURL
cleanRepoUrl, err := normalizeRepoUrl(rawRepoUrl)
if err != nil {
log.Warn().Err(err).Msgf("%s/%s: failed to parse %s", app.Namespace, app.Name, rawRepoUrl)
return
}

log.Debug().Msgf("%s/%s: %s => %s", app.Namespace, app.Name, rawRepoUrl, cleanRepoUrl)

appDirectory := v2a.appDirByRepo[cleanRepoUrl]
if appDirectory == nil {
appDirectory = NewAppDirectory()
}
appDirectory := v2a.GetAppsInRepo(app.Spec.Source.RepoURL)
appDirectory.ProcessApp(app)
v2a.appDirByRepo[cleanRepoUrl] = appDirectory
}

type ServerConfig struct {
Expand Down
28 changes: 17 additions & 11 deletions pkg/config/vcstoargomap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package config
import (
"io/fs"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"

"github.com/zapier/kubechecks/pkg/repo"
)

Expand All @@ -24,24 +24,30 @@ func (v2a *VcsToArgoMap) GetAppsInRepo(repoCloneUrl string) *AppDirectory {
log.Warn().Err(err).Msgf("failed to parse %s", repoCloneUrl)
}

return v2a.appDirByRepo[repoUrl]
appdir := v2a.appDirByRepo[repoUrl]
if appdir == nil {
appdir = new(AppDirectory)
v2a.appDirByRepo[repoUrl] = appdir
}

return appdir
}

func (v2a *VcsToArgoMap) WalkKustomizeApps(repo *repo.Repo, fs fs.FS) (*AppDirectory, error) {
repoUrl, err := normalizeRepoUrl(repo.CloneURL)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", repo.CloneURL)
}
func (v2a *VcsToArgoMap) WalkKustomizeApps(repo *repo.Repo, fs fs.FS) *AppDirectory {

appdir := v2a.appDirByRepo[repoUrl]
apps := appdir.GetApps(nil)
var (
result AppDirectory
err error

appdir = v2a.GetAppsInRepo(repo.CloneURL)
apps = appdir.GetApps(nil)
)

var result AppDirectory
for _, app := range apps {
if err = walkKustomizeFiles(&result, fs, app.Name, app.Path); err != nil {
log.Error().Err(err).Msgf("failed to parse kustomize.yaml in %s", app.Path)
}
}

return &result, nil
return &result
}

0 comments on commit 0cc8484

Please sign in to comment.