Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module/git): allow module.git use cached content when failed to fetch #5712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ Main (unreleased)

- Updated windows exporter to use prometheus-community/windows_exporter commit 1836cd1. (@mattdurham)

- Allow agent to start with `module.git` config if cached before. (@hainenber)

### Bugfixes

- Set exit code 1 on grafana-agentctl non-runnable command. (@fgouteroux)
Expand Down
18 changes: 16 additions & 2 deletions component/module/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git

import (
"context"
"errors"
"path/filepath"
"reflect"
"sync"
Expand Down Expand Up @@ -91,8 +92,15 @@ func New(o component.Options, args Arguments) (*Component, error) {
argsChanged: make(chan struct{}, 1),
}

// Only acknowledge the error from Update if it's not a
// vcs.UpdateFailedError; vcs.UpdateFailedError means that the Git repo
// exists but we were just unable to update it.
if err := c.Update(args); err != nil {
return nil, err
if errors.As(err, &vcs.UpdateFailedError{}) {
level.Error(c.log).Log("msg", "failed to update repository", "err", err)
} else {
return nil, err
}
}
return c, nil
}
Expand Down Expand Up @@ -193,10 +201,16 @@ func (c *Component) Update(args component.Arguments) (err error) {
}

// Create or update the repo field.
// Failure to update repository makes the module loader temporarily use cached contents on disk
if c.repo == nil || !reflect.DeepEqual(repoOpts, c.repoOpts) {
r, err := vcs.NewGitRepo(context.Background(), repoPath, repoOpts)
if err != nil {
return err
if errors.As(err, &vcs.UpdateFailedError{}) {
level.Error(c.log).Log("msg", "failed to update repository", "err", err)
c.updateHealth(err)
} else {
return err
}
Comment on lines +208 to +213
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm following whether this is necessary still; with the latest change, it's currently the callers responsibility for whether UpdateFailedError needs separate handling or not.

Am I missing something that this check is changing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The err check for UpdateFailedError in New method is to check for returned error from pollFile method.

This check right here is to inform Flow controller component's unhealthiness from failing to update Git repo and to continue assigning the Repo object and options to internal attributes. Returning early will leave the attributes, namely r.repo empty and can cause null pointer exception, I'd wager.

}
c.repo = r
c.repoOpts = repoOpts
Expand Down
28 changes: 18 additions & 10 deletions component/module/git/internal/vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,24 @@ func NewGitRepo(ctx context.Context, storagePath string, opts GitRepoOptions) (*
}

// Fetch the latest contents. This may be a no-op if we just did a clone.
err = repo.FetchContext(ctx, &git.FetchOptions{
fetchRepoErr := repo.FetchContext(ctx, &git.FetchOptions{
RemoteName: "origin",
Force: true,
Auth: opts.Auth.Convert(),
})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil, UpdateFailedError{
Repository: opts.Repository,
Inner: err,
if fetchRepoErr != nil && !errors.Is(fetchRepoErr, git.NoErrAlreadyUpToDate) {
workTree, err := repo.Worktree()
if err != nil {
return nil, err
}
return &GitRepo{
opts: opts,
repo: repo,
workTree: workTree,
}, UpdateFailedError{
Repository: opts.Repository,
Inner: fetchRepoErr,
}
}

// Finally, hard reset to our requested revision.
Expand All @@ -92,7 +100,7 @@ func NewGitRepo(ctx context.Context, storagePath string, opts GitRepoOptions) (*
opts: opts,
repo: repo,
workTree: workTree,
}, nil
}, err
}

func isRepoCloned(dir string) bool {
Expand All @@ -103,15 +111,16 @@ func isRepoCloned(dir string) bool {
// Update updates the repository by fetching new content and re-checking out to
// latest version of Revision.
func (repo *GitRepo) Update(ctx context.Context) error {
err := repo.repo.FetchContext(ctx, &git.FetchOptions{
var err error
fetchRepoErr := repo.repo.FetchContext(ctx, &git.FetchOptions{
RemoteName: "origin",
Force: true,
Auth: repo.opts.Auth.Convert(),
})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
if fetchRepoErr != nil && !errors.Is(fetchRepoErr, git.NoErrAlreadyUpToDate) {
return UpdateFailedError{
Repository: repo.opts.Repository,
Inner: err,
Inner: fetchRepoErr,
}
}

Expand All @@ -120,7 +129,6 @@ func (repo *GitRepo) Update(ctx context.Context) error {
if err != nil {
return InvalidRevisionError{Revision: repo.opts.Revision}
}

err = repo.workTree.Reset(&git.ResetOptions{
Commit: hash,
Mode: git.HardReset,
Expand Down