Skip to content

Commit

Permalink
simplify download artifact
Browse files Browse the repository at this point in the history
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
  • Loading branch information
nikpivkin committed Oct 1, 2024
1 parent 9fe203f commit d19ea06
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions pkg/oci/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,31 @@ func DownloadArtifact(ctx context.Context, artifacts []*Artifact, dst string, op
return nil
}

if !shouldTryOtherRepo(err, i, len(artifacts)) {
return xerrors.Errorf("failed to download artifact from %s: %w", art.Repository(), err)
}
log.Error("Failed to download artifact", log.String("repo", art.Repository()), log.Err(err))
log.Info("Trying to download artifact from other repository...")
}

return xerrors.New("failed to download artifact from any source")
}

func shouldTryOtherRepo(err error, current, total int) bool {
var terr *transport.Error
if !errors.As(err, &terr) {
return false
}

var terr *transport.Error
if errors.As(err, &terr) {
for _, diagnostic := range terr.Errors {
// For better user experience
if diagnostic.Code == transport.DeniedErrorCode || diagnostic.Code == transport.UnauthorizedErrorCode {
// e.g. https://aquasecurity.github.io/trivy/latest/docs/references/troubleshooting/#db
log.Warnf("See %s", doc.URL("/docs/references/troubleshooting/", "db"))
break
}
}

// try the following artifact only if a temporary error occurs
if terr.Temporary() && i < len(artifacts)-1 {
log.Info("Trying to download artifact from other repository...")
continue
}
for _, diagnostic := range terr.Errors {
// For better user experience
if diagnostic.Code == transport.DeniedErrorCode || diagnostic.Code == transport.UnauthorizedErrorCode {
// e.g. https://aquasecurity.github.io/trivy/latest/docs/references/troubleshooting/#db
log.Warnf("See %s", doc.URL("/docs/references/troubleshooting/", "db"))
break
}
return xerrors.Errorf("failed to download artifact from %s", art.Repository())
}

return xerrors.New("failed to download artifact from any source")
// try the following artifact only if a temporary error occurs
return terr.Temporary() && current < total-1
}

0 comments on commit d19ea06

Please sign in to comment.