Skip to content

Commit 5a849d1

Browse files
committed
commit: always return the config digest as the image ID
When committing, if we didn't get an image ID off the bat because the image wasn't being committed to local storage, try to return the image's configuration blob digest, which is what is traditionally used as the image's ID. This allows the --iidfile flag to write a value to a file in situations where the image isn't being written to local storage. The image ID is of limited value in these cases, since we can't use it to look up the image anywhere else, but at least we don't write a file that just has the digest name prefix or log an empty string. Signed-off-by: Nalin Dahyabhai <[email protected]>
1 parent ac90794 commit 5a849d1

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

commit.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options
531531
if err != nil {
532532
return imgID, nil, "", fmt.Errorf("computing digest of manifest of new image %q: %w", transports.ImageName(dest), err)
533533
}
534+
if imgID == "" {
535+
parsedManifest, err := manifest.FromBlob(manifestBytes, manifest.GuessMIMEType(manifestBytes))
536+
if err != nil {
537+
return imgID, nil, "", fmt.Errorf("parsing written manifest to determine the image's ID: %w", err)
538+
}
539+
configInfo := parsedManifest.ConfigInfo()
540+
if configInfo.Size > 2 && configInfo.Digest.Validate() == nil { // don't be returning a digest of "" or "{}"
541+
imgID = configInfo.Digest.Encoded()
542+
}
543+
}
534544

535545
var ref reference.Canonical
536546
if name := dest.DockerReference(); name != nil {

imagebuildah/executor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,11 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
10861086
}
10871087
logrus.Debugf("printing final image id %q", imageID)
10881088
if b.iidfile != "" {
1089-
if err = os.WriteFile(b.iidfile, []byte("sha256:"+imageID), 0o644); err != nil {
1089+
iid := imageID
1090+
if iid != "" {
1091+
iid = "sha256:" + iid // only prepend a digest algorithm name if we actually got a value back
1092+
}
1093+
if err = os.WriteFile(b.iidfile, []byte(iid), 0o644); err != nil {
10901094
return imageID, ref, fmt.Errorf("failed to write image ID to file %q: %w", b.iidfile, err)
10911095
}
10921096
} else {

tests/bud.bats

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8958,3 +8958,29 @@ EOF
89588958
run_buildah build --layers=false "${contextdir}"
89598959
expect_output --substring /mounted/brand-new-subdir
89608960
}
8961+
8962+
@test "bud with --iidfile" {
8963+
target=scratch-image
8964+
local contextdir=${TEST_SCRATCH_DIR}/context
8965+
mkdir -p "${contextdir}"
8966+
cat > "${contextdir}"/Dockerfile <<-EOF
8967+
FROM scratch
8968+
COPY . .
8969+
EOF
8970+
for layers in "--layers=true" "--layers=false" ; do
8971+
for destination in "dir:${TEST_SCRATCH_DIR}/dir" "oci-archive:${TEST_SCRATCH_DIR}/oci-archive" "docker-archive:${TEST_SCRATCH_DIR}/docker-archive" "oci:${TEST_SCRATCH_DIR}/oci-layout" "local" ; do
8972+
rm -f "${TEST_SCRATCH_DIR}"/iidfile
8973+
fsname="${destination#*:}" # assume : is used in a non-containers-storage name rather than a repository name + tag combination
8974+
if test "${fsname}" != "${destination}" ; then
8975+
rm -fr "${fsname}"
8976+
fi
8977+
run_buildah build --iidfile "${TEST_SCRATCH_DIR}"/iidfile --no-cache "${layers}" -t "${destination}" "${contextdir}"
8978+
local iid=$(cat "${TEST_SCRATCH_DIR}"/iidfile)
8979+
assert "${iid}" != ""
8980+
assert "${iid#sha256:}" != ""
8981+
if test "${fsname}" != "${destination}" ; then
8982+
test -e "${fsname}"
8983+
fi
8984+
done
8985+
done
8986+
}

0 commit comments

Comments
 (0)