fix: Out of bounds panic when not retrying push

This commit is contained in:
Gerald Pinder 2024-08-11 00:16:54 -04:00
parent 82606cc144
commit 464fdf94a9
4 changed files with 13 additions and 18 deletions

View file

@ -63,10 +63,10 @@ pub struct BuildCommand {
#[builder(default)] #[builder(default)]
compression_format: CompressionType, compression_format: CompressionType,
/// Block `bluebuild` from retrying to push the image. /// Enable retrying to push the image.
#[arg(short, long, default_value_t = true)] #[arg(short, long)]
#[builder(default)] #[builder(default)]
no_retry_push: bool, retry_push: bool,
/// The number of times to retry pushing the image. /// The number of times to retry pushing the image.
#[arg(long, default_value_t = 1)] #[arg(long, default_value_t = 1)]
@ -246,7 +246,7 @@ impl BuildCommand {
.containerfile(&containerfile) .containerfile(&containerfile)
.tags(tags.iter().map(String::as_str).collect::<Vec<_>>()) .tags(tags.iter().map(String::as_str).collect::<Vec<_>>())
.push(self.push) .push(self.push)
.no_retry_push(self.no_retry_push) .retry_push(self.retry_push)
.retry_count(self.retry_count) .retry_count(self.retry_count)
.compression(self.compression_format) .compression(self.compression_format)
.squash(self.squash) .squash(self.squash)
@ -292,7 +292,7 @@ impl BuildCommand {
.containerfile(&containerfile) .containerfile(&containerfile)
.tags(tags.iter().map(String::as_str).collect::<Vec<_>>()) .tags(tags.iter().map(String::as_str).collect::<Vec<_>>())
.push(self.push) .push(self.push)
.no_retry_push(self.no_retry_push) .retry_push(self.retry_push)
.retry_count(self.retry_count) .retry_count(self.retry_count)
.compression(self.compression_format) .compression(self.compression_format)
.squash(self.squash) .squash(self.squash)

View file

@ -216,15 +216,11 @@ pub trait BuildDriver: Sync + Send {
self.tag(&tag_opts)?; self.tag(&tag_opts)?;
if opts.push { if opts.push {
let retry_count = if opts.no_retry_push { let retry_count = if opts.retry_push { opts.retry_count } else { 0 };
0
} else {
opts.retry_count
};
debug!("Pushing all images"); debug!("Pushing all images");
// Push images with retries (1s delay between retries) // Push images with retries (1s delay between retries)
blue_build_utils::retry(retry_count, 1000, || { blue_build_utils::retry(retry_count, 10, || {
let tag_image = format!("{image}:{tag}"); let tag_image = format!("{image}:{tag}");
debug!("Pushing image {tag_image}"); debug!("Pushing image {tag_image}");

View file

@ -63,9 +63,9 @@ pub struct BuildTagPushOpts<'a> {
#[builder(default)] #[builder(default)]
pub push: bool, pub push: bool,
/// Disable retry logic for pushing. /// Enable retry logic for pushing.
#[builder(default)] #[builder(default)]
pub no_retry_push: bool, pub retry_push: bool,
/// Number of times to retry pushing. /// Number of times to retry pushing.
/// ///

View file

@ -70,18 +70,17 @@ pub fn serde_yaml_err(contents: &str) -> impl Fn(serde_yaml::Error) -> SerdeErro
/// ///
/// # Errors /// # Errors
/// Will error when retries have been expended. /// Will error when retries have been expended.
pub fn retry<V, F>(attempts: u8, delay: u64, f: F) -> miette::Result<V> pub fn retry<V, F>(mut retries: u8, delay_secs: u64, f: F) -> miette::Result<V>
where where
F: Fn() -> miette::Result<V>, F: Fn() -> miette::Result<V>,
{ {
let mut attempts = attempts;
loop { loop {
match f() { match f() {
Ok(v) => return Ok(v), Ok(v) => return Ok(v),
Err(e) if attempts == 1 => return Err(e), Err(e) if retries == 0 => return Err(e),
_ => { _ => {
attempts -= 1; retries -= 1;
thread::sleep(Duration::from_secs(delay)); thread::sleep(Duration::from_secs(delay_secs));
} }
}; };
} }