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 Sep 3, 2024
1 parent 62f097c commit f12435b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
10 changes: 3 additions & 7 deletions task/buildah-oci-ta/0.2/buildah-oci-ta.yaml
Original file line number Diff line number Diff line change
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 @@ -415,9 +413,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
10 changes: 3 additions & 7 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 @@ -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 @@ -450,9 +448,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
10 changes: 3 additions & 7 deletions task/buildah-remote/0.2/buildah-remote.yaml
Original file line number Diff line number Diff line change
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 @@ -432,9 +430,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
10 changes: 3 additions & 7 deletions task/buildah/0.2/buildah.yaml
Original file line number Diff line number Diff line change
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 @@ -352,9 +350,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 f12435b

Please sign in to comment.