Skip to content

Commit

Permalink
Replace grep base images parsing with dockerfile-json
Browse files Browse the repository at this point in the history
This is more reliable and allow us to fix bugs where base images were
loaded incorrectly.

For example, previously this part in Dockerfile:

LABEL description="this is a build \
                   from single-arch"

Would return "single-arch" as a base image.

Using dockerfile-json also solves the problem of omitting "scratch" from
the results.

Another advantage is that when we have something such as:

FROM registry.access.redhat.com/ubi9/ubi:latest as builder
...
FROM builder AS build1

then only the original image
"registry.access.redhat.com/ubi9/ubi:latest" will be reported.

KFLUXBUGS-1269

Signed-off-by: mkosiarc <mkosiarc@redhat.com>
  • Loading branch information
mkosiarc committed Aug 17, 2024
1 parent 24b12ab commit 4e3d0ae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
12 changes: 4 additions & 8 deletions task/buildah-oci-ta/0.2/buildah-oci-ta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ spec:
- $(params.SOURCE_ARTIFACT)=/var/workdir/source
- $(params.CACHI2_ARTIFACT)=/var/workdir/cachi2
- name: build
image: quay.io/konflux-ci/buildah-task:latest@sha256:5d933087a49a6e0b959b9a9fa5a91d545380217e565d7be7cc74a9588f64c314
image: quay.io/redhat-user-workloads/rhtap-build-tenant/buildah-container/buildah-task:on-pr-975a99f3a66987703e5d5c08f3f2a049165f71e5
args:
- $(params.BUILD_ARGS[*])
workingDir: /var/workdir
Expand Down Expand Up @@ -288,14 +288,12 @@ spec:
BUILDAH_ARGS=()
BASE_IMAGES=$(grep -i '^\s*FROM' "$dockerfile_path" | sed 's/--platform=\S*//' | awk '{print $2}' | (grep -v ^oci-archive: || true))
BASE_IMAGES=$(dockerfile-json "$dockerfile_path" | jq -r '.Stages[] | select(.From | .Stage or .Scratch | not) | .BaseName')
if [ "${HERMETIC}" == "true" ]; then
BUILDAH_ARGS+=("--pull=never")
UNSHARE_ARGS="--net"
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
fi
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
done
echo "Build will be executed with network isolation"
fi
Expand Down Expand Up @@ -413,9 +411,7 @@ spec:
touch /shared/base_images_digests
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >>/shared/base_images_digests
fi
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >>/shared/base_images_digests
done
# Needed to generate base images SBOM
Expand Down
14 changes: 5 additions & 9 deletions task/buildah-remote-oci-ta/0.2/buildah-remote-oci-ta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ spec:
- name: YUM_REPOS_D_TARGET
value: $(params.YUM_REPOS_D_TARGET)
- name: BUILDER_IMAGE
value: quay.io/konflux-ci/buildah-task:latest@sha256:5d933087a49a6e0b959b9a9fa5a91d545380217e565d7be7cc74a9588f64c314
value: quay.io/redhat-user-workloads/rhtap-build-tenant/buildah-container/buildah-task:on-pr-975a99f3a66987703e5d5c08f3f2a049165f71e5
- name: PLATFORM
value: $(params.PLATFORM)
- name: IMAGE_APPEND_PLATFORM
Expand Down Expand Up @@ -221,7 +221,7 @@ spec:
env:
- name: COMMIT_SHA
value: $(params.COMMIT_SHA)
image: quay.io/konflux-ci/buildah-task:latest@sha256:5d933087a49a6e0b959b9a9fa5a91d545380217e565d7be7cc74a9588f64c314
image: quay.io/redhat-user-workloads/rhtap-build-tenant/buildah-container/buildah-task:on-pr-975a99f3a66987703e5d5c08f3f2a049165f71e5
name: build
script: |-
#!/bin/bash
Expand Down Expand Up @@ -323,14 +323,12 @@ spec:
BUILDAH_ARGS=()
BASE_IMAGES=$(grep -i '^\s*FROM' "$dockerfile_path" | sed 's/--platform=\S*//' | awk '{print $2}' | (grep -v ^oci-archive: || true))
BASE_IMAGES=$(dockerfile-json "$dockerfile_path" | jq -r '.Stages[] | select(.From | .Stage or .Scratch | not) | .BaseName')
if [ "${HERMETIC}" == "true" ]; then
BUILDAH_ARGS+=("--pull=never")
UNSHARE_ARGS="--net"
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
fi
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
done
echo "Build will be executed with network isolation"
fi
Expand Down Expand Up @@ -448,9 +446,7 @@ spec:
touch /shared/base_images_digests
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >>/shared/base_images_digests
fi
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >>/shared/base_images_digests
done
# Needed to generate base images SBOM
Expand Down
14 changes: 5 additions & 9 deletions task/buildah-remote/0.2/buildah-remote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ spec:
- name: SKIP_UNUSED_STAGES
value: $(params.SKIP_UNUSED_STAGES)
- name: BUILDER_IMAGE
value: quay.io/konflux-ci/buildah-task:latest@sha256:5d933087a49a6e0b959b9a9fa5a91d545380217e565d7be7cc74a9588f64c314
value: quay.io/redhat-user-workloads/rhtap-build-tenant/buildah-container/buildah-task:on-pr-975a99f3a66987703e5d5c08f3f2a049165f71e5
- name: PLATFORM
value: $(params.PLATFORM)
- name: IMAGE_APPEND_PLATFORM
Expand All @@ -203,7 +203,7 @@ spec:
env:
- name: COMMIT_SHA
value: $(params.COMMIT_SHA)
image: quay.io/konflux-ci/buildah-task:latest@sha256:5d933087a49a6e0b959b9a9fa5a91d545380217e565d7be7cc74a9588f64c314
image: quay.io/redhat-user-workloads/rhtap-build-tenant/buildah-container/buildah-task:on-pr-975a99f3a66987703e5d5c08f3f2a049165f71e5
name: build
script: |-
#!/bin/bash
Expand Down Expand Up @@ -305,14 +305,12 @@ spec:
BUILDAH_ARGS=()
BASE_IMAGES=$(grep -i '^\s*FROM' "$dockerfile_path" | sed 's/--platform=\S*//' | awk '{print $2}' | (grep -v ^oci-archive: || true))
BASE_IMAGES=$(dockerfile-json "$dockerfile_path" | jq -r '.Stages[] | select(.From | .Stage or .Scratch | not) | .BaseName')
if [ "${HERMETIC}" == "true" ]; then
BUILDAH_ARGS+=("--pull=never")
UNSHARE_ARGS="--net"
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
fi
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
done
echo "Build will be executed with network isolation"
fi
Expand Down Expand Up @@ -430,9 +428,7 @@ spec:
touch /shared/base_images_digests
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >> /shared/base_images_digests
fi
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >> /shared/base_images_digests
done
# Needed to generate base images SBOM
Expand Down
12 changes: 4 additions & 8 deletions task/buildah/0.2/buildah.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ spec:
value: $(params.SKIP_UNUSED_STAGES)

