diff --git a/Earthfile b/Earthfile index eb7af81b..0fe49fec 100644 --- a/Earthfile +++ b/Earthfile @@ -159,13 +159,13 @@ fmt-golang: && ./hacks/exit-on-changed-files.sh lint-golang: - ARG STATICCHECK_VERSION="2023.1.3" + ARG STATICCHECK_VERSION="2023.1.6" FROM +go-deps # install staticcheck RUN FILE=staticcheck.tgz \ - && URL=https://github.com/dominikh/go-tools/releases/download/$STATICCHECK_VERSION/staticcheck_linux_amd64.tar.gz \ + && URL=https://github.com/dominikh/go-tools/releases/download/$STATICCHECK_VERSION/staticcheck_linux_$GOARCH.tar.gz \ && wget ${URL} \ --output-document ${FILE} \ && tar \ diff --git a/cmd/controller_cmd.go b/cmd/controller_cmd.go index 6b89d88f..03f3899f 100644 --- a/cmd/controller_cmd.go +++ b/cmd/controller_cmd.go @@ -44,9 +44,9 @@ var ControllerCmd = &cobra.Command{ } fmt.Println("Starting KubeChecks:", pkg.GitTag, pkg.GitCommit) - server := server.NewServer(&cfg) - ctx := context.Background() + server := server.NewServer(ctx, &cfg) + go server.Start(ctx) // graceful termination handler. diff --git a/pkg/app_watcher/app_watcher.go b/pkg/app_watcher/app_watcher.go index cbdd6b57..39c2ae31 100644 --- a/pkg/app_watcher/app_watcher.go +++ b/pkg/app_watcher/app_watcher.go @@ -69,7 +69,8 @@ func (ctrl *ApplicationWatcher) Run(ctx context.Context, processors int) { // onAdd is the function executed when the informer notifies the // presence of a new Application in the namespace func (ctrl *ApplicationWatcher) onApplicationAdded(obj interface{}) { - if !canProcessApp(obj) { + app, ok := canProcessApp(obj) + if !ok { return } key, err := cache.MetaNamespaceKeyFunc(obj) @@ -77,16 +78,20 @@ func (ctrl *ApplicationWatcher) onApplicationAdded(obj interface{}) { log.Error().Err(err).Msg("appwatcher: could not get key for added application") } log.Debug().Str("key", key).Msg("appwatcher: onApplicationAdded") - ctrl.cfg.VcsToArgoMap.AddApp(obj.(*appv1alpha1.Application)) + ctrl.cfg.VcsToArgoMap.AddApp(app) } func (ctrl *ApplicationWatcher) onApplicationUpdated(old, new interface{}) { + newApp, newOk := canProcessApp(new) + oldApp, oldOk := canProcessApp(old) + if !newOk || !oldOk { + return + } + key, err := cache.MetaNamespaceKeyFunc(new) if err != nil { log.Warn().Err(err).Msg("appwatcher: could not get key for updated application") } - oldApp := old.(*appv1alpha1.Application) - newApp := new.(*appv1alpha1.Application) // We want to update when any of Source or Sources parameters has changed if !reflect.DeepEqual(oldApp.Spec.Source, newApp.Spec.Source) || !reflect.DeepEqual(oldApp.Spec.Sources, newApp.Spec.Sources) { @@ -97,7 +102,8 @@ func (ctrl *ApplicationWatcher) onApplicationUpdated(old, new interface{}) { } func (ctrl *ApplicationWatcher) onApplicationDeleted(obj interface{}) { - if !canProcessApp(obj) { + app, ok := canProcessApp(obj) + if !ok { return } key, err := cache.MetaNamespaceKeyFunc(obj) @@ -106,7 +112,7 @@ func (ctrl *ApplicationWatcher) onApplicationDeleted(obj interface{}) { } log.Debug().Str("key", key).Msg("appwatcher: onApplicationDeleted") - ctrl.cfg.VcsToArgoMap.DeleteApp(obj.(*appv1alpha1.Application)) + ctrl.cfg.VcsToArgoMap.DeleteApp(app) } /* @@ -130,23 +136,23 @@ func (ctrl *ApplicationWatcher) newApplicationInformerAndLister(refreshTimeout t return informer, lister } -func canProcessApp(obj interface{}) bool { +func canProcessApp(obj interface{}) (*appv1alpha1.Application, bool) { app, ok := obj.(*appv1alpha1.Application) if !ok { - return false + return &appv1alpha1.Application{}, false } for _, src := range app.Spec.Sources { if isGitRepo(src.RepoURL) { - return true + return app, true } } if !isGitRepo(app.Spec.Source.RepoURL) { - return false + return app, false } - return true + return app, true } func isGitRepo(url string) bool { diff --git a/pkg/app_watcher/app_watcher_test.go b/pkg/app_watcher/app_watcher_test.go index 98953eff..e47dc85c 100644 --- a/pkg/app_watcher/app_watcher_test.go +++ b/pkg/app_watcher/app_watcher_test.go @@ -36,7 +36,7 @@ func initTestObjects() *ApplicationWatcher { }, } - appInformer, appLister := ctrl.newApplicationInformerAndLister(time.Millisecond * 500) + appInformer, appLister := ctrl.newApplicationInformerAndLister(time.Second * 1) ctrl.appInformer = appInformer ctrl.appLister = appLister diff --git a/pkg/server/server.go b/pkg/server/server.go index be35585b..032af4ab 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -27,10 +27,10 @@ type Server struct { appWatcher *app_watcher.ApplicationWatcher } -func NewServer(cfg *config.ServerConfig) *Server { +func NewServer(ctx context.Context, cfg *config.ServerConfig) *Server { var appWatcher *app_watcher.ApplicationWatcher if viper.GetBool("monitor-all-applications") { - argoMap, err := config.BuildAppsMap(context.TODO()) + argoMap, err := config.BuildAppsMap(ctx) if err != nil { log.Fatal().Err(err).Msg("could not build VcsToArgoMap") } @@ -49,10 +49,10 @@ func NewServer(cfg *config.ServerConfig) *Server { func (s *Server) Start(ctx context.Context) { if s.appWatcher != nil { - go s.appWatcher.Run(context.Background(), 1) + go s.appWatcher.Run(ctx, 1) } - if err := s.ensureWebhooks(); err != nil { + if err := s.ensureWebhooks(ctx); err != nil { log.Warn().Err(err).Msg("failed to create webhooks") } @@ -94,7 +94,7 @@ func (s *Server) hooksPrefix() string { return strings.TrimSuffix(serverUrl, "/") } -func (s *Server) ensureWebhooks() error { +func (s *Server) ensureWebhooks(ctx context.Context) error { if !viper.GetBool("ensure-webhooks") { return nil } @@ -110,7 +110,6 @@ func (s *Server) ensureWebhooks() error { log.Info().Msg("ensuring all webhooks are created correctly") - ctx := context.TODO() vcsClient := s.cfg.VcsClient fullUrl, err := url.JoinPath(urlBase, s.hooksPrefix(), vcsClient.GetName(), "project") diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index bffa5541..a9e29719 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -1,6 +1,7 @@ package server import ( + "context" "testing" "github.com/zapier/kubechecks/pkg/config" @@ -50,7 +51,7 @@ func TestHooksPrefix(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := NewServer(tt.cfg) + s := NewServer(context.TODO(), tt.cfg) if got := s.hooksPrefix(); got != tt.want { t.Errorf("hooksPrefix() = %v, want %v", got, tt.want) }