Skip to content

Commit

Permalink
walk kustomize files (#45)
Browse files Browse the repository at this point in the history
* walk kustomize files

this allows us to more reliably trigger diffs for all apps

* add some debugging information

* use schemas that might be in the clone repo

* fix a dumb mistake

* there are 4 different places to add schemas

* add a bunch of debugging

* clean up

* walk some more kustomize paths

* protect against more nil maps

* remove dead code

* use the right constructor

* this should use the constructor too

* found another one

* updates to clean up the commit status

* some more tweaks
  • Loading branch information
djeebus authored Dec 13, 2023
1 parent 03c7578 commit 9e97ae8
Show file tree
Hide file tree
Showing 24 changed files with 556 additions and 119 deletions.
7 changes: 4 additions & 3 deletions cmd/controller_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"syscall"
"time"

"github.com/zapier/kubechecks/pkg/events"

_ "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/events"
"github.com/zapier/kubechecks/pkg/server"
)

Expand All @@ -25,7 +26,7 @@ var ControllerCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Starting KubeChecks:", pkg.GitTag, pkg.GitCommit)

server := server.NewServer(&pkg.ServerConfig{
server := server.NewServer(&config.ServerConfig{
UrlPrefix: viper.GetString("webhook-url-prefix"),
WebhookSecret: viper.GetString("webhook-secret"),
})
Expand Down
1 change: 1 addition & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/spf13/cobra"

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

Expand Down
1 change: 1 addition & 0 deletions hacks/env-to-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/zapier/kubechecks/cmd"
)

Expand Down
44 changes: 35 additions & 9 deletions pkg/affected_apps/argocd_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,55 @@ package affected_apps

import (
"context"
"os"

"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/app_directory"

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

type ArgocdMatcher struct {
appsDirectory *app_directory.AppDirectory
appsDirectory *config.AppDirectory
}

func NewArgocdMatcher(vcsToArgoMap pkg.VcsToArgoMap, repo *repo.Repo) *ArgocdMatcher {
log.Debug().Msgf("looking for %s repos", repo.CloneURL)
repoApps := vcsToArgoMap.GetAppsInRepo(repo.CloneURL)
func NewArgocdMatcher(vcsToArgoMap config.VcsToArgoMap, repo *repo.Repo, repoPath string) (*ArgocdMatcher, error) {
repoApps := getArgocdApps(vcsToArgoMap, repo)
kustomizeAppFiles := getKustomizeApps(vcsToArgoMap, repo, repoPath)

appDirectory := config.NewAppDirectory().
Union(repoApps).
Union(kustomizeAppFiles)

return &ArgocdMatcher{
appsDirectory: appDirectory,
}, nil
}

func logCounts(repoApps *config.AppDirectory) {
if repoApps == nil {
log.Debug().Msg("found no apps")
} else {
log.Debug().Msgf("found %d apps", repoApps.Count())
}
}

return &ArgocdMatcher{
appsDirectory: repoApps,
}
func getKustomizeApps(vcsToArgoMap config.VcsToArgoMap, repo *repo.Repo, repoPath string) *config.AppDirectory {
log.Debug().Msgf("creating fs for %s", repoPath)
fs := os.DirFS(repoPath)
log.Debug().Msg("following kustomize apps")
kustomizeAppFiles := vcsToArgoMap.WalkKustomizeApps(repo, fs)

logCounts(kustomizeAppFiles)
return kustomizeAppFiles
}

func getArgocdApps(vcsToArgoMap config.VcsToArgoMap, repo *repo.Repo) *config.AppDirectory {
log.Debug().Msgf("looking for %s repos", repo.CloneURL)
repoApps := vcsToArgoMap.GetAppsInRepo(repo.CloneURL)

logCounts(repoApps)
return repoApps
}

func (a *ArgocdMatcher) AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error) {
Expand Down
14 changes: 9 additions & 5 deletions pkg/affected_apps/argocd_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/zapier/kubechecks/pkg"

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

func TestCreateNewMatcherWithNilVcsMap(t *testing.T) {
// setup
var (
vcsMap pkg.VcsToArgoMap
repo repo2.Repo
repo repo2.Repo
path string

vcsMap = config.NewVcsToArgoMap()
)

// run test
matcher := NewArgocdMatcher(vcsMap, &repo)
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
22 changes: 9 additions & 13 deletions pkg/affected_apps/best_effort.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import (
"strings"

"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg/app_directory"
)

// TODO: move this out to config and or in an .kubechecks.yaml as well
var (
HelmPath = []string{"apps/", "argocd/", "charts/", "manifests/"}
HelmFileTypes = []string{".yaml", ".yml", ".tpl"}
KustomizeSubPaths = []string{"base/", "bases/", "components/", "overlays/", "resources/"}
"github.com/zapier/kubechecks/pkg/config"
)

var KustomizeSubPaths = []string{"base/", "bases/", "components/", "overlays/", "resources/"}

type BestEffort struct {
repoName string
repoFileList []string
Expand All @@ -29,7 +25,7 @@ func NewBestEffortMatcher(repoName string, repoFileList []string) *BestEffort {
}
}

func (b *BestEffort) AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error) {
func (b *BestEffort) AffectedApps(_ context.Context, changeList []string) (AffectedItems, error) {
appsMap := make(map[string]string)

for _, file := range changeList {
Expand Down Expand Up @@ -87,9 +83,9 @@ func (b *BestEffort) AffectedApps(ctx context.Context, changeList []string) (Aff
}
}

var appsSlice []app_directory.ApplicationStub
var appsSlice []config.ApplicationStub
for name, path := range appsMap {
appsSlice = append(appsSlice, app_directory.ApplicationStub{Name: name, Path: path})
appsSlice = append(appsSlice, config.ApplicationStub{Name: name, Path: path})
}

return AffectedItems{Applications: appsSlice}, nil
Expand All @@ -115,9 +111,9 @@ func isKustomizeApp(file string) bool {

func isKustomizeBaseComponentsChange(file string) bool {
return strings.Contains(file, "base/") ||
strings.Contains(file, "bases/") ||
strings.Contains(file, "components/") ||
strings.Contains(file, "resources/")
strings.Contains(file, "bases/") ||
strings.Contains(file, "components/") ||
strings.Contains(file, "resources/")
}

func overlaysDir(file string) string {
Expand Down
22 changes: 11 additions & 11 deletions pkg/affected_apps/best_effort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zapier/kubechecks/pkg/app_directory"
"github.com/zapier/kubechecks/pkg/config"
)

func TestBestEffortMatcher(t *testing.T) {
Expand All @@ -28,7 +28,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-echo-server", Path: "apps/echo-server/foo-eks-01/"},
},
},
Expand All @@ -42,7 +42,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-echo-server", Path: "apps/echo-server/foo-eks-01/"},
{Name: "foo-eks-02-echo-server", Path: "apps/echo-server/foo-eks-02/"},
},
Expand All @@ -58,7 +58,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-echo-server", Path: "apps/echo-server/foo-eks-01/"},
{Name: "foo-eks-02-echo-server", Path: "apps/echo-server/foo-eks-02/"},
},
Expand All @@ -75,7 +75,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-echo-server", Path: "apps/echo-server/foo-eks-01/"},
{Name: "foo-eks-02-echo-server", Path: "apps/echo-server/foo-eks-02/"},
},
Expand All @@ -90,7 +90,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-httpbin", Path: "apps/httpbin/overlays/foo-eks-01/"},
},
},
Expand All @@ -104,7 +104,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-httpbin", Path: "apps/httpbin/overlays/foo-eks-01/"},
},
},
Expand All @@ -118,7 +118,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-httpbin", Path: "apps/httpbin/overlays/foo-eks-01/"},
},
},
Expand All @@ -132,7 +132,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-httpbin", Path: "apps/httpbin/overlays/foo-eks-01/"},
},
},
Expand All @@ -146,7 +146,7 @@ func TestBestEffortMatcher(t *testing.T) {
repoName: "",
},
want: AffectedItems{
Applications: []app_directory.ApplicationStub{
Applications: []config.ApplicationStub{
{Name: "foo-eks-01-httpbin", Path: "apps/httpbin/overlays/foo-eks-01/"},
},
},
Expand Down Expand Up @@ -180,7 +180,7 @@ func appSetKey(item ApplicationSet) string {
return item.Name
}

func appStubKey(stub app_directory.ApplicationStub) string {
func appStubKey(stub config.ApplicationStub) string {
return stub.Name
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/affected_apps/config_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"strings"

"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg/app_directory"

"github.com/zapier/kubechecks/pkg/argo_client"
"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/repo_config"
)

Expand Down Expand Up @@ -40,9 +39,9 @@ func (b *ConfigMatcher) AffectedApps(ctx context.Context, changeList []string) (
appSetList = append(appSetList, ApplicationSet{appset.Name})
}

var appsSlice []app_directory.ApplicationStub
var appsSlice []config.ApplicationStub
for name, appPath := range appsMap {
appsSlice = append(appsSlice, app_directory.ApplicationStub{Name: name, Path: appPath})
appsSlice = append(appsSlice, config.ApplicationStub{Name: name, Path: appPath})
}

return AffectedItems{Applications: appsSlice, ApplicationSets: appSetList}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/affected_apps/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"path"

"github.com/zapier/kubechecks/pkg/app_directory"
"github.com/zapier/kubechecks/pkg/config"
)

type AffectedItems struct {
Applications []app_directory.ApplicationStub
Applications []config.ApplicationStub
ApplicationSets []ApplicationSet
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/commitState.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func (s CommitState) Emoji() string {
}
}

func (s CommitState) BareString() string {
text, ok := stateString[s]
if !ok {
text = defaultString
}
return text
}

func (s CommitState) String() string {
text, ok := stateString[s]
if !ok {
Expand Down
Loading

0 comments on commit 9e97ae8

Please sign in to comment.