From 179e628572faebb97bb5ac6364c5e619ab811337 Mon Sep 17 00:00:00 2001 From: nikpivkin Date: Tue, 1 Oct 2024 11:51:05 +0600 Subject: [PATCH] simplify download artifact Signed-off-by: nikpivkin --- pkg/oci/artifact.go | 42 +++++++++++++++++++++++----------------- pkg/oci/artifact_test.go | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pkg/oci/artifact.go b/pkg/oci/artifact.go index 84a9bfc8471d..7e0f42b6d0e0 100644 --- a/pkg/oci/artifact.go +++ b/pkg/oci/artifact.go @@ -224,27 +224,33 @@ func DownloadArtifact(ctx context.Context, artifacts []*Artifact, dst string, op return nil } + if !shouldTryOtherRepo(err) { + 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)) - - 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 - } + if i < len(artifacts)-1 { + log.Info("Trying to download artifact from other repository...") } - return xerrors.Errorf("failed to download artifact from %s", art.Repository()) } return xerrors.New("failed to download artifact from any source") } + +func shouldTryOtherRepo(err error) bool { + var terr *transport.Error + if !errors.As(err, &terr) { + return false + } + + 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 + return terr.Temporary() +} diff --git a/pkg/oci/artifact_test.go b/pkg/oci/artifact_test.go index 3872c7bc9ab5..a8ce6e542641 100644 --- a/pkg/oci/artifact_test.go +++ b/pkg/oci/artifact_test.go @@ -81,7 +81,7 @@ func TestArtifact_Download(t *testing.T) { layersReturns: layersReturns{ layers: []v1.Layer{txtLayer}, }, - wantErr: "failed to download vulnerability DB: failed to download artifact", + wantErr: "unexpected EOF", }, { name: "sad: media type doesn't match",