From 94cd914c411fe7929a237d773081d8ef480f01d3 Mon Sep 17 00:00:00 2001 From: Wei Weng Date: Tue, 24 Nov 2026 15:10:21 +0000 Subject: [PATCH 1/4] ci: retry transient E2E module download failures Retry only go mod download up to three total attempts with 10s and 20s delays. Keep provisioning and other prerequisite commands outside the retry loop and fail explicitly when attempts are exhausted. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .pipeline/e2e-tests.yaml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.pipeline/e2e-tests.yaml b/.pipeline/e2e-tests.yaml index 2a9bf5e0..47386af6 100644 --- a/.pipeline/e2e-tests.yaml +++ b/.pipeline/e2e-tests.yaml @@ -105,7 +105,17 @@ jobs: helm version --short kubectl version --client yq --version - go mod download + for attempt in 1 2 3; do + if go mod download; then + break + fi + if (( attempt == 3 )); then + echo "error: go mod download failed after 3 attempts" >&2 + exit 1 + fi + echo "go mod download failed; retrying in $((attempt * 10)) seconds..." >&2 + sleep "$((attempt * 10))" + done df -h / mkdir -p "$LOG_DIR" displayName: Check E2E prerequisites From d7cd15dc4950951f591ebe94ce48c3d2bf40cd42 Mon Sep 17 00:00:00 2001 From: Wei Weng Date: Tue, 24 Nov 2026 15:22:26 +0000 Subject: [PATCH 2/4] ci: retry transient Go tool installation failures Retry only the shared installer's go install command up to three total attempts with 10s and 20s delays. Preserve binary publication and cleanup behavior, and fail explicitly after exhaustion. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- hack/go-install.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hack/go-install.sh b/hack/go-install.sh index 8f035620..29f74295 100755 --- a/hack/go-install.sh +++ b/hack/go-install.sh @@ -39,6 +39,16 @@ cd "${tmp_dir}" go mod init fake/mod # install the golang module specified as the first argument -go install "${1}@${3}" +for attempt in 1 2 3; do + if go install "${1}@${3}"; then + break + fi + if (( attempt == 3 )); then + echo "error: go install ${1}@${3} failed after 3 attempts" >&2 + exit 1 + fi + echo "go install failed; retrying in $((attempt * 10)) seconds..." >&2 + sleep "$((attempt * 10))" +done mv "${GOBIN}/${2}" "${GOBIN}/${2}-${3}" ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}" From 460a1c28bfb1e98765681d36157b5a2ab8edb509 Mon Sep 17 00:00:00 2001 From: Wei Weng Date: Tue, 24 Nov 2026 17:23:11 +0000 Subject: [PATCH 3/4] ci: retry Go module downloads in image builds Use a shared POSIX retry script for the module download layer of all five Go images, preserving the existing BuildKit module cache and failing after three attempts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../hub-gateway-controller-manager.Dockerfile | 4 +- docker/hub-net-controller-manager.Dockerfile | 4 +- docker/mcs-controller-manager.Dockerfile | 4 +- .../member-net-controller-manager.Dockerfile | 4 +- docker/net-crd-installer.Dockerfile | 4 +- hack/retry-go-mod-download.sh | 15 +++++ test/scripts/go_mod_download_test.go | 64 +++++++++++++++++++ 7 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 hack/retry-go-mod-download.sh create mode 100644 test/scripts/go_mod_download_test.go diff --git a/docker/hub-gateway-controller-manager.Dockerfile b/docker/hub-gateway-controller-manager.Dockerfile index f3e50854..db560494 100644 --- a/docker/hub-gateway-controller-manager.Dockerfile +++ b/docker/hub-gateway-controller-manager.Dockerfile @@ -7,7 +7,9 @@ ARG GOARCH=amd64 WORKDIR /workspace COPY go.mod go.mod COPY go.sum go.sum -RUN --mount=type=cache,target=/go/pkg/mod go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ + sh /tmp/retry-go-mod-download.sh COPY cmd/hub-gateway-controller-manager/ cmd/hub-gateway-controller-manager/ COPY api/ api/ diff --git a/docker/hub-net-controller-manager.Dockerfile b/docker/hub-net-controller-manager.Dockerfile index ef60b0a0..19f38df3 100644 --- a/docker/hub-net-controller-manager.Dockerfile +++ b/docker/hub-net-controller-manager.Dockerfile @@ -10,7 +10,9 @@ COPY go.mod go.mod COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. -RUN --mount=type=cache,target=/go/pkg/mod go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ + sh /tmp/retry-go-mod-download.sh # Copy the go source COPY cmd/hub-net-controller-manager/main.go main.go diff --git a/docker/mcs-controller-manager.Dockerfile b/docker/mcs-controller-manager.Dockerfile index 7d5b79ce..5e0453db 100644 --- a/docker/mcs-controller-manager.Dockerfile +++ b/docker/mcs-controller-manager.Dockerfile @@ -10,7 +10,9 @@ COPY go.mod go.mod COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. -RUN --mount=type=cache,target=/go/pkg/mod go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ + sh /tmp/retry-go-mod-download.sh # Copy the go source COPY cmd/mcs-controller-manager/main.go main.go diff --git a/docker/member-net-controller-manager.Dockerfile b/docker/member-net-controller-manager.Dockerfile index 0297da2d..acf83eac 100644 --- a/docker/member-net-controller-manager.Dockerfile +++ b/docker/member-net-controller-manager.Dockerfile @@ -10,7 +10,9 @@ COPY go.mod go.mod COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. -RUN --mount=type=cache,target=/go/pkg/mod go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ + sh /tmp/retry-go-mod-download.sh # Copy the go source COPY cmd/member-net-controller-manager/main.go main.go diff --git a/docker/net-crd-installer.Dockerfile b/docker/net-crd-installer.Dockerfile index 146d255c..dff32c3f 100644 --- a/docker/net-crd-installer.Dockerfile +++ b/docker/net-crd-installer.Dockerfile @@ -10,7 +10,9 @@ COPY go.mod go.mod COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. -RUN --mount=type=cache,target=/go/pkg/mod go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ + sh /tmp/retry-go-mod-download.sh # Copy the go source COPY cmd/net-crd-installer/ cmd/net-crd-installer/ diff --git a/hack/retry-go-mod-download.sh b/hack/retry-go-mod-download.sh new file mode 100644 index 00000000..ffe0a01c --- /dev/null +++ b/hack/retry-go-mod-download.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -eu + +for attempt in 1 2 3; do + if go mod download; then + exit 0 + fi + if [ "$attempt" -eq 3 ]; then + echo "error: go mod download failed after 3 attempts" >&2 + exit 1 + fi + delay=$((attempt * 10)) + echo "go mod download failed; retrying in $delay seconds..." >&2 + sleep "$delay" +done diff --git a/test/scripts/go_mod_download_test.go b/test/scripts/go_mod_download_test.go new file mode 100644 index 00000000..b1d729b2 --- /dev/null +++ b/test/scripts/go_mod_download_test.go @@ -0,0 +1,64 @@ +/* +Copyright (c) Microsoft Corporation. +Licensed under the MIT license. +*/ + +package scripts + +import ( + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "testing" +) + +func TestDockerGoModDownloadRetry(t *testing.T) { + tests := []struct { + failures int + calls int + delays string + wantErr bool + }{ + {0, 1, "", false}, + {1, 2, "10\n", false}, + {2, 3, "10\n20\n", false}, + {3, 3, "10\n20\n", true}, + } + for _, tt := range tests { + t.Run(strconv.Itoa(tt.failures), func(t *testing.T) { + dir := t.TempDir() + writeAFDCommand(t, filepath.Join(dir, "go"), `#!/bin/sh +[ "$*" = "mod download" ] || exit 99 +echo attempt >> "$TEST_ATTEMPTS" +attempts=$(wc -l < "$TEST_ATTEMPTS") +[ "$attempts" -gt "$TEST_FAILURES" ] +`) + writeAFDCommand(t, filepath.Join(dir, "sleep"), "#!/bin/sh\necho \"$1\" >> \"$TEST_DELAYS\"\n") + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("TEST_ATTEMPTS", filepath.Join(dir, "attempts")) + t.Setenv("TEST_DELAYS", filepath.Join(dir, "delays")) + t.Setenv("TEST_FAILURES", strconv.Itoa(tt.failures)) + + output, err := exec.Command("sh", "../../hack/retry-go-mod-download.sh").CombinedOutput() + if (err != nil) != tt.wantErr { + t.Fatalf("unexpected result: %v, output: %s", err, output) + } + attempts, err := os.ReadFile(filepath.Join(dir, "attempts")) + if err != nil || strings.Count(string(attempts), "attempt\n") != tt.calls { + t.Fatalf("unexpected attempts %q: %v", attempts, err) + } + delays, err := os.ReadFile(filepath.Join(dir, "delays")) + if err != nil && !os.IsNotExist(err) { + t.Fatal(err) + } + if string(delays) != tt.delays { + t.Fatalf("got delays %q, want %q", delays, tt.delays) + } + if tt.wantErr && !strings.Contains(string(output), "failed after 3 attempts") { + t.Fatalf("missing exhaustion error: %s", output) + } + }) + } +} From 4b983367863bedbb520d6a594a9463a78d36af7d Mon Sep 17 00:00:00 2001 From: Wei Weng Date: Tue, 24 Nov 2026 17:26:02 +0000 Subject: [PATCH 4/4] ci: share Go command retry helper Reuse the same bounded retry for module downloads and tool installation across Azure DevOps and image builds. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .pipeline/e2e-tests.yaml | 12 +--- .../hub-gateway-controller-manager.Dockerfile | 4 +- docker/hub-net-controller-manager.Dockerfile | 4 +- docker/mcs-controller-manager.Dockerfile | 4 +- .../member-net-controller-manager.Dockerfile | 4 +- docker/net-crd-installer.Dockerfile | 4 +- hack/go-install.sh | 13 +--- hack/retry-go-mod-download.sh | 15 ----- hack/retry-go.sh | 20 ++++++ test/scripts/go_mod_download_test.go | 64 ------------------- 10 files changed, 33 insertions(+), 111 deletions(-) delete mode 100644 hack/retry-go-mod-download.sh create mode 100644 hack/retry-go.sh delete mode 100644 test/scripts/go_mod_download_test.go diff --git a/.pipeline/e2e-tests.yaml b/.pipeline/e2e-tests.yaml index 47386af6..eede806b 100644 --- a/.pipeline/e2e-tests.yaml +++ b/.pipeline/e2e-tests.yaml @@ -105,17 +105,7 @@ jobs: helm version --short kubectl version --client yq --version - for attempt in 1 2 3; do - if go mod download; then - break - fi - if (( attempt == 3 )); then - echo "error: go mod download failed after 3 attempts" >&2 - exit 1 - fi - echo "go mod download failed; retrying in $((attempt * 10)) seconds..." >&2 - sleep "$((attempt * 10))" - done + sh ./hack/retry-go.sh mod download df -h / mkdir -p "$LOG_DIR" displayName: Check E2E prerequisites diff --git a/docker/hub-gateway-controller-manager.Dockerfile b/docker/hub-gateway-controller-manager.Dockerfile index db560494..76842db0 100644 --- a/docker/hub-gateway-controller-manager.Dockerfile +++ b/docker/hub-gateway-controller-manager.Dockerfile @@ -8,8 +8,8 @@ WORKDIR /workspace COPY go.mod go.mod COPY go.sum go.sum RUN --mount=type=cache,target=/go/pkg/mod \ - --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ - sh /tmp/retry-go-mod-download.sh + --mount=type=bind,source=hack/retry-go.sh,target=/tmp/retry-go.sh \ + sh /tmp/retry-go.sh mod download COPY cmd/hub-gateway-controller-manager/ cmd/hub-gateway-controller-manager/ COPY api/ api/ diff --git a/docker/hub-net-controller-manager.Dockerfile b/docker/hub-net-controller-manager.Dockerfile index 19f38df3..7b192eeb 100644 --- a/docker/hub-net-controller-manager.Dockerfile +++ b/docker/hub-net-controller-manager.Dockerfile @@ -11,8 +11,8 @@ COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. RUN --mount=type=cache,target=/go/pkg/mod \ - --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ - sh /tmp/retry-go-mod-download.sh + --mount=type=bind,source=hack/retry-go.sh,target=/tmp/retry-go.sh \ + sh /tmp/retry-go.sh mod download # Copy the go source COPY cmd/hub-net-controller-manager/main.go main.go diff --git a/docker/mcs-controller-manager.Dockerfile b/docker/mcs-controller-manager.Dockerfile index 5e0453db..778bf74f 100644 --- a/docker/mcs-controller-manager.Dockerfile +++ b/docker/mcs-controller-manager.Dockerfile @@ -11,8 +11,8 @@ COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. RUN --mount=type=cache,target=/go/pkg/mod \ - --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ - sh /tmp/retry-go-mod-download.sh + --mount=type=bind,source=hack/retry-go.sh,target=/tmp/retry-go.sh \ + sh /tmp/retry-go.sh mod download # Copy the go source COPY cmd/mcs-controller-manager/main.go main.go diff --git a/docker/member-net-controller-manager.Dockerfile b/docker/member-net-controller-manager.Dockerfile index acf83eac..165a4738 100644 --- a/docker/member-net-controller-manager.Dockerfile +++ b/docker/member-net-controller-manager.Dockerfile @@ -11,8 +11,8 @@ COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. RUN --mount=type=cache,target=/go/pkg/mod \ - --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ - sh /tmp/retry-go-mod-download.sh + --mount=type=bind,source=hack/retry-go.sh,target=/tmp/retry-go.sh \ + sh /tmp/retry-go.sh mod download # Copy the go source COPY cmd/member-net-controller-manager/main.go main.go diff --git a/docker/net-crd-installer.Dockerfile b/docker/net-crd-installer.Dockerfile index dff32c3f..273f1e09 100644 --- a/docker/net-crd-installer.Dockerfile +++ b/docker/net-crd-installer.Dockerfile @@ -11,8 +11,8 @@ COPY go.sum go.sum # Cache the downloaded dependency modules across different builds to expedite the progress. # This also helps reduce downloading related reliability issues in our build environment. RUN --mount=type=cache,target=/go/pkg/mod \ - --mount=type=bind,source=hack/retry-go-mod-download.sh,target=/tmp/retry-go-mod-download.sh \ - sh /tmp/retry-go-mod-download.sh + --mount=type=bind,source=hack/retry-go.sh,target=/tmp/retry-go.sh \ + sh /tmp/retry-go.sh mod download # Copy the go source COPY cmd/net-crd-installer/ cmd/net-crd-installer/ diff --git a/hack/go-install.sh b/hack/go-install.sh index 29f74295..cdd092d4 100755 --- a/hack/go-install.sh +++ b/hack/go-install.sh @@ -25,6 +25,7 @@ if [[ -z "${GOBIN}" ]]; then exit 1 fi +retry_go="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/retry-go.sh" tmp_dir=$(mktemp -d -t goinstall_XXXXXXXXXX) function clean { rm -rf "${tmp_dir}" @@ -39,16 +40,6 @@ cd "${tmp_dir}" go mod init fake/mod # install the golang module specified as the first argument -for attempt in 1 2 3; do - if go install "${1}@${3}"; then - break - fi - if (( attempt == 3 )); then - echo "error: go install ${1}@${3} failed after 3 attempts" >&2 - exit 1 - fi - echo "go install failed; retrying in $((attempt * 10)) seconds..." >&2 - sleep "$((attempt * 10))" -done +sh "$retry_go" install "${1}@${3}" mv "${GOBIN}/${2}" "${GOBIN}/${2}-${3}" ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}" diff --git a/hack/retry-go-mod-download.sh b/hack/retry-go-mod-download.sh deleted file mode 100644 index ffe0a01c..00000000 --- a/hack/retry-go-mod-download.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -set -eu - -for attempt in 1 2 3; do - if go mod download; then - exit 0 - fi - if [ "$attempt" -eq 3 ]; then - echo "error: go mod download failed after 3 attempts" >&2 - exit 1 - fi - delay=$((attempt * 10)) - echo "go mod download failed; retrying in $delay seconds..." >&2 - sleep "$delay" -done diff --git a/hack/retry-go.sh b/hack/retry-go.sh new file mode 100644 index 00000000..cb46d67f --- /dev/null +++ b/hack/retry-go.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -eu + +if [ "$#" -eq 0 ]; then + echo "error: provide a Go command to retry" >&2 + exit 1 +fi + +for attempt in 1 2 3; do + if go "$@"; then + exit 0 + fi + if [ "$attempt" -eq 3 ]; then + echo "error: go $* failed after 3 attempts" >&2 + exit 1 + fi + delay=$((attempt * 10)) + echo "go $* failed; retrying in $delay seconds..." >&2 + sleep "$delay" +done diff --git a/test/scripts/go_mod_download_test.go b/test/scripts/go_mod_download_test.go deleted file mode 100644 index b1d729b2..00000000 --- a/test/scripts/go_mod_download_test.go +++ /dev/null @@ -1,64 +0,0 @@ -/* -Copyright (c) Microsoft Corporation. -Licensed under the MIT license. -*/ - -package scripts - -import ( - "os" - "os/exec" - "path/filepath" - "strconv" - "strings" - "testing" -) - -func TestDockerGoModDownloadRetry(t *testing.T) { - tests := []struct { - failures int - calls int - delays string - wantErr bool - }{ - {0, 1, "", false}, - {1, 2, "10\n", false}, - {2, 3, "10\n20\n", false}, - {3, 3, "10\n20\n", true}, - } - for _, tt := range tests { - t.Run(strconv.Itoa(tt.failures), func(t *testing.T) { - dir := t.TempDir() - writeAFDCommand(t, filepath.Join(dir, "go"), `#!/bin/sh -[ "$*" = "mod download" ] || exit 99 -echo attempt >> "$TEST_ATTEMPTS" -attempts=$(wc -l < "$TEST_ATTEMPTS") -[ "$attempts" -gt "$TEST_FAILURES" ] -`) - writeAFDCommand(t, filepath.Join(dir, "sleep"), "#!/bin/sh\necho \"$1\" >> \"$TEST_DELAYS\"\n") - t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) - t.Setenv("TEST_ATTEMPTS", filepath.Join(dir, "attempts")) - t.Setenv("TEST_DELAYS", filepath.Join(dir, "delays")) - t.Setenv("TEST_FAILURES", strconv.Itoa(tt.failures)) - - output, err := exec.Command("sh", "../../hack/retry-go-mod-download.sh").CombinedOutput() - if (err != nil) != tt.wantErr { - t.Fatalf("unexpected result: %v, output: %s", err, output) - } - attempts, err := os.ReadFile(filepath.Join(dir, "attempts")) - if err != nil || strings.Count(string(attempts), "attempt\n") != tt.calls { - t.Fatalf("unexpected attempts %q: %v", attempts, err) - } - delays, err := os.ReadFile(filepath.Join(dir, "delays")) - if err != nil && !os.IsNotExist(err) { - t.Fatal(err) - } - if string(delays) != tt.delays { - t.Fatalf("got delays %q, want %q", delays, tt.delays) - } - if tt.wantErr && !strings.Contains(string(output), "failed after 3 attempts") { - t.Fatalf("missing exhaustion error: %s", output) - } - }) - } -}