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

Commit

Permalink
Pseudo randomize push check tag (#2684)
Browse files Browse the repository at this point in the history
Pseudo randomize push check tag
  • Loading branch information
hiddeco authored Dec 11, 2019
2 parents 0cfe85c + c5158c1 commit 3b5c66d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pkg/git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -98,19 +99,26 @@ func add(ctx context.Context, workingDir, path string) error {
// (being able to `clone` is an adequate check that we can read the
// upstream).
func checkPush(ctx context.Context, workingDir, upstream, branch string) error {
// --force just in case we fetched the tag from upstream when cloning
args := []string{"tag", "--force", CheckPushTag}
// we need to pseudo randomize the tag we use for the write check
// as multiple Flux instances can perform the check simultaneously
// for different branches, causing commit reference conflicts
b := make([]byte, 5)
if _, err := rand.Read(b); err != nil {
return err
}
pseudoRandPushTag := fmt.Sprintf("%s-%x", CheckPushTagPrefix, b)
args := []string{"tag", pseudoRandPushTag}
if branch != "" {
args = append(args, branch)
}
if err := execGitCmd(ctx, args, gitCmdConfig{dir: workingDir}); err != nil {
return errors.Wrap(err, "tag for write check")
}
args = []string{"push", "--force", upstream, "tag", CheckPushTag}
args = []string{"push", upstream, "tag", pseudoRandPushTag}
if err := execGitCmd(ctx, args, gitCmdConfig{dir: workingDir}); err != nil {
return errors.Wrap(err, "attempt to push tag")
}
return deleteTag(ctx, workingDir, CheckPushTag, upstream)
return deleteTag(ctx, workingDir, pseudoRandPushTag, upstream)
}

// deleteTag deletes the given git tag
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
defaultInterval = 5 * time.Minute
defaultTimeout = 20 * time.Second

CheckPushTag = "flux-write-check"
CheckPushTagPrefix = "flux-write-check"
)

var (
Expand Down

0 comments on commit 3b5c66d

Please sign in to comment.