Skip to content

Commit

Permalink
impl: add a retry with result function (#2837)
Browse files Browse the repository at this point in the history
* impl: add a retry with result function

* fix ci errs
  • Loading branch information
alevenberg committed Nov 14, 2023
1 parent 11f0de8 commit ac52976
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ _If you are interested in contributing to kaniko, see
- [Flag `--ignore-var-run`](#flag---ignore-var-run)
- [Flag `--ignore-path`](#flag---ignore-path)
- [Flag `--image-fs-extract-retry`](#flag---image-fs-extract-retry)
- [Debug Image](#debug-image)
- [Flag `--image-download-retry`](#flag---image-download-retry)
- [Debug Image](#debug-image)
- [Security](#security)
- [Verifying Signed Kaniko Images](#verifying-signed-kaniko-images)
- [Kaniko Builds - Profiling](#kaniko-builds---profiling)
Expand Down Expand Up @@ -1094,6 +1095,10 @@ snapshot. Set it multiple times for multiple ignore paths.
Set this flag to the number of retries that should happen for the extracting an
image filesystem. Defaults to `0`.

#### Flag `--image-download-retry`

Set this flag to the number of retries that should happen when downloading the remote image. Defaults to `0`.

### Debug Image

The kaniko executor image is based on scratch and doesn't contain a shell. We
Expand Down
1 change: 1 addition & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().BoolVarP(&opts.SkipTLSVerifyPull, "skip-tls-verify-pull", "", false, "Pull from insecure registry ignoring TLS verify")
RootCmd.PersistentFlags().IntVar(&opts.PushRetry, "push-retry", 0, "Number of retries for the push operation")
RootCmd.PersistentFlags().IntVar(&opts.ImageFSExtractRetry, "image-fs-extract-retry", 0, "Number of retries for image FS extraction")
RootCmd.PersistentFlags().IntVar(&opts.ImageDownloadRetry, "image-download-retry", 0, "Number of retries for downloading the remote image")
RootCmd.PersistentFlags().StringVarP(&opts.KanikoDir, "kaniko-dir", "", constants.DefaultKanikoPath, "Path to the kaniko directory, this takes precedence over the KANIKO_DIR environment variable.")
RootCmd.PersistentFlags().StringVarP(&opts.TarPath, "tar-path", "", "", "Path to save the image in as a tarball instead of pushing")
RootCmd.PersistentFlags().BoolVarP(&opts.SingleSnapshot, "single-snapshot", "", false, "Take a single snapshot at the end of the build.")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type RegistryOptions struct {
InsecurePull bool
SkipTLSVerifyPull bool
PushRetry int
ImageDownloadRetry int
}

// KanikoOptions are options that are set by command line arguments
Expand Down
16 changes: 12 additions & 4 deletions pkg/image/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ func RetrieveRemoteImage(image string, opts config.RegistryOptions, customPlatfo
ref := setNewRegistry(ref, newReg)

logrus.Infof("Retrieving image %s from registry mirror %s", ref, registryMirror)
remoteImage, err := remoteImageFunc(ref, remoteOptions(registryMirror, opts, customPlatform)...)
if err != nil {
retryFunc := func() (v1.Image, error) {
return remoteImageFunc(ref, remoteOptions(registryMirror, opts, customPlatform)...)
}

var remoteImage v1.Image
var err error
if remoteImage, err = util.RetryWithResult(retryFunc, opts.ImageDownloadRetry, 1000); err != nil {
logrus.Warnf("Failed to retrieve image %s from registry mirror %s: %s. Will try with the next mirror, or fallback to the default registry.", ref, registryMirror, err)
continue
}
Expand All @@ -97,9 +102,12 @@ func RetrieveRemoteImage(image string, opts config.RegistryOptions, customPlatfo

logrus.Infof("Retrieving image %s from registry %s", ref, registryName)

remoteImage, err := remoteImageFunc(ref, remoteOptions(registryName, opts, customPlatform)...)
retryFunc := func() (v1.Image, error) {
return remoteImageFunc(ref, remoteOptions(registryName, opts, customPlatform)...)
}

if remoteImage != nil {
var remoteImage v1.Image
if remoteImage, err = util.RetryWithResult(retryFunc, opts.ImageDownloadRetry, 1000); remoteImage != nil {
manifestCache[image] = remoteImage
}

Expand Down

0 comments on commit ac52976

Please sign in to comment.