tag v0.165.0 Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com> Changes with 0.165.0 ---------------- * distro: move rhel9 into a generic distro (osbuild/images#1645) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger * Revert "distro: drop `ImageType.BasePartitionTable()`" (osbuild/images#1691) * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza * Update dependencies 2025-07-20 (osbuild/images#1675) * Author: SchutzBot, Reviewers: Achilleas Koutsou, Simon de Vlieger * defs: add missing `bootstrap_containers` (osbuild/images#1679) * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza * disk: handle adding `PReP` partition on PPC64/s390x (HMS-8884) (osbuild/images#1681) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger * distro: bring per-distro checkOptions back (osbuild/images#1678) * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza * distro: cleanups in the pkg/distro/generic area (osbuild/images#1686) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger * distro: move rhel8 into a generic distro (osbuild/images#1643) * Author: Michael Vogt, Reviewers: Nobody * distro: small followups for PR#1682 (osbuild/images#1689) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger, Tomáš Hozza * distro: unify transform/match into a single concept (osbuild/images#1682) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Tomáš Hozza * distros: de-duplicate runner build packages for centos10 (osbuild/images#1680) * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza * github: disable Go dep updates through dependabot (osbuild/images#1683) * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza * repos: include almalinux 9.6 (osbuild/images#1677) * Author: Simon de Vlieger, Reviewers: Lukáš Zapletal, Tomáš Hozza * rhel9: wsl distribution config (osbuild/images#1694) * Author: Simon de Vlieger, Reviewers: Michael Vogt, Sanne Raymaekers * test/manifests/all-customizations: don't embed local file via URI (osbuild/images#1684) * Author: Tomáš Hozza, Reviewers: Achilleas Koutsou, Brian C. Lane — Somewhere on the Internet, 2025-07-28 --- tag v0.166.0 Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com> Changes with 0.166.0 ---------------- * customizations/subscription: conditionally enable semanage call (HMS-8866) (osbuild/images#1673) * Author: Sanne Raymaekers, Reviewers: Achilleas Koutsou, Michael Vogt * distro/rhel-10: versionlock shim-x64 in the azure-cvm image (osbuild/images#1697) * Author: Achilleas Koutsou, Reviewers: Michael Vogt, Simon de Vlieger * manifestmock: move container/pkg/commit mocks into helper (osbuild/images#1700) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger * rhel9: `vagrant-libvirt`, `vagrant-virtualbox` (osbuild/images#1693) * Author: Simon de Vlieger, Reviewers: Michael Vogt, Sanne Raymaekers * rhel{9,10}: centos WSL refinement (HMS-8922) (osbuild/images#1690) * Author: Simon de Vlieger, Reviewers: Ondřej Budai, Sanne Raymaekers, Tomáš Hozza — Somewhere on the Internet, 2025-07-29 --- tag v0.167.0 Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com> Changes with 0.167.0 ---------------- * RHEL/Azure: drop obsolete WAAgentConfig keys [RHEL-93894] and remove loglevel kernel option [RHEL-102372] (osbuild/images#1611) * Author: Achilleas Koutsou, Reviewers: Michael Vogt, Ondřej Budai, Sanne Raymaekers * Update dependencies 2025-07-27 (osbuild/images#1699) * Author: SchutzBot, Reviewers: Achilleas Koutsou, Simon de Vlieger * distro/rhel9: set default_kernel to kernel-uki-virt (osbuild/images#1704) * Author: Achilleas Koutsou, Reviewers: Ondřej Budai, Simon de Vlieger * distro: drop legacy loaders and update tests (osbuild/images#1687) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Tomáš Hozza * distro: fix issues with yaml distro definitions and enable yaml checks (osbuild/images#1702) * Author: Achilleas Koutsou, Reviewers: Michael Vogt, Ondřej Budai, Simon de Vlieger — Somewhere on the Internet, 2025-07-30 --- tag v0.168.0 Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com> Changes with 0.168.0 ---------------- * distro: fix bug in variable substitution for static distros (osbuild/images#1710) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger * rhel{9,10}: azure for non-RHEL (HMS-8949) (osbuild/images#1707) * Author: Simon de Vlieger, Reviewers: Achilleas Koutsou, Michael Vogt — Somewhere on the Internet, 2025-07-30 ---
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
package retry
|
||
|
||
import (
|
||
"context"
|
||
"io"
|
||
"math"
|
||
"math/rand/v2"
|
||
"net"
|
||
"net/http"
|
||
"net/url"
|
||
"syscall"
|
||
"time"
|
||
|
||
"github.com/containers/image/v5/docker"
|
||
"github.com/docker/distribution/registry/api/errcode"
|
||
errcodev2 "github.com/docker/distribution/registry/api/v2"
|
||
"github.com/hashicorp/go-multierror"
|
||
"github.com/sirupsen/logrus"
|
||
)
|
||
|
||
// Options defines the option to retry.
|
||
type Options struct {
|
||
MaxRetry int // The number of times to possibly retry.
|
||
Delay time.Duration // The delay to use between retries, if set.
|
||
IsErrorRetryable func(error) bool
|
||
}
|
||
|
||
// RetryOptions is deprecated, use Options.
|
||
type RetryOptions = Options // nolint:revive
|
||
|
||
// RetryIfNecessary deprecated function use IfNecessary.
|
||
func RetryIfNecessary(ctx context.Context, operation func() error, options *Options) error { // nolint:revive
|
||
return IfNecessary(ctx, operation, options)
|
||
}
|
||
|
||
// IfNecessary retries the operation in exponential backoff with the retry Options.
|
||
func IfNecessary(ctx context.Context, operation func() error, options *Options) error {
|
||
var isRetryable func(error) bool
|
||
if options.IsErrorRetryable != nil {
|
||
isRetryable = options.IsErrorRetryable
|
||
} else {
|
||
isRetryable = IsErrorRetryable
|
||
}
|
||
err := operation()
|
||
for attempt := 0; err != nil && isRetryable(err) && attempt < options.MaxRetry; attempt++ {
|
||
delay := time.Duration(int(math.Pow(2, float64(attempt)))) * time.Second
|
||
if options.Delay != 0 {
|
||
delay = options.Delay
|
||
}
|
||
logrus.Warnf("Failed, retrying in %s ... (%d/%d). Error: %v", delay, attempt+1, options.MaxRetry, err)
|
||
delay += rand.N(delay / 10) // 10 % jitter so that a failure blip doesn’t cause a deterministic stampede
|
||
logrus.Debugf("Retry delay with added jitter: %s", delay)
|
||
select {
|
||
case <-time.After(delay):
|
||
// Do nothing.
|
||
case <-ctx.Done():
|
||
return err
|
||
}
|
||
err = operation()
|
||
}
|
||
return err
|
||
}
|
||
|
||
// IsErrorRetryable makes a HEURISTIC determination whether it is worth retrying upon encountering an error.
|
||
// That heuristic is NOT STABLE and it CAN CHANGE AT ANY TIME.
|
||
// Callers that have a hard requirement for specific treatment of a class of errors should make their own check
|
||
// instead of relying on this function maintaining its past behavior.
|
||
func IsErrorRetryable(err error) bool {
|
||
switch err {
|
||
case nil:
|
||
return false
|
||
case context.Canceled, context.DeadlineExceeded:
|
||
return false
|
||
default: // continue
|
||
}
|
||
|
||
type unwrapper interface {
|
||
Unwrap() error
|
||
}
|
||
|
||
switch e := err.(type) {
|
||
case errcode.Error:
|
||
switch e.Code {
|
||
case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeDenied,
|
||
errcodev2.ErrorCodeNameUnknown, errcodev2.ErrorCodeManifestUnknown:
|
||
return false
|
||
}
|
||
return true
|
||
case docker.UnexpectedHTTPStatusError:
|
||
// Retry on 502, 502 and 503 http server errors, they appear to be quite common in the field.
|
||
// https://github.com/containers/common/issues/2299
|
||
if e.StatusCode >= http.StatusBadGateway && e.StatusCode <= http.StatusGatewayTimeout {
|
||
return true
|
||
}
|
||
return false
|
||
case *net.OpError:
|
||
return IsErrorRetryable(e.Err)
|
||
case *url.Error: // This includes errors returned by the net/http client.
|
||
if e.Err == io.EOF { // Happens when a server accepts a HTTP connection and sends EOF
|
||
return true
|
||
}
|
||
return IsErrorRetryable(e.Err)
|
||
case syscall.Errno:
|
||
return isErrnoRetryable(e)
|
||
case errcode.Errors:
|
||
// if this error is a group of errors, process them all in turn
|
||
for i := range e {
|
||
if !IsErrorRetryable(e[i]) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
case *multierror.Error:
|
||
// if this error is a group of errors, process them all in turn
|
||
for i := range e.Errors {
|
||
if !IsErrorRetryable(e.Errors[i]) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
case net.Error:
|
||
if e.Timeout() {
|
||
return true
|
||
}
|
||
if unwrappable, ok := e.(unwrapper); ok {
|
||
err = unwrappable.Unwrap()
|
||
return IsErrorRetryable(err)
|
||
}
|
||
case unwrapper: // Test this last, because various error types might implement .Unwrap()
|
||
err = e.Unwrap()
|
||
return IsErrorRetryable(err)
|
||
}
|
||
|
||
return false
|
||
}
|
||
|
||
func isErrnoRetryable(e error) bool {
|
||
switch e {
|
||
case syscall.ECONNREFUSED, syscall.EINTR, syscall.EAGAIN, syscall.EBUSY, syscall.ENETDOWN, syscall.ENETUNREACH, syscall.ENETRESET, syscall.ECONNABORTED, syscall.ECONNRESET, syscall.ETIMEDOUT, syscall.EHOSTDOWN, syscall.EHOSTUNREACH:
|
||
return true
|
||
}
|
||
return isErrnoERESTART(e)
|
||
}
|