Skip to content

Commit

Permalink
fix: Out of bounds panic when not retrying push
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Aug 11, 2024
1 parent 82606cc commit 464fdf9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions src/drivers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/opts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
9 changes: 4 additions & 5 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
}
Expand Down

0 comments on commit 464fdf9

Please sign in to comment.