Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MeNsaaH committed Jan 11, 2024
1 parent ba14d23 commit 5632270
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 2 additions & 2 deletions cmd/controller_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 17 additions & 11 deletions pkg/app_watcher/app_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,29 @@ 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)
if err != nil {
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) {
Expand All @@ -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)
Expand All @@ -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)
}

/*
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/app_watcher/app_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 5 additions & 6 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"testing"

"github.com/zapier/kubechecks/pkg/config"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 5632270

Please sign in to comment.