Skip to content

Commit 0065159

Browse files
authored
feat(boil): Support Containerfiles per version (#1302)
* feat(boil): Support Containerfiles per version * feat(boil): Add image directory to error message * chore: Use 'dockerfile' as config option by default
1 parent ddfeaf4 commit 0065159

File tree

5 files changed

+154
-7
lines changed

5 files changed

+154
-7
lines changed

Cargo.lock

Lines changed: 117 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["Stackable <info@stackable.tech>"]
88
license = "Apache-2.0"
99

1010
[workspace.dependencies]
11+
cap-std = "3.4.4"
1112
clap = { version = "4.5.41", features = ["derive"] }
1213
clap_complete = "4.5.55"
1314
clap_complete_nushell = "4.5.8"

rust/boil/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ repository.workspace = true
88
publish = false
99

1010
[dependencies]
11+
cap-std.workspace = true
1112
clap.workspace = true
1213
clap_complete.workspace = true
1314
clap_complete_nushell.workspace = true

rust/boil/src/build/bakefile.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ use std::{
55
path::PathBuf,
66
};
77

8+
use cap_std::{ambient_authority, fs::Dir};
89
use glob::glob;
910
use oci_spec::image::{
1011
ANNOTATION_AUTHORS, ANNOTATION_CREATED, ANNOTATION_DOCUMENTATION, ANNOTATION_LICENSES,
1112
ANNOTATION_REVISION, ANNOTATION_SOURCE, ANNOTATION_VENDOR, ANNOTATION_VERSION,
1213
};
1314
use semver::Version;
1415
use serde::Serialize;
15-
use snafu::{OptionExt, ResultExt, Snafu};
16+
use snafu::{OptionExt, ResultExt, Snafu, ensure};
1617
use time::format_description::well_known::Rfc3339;
1718

1819
use crate::{
@@ -55,6 +56,9 @@ pub enum Error {
5556

5657
#[snafu(display("failed to parse build arguments"))]
5758
ParseBuildArguments { source: ParseBuildArgumentsError },
59+
60+
#[snafu(display("failed to locate containerfile relative to the {path:?} directory"))]
61+
NoSuchContainerfileExists { path: String },
5862
}
5963

6064
#[derive(Debug, Snafu)]
@@ -329,9 +333,28 @@ impl Bakefile {
329333
args.strip_architecture,
330334
);
331335

332-
let dockerfile = PathBuf::new()
333-
.join(&image_name)
334-
.join(&args.target_containerfile);
336+
// By using a cap-std Dir, we can ensure that the paths provided must be relative to
337+
// the appropriate image folder and wont escape it by providing absolute or relative
338+
// paths with traversals (..).
339+
let image_dir = Dir::open_ambient_dir(&image_name, ambient_authority()).unwrap();
340+
341+
let dockerfile_path = if let Some(custom_path) = &image_options.dockerfile {
342+
ensure!(
343+
image_dir.exists(custom_path),
344+
NoSuchContainerfileExistsSnafu { path: image_name }
345+
);
346+
347+
PathBuf::new().join(&image_name).join(custom_path)
348+
} else {
349+
ensure!(
350+
image_dir.exists(&args.target_containerfile),
351+
NoSuchContainerfileExistsSnafu { path: image_name }
352+
);
353+
354+
PathBuf::new()
355+
.join(&image_name)
356+
.join(&args.target_containerfile)
357+
};
335358

336359
let target_name = if is_entry {
337360
Self::format_entry_target_name(&image_name, &image_version)
@@ -359,7 +382,7 @@ impl Bakefile {
359382
platforms: vec![args.target_platform.clone()],
360383
// NOTE (@Techassi): Should this instead be scoped to the folder of the image we build
361384
context: Some(PathBuf::from(".")),
362-
dockerfile: Some(dockerfile),
385+
dockerfile: Some(dockerfile_path),
363386
inherits: vec![COMMON_TARGET_NAME.to_owned()],
364387
annotations,
365388
contexts,

rust/boil/src/build/image.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ pub struct ImageOptions {
182182
// suffixed with _VERSION.
183183
#[serde(default)]
184184
pub build_arguments: BuildArguments,
185+
186+
/// A custom path to a Dockerfile/Containerfile for a particular version of an image.
187+
///
188+
/// This is usefull for cases where the same image is being built differently depending on it's
189+
/// version and it is too difficult/messy to do it the same Dockerfile/Containerfile.
190+
#[serde(alias = "containerfile")]
191+
pub dockerfile: Option<PathBuf>,
185192
}
186193

187194
#[derive(Debug)]

0 commit comments

Comments
 (0)