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

View file

@ -216,15 +216,11 @@ pub trait BuildDriver: Sync + Send {
self.tag(&tag_opts)?;
if opts.push {
let retry_count = if opts.no_retry_push {
0
} else {
opts.retry_count
};
let retry_count = if opts.retry_push { opts.retry_count } else { 0 };
debug!("Pushing all images");
// 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}");
debug!("Pushing image {tag_image}");

View file

@ -63,9 +63,9 @@ pub struct BuildTagPushOpts<'a> {
#[builder(default)]
pub push: bool,
/// Disable retry logic for pushing.
/// Enable retry logic for pushing.
#[builder(default)]
pub no_retry_push: bool,
pub retry_push: bool,
/// 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
/// 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
F: Fn() -> miette::Result<V>,
{
let mut attempts = attempts;
loop {
match f() {
Ok(v) => return Ok(v),
Err(e) if attempts == 1 => return Err(e),
Err(e) if retries == 0 => return Err(e),
_ => {
attempts -= 1;
thread::sleep(Duration::from_secs(delay));
retries -= 1;
thread::sleep(Duration::from_secs(delay_secs));
}
};
}