Skip to content

Commit

Permalink
refactoring, moved code around, generate manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Nov 6, 2024
1 parent 10c2ed0 commit 37e31f1
Show file tree
Hide file tree
Showing 15 changed files with 592 additions and 226 deletions.
123 changes: 0 additions & 123 deletions cmd/container.go

This file was deleted.

20 changes: 19 additions & 1 deletion cmd/controller_cmd.go → cmd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zapier/kubechecks/pkg/app_watcher"

"github.com/zapier/kubechecks/pkg"
"github.com/zapier/kubechecks/pkg/checks"
Expand Down Expand Up @@ -41,11 +42,28 @@ var ControllerCmd = &cobra.Command{
log.Fatal().Err(err).Msg("failed to parse configuration")
}

ctr, err := newContainer(ctx, cfg, true)
ctr, err := container.New(ctx, cfg)
if err != nil {
log.Fatal().Err(err).Msg("failed to create container")
}

// watch app modifications, if necessary
if cfg.MonitorAllApplications {
appWatcher, err := app_watcher.NewApplicationWatcher(ctr)
if err != nil {
log.Fatal().Err(err).Msg("failed to create watch applications")
}
go appWatcher.Run(ctx, 1)

appSetWatcher, err := app_watcher.NewApplicationSetWatcher(ctr)
if err != nil {
log.Fatal().Err(err).Msg("failed to create watch application sets")
}
go appSetWatcher.Run(ctx)
} else {
log.Info().Msgf("not monitoring applications, MonitorAllApplications: %+v", cfg.MonitorAllApplications)
}

log.Info().Msg("initializing git settings")
if err = initializeGit(ctr); err != nil {
log.Fatal().Err(err).Msg("failed to initialize git settings")
Expand Down
37 changes: 35 additions & 2 deletions cmd/process.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cmd

import (
"os"
"path/filepath"

"github.com/argoproj/argo-cd/v2/common"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/zapier/kubechecks/pkg/container"

"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/server"
Expand All @@ -15,14 +20,42 @@ var processCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

tempPath, err := os.MkdirTemp("", "")
if err != nil {
log.Fatal().Err(err).Msg("fail to create ssh data dir")
}
defer func() {
os.RemoveAll(tempPath)
}()

// symlink local ssh known hosts to argocd ssh known hosts
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal().Err(err).Msg("failed to get user home dir")
}
source := filepath.Join(homeDir, ".ssh", "known_hosts")
target := filepath.Join(tempPath, common.DefaultSSHKnownHostsName)

if err := os.Symlink(source, target); err != nil {
log.Fatal().Err(err).Msg("fail to symlink ssh_known_hosts file")
}

if err := os.Setenv("ARGOCD_SSH_DATA_PATH", tempPath); err != nil {
log.Fatal().Err(err).Msg("fail to set ARGOCD_SSH_DATA_PATH")
}

cfg, err := config.New()
if err != nil {
log.Fatal().Err(err).Msg("failed to generate config")
}

ctr, err := newContainer(ctx, cfg, false)
if len(args) != 1 {
log.Fatal().Msg("usage: kubechecks process PR_REF")
}

ctr, err := container.New(ctx, cfg)
if err != nil {
log.Fatal().Err(err).Msg("failed to create container")
log.Fatal().Err(err).Msg("failed to create clients")
}

log.Info().Msg("initializing git settings")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
github.com/prometheus/client_golang v1.20.2
github.com/rikatz/kubepug v1.4.0
github.com/rs/zerolog v1.32.0
github.com/rs/zerolog v1.33.0
github.com/sashabaranov/go-openai v1.31.0
github.com/shurcooL/githubv4 v0.0.0-20231126234147-1cffa1f02456
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
9 changes: 4 additions & 5 deletions pkg/affected_apps/argocd_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg/appdir"
"github.com/zapier/kubechecks/pkg/container"
"github.com/zapier/kubechecks/pkg/git"
)

Expand All @@ -15,7 +14,7 @@ type ArgocdMatcher struct {
appSetsDirectory *appdir.AppSetDirectory
}

