Skip to content

Commit 101af17

Browse files
authored
Improve usability of Target (#821)
* Makes the distro name/version fields non-optional (`String` instead of `Option<String>`), since (a) they will never be `None` in practice (see #820), and (b) it otherwise causes a lot of unnecessary friction for languages that have distro-version-specific binaries and need to use the metadata in multiple places in the buildpack. * Implements `Clone` and `Debug` on `Target`, which unblocks a number of use-cases, such as including the target as part of a buildpack Error enum variant. Fixes #820. GUS-W-15639138.
1 parent 65cfb80 commit 101af17

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [Unreleased]
1111

12+
### Added
13+
14+
- `libcnb`:
15+
- `Target` now implements `Clone` and `Debug`. ([#821](https://github.com/heroku/libcnb.rs/pull/821))
16+
17+
### Changed
18+
19+
- `libcnb`:
20+
- Changed the type of `Target`'s `distro_name` and `distro_version` fields from `Option<String>` to `String`. ([#821](https://github.com/heroku/libcnb.rs/pull/821))
21+
- The libcnb runtime now enforces that the `CNB_TARGET_DISTRO_NAME` and `CNB_TARGET_DISTRO_VERSION` env vars have been set by `lifecycle`. ([#821](https://github.com/heroku/libcnb.rs/pull/821))
1222

1323
## [0.20.0] - 2024-04-12
1424

libcnb/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ pub enum Error<E> {
2929
#[error("Couldn't determine target arch: {0}")]
3030
CannotDetermineTargetArch(std::env::VarError),
3131

32+
#[error("Couldn't determine target distro name: {0}. Ensure the `io.buildpacks.base.distro.*` Docker labels are set on the base image.")]
33+
CannotDetermineTargetDistroName(std::env::VarError),
34+
35+
#[error("Couldn't determine target distro version: {0}. Ensure the `io.buildpacks.base.distro.*` Docker labels are set on the base image.")]
36+
CannotDetermineTargetDistroVersion(std::env::VarError),
37+
3238
#[error("Couldn't create platform from platform path: {0}")]
3339
CannotCreatePlatformFromPath(std::io::Error),
3440

libcnb/src/layer/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,8 @@ fn build_context(temp_dir: &TempDir) -> BuildContext<TestBuildpack> {
904904
os: String::from("linux"),
905905
arch: String::from("amd64"),
906906
arch_variant: None,
907-
distro_name: Some(String::from("ubuntu")),
908-
distro_version: Some(String::from("22.04")),
907+
distro_name: String::from("ubuntu"),
908+
distro_version: String::from("22.04"),
909909
},
910910
platform: GenericPlatform::new(Env::new()),
911911
buildpack_plan: BuildpackPlan {

libcnb/src/runtime.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,10 @@ where
364364
let os = env::var("CNB_TARGET_OS").map_err(Error::CannotDetermineTargetOs)?;
365365
let arch = env::var("CNB_TARGET_ARCH").map_err(Error::CannotDetermineTargetArch)?;
366366
let arch_variant = env::var("CNB_TARGET_ARCH_VARIANT").ok();
367-
let distro_name = env::var("CNB_TARGET_DISTRO_NAME").ok();
368-
let distro_version = env::var("CNB_TARGET_DISTRO_VERSION").ok();
367+
let distro_name =
368+
env::var("CNB_TARGET_DISTRO_NAME").map_err(Error::CannotDetermineTargetDistroName)?;
369+
let distro_version =
370+
env::var("CNB_TARGET_DISTRO_VERSION").map_err(Error::CannotDetermineTargetDistroVersion)?;
369371

370372
Ok(Target {
371373
os,

libcnb/src/target.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1+
#[derive(Clone, Debug)]
12
pub struct Target {
23
/// The name of the target operating system.
34
///
45
/// The value should conform to [Go's `$GOOS`](https://golang.org/doc/install/source#environment), for example
56
/// `linux` or `windows`.
67
///
7-
/// CNB `lifecycle` sources this value from the build OCI image's [`os` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
8+
/// CNB `lifecycle` sources this value from the run OCI image's [`os` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
89
pub os: String,
910
/// The name of the target CPU architecture.
1011
///
1112
/// The value should conform to [Go's $GOARCH](https://golang.org/doc/install/source#environment), for example
1213
/// `amd64` or `arm64`.
1314
///
14-
/// CNB `lifecycle` sources this value from the build OCI image's [`architecture` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
15+
/// CNB `lifecycle` sources this value from the run OCI image's [`architecture` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
1516
pub arch: String,
1617
/// The variant of the specified CPU architecture.
1718
///
1819
/// The value should conform to [OCI image spec platform variants](https://github.com/opencontainers/image-spec/blob/main/image-index.md#platform-variants), for example
1920
/// `v7` or `v8`.
2021
///
21-
/// CNB `lifecycle` sources this value from the build OCI image's [`variant` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
22+
/// CNB `lifecycle` sources this value from the run OCI image's [`variant` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
2223
pub arch_variant: Option<String>,
2324
/// The name of the operating system distribution. Should be empty for Windows.
2425
///
2526
/// For example: `ubuntu` or `alpine`.
2627
///
27-
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.base.distro.name` label.
28-
pub distro_name: Option<String>,
28+
/// CNB `lifecycle` sources this value from either:
29+
/// 1. The `io.buildpacks.base.distro.name` OCI image label, if set on the run image.
30+
/// 2. Or else, the `ID` field of the `/etc/os-release` file in the build image.
31+
pub distro_name: String,
2932
/// The version of the operating system distribution.
3033
///
3134
/// For example: `22.04` or `3.19`.
3235
///
33-
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.base.distro.version` label.
34-
pub distro_version: Option<String>,
36+
/// CNB `lifecycle` sources this value from either:
37+
/// 1. The `io.buildpacks.base.distro.version` OCI image label, if set on the run image.
38+
/// 2. Or else, the `VERSION_ID` field of the `/etc/os-release` file in the build image.
39+
pub distro_version: String,
3540
}

0 commit comments

Comments
 (0)