diff --git a/.github/workflows/test_unit.yml b/.github/workflows/test_unit.yml index 3bbdc3d1..a2a6a890 100644 --- a/.github/workflows/test_unit.yml +++ b/.github/workflows/test_unit.yml @@ -25,4 +25,4 @@ jobs: go mod download go install github.com/matryer/moq@v0.3.3 go generate ./... - go test --race --coverprofile cover.out -v ./... \ No newline at end of file + go test --race --count=1 --coverprofile cover.out -v ./... \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 76b30808..d850a9b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: hooks: - id: go-mod-tidy-repo - id: go-test-repo-mod - args: [ -race ] + args: [ -race, -count=1 ] - id: go-vet-repo-mod - id: go-fumpt-repo args: [ -l, -w ] diff --git a/pkg/sparrow/gitlab/gitlab.go b/pkg/sparrow/gitlab/gitlab.go index a63f4219..5527de88 100644 --- a/pkg/sparrow/gitlab/gitlab.go +++ b/pkg/sparrow/gitlab/gitlab.go @@ -43,7 +43,7 @@ func New(baseURL, token string, pid int) Gitlab { // FetchFiles fetches the files from the global targets repository from the configured gitlab repository func (g *Client) FetchFiles(ctx context.Context) ([]checks.GlobalTarget, error) { - log := logger.FromContext(ctx).With("name", "FetchFiles") + log := logger.FromContext(ctx) fl, err := g.fetchFileList(ctx) if err != nil { log.Error("Failed to fetch files", "error", err) @@ -111,7 +111,7 @@ func (g *Client) fetchFile(ctx context.Context, f string) (checks.GlobalTarget, // fetchFileList fetches the filenames from the global targets repository from the configured gitlab repository, // so they may be fetched individually func (g *Client) fetchFileList(ctx context.Context) ([]string, error) { - log := logger.FromContext(ctx).With("name", "fetchFileList") + log := logger.FromContext(ctx) log.Debug("Fetching global files") type file struct { Name string `json:"name"` @@ -160,7 +160,7 @@ func (g *Client) fetchFileList(ctx context.Context) ([]string, error) { // PutFile commits the current instance to the configured gitlab repository // as a global target for other sparrow instances to discover func (g *Client) PutFile(ctx context.Context, body File) error { //nolint: dupl,gocritic // no need to refactor yet - log := logger.FromContext(ctx).With("name", "AddRegistration") + log := logger.FromContext(ctx) log.Debug("Registering sparrow instance to gitlab") // chose method based on whether the registration has already happened @@ -202,7 +202,7 @@ func (g *Client) PutFile(ctx context.Context, body File) error { //nolint: dupl, // PostFile commits the current instance to the configured gitlab repository // as a global target for other sparrow instances to discover func (g *Client) PostFile(ctx context.Context, body File) error { //nolint:dupl,gocritic // no need to refactor yet - log := logger.FromContext(ctx).With("name", "AddRegistration") + log := logger.FromContext(ctx) log.Debug("Registering sparrow instance to gitlab") // chose method based on whether the registration has already happened diff --git a/pkg/sparrow/targets/gitlab.go b/pkg/sparrow/targets/gitlab.go index 6804a45e..c68159eb 100644 --- a/pkg/sparrow/targets/gitlab.go +++ b/pkg/sparrow/targets/gitlab.go @@ -52,7 +52,7 @@ func NewGitlabManager(g gitlab.Gitlab, name string, checkInterval, unhealthyThre // The global targets are evaluated for healthiness and // unhealthy gitlabTargetManager are removed. func (t *gitlabTargetManager) Reconcile(ctx context.Context) { - log := logger.FromContext(ctx).With("name", "ReconcileGlobalTargets") + log := logger.FromContext(ctx) log.Debug("Starting global gitlabTargetManager reconciler") checkTimer := time.NewTimer(t.checkInterval) @@ -103,18 +103,24 @@ func (t *gitlabTargetManager) GetTargets() []checks.GlobalTarget { // Shutdown shuts down the gitlabTargetManager and deletes the file containing // the sparrow's registration from Gitlab func (t *gitlabTargetManager) Shutdown(ctx context.Context) error { - log := logger.FromContext(ctx).With("name", "Shutdown") - log.Debug("Shutting down global gitlabTargetManager") t.mu.Lock() defer t.mu.Unlock() + log := logger.FromContext(ctx) + log.Info("Shutting down global gitlabTargetManager") t.registered = false - t.done <- struct{}{} + + select { + case t.done <- struct{}{}: + log.Debug("Stopping reconcile routine") + default: + } + return nil } // updateRegistration registers the current instance as a global target func (t *gitlabTargetManager) updateRegistration(ctx context.Context) error { - log := logger.FromContext(ctx).With("name", t.name, "registered", t.registered) + log := logger.FromContext(ctx) log.Debug("Updating registration") f := gitlab.File{ @@ -154,10 +160,10 @@ func (t *gitlabTargetManager) updateRegistration(ctx context.Context) error { // refreshTargets updates the targets of the gitlabTargetManager // with the latest available healthy targets func (t *gitlabTargetManager) refreshTargets(ctx context.Context) error { - log := logger.FromContext(ctx).With("name", "updateGlobalTargets") + log := logger.FromContext(ctx) t.mu.Lock() - var healthyTargets []checks.GlobalTarget defer t.mu.Unlock() + var healthyTargets []checks.GlobalTarget targets, err := t.gitlab.FetchFiles(ctx) if err != nil { log.Error("Failed to update global targets", "error", err) @@ -178,8 +184,9 @@ func (t *gitlabTargetManager) refreshTargets(ctx context.Context) error { return nil } +// Registered returns whether the instance is registered as a global target func (t *gitlabTargetManager) Registered() bool { - t.mu.RLock() - defer t.mu.RUnlock() + t.mu.Lock() + defer t.mu.Unlock() return t.registered } diff --git a/pkg/sparrow/targets/gitlab_test.go b/pkg/sparrow/targets/gitlab_test.go index 7ab7bd55..fce9400c 100644 --- a/pkg/sparrow/targets/gitlab_test.go +++ b/pkg/sparrow/targets/gitlab_test.go @@ -363,7 +363,7 @@ func Test_gitlabTargetManager_Reconcile_Context_Canceled(t *testing.T) { time.Sleep(time.Millisecond * 250) cancel() - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Millisecond * 250) // instance shouldn't be registered anymore if gtm.Registered() {