Skip to content

Commit

Permalink
Merge pull request containers#3893 from nalind/truncated-image-id-name
Browse files Browse the repository at this point in the history
Create shorter names for containers based on image IDs
  • Loading branch information
openshift-merge-robot authored Apr 2, 2022
2 parents aad5015 + a677a93 commit dc6c82e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/containers/image/v5/transports"
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/containers/storage/pkg/stringid"
digest "github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/openshift/imagebuilder"
Expand Down Expand Up @@ -48,6 +49,15 @@ func getImageName(name string, img *storage.Image) string {

func imageNamePrefix(imageName string) string {
prefix := imageName
if d, err := digest.Parse(imageName); err == nil {
prefix = d.Encoded()
if len(prefix) > 12 {
prefix = prefix[:12]
}
}
if stringid.ValidateID(prefix) == nil {
prefix = stringid.TruncateID(prefix)
}
s := strings.Split(prefix, ":")
if len(s) > 0 {
prefix = s[0]
Expand Down
16 changes: 16 additions & 0 deletions tests/from.bats
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,19 @@ load helpers
expect_output "$tmp"
BOGUS_PROXY=$tmp run_buildah 1 run $cid printenv BOGUS_PROXY
}

@test "from-image-by-id" {
_prefetch busybox
run_buildah from --cidfile ${TESTDIR}/cid busybox
cid=$(cat ${TESTDIR}/cid)
createrandom ${TESTDIR}/randomfile
run_buildah copy ${cid} ${TESTDIR}/randomfile /
run_buildah commit --iidfile ${TESTDIR}/iid ${cid}
iid=$(cat ${TESTDIR}/iid)
run_buildah from --cidfile ${TESTDIR}/cid2 ${iid}
cid2=$(cat ${TESTDIR}/cid2)
run_buildah run ${cid2} hostname -f
truncated=${iid##*:}
truncated=$(echo ${truncated} | cut -c-12)
expect_output ${truncated}-working-container
}

0 comments on commit dc6c82e

Please sign in to comment.