Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Include more information re "git repo not ready" #1171

Merged
merged 2 commits into from
Jul 2, 2018
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
3 changes: 2 additions & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ func (d *Daemon) getPolicyResourceMap(ctx context.Context) (policy.ResourceMap,

// The reason something is missing from the map differs depending
// on the state of the git repo.
_, notReady := err.(git.NotReadyError)
switch {
case err == git.ErrNotReady:
case notReady:
globalReadOnly = v6.ReadOnlyNotReady
case err == git.ErrNoConfig:
globalReadOnly = v6.ReadOnlyNoRepo
Expand Down
9 changes: 3 additions & 6 deletions git/gittest/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,14 @@ func execCommand(cmd string, args ...string) error {
}

func WaitForRepoReady(r *git.Repo, t *testing.T) {
retries := 5
retries := 30
for {
s, err := r.Status()
if err != nil {
t.Fatal(err)
}
s, _ := r.Status()
if s == git.RepoReady {
return
}
if retries == 0 {
t.Fatalf("repo was not ready after 5 seconds")
t.Fatalf("repo was not ready after 3 seconds")
return
}
retries--
Expand Down
42 changes: 29 additions & 13 deletions git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@ const (
)

var (
ErrNoChanges = errors.New("no changes made in repo")
ErrNotReady = errors.New("git repo not ready")
ErrNoConfig = errors.New("git repo does not have valid config")
ErrNoChanges = errors.New("no changes made in repo")
ErrNoConfig = errors.New("git repo does not have valid config")
ErrNotCloned = errors.New("git repo has not been cloned yet")
ErrClonedOnly = errors.New("git repo has been cloned but not yet checked for write access")
)

type NotReadyError struct {
underlying error
}

func (err NotReadyError) Error() string {
return "git repo not ready: " + err.underlying.Error()
}

// GitRepoStatus represents the progress made synchronising with a git
// repo. These are given below in expected order, but the status may
// go backwards if e.g., a deploy key is deleted.
Expand Down Expand Up @@ -76,7 +85,7 @@ func NewRepo(origin Remote, opts ...Option) *Repo {
origin: origin,
status: status,
interval: defaultInterval,
err: nil,
err: ErrNotCloned,
notify: make(chan struct{}, 1), // `1` so that Notify doesn't block
C: make(chan struct{}, 1), // `1` so we don't block on completing a refresh
}
Expand Down Expand Up @@ -114,21 +123,28 @@ func (r *Repo) Clean() {
}

// Status reports that readiness status of this Git repo: whether it
// has been cloned, whether it is writable, and if not, the error
// stopping it getting to the next state.
// has been cloned and is writable, and if not, the error stopping it
// getting to the next state.
func (r *Repo) Status() (GitRepoStatus, error) {
r.mu.RLock()
defer r.mu.RUnlock()
return r.status, r.err
}

func (r *Repo) setStatus(s GitRepoStatus, err error) {
func (r *Repo) setUnready(s GitRepoStatus, err error) {
r.mu.Lock()
r.status = s
r.err = err
r.mu.Unlock()
}

func (r *Repo) setReady() {
r.mu.Lock()
r.status = RepoReady
r.err = nil
r.mu.Unlock()
}

// Notify tells the repo that it should fetch from the origin as soon
// as possible. It does not block.
func (r *Repo) Notify() {
Expand Down Expand Up @@ -157,7 +173,7 @@ func (r *Repo) errorIfNotReady() error {
case RepoNoConfig:
return ErrNoConfig
default:
return ErrNotReady
return NotReadyError{r.err}
}
}

Expand Down Expand Up @@ -229,29 +245,29 @@ func (r *Repo) Start(shutdown <-chan struct{}, done *sync.WaitGroup) error {
r.mu.Unlock()
}
if err == nil {
r.setStatus(RepoCloned, nil)
r.setUnready(RepoCloned, ErrClonedOnly)
continue // with new status, skipping timer
}
dir = ""
os.RemoveAll(rootdir)
r.setStatus(RepoNew, err)
r.setUnready(RepoNew, err)

case RepoCloned:
ctx, cancel := context.WithTimeout(bg, opTimeout)
err := checkPush(ctx, dir, url)
cancel()
if err == nil {
r.setStatus(RepoReady, nil)
r.setReady()
// Treat every transition to ready as a refresh, so
// that any listeners can respond in the same way.
r.refreshed()
continue // with new status, skipping timer
}
r.setStatus(RepoCloned, err)
r.setUnready(RepoCloned, err)

case RepoReady:
if err := r.refreshLoop(shutdown); err != nil {
r.setStatus(RepoNew, err)
r.setUnready(RepoNew, err)
continue // with new status, skipping timer
}
}
Expand Down