Skip to content

Commit

Permalink
Try to prevent data race
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBergmeier6176 committed Mar 5, 2024
1 parent 76685de commit ab84c4d
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions pkg/dockerimage/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"strings"
"sync"
)

const (
Expand All @@ -14,16 +15,37 @@ const (
remoteDigestPrefix = "latest: digest: sha256:"
)

var (
DigestOutputError = digestOutputError{
errors.New("no digest in output found"),
}
)

type digestOutputError struct {
error
}

func (err digestOutputError) Is(other error) bool {
_, ok := other.(digestOutputError)
return ok
}

// Push calls docker and pushes a tag.
// Returns the remote digest for the pushed image.
func PushWithWriters(tag string, out io.Writer, errOut io.Writer) (string, error) {
r, w := io.Pipe()
defer r.Close()
defer w.Close()

digest := make(chan string)
defer close(digest)
digest := make(chan string, 1)
errs := make(chan error, 1)
errG := sync.WaitGroup{}

errG.Add(1)
go func() {
defer close(digest)
defer errG.Done()

scanner := bufio.NewScanner(r)
for scanner.Scan() {
if !strings.HasPrefix(scanner.Text(), remoteDigestPrefix) {
Expand All @@ -33,18 +55,32 @@ func PushWithWriters(tag string, out io.Writer, errOut io.Writer) (string, error
digest <- scanner.Text()[len(remoteDigestPrefix) : len(remoteDigestPrefix)+64]
return
}
errs <- DigestOutputError
}()
err := executeDockerCommandWithWriters(io.MultiWriter(out, w), errOut, "push", tag)
if err != nil {
return "", err
}

select {
case d := <-digest:
errG.Add(1)
go func() {
defer errG.Done()

errs <- executeDockerCommandWithWriters(io.MultiWriter(out, w), errOut, "push", tag)
}()

go func() {
defer close(errs)

errG.Wait()
}()

d, ok := <-digest
if ok {
return d, nil
default:
return "", errors.New("no digest in output found")
}
for err := range errs {
if err != nil {
return "", err
}
}
panic("should have resulted in error before")
}

// Push calls docker and pushes a tag.
Expand Down

0 comments on commit ab84c4d

Please sign in to comment.