Skip to content

Commit fa24f81

Browse files
committed
INCIDENT-37491: ocitool copy content: add retry mechanism
1 parent d463434 commit fa24f81

File tree

10 files changed

+34
-11
lines changed

10 files changed

+34
-11
lines changed

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bazel_dep(name = "gazelle", version = "0.38.0")
99
bazel_dep(name = "rules_go", version = "0.47.1")
1010
bazel_dep(name = "rules_pkg", version = "0.10.1")
1111
bazel_dep(name = "stardoc", version = "0.6.2")
12+
bazel_dep(name = "zlib", version = "1.3.1")
1213

1314
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.6.1", dev_dependency = True)
1415
bazel_dep(name = "buildifier_prebuilt", version = "7.3.1", dev_dependency = True)
@@ -39,6 +40,7 @@ use_repo(
3940
"com_github_opencontainers_go_digest",
4041
"com_github_opencontainers_image_spec",
4142
"com_github_opencontainers_runtime_spec",
43+
"com_github_sethvargo_go_retry",
4244
"com_github_sirupsen_logrus",
4345
"com_github_stretchr_testify",
4446
"com_github_urfave_cli_v2",

MODULE.bazel.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ require (
6565
github.com/prometheus/common v0.53.0 // indirect
6666
github.com/prometheus/procfs v0.14.0 // indirect
6767
github.com/russross/blackfriday/v2 v2.1.0 // indirect
68+
github.com/sethvargo/go-retry v0.3.0 // indirect
6869
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
6970
go.opencensus.io v0.24.0 // indirect
7071
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
209209
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
210210
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
211211
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
212+
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
213+
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
212214
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
213215
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
214216
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=

go/cmd/ocitool/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ go_library(
3434
"@com_github_opencontainers_go_digest//:go_default_library",
3535
"@com_github_opencontainers_image_spec//specs-go:go_default_library",
3636
"@com_github_opencontainers_image_spec//specs-go/v1:go_default_library",
37+
"@com_github_sethvargo_go_retry//:go_default_library",
3738
"@com_github_sirupsen_logrus//:go_default_library",
3839
"@com_github_urfave_cli_v2//:go_default_library",
3940
"@gazelle//rule:go_default_library",

go/cmd/ocitool/imagelayout_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func CreateOciImageLayoutCmd(c *cli.Context) error {
9898
}
9999

100100
// copy the parent last (in case of image index)
101-
err = ociutil.CopyContent(c.Context, multiProvider, &ociIngester, baseDesc)
101+
err = ociutil.CopyContentWithRetries(c.Context, multiProvider, &ociIngester, baseDesc)
102102
if err != nil {
103103
return fmt.Errorf("failed to copy parent content to OCI Image Layout: %w", err)
104104
}

go/cmd/ocitool/push_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func PushCmd(c *cli.Context) error {
7070
}
7171

7272
// push the parent last (in case of image index)
73-
err = ociutil.CopyContent(c.Context, allProviders, regIng, baseDesc)
73+
err = ociutil.CopyContentWithRetries(c.Context, allProviders, regIng, baseDesc)
7474
if err != nil {
7575
return fmt.Errorf("failed to push parent content to registry: %w", err)
7676
}

go/pkg/ociutil/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// ingestor
1515
func CopyContentHandler(handler images.HandlerFunc, from content.Provider, to content.Ingester) images.HandlerFunc {
1616
return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
17-
err := CopyContent(ctx, from, to, desc)
17+
err := CopyContentWithRetries(ctx, from, to, desc)
1818
if err != nil {
1919
return nil, err
2020
}
@@ -46,7 +46,7 @@ func copyContentFromHandler(ctx context.Context, handler images.HandlerFunc, fro
4646
return err
4747
}
4848

49-
err = CopyContent(ctx, from, to, desc)
49+
err = CopyContentWithRetries(ctx, from, to, desc)
5050
if err != nil {
5151
return err
5252
}

go/pkg/ociutil/push.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"fmt"
99
"net/http"
1010
"os"
11+
"time"
1112

13+
"github.com/DataDog/rules_oci/bazel-rules_oci/go/pkg/ociutil"
1214
"github.com/DataDog/rules_oci/go/pkg/credhelper"
15+
"github.com/sethvargo/go-retry"
1316

1417
"github.com/containerd/containerd/content"
1518
"github.com/containerd/containerd/errdefs"
@@ -305,3 +308,16 @@ func CopyContent(ctx context.Context, from content.Provider, to content.Ingester
305308

306309
return nil
307310
}
311+
312+
func CopyContentWithRetries(ctx context.Context, from content.Provider, to content.Ingester, desc ocispec.Descriptor) error {
313+
attempt := 0
314+
const maxRetries uint64 = 2
315+
return retry.Do(ctx, retry.WithMaxRetries(maxRetries, retry.NewFibonacci(1*time.Second)), func(_ context.Context) error {
316+
attempt++
317+
err := ociutil.CopyContent(ctx, from, to, desc)
318+
if err != nil {
319+
fmt.Printf("Attempt %d of %d failed: %v\n", attempt, maxRetries+1, err)
320+
}
321+
return err
322+
})
323+
}

oci/BUILD.bazel

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ bzl_library(
9999
)
100100

101101
bzl_library(
102-
name = "debug_flag",
103-
srcs = ["debug_flag.bzl"],
102+
name = "layer",
103+
srcs = ["layer.bzl"],
104104
visibility = ["//visibility:public"],
105+
deps = ["@com_github_datadog_rules_oci//oci:providers"],
105106
)
106107

107108
bzl_library(
108-
name = "layer",
109-
srcs = ["layer.bzl"],
109+
name = "debug_flag",
110+
srcs = ["debug_flag.bzl"],
110111
visibility = ["//visibility:public"],
111-
deps = ["@com_github_datadog_rules_oci//oci:providers"],
112112
)
113113

114114
bzl_library(

0 commit comments

Comments
 (0)