Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry pulling artifacts when running download images cmd #8004

Merged
merged 1 commit into from
Apr 18, 2024
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
14 changes: 12 additions & 2 deletions pkg/curatedpackages/curatedpackages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
"oras.land/oras-go/pkg/content"
Expand All @@ -15,12 +16,13 @@
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/types"
releasev1 "github.com/aws/eks-anywhere/release/api/v1alpha1"
)

const (
license = `The Amazon EKS Anywhere Curated Packages are only available to customers with the
license = `The Amazon EKS Anywhere Curated Packages are only available to customers with the
Amazon EKS Anywhere Enterprise Subscription`
width = 86
)
Expand Down Expand Up @@ -70,7 +72,15 @@
func PullLatestBundle(ctx context.Context, log logr.Logger, artifact string) ([]byte, error) {
puller := artifacts.NewRegistryPuller(log)

data, err := puller.Pull(ctx, artifact, "")
var data []byte
err := retrier.Retry(5, 200*time.Millisecond, func() error {
d, err := puller.Pull(ctx, artifact, "")
if err != nil {
return err

Check warning on line 79 in pkg/curatedpackages/curatedpackages.go

View check run for this annotation

Codecov / codecov/patch

pkg/curatedpackages/curatedpackages.go#L75-L79

Added lines #L75 - L79 were not covered by tests
}
data = d
return nil

Check warning on line 82 in pkg/curatedpackages/curatedpackages.go

View check run for this annotation

Codecov / codecov/patch

pkg/curatedpackages/curatedpackages.go#L81-L82

Added lines #L81 - L82 were not covered by tests
})
if err != nil {
return nil, fmt.Errorf("unable to pull artifacts %v", err)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/docker/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"runtime"
"strings"
"time"

"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/retrier"
)

// These constants are temporary since currently there is a limitation on harbor
Expand Down Expand Up @@ -64,12 +66,14 @@ func (d *ImageRegistryDestination) Write(ctx context.Context, images ...string)
type ImageOriginalRegistrySource struct {
client ImagePuller
processor *ConcurrentImageProcessor
Retrier retrier.Retrier
}

func NewOriginalRegistrySource(client ImagePuller) *ImageOriginalRegistrySource {
return &ImageOriginalRegistrySource{
client: client,
processor: NewConcurrentImageProcessor(runtime.GOMAXPROCS(0)),
Retrier: *retrier.NewWithMaxRetries(5, 200*time.Second),
}
}

Expand All @@ -79,7 +83,8 @@ func (s *ImageOriginalRegistrySource) Load(ctx context.Context, images ...string
logger.V(3).Info("Starting pull", "numberOfImages", len(images))

err := s.processor.Process(ctx, images, func(ctx context.Context, image string) error {
if err := s.client.PullImage(ctx, image); err != nil {
err := s.Retrier.Retry(func() error { return s.client.PullImage(ctx, image) })
if err != nil {
return err
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/docker/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aws/eks-anywhere/internal/test"
"github.com/aws/eks-anywhere/pkg/docker"
"github.com/aws/eks-anywhere/pkg/docker/mocks"
"github.com/aws/eks-anywhere/pkg/retrier"
)

func TestNewRegistryDestination(t *testing.T) {
Expand Down Expand Up @@ -119,6 +120,7 @@ func TestNewOriginalRegistrySource(t *testing.T) {
images := []string{"image1:1", "image2:2"}
ctx := context.Background()
dstLoader := docker.NewOriginalRegistrySource(client)
dstLoader.Retrier = *retrier.NewWithMaxRetries(1, 0)
for _, i := range images {
client.EXPECT().PullImage(test.AContext(), i)
}
Expand All @@ -134,6 +136,7 @@ func TestOriginalRegistrySourceError(t *testing.T) {
images := []string{"image1:1", "image2:2"}
ctx := context.Background()
dstLoader := docker.NewOriginalRegistrySource(client)
dstLoader.Retrier = *retrier.NewWithMaxRetries(1, 0)
client.EXPECT().PullImage(test.AContext(), images[0]).Return(errors.New("error pulling"))
client.EXPECT().PullImage(test.AContext(), images[1]).MaxTimes(1)

Expand Down
10 changes: 9 additions & 1 deletion pkg/helm/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@ import (
"context"
"fmt"
"sort"
"time"

"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/utils/oci"
)

type ChartRegistryDownloader struct {
client Client
dstFolder string

Retrier retrier.Retrier
}

func NewChartRegistryDownloader(client Client, dstFolder string) *ChartRegistryDownloader {
return &ChartRegistryDownloader{
client: client,
dstFolder: dstFolder,
Retrier: *retrier.NewWithMaxRetries(5, 200*time.Second),
}
}

func (d *ChartRegistryDownloader) Download(ctx context.Context, charts ...string) error {
for _, chart := range uniqueCharts(charts) {
chartURL, chartVersion := oci.ChartURLAndVersion(chart)
logger.Info("Saving helm chart to disk", "chart", chart)
if err := d.client.SaveChart(ctx, chartURL, chartVersion, d.dstFolder); err != nil {
err := d.Retrier.Retry(func() error {
return d.client.SaveChart(ctx, chartURL, chartVersion, d.dstFolder)
})
if err != nil {
return fmt.Errorf("downloading chart [%s] from registry: %v", chart, err)
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/helm/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/aws/eks-anywhere/pkg/helm"
"github.com/aws/eks-anywhere/pkg/helm/mocks"
"github.com/aws/eks-anywhere/pkg/retrier"
)

func TestChartRegistryDownloaderDownload(t *testing.T) {
Expand Down Expand Up @@ -37,5 +38,6 @@ func TestChartRegistryDownloaderDownloadError(t *testing.T) {
client.EXPECT().SaveChart(ctx, "oci://ecr.com/chart2", "v2.2.0", folder).Return(errors.New("failed downloading"))

d := helm.NewChartRegistryDownloader(client, folder)
d.Retrier = *retrier.NewWithMaxRetries(1, 0)
g.Expect(d.Download(ctx, charts...)).To(MatchError(ContainSubstring("downloading chart [ecr.com/chart2:v2.2.0] from registry: failed downloading")))
}