steps:
- image: quay.io/konflux-ci/buildah-task:latest@sha256:5d933087a49a6e0b959b9a9fa5a91d545380217e565d7be7cc74a9588f64c314
- image: quay.io/redhat-user-workloads/rhtap-build-tenant/buildah-container/buildah-task:on-pr-975a99f3a66987703e5d5c08f3f2a049165f71e5
name: build
computeResources:
limits:
Expand Down Expand Up @@ -225,14 +225,12 @@ spec:
BUILDAH_ARGS=()
BASE_IMAGES=$(grep -i '^\s*FROM' "$dockerfile_path" | sed 's/--platform=\S*//' | awk '{print $2}' | (grep -v ^oci-archive: || true))
BASE_IMAGES=$(dockerfile-json "$dockerfile_path" | jq -r '.Stages[] | select(.From | .Stage or .Scratch | not) | .BaseName')
if [ "${HERMETIC}" == "true" ]; then
BUILDAH_ARGS+=("--pull=never")
UNSHARE_ARGS="--net"
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
fi
unshare -Ufp --keep-caps -r --map-users 1,1,65536 --map-groups 1,1,65536 -- buildah pull $image
done
echo "Build will be executed with network isolation"
fi
Expand Down Expand Up @@ -350,9 +348,7 @@ spec:
touch /shared/base_images_digests
for image in $BASE_IMAGES; do
if [ "${image}" != "scratch" ]; then
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >> /shared/base_images_digests
fi
buildah images --format '{{ .Name }}:{{ .Tag }}@{{ .Digest }}' --filter reference="$image" >> /shared/base_images_digests
done
# Needed to generate base images SBOM
Expand Down

0 comments on commit 4e3d0ae

Please sign in to comment.