func NewArgocdMatcher(vcsToArgoMap container.VcsToArgoMap, repo *git.Repo) (*ArgocdMatcher, error) {
func NewArgocdMatcher(vcsToArgoMap appdir.VcsToArgoMap, repo *git.Repo) (*ArgocdMatcher, error) {
repoApps := getArgocdApps(vcsToArgoMap, repo)
kustomizeAppFiles := getKustomizeApps(vcsToArgoMap, repo, repo.Directory)

Expand All @@ -41,7 +40,7 @@ func logCounts(repoApps *appdir.AppDirectory) {
}
}

func getKustomizeApps(vcsToArgoMap container.VcsToArgoMap, repo *git.Repo, repoPath string) *appdir.AppDirectory {
func getKustomizeApps(vcsToArgoMap appdir.VcsToArgoMap, repo *git.Repo, repoPath string) *appdir.AppDirectory {
log.Debug().Msgf("creating fs for %s", repoPath)
fs := os.DirFS(repoPath)
log.Debug().Msg("following kustomize apps")
Expand All @@ -51,15 +50,15 @@ func getKustomizeApps(vcsToArgoMap container.VcsToArgoMap, repo *git.Repo, repoP
return kustomizeAppFiles
}

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

logCounts(repoApps)
return repoApps
}

func getArgocdAppSets(vcsToArgoMap container.VcsToArgoMap, repo *git.Repo) *appdir.AppSetDirectory {
func getArgocdAppSets(vcsToArgoMap appdir.VcsToArgoMap, repo *git.Repo) *appdir.AppSetDirectory {
log.Debug().Msgf("looking for %s repos", repo.CloneURL)
repoApps := vcsToArgoMap.GetAppSetsInRepo(repo.CloneURL)

Expand Down
14 changes: 7 additions & 7 deletions pkg/app_watcher/app_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
informers "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions/application/v1alpha1"
applisters "github.com/argoproj/argo-cd/v2/pkg/client/listers/application/v1alpha1"
"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg/appdir"
"github.com/zapier/kubechecks/pkg/container"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"

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

Expand All @@ -34,16 +34,16 @@ type ApplicationWatcher struct {
// - kubeCfg is the Kubernetes configuration.
// - vcsToArgoMap is the mapping between VCS and Argo applications.
// - cfg is the server configuration.
func NewApplicationWatcher(kubeCfg *rest.Config, vcsToArgoMap appdir.VcsToArgoMap, cfg config.ServerConfig) (*ApplicationWatcher, error) {
if kubeCfg == nil {
func NewApplicationWatcher(ctr container.Container) (*ApplicationWatcher, error) {
if ctr.KubeClientSet == nil {
return nil, fmt.Errorf("kubeCfg cannot be nil")
}
ctrl := ApplicationWatcher{
applicationClientset: appclientset.NewForConfigOrDie(kubeCfg),
vcsToArgoMap: vcsToArgoMap,
applicationClientset: appclientset.NewForConfigOrDie(ctr.KubeClientSet.Config()),
vcsToArgoMap: ctr.VcsToArgoMap,
}

appInformer, appLister := ctrl.newApplicationInformerAndLister(time.Second*30, cfg)
appInformer, appLister := ctrl.newApplicationInformerAndLister(time.Second*30, ctr.Config)

ctrl.appInformer = appInformer
ctrl.appLister = appLister
Expand Down
12 changes: 6 additions & 6 deletions pkg/app_watcher/appset_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/rs/zerolog/log"
"github.com/zapier/kubechecks/pkg/appdir"
"github.com/zapier/kubechecks/pkg/config"
"github.com/zapier/kubechecks/pkg/container"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)

Expand All @@ -28,16 +28,16 @@ type ApplicationSetWatcher struct {
}

// NewApplicationSetWatcher creates new instance of ApplicationWatcher.
func NewApplicationSetWatcher(kubeCfg *rest.Config, vcsToArgoMap appdir.VcsToArgoMap, cfg config.ServerConfig) (*ApplicationSetWatcher, error) {
if kubeCfg == nil {
func NewApplicationSetWatcher(ctr container.Container) (*ApplicationSetWatcher, error) {
if ctr.KubeClientSet == nil {
return nil, fmt.Errorf("kubeCfg cannot be nil")
}
ctrl := ApplicationSetWatcher{
applicationClientset: appclientset.NewForConfigOrDie(kubeCfg),
vcsToArgoMap: vcsToArgoMap,
applicationClientset: appclientset.NewForConfigOrDie(ctr.KubeClientSet.Config()),
vcsToArgoMap: ctr.VcsToArgoMap,
}

appInformer, appLister := ctrl.newApplicationSetInformerAndLister(time.Second*30, cfg)
appInformer, appLister := ctrl.newApplicationSetInformerAndLister(time.Second*30, ctr.Config)

ctrl.appInformer = appInformer
ctrl.appLister = appLister
Expand Down
Loading

0 comments on commit 37e31f1

Please sign in to comment.