[v6,3/5] classes: allow more control over container image name and tag

Message ID 20211215172550.27153-4-henning.schild@siemens.com
State Accepted, archived
Headers show
Series Allow better control over container tags | expand

Commit Message

Henning Schild Dec. 15, 2021, 7:25 a.m. UTC
This patch allows more fine-grained control over how the resulting
container will be tagged. Where the default name will be PN together
with DISTRO and ARCH, and tag will be derived from PV and PR

Signed-off-by: Henning Schild <henning.schild@siemens.com>
---
 RECIPE-API-CHANGELOG.md                        |  4 ++++
 doc/user_manual.md                             |  8 ++++----
 meta/classes/container-img.bbclass             |  4 +---
 meta/classes/image-container-extension.bbclass | 12 +++++++-----
 meta/classes/image-sdk-extension.bbclass       |  2 +-
 5 files changed, 17 insertions(+), 13 deletions(-)

Comments

Henning Schild Jan. 4, 2022, 7:53 a.m. UTC | #1
Am Wed, 15 Dec 2021 18:25:48 +0100
schrieb Henning Schild <henning.schild@siemens.com>:

> This patch allows more fine-grained control over how the resulting
> container will be tagged. Where the default name will be PN together
> with DISTRO and ARCH, and tag will be derived from PV and PR
> 
> Signed-off-by: Henning Schild <henning.schild@siemens.com>
> ---
>  RECIPE-API-CHANGELOG.md                        |  4 ++++
>  doc/user_manual.md                             |  8 ++++----
>  meta/classes/container-img.bbclass             |  4 +---
>  meta/classes/image-container-extension.bbclass | 12 +++++++-----
>  meta/classes/image-sdk-extension.bbclass       |  2 +-
>  5 files changed, 17 insertions(+), 13 deletions(-)
> 
> diff --git a/RECIPE-API-CHANGELOG.md b/RECIPE-API-CHANGELOG.md
> index 55836258ce49..b3e6a2af199d 100644
> --- a/RECIPE-API-CHANGELOG.md
> +++ b/RECIPE-API-CHANGELOG.md
> @@ -306,3 +306,7 @@ When using the plugins it is advised to name the
> partition "/boot" and to exclud 
>  The variable is renamed to get closer to OE/Poky variables naming.
> The old naming will still also work, but with deprecation warning
> shown. +
> +### Change default "NAME:TAG" when building container images
> +
> +The "NAME" used to be rather static and the TAG was always "latest",
> now the values are derived from recipe variables PN, PV, PR. diff
> --git a/doc/user_manual.md b/doc/user_manual.md index
> c81c6e466e40..eaafd6a9d708 100644 --- a/doc/user_manual.md
> +++ b/doc/user_manual.md
> @@ -305,14 +305,14 @@ bitbake mc:qemuarm-buster:isar-image-base
>   - Load the container image into the Docker Daemon
>  
>  ```
> -docker load -i
> build/tmp/deploy/images/qemuarm/debian-buster-armhf-docker-archive.tar.xz
> +docker load -i
> build/tmp/deploy/images/qemuarm/isar-image-base-debian-buster-armhf-1.0-r0-docker-archive.tar.xz

I just tried this for real in the layer i wrote it for. Turns out that
the version in the filename might not be the best idea, because none of
our images do that.
Having the version in the metadata might be good enough while having a
stable filename.

What do you guys think, should i write another patch to remove the
version from the deployed image? I tend to do so and it would be more
in line with all our other image types.

On the other hand we could think about PV in the filename of all our
images, but that would be a pretty dramatic change. In fact the PV of
an image is not too meaningful to most people and hardly ever bumped,
people rely on metadata ... /etc/os-release.

Henning

> ``` 
>   - Run a container using the container image (following commands
> starting with `#~:` are to be run in the container)
>  
>  ```
> -docker run --rm -ti --volume "$(pwd):/build" isar-buster-armhf:latest
> +docker run --rm -ti --volume "$(pwd):/build"
> isar-image-base-debian-buster-armhf:1.0-r0 ```
>  
>  ---
> @@ -1139,14 +1139,14 @@ bitbake -c do_populate_sdk
> mc:qemuarm-buster:isar-image-base
>   - Load the SDK container image into the Docker Daemon
>  
>  ```
> -docker load -i
> build/tmp/deploy/images/qemuarm/sdk-debian-buster-armhf-docker-archive.tar.xz
> +docker load -i
> build/tmp/deploy/images/qemuarm/sdk-isar-image-base-debian-buster-armhf-1.0-r0-docker-archive.tar.xz
> ``` 
>   - Run a container using the SDK container image (following commands
> starting with `#~:` are to be run in the container)
>  
>  ```
> -docker run --rm -ti --volume "$(pwd):/build"
> isar-sdk-buster-armhf:latest +docker run --rm -ti --volume
> "$(pwd):/build" sdk-isar-image-base-debian-buster-armhf:1.0-r0 ```
>  
>   - Check that cross toolchains are installed
> diff --git a/meta/classes/container-img.bbclass
> b/meta/classes/container-img.bbclass index 8fef52a7195c..4e0fe0efb484
> 100644 --- a/meta/classes/container-img.bbclass
> +++ b/meta/classes/container-img.bbclass
> @@ -10,10 +10,8 @@ do_container_image[dirs] = "${DEPLOY_DIR_IMAGE}"
>  do_container_image[stamp-extra-info] = "${DISTRO}-${MACHINE}"
>  do_container_image[vardeps] += "CONTAINER_FORMATS"
>  do_container_image(){
> -    rootfs_id="${DISTRO}-${DISTRO_ARCH}"
> -
>      bbdebug 1 "Generate container image in these formats:
> ${CONTAINER_FORMATS}"
> -    containerize_rootfs "${IMAGE_ROOTFS}" "${rootfs_id}"
> "${CONTAINER_FORMATS}"
> +    containerize_rootfs "${IMAGE_ROOTFS}" "${CONTAINER_FORMATS}"
>  }
>  
>  addtask container_image before do_image after do_image_tools
> diff --git a/meta/classes/image-container-extension.bbclass
> b/meta/classes/image-container-extension.bbclass index
> 0e70ba9c1405..b8cf85a5c256 100644 ---
> a/meta/classes/image-container-extension.bbclass +++
> b/meta/classes/image-container-extension.bbclass @@ -6,15 +6,17 @@
>  # This class extends the image.bbclass for containerizing the root
> filesystem. 
>  CONTAINER_FORMATS ?= "docker-archive"
> +CONTAINER_IMAGE_NAME ?= "${PN}-${DISTRO}-${DISTRO_ARCH}"
> +CONTAINER_IMAGE_TAG ?= "${PV}-${PR}"
>  
>  containerize_rootfs() {
>      local cmd="/bin/dash"
>      local empty_tag="empty"
> -    local tag="latest"
> +    local tag="${CONTAINER_IMAGE_TAG}"
>      local oci_img_dir="${WORKDIR}/oci-image"
>      local rootfs="$1"
> -    local rootfs_id="$2"
> -    local container_formats="$3"
> +    local container_formats="$2"
> +    local container_name_prefix="$3"
>  
>      # prepare OCI container image skeleton
>      bbdebug 1 "prepare OCI container image skeleton"
> @@ -42,9 +44,9 @@ containerize_rootfs() {
>      sudo chown --recursive $(id -u):$(id -g) "${oci_img_dir}"
>  
>      # convert the OCI container image to the desired format
> -    image_name="isar-${rootfs_id}"
> +    image_name="${container_name_prefix}${CONTAINER_IMAGE_NAME}"
>      for image_type in ${CONTAINER_FORMATS} ; do
> -
> image_archive="${DEPLOY_DIR_IMAGE}/${rootfs_id}-${image_type}.tar"
> +
> image_archive="${DEPLOY_DIR_IMAGE}/${image_name}-${tag}-${image_type}.tar"
> bbdebug 1 "Creating container image type: ${image_type}" case
> "${image_type}" in "docker-archive" | "oci-archive")
> diff --git a/meta/classes/image-sdk-extension.bbclass
> b/meta/classes/image-sdk-extension.bbclass index
> 426b92595554..052c1b580451 100644 ---
> a/meta/classes/image-sdk-extension.bbclass +++
> b/meta/classes/image-sdk-extension.bbclass @@ -80,7 +80,7 @@
> do_populate_sdk() { # generate the SDK in all the desired container
> formats if [ -n "${sdk_container_formats}" ] ; then
>          bbnote "Generating SDK container in ${sdk_container_formats}
> format"
> -        containerize_rootfs "${SDKCHROOT_DIR}"
> "sdk-${DISTRO}-${DISTRO_ARCH}" "${sdk_container_formats}"
> +        containerize_rootfs "${SDKCHROOT_DIR}"
> "${sdk_container_formats}" "sdk-" fi
>  }
>

Patch

diff --git a/RECIPE-API-CHANGELOG.md b/RECIPE-API-CHANGELOG.md
index 55836258ce49..b3e6a2af199d 100644
--- a/RECIPE-API-CHANGELOG.md
+++ b/RECIPE-API-CHANGELOG.md
@@ -306,3 +306,7 @@  When using the plugins it is advised to name the partition "/boot" and to exclud
 
 The variable is renamed to get closer to OE/Poky variables naming. The old naming
 will still also work, but with deprecation warning shown.
+
+### Change default "NAME:TAG" when building container images
+
+The "NAME" used to be rather static and the TAG was always "latest", now the values are derived from recipe variables PN, PV, PR.
diff --git a/doc/user_manual.md b/doc/user_manual.md
index c81c6e466e40..eaafd6a9d708 100644
--- a/doc/user_manual.md
+++ b/doc/user_manual.md
@@ -305,14 +305,14 @@  bitbake mc:qemuarm-buster:isar-image-base
  - Load the container image into the Docker Daemon
 
 ```
-docker load -i build/tmp/deploy/images/qemuarm/debian-buster-armhf-docker-archive.tar.xz
+docker load -i build/tmp/deploy/images/qemuarm/isar-image-base-debian-buster-armhf-1.0-r0-docker-archive.tar.xz
 ```
 
  - Run a container using the container image (following commands starting with 
    `#~:` are to be run in the container)
 
 ```
-docker run --rm -ti --volume "$(pwd):/build" isar-buster-armhf:latest
+docker run --rm -ti --volume "$(pwd):/build" isar-image-base-debian-buster-armhf:1.0-r0
 ```
 
 ---
@@ -1139,14 +1139,14 @@  bitbake -c do_populate_sdk mc:qemuarm-buster:isar-image-base
  - Load the SDK container image into the Docker Daemon
 
 ```
-docker load -i build/tmp/deploy/images/qemuarm/sdk-debian-buster-armhf-docker-archive.tar.xz
+docker load -i build/tmp/deploy/images/qemuarm/sdk-isar-image-base-debian-buster-armhf-1.0-r0-docker-archive.tar.xz
 ```
 
  - Run a container using the SDK container image (following commands starting 
    with `#~:` are to be run in the container)
 
 ```
-docker run --rm -ti --volume "$(pwd):/build" isar-sdk-buster-armhf:latest
+docker run --rm -ti --volume "$(pwd):/build" sdk-isar-image-base-debian-buster-armhf:1.0-r0
 ```
 
  - Check that cross toolchains are installed
diff --git a/meta/classes/container-img.bbclass b/meta/classes/container-img.bbclass
index 8fef52a7195c..4e0fe0efb484 100644
--- a/meta/classes/container-img.bbclass
+++ b/meta/classes/container-img.bbclass
@@ -10,10 +10,8 @@  do_container_image[dirs] = "${DEPLOY_DIR_IMAGE}"
 do_container_image[stamp-extra-info] = "${DISTRO}-${MACHINE}"
 do_container_image[vardeps] += "CONTAINER_FORMATS"
 do_container_image(){
-    rootfs_id="${DISTRO}-${DISTRO_ARCH}"
-
     bbdebug 1 "Generate container image in these formats: ${CONTAINER_FORMATS}"
-    containerize_rootfs "${IMAGE_ROOTFS}" "${rootfs_id}" "${CONTAINER_FORMATS}"
+    containerize_rootfs "${IMAGE_ROOTFS}" "${CONTAINER_FORMATS}"
 }
 
 addtask container_image before do_image after do_image_tools
diff --git a/meta/classes/image-container-extension.bbclass b/meta/classes/image-container-extension.bbclass
index 0e70ba9c1405..b8cf85a5c256 100644
--- a/meta/classes/image-container-extension.bbclass
+++ b/meta/classes/image-container-extension.bbclass
@@ -6,15 +6,17 @@ 
 # This class extends the image.bbclass for containerizing the root filesystem.
 
 CONTAINER_FORMATS ?= "docker-archive"
+CONTAINER_IMAGE_NAME ?= "${PN}-${DISTRO}-${DISTRO_ARCH}"
+CONTAINER_IMAGE_TAG ?= "${PV}-${PR}"
 
 containerize_rootfs() {
     local cmd="/bin/dash"
     local empty_tag="empty"
-    local tag="latest"
+    local tag="${CONTAINER_IMAGE_TAG}"
     local oci_img_dir="${WORKDIR}/oci-image"
     local rootfs="$1"
-    local rootfs_id="$2"
-    local container_formats="$3"
+    local container_formats="$2"
+    local container_name_prefix="$3"
 
     # prepare OCI container image skeleton
     bbdebug 1 "prepare OCI container image skeleton"
@@ -42,9 +44,9 @@  containerize_rootfs() {
     sudo chown --recursive $(id -u):$(id -g) "${oci_img_dir}"
 
     # convert the OCI container image to the desired format
-    image_name="isar-${rootfs_id}"
+    image_name="${container_name_prefix}${CONTAINER_IMAGE_NAME}"
     for image_type in ${CONTAINER_FORMATS} ; do
-        image_archive="${DEPLOY_DIR_IMAGE}/${rootfs_id}-${image_type}.tar"
+        image_archive="${DEPLOY_DIR_IMAGE}/${image_name}-${tag}-${image_type}.tar"
         bbdebug 1 "Creating container image type: ${image_type}"
         case "${image_type}" in
             "docker-archive" | "oci-archive")
diff --git a/meta/classes/image-sdk-extension.bbclass b/meta/classes/image-sdk-extension.bbclass
index 426b92595554..052c1b580451 100644
--- a/meta/classes/image-sdk-extension.bbclass
+++ b/meta/classes/image-sdk-extension.bbclass
@@ -80,7 +80,7 @@  do_populate_sdk() {
     # generate the SDK in all the desired container formats
     if [ -n "${sdk_container_formats}" ] ; then
         bbnote "Generating SDK container in ${sdk_container_formats} format"
-        containerize_rootfs "${SDKCHROOT_DIR}" "sdk-${DISTRO}-${DISTRO_ARCH}" "${sdk_container_formats}"
+        containerize_rootfs "${SDKCHROOT_DIR}" "${sdk_container_formats}" "sdk-"
     fi
 }