Skip to content

Commit

Permalink
Use retry logic from containers/common
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Feb 27, 2024
1 parent f09bf17 commit 19a7f5a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 46 deletions.
12 changes: 6 additions & 6 deletions cmd/buildah/addcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,20 @@ func addAndCopyCmd(c *cobra.Command, args []string, verb string, iopts addCopyRe
if err2 != nil {
return fmt.Errorf("unable to obtain decrypt config: %w", err2)
}
var pullPushRetryDelay time.Duration
pullPushRetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
options := buildah.BuilderOptions{
FromImage: iopts.from,
BlobDirectory: iopts.blobCache,
SignaturePolicyPath: iopts.signaturePolicy,
SystemContext: systemContext,
MaxPullRetries: iopts.retry,
PullRetryDelay: pullPushRetryDelay,
OciDecryptConfig: decryptConfig,
}
if iopts.retryDelay != "" {
options.PullRetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
}
if !iopts.quiet {
options.ReportWriter = os.Stderr
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/buildah/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,6 @@ func fromCmd(c *cobra.Command, args []string, iopts fromReply) error {
return fmt.Errorf("unable to obtain decrypt config: %w", err)
}

var pullPushRetryDelay time.Duration
pullPushRetryDelay, err = time.ParseDuration(iopts.RetryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.RetryDelay, err)
}

options := buildah.BuilderOptions{
FromImage: args[0],
Container: iopts.name,
Expand All @@ -296,10 +290,16 @@ func fromCmd(c *cobra.Command, args []string, iopts fromReply) error {
BlobDirectory: iopts.BlobCache,
Devices: devices,
MaxPullRetries: iopts.Retry,
PullRetryDelay: pullPushRetryDelay,
OciDecryptConfig: decConfig,
}

if iopts.RetryDelay != "" {
options.PullRetryDelay, err = time.ParseDuration(iopts.RetryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.RetryDelay, err)
}
}

if !iopts.quiet {
options.ReportWriter = os.Stderr
}
Expand Down
16 changes: 9 additions & 7 deletions cmd/buildah/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func init() {
}
flags.BoolVar(&manifestPushOpts.tlsVerify, "tls-verify", true, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.")
flags.BoolVarP(&manifestPushOpts.quiet, "quiet", "q", false, "don't output progress information when pushing lists")
flags.IntVar(&manifestPushOpts.retry, "retry", cli.MaxPullPushRetries, "number of times to retry in case of failure when performing push")
flags.StringVar(&manifestPushOpts.retryDelay, "retry-delay", cli.PullPushRetryDelay.String(), "delay between retries in case of push failures")
flags.IntVar(&manifestPushOpts.retry, "retry", int(defaultContainerConfig.Engine.Retry), "number of times to retry in case of failure when performing push")
flags.StringVar(&manifestPushOpts.retryDelay, "retry-delay", defaultContainerConfig.Engine.RetryDelay, "delay between retries in case of push failures")
flags.SetNormalizeFunc(cli.AliasFlags)
manifestCommand.AddCommand(manifestPushCommand)

Expand Down Expand Up @@ -1161,10 +1161,6 @@ func manifestPush(systemContext *types.SystemContext, store storage.Store, listI
}

retry := uint(opts.retry)
retryDelay, err := time.ParseDuration(opts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse retryDelay %q: %w", opts.retryDelay, err)
}

options := manifests.PushOptions{
Store: store,
Expand All @@ -1177,7 +1173,13 @@ func manifestPush(systemContext *types.SystemContext, store storage.Store, listI
AddCompression: opts.addCompression,
ForceCompressionFormat: opts.forceCompressionFormat,
MaxRetries: &retry,
RetryDelay: &retryDelay,
}
if opts.retryDelay != "" {
retryDelay, err := time.ParseDuration(opts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse retryDelay %q: %w", opts.retryDelay, err)
}
options.RetryDelay = &retryDelay
}
if opts.all {
options.ImageListSelection = cp.CopyAllImages
Expand Down
17 changes: 9 additions & 8 deletions cmd/buildah/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func init() {
flags.StringSlice("platform", []string{parse.DefaultPlatform()}, "prefer OS/ARCH instead of the current operating system and architecture for choosing images")
flags.String("variant", "", "override the `variant` of the specified image")
flags.BoolVar(&opts.tlsVerify, "tls-verify", true, "require HTTPS and verify certificates when accessing the registry. TLS verification cannot be used when talking to an insecure registry.")
flags.IntVar(&opts.retry, "retry", cli.MaxPullPushRetries, "number of times to retry in case of failure when performing pull")
flags.StringVar(&opts.retryDelay, "retry-delay", cli.PullPushRetryDelay.String(), "delay between retries in case of pull failures")
flags.IntVar(&opts.retry, "retry", int(defaultContainerConfig.Engine.Retry), "number of times to retry in case of failure when performing pull")
flags.StringVar(&opts.retryDelay, "retry-delay", defaultContainerConfig.Engine.RetryDelay, "delay between retries in case of pull failures")
if err := flags.MarkHidden("blob-cache"); err != nil {
panic(fmt.Sprintf("error marking blob-cache as hidden: %v", err))
}
Expand Down Expand Up @@ -123,11 +123,6 @@ func pullCmd(c *cobra.Command, args []string, iopts pullOptions) error {
if !ok {
return fmt.Errorf("unsupported pull policy %q", iopts.pullPolicy)
}
var pullPushRetryDelay time.Duration
pullPushRetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
options := buildah.PullOptions{
SignaturePolicyPath: iopts.signaturePolicy,
Store: store,
Expand All @@ -137,11 +132,17 @@ func pullCmd(c *cobra.Command, args []string, iopts pullOptions) error {
ReportWriter: os.Stderr,
RemoveSignatures: iopts.removeSignatures,
MaxRetries: iopts.retry,
RetryDelay: pullPushRetryDelay,
OciDecryptConfig: decConfig,
PullPolicy: policy,
}

if iopts.retryDelay != "" {
options.RetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
}

if iopts.quiet {
options.ReportWriter = nil // Turns off logging output
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/buildah/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func init() {
flags.StringVar(&opts.compressionFormat, "compression-format", "", "compression format to use")
flags.IntVar(&opts.compressionLevel, "compression-level", 0, "compression level to use")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "don't output progress information when pushing images")
flags.IntVar(&opts.retry, "retry", cli.MaxPullPushRetries, "number of times to retry in case of failure when performing push/pull")
flags.StringVar(&opts.retryDelay, "retry-delay", cli.PullPushRetryDelay.String(), "delay between retries in case of push/pull failures")
flags.IntVar(&opts.retry, "retry", int(defaultContainerConfig.Engine.Retry), "number of times to retry in case of failure when performing push")
flags.StringVar(&opts.retryDelay, "retry-delay", defaultContainerConfig.Engine.RetryDelay, "delay between retries in case of push failures")
flags.BoolVar(&opts.rm, "rm", false, "remove the manifest list if push succeeds")
flags.BoolVarP(&opts.removeSignatures, "remove-signatures", "", false, "don't copy signatures when pushing image")
flags.StringVar(&opts.signBy, "sign-by", "", "sign the image using a GPG key with the specified `FINGERPRINT`")
Expand Down Expand Up @@ -194,11 +194,6 @@ func pushCmd(c *cobra.Command, args []string, iopts pushOptions) error {
return fmt.Errorf("unable to obtain encryption config: %w", err)
}

var pullPushRetryDelay time.Duration
pullPushRetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
if c.Flag("compression-format").Changed {
if !c.Flag("force-compression").Changed {
// If `compression-format` is set and no value for `--force-compression`
Expand All @@ -217,11 +212,16 @@ func pushCmd(c *cobra.Command, args []string, iopts pushOptions) error {
RemoveSignatures: iopts.removeSignatures,
SignBy: iopts.signBy,
MaxRetries: iopts.retry,
RetryDelay: pullPushRetryDelay,
OciEncryptConfig: encConfig,
OciEncryptLayers: encLayers,
ForceCompressionFormat: iopts.forceCompressionFormat,
}
if iopts.retryDelay != "" {
options.RetryDelay, err = time.ParseDuration(iopts.retryDelay)
if err != nil {
return fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.retryDelay, err)
}
}
if !iopts.quiet {
options.ReportWriter = os.Stderr
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,6 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
iopts.NoCache = true
}
}
var pullPushRetryDelay time.Duration
pullPushRetryDelay, err = time.ParseDuration(iopts.RetryDelay)
if err != nil {
return options, nil, nil, fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.RetryDelay, err)
}
// Following log line is used in integration test.
logrus.Debugf("Setting MaxPullPushRetries to %d and PullPushRetryDelay to %v", iopts.Retry, pullPushRetryDelay)

if c.Flag("network").Changed && c.Flag("isolation").Changed {
if isolation == define.IsolationChroot {
Expand Down Expand Up @@ -405,7 +398,6 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
OutputFormat: format,
Platforms: platforms,
PullPolicy: pullPolicy,
PullPushRetryDelay: pullPushRetryDelay,
Quiet: iopts.Quiet,
RemoveIntermediateCtrs: iopts.Rm,
ReportWriter: reporter,
Expand All @@ -424,6 +416,15 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
UnsetEnvs: iopts.UnsetEnvs,
UnsetLabels: iopts.UnsetLabels,
}
if iopts.RetryDelay != "" {
options.PullPushRetryDelay, err = time.ParseDuration(iopts.RetryDelay)
if err != nil {
return options, nil, nil, fmt.Errorf("unable to parse value provided %q as --retry-delay: %w", iopts.RetryDelay, err)
}
// Following log line is used in integration test.
logrus.Debugf("Setting MaxPullPushRetries to %d and PullPushRetryDelay to %v", iopts.Retry, options.PullPushRetryDelay)
}

if iopts.Quiet {
options.ReportWriter = io.Discard
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ func GetFromAndBudFlags(flags *FromAndBudResults, usernsResults *UserNSResults,
fs.StringVar(&flags.Isolation, "isolation", DefaultIsolation(), "`type` of process isolation to use. Use BUILDAH_ISOLATION environment variable to override.")
fs.StringVarP(&flags.Memory, "memory", "m", "", "memory limit (format: <number>[<unit>], where unit = b, k, m or g)")
fs.StringVar(&flags.MemorySwap, "memory-swap", "", "swap limit equal to memory plus swap: '-1' to enable unlimited swap")
fs.IntVar(&flags.Retry, "retry", MaxPullPushRetries, "number of times to retry in case of failure when performing push/pull")
fs.StringVar(&flags.RetryDelay, "retry-delay", PullPushRetryDelay.String(), "delay between retries in case of push/pull failures")
fs.IntVar(&flags.Retry, "retry", int(defaultContainerConfig.Engine.Retry), "number of times to retry in case of failure when performing push/pull")
fs.StringVar(&flags.RetryDelay, "retry-delay", defaultContainerConfig.Engine.RetryDelay, "delay between retries in case of push/pull failures")
fs.String("arch", runtime.GOARCH, "set the ARCH of the image to the provided value instead of the architecture of the host")
fs.String("os", runtime.GOOS, "prefer `OS` instead of the running OS when pulling images")
fs.StringSlice("platform", []string{parse.DefaultPlatform()}, "set the `OS/ARCH[/VARIANT]` of the image to the provided value instead of the current operating system and architecture of the host (for example \"linux/arm\")")
Expand Down
29 changes: 29 additions & 0 deletions tests/containers_conf.bats
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,32 @@ _EOF
fi

}


@test "containers.conf retry" {
cat >${TEST_SCRATCH_DIR}/containers.conf << EOF
[engine]
retry=10
retry_delay="5s"
EOF
_prefetch alpine
CONTAINERS_CONF=${TEST_SCRATCH_DIR}/containers.conf run_buildah build --help
expect_output --substring "retry.*\(default 10\)"
expect_output --substring "retry-delay.*\(default \"5s\"\)"

CONTAINERS_CONF=${TEST_SCRATCH_DIR}/containers.conf run_buildah push --help
expect_output --substring "retry.*\(default 10\)"
expect_output --substring "retry-delay.*\(default \"5s\"\)"

CONTAINERS_CONF=${TEST_SCRATCH_DIR}/containers.conf run_buildah pull --help
expect_output --substring "retry.*\(default 10\)"
expect_output --substring "retry-delay.*\(default \"5s\"\)"

CONTAINERS_CONF=${TEST_SCRATCH_DIR}/containers.conf run_buildah from --help
expect_output --substring "retry.*\(default 10\)"
expect_output --substring "retry-delay.*\(default \"5s\"\)"

CONTAINERS_CONF=${TEST_SCRATCH_DIR}/containers.conf run_buildah manifest push --help
expect_output --substring "retry.*\(default 10\)"
expect_output --substring "retry-delay.*\(default \"5s\"\)"
}

0 comments on commit 19a7f5a

Please sign in to comment.