Skip to content

Commit c3e2a4d

Browse files
committed
INCIDENT-37491: ocitool push: add retry mechanism
1 parent d463434 commit c3e2a4d

File tree

7 files changed

+29
-6
lines changed

7 files changed

+29
-6
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use_repo(
3939
"com_github_opencontainers_go_digest",
4040
"com_github_opencontainers_image_spec",
4141
"com_github_opencontainers_runtime_spec",
42+
"com_github_sethvargo_go_retry",
4243
"com_github_sirupsen_logrus",
4344
"com_github_stretchr_testify",
4445
"com_github_urfave_cli_v2",

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/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ var app = &cli.App{
153153
Name: "x_meta_headers",
154154
Value: &flagutil.KeyValueFlag{},
155155
},
156+
&cli.UintFlag{
157+
Name: "retry",
158+
Usage: "Maximum number of retries for pushing content to the registry",
159+
Value: 3,
160+
},
156161
},
157162
},
158163
{

go/cmd/ocitool/push_cmd.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
6+
"time"
57

68
"github.com/DataDog/rules_oci/go/internal/flagutil"
79
"github.com/DataDog/rules_oci/go/pkg/ociutil"
810

911
"github.com/containerd/containerd/content"
1012
"github.com/containerd/containerd/images"
13+
"github.com/sethvargo/go-retry"
1114
"github.com/urfave/cli/v2"
1215
)
1316

@@ -70,7 +73,17 @@ func PushCmd(c *cli.Context) error {
7073
}
7174

7275
// push the parent last (in case of image index)
73-
err = ociutil.CopyContent(c.Context, allProviders, regIng, baseDesc)
76+
attempt := 0
77+
maxRetries := uint64(c.Uint("retry"))
78+
err = retry.Do(c.Context, retry.WithMaxRetries(maxRetries, retry.NewFibonacci(1*time.Second)), func(_ context.Context) error {
79+
attempt++
80+
fmt.Printf("Retry attempt %d/%d\n", attempt, maxRetries+1) // MaxAttempt = maxRetries + 1
81+
err := ociutil.CopyContent(c.Context, allProviders, regIng, baseDesc)
82+
if err != nil {
83+
fmt.Printf("Attempt %d failed: %v\n", attempt, err)
84+
}
85+
return err
86+
})
7487
if err != nil {
7588
return fmt.Errorf("failed to push parent content to registry: %w", err)
7689
}

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)