mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #35772 from arm64b/multi-arch-manifest-support
Download support of images with multi-arch manifest
This commit is contained in:
commit
fda904911d
2 changed files with 132 additions and 92 deletions
|
@ -6,7 +6,6 @@ set -eo pipefail
|
|||
|
||||
# debian latest f6fab3b798be 10 weeks ago 85.1 MB
|
||||
# debian latest f6fab3b798be3174f45aa1eb731f8182705555f89c9026d8c1ef230cbf8301dd 10 weeks ago 85.1 MB
|
||||
|
||||
if ! command -v curl &> /dev/null; then
|
||||
echo >&2 'error: "curl" not found!'
|
||||
exit 1
|
||||
|
@ -80,67 +79,32 @@ fetch_blob() {
|
|||
fi
|
||||
}
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
imageTag="$1"
|
||||
shift
|
||||
image="${imageTag%%[:@]*}"
|
||||
imageTag="${imageTag#*:}"
|
||||
digest="${imageTag##*@}"
|
||||
tag="${imageTag%%@*}"
|
||||
# handle 'application/vnd.docker.distribution.manifest.v2+json' manifest
|
||||
handle_single_manifest_v2() {
|
||||
local manifestJson="$1"; shift
|
||||
|
||||
# add prefix library if passed official image
|
||||
if [[ "$image" != *"/"* ]]; then
|
||||
image="library/$image"
|
||||
fi
|
||||
local configDigest="$(echo "$manifestJson" | jq --raw-output '.config.digest')"
|
||||
local imageId="${configDigest#*:}" # strip off "sha256:"
|
||||
|
||||
imageFile="${image//\//_}" # "/" can't be in filenames :)
|
||||
|
||||
token="$(curl -fsSL "$authBase/token?service=$authService&scope=repository:$image:pull" | jq --raw-output '.token')"
|
||||
|
||||
manifestJson="$(
|
||||
curl -fsSL \
|
||||
-H "Authorization: Bearer $token" \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.v1+json' \
|
||||
"$registryBase/v2/$image/manifests/$digest"
|
||||
)"
|
||||
if [ "${manifestJson:0:1}" != '{' ]; then
|
||||
echo >&2 "error: /v2/$image/manifests/$digest returned something unexpected:"
|
||||
echo >&2 " $manifestJson"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
imageIdentifier="$image:$tag@$digest"
|
||||
|
||||
schemaVersion="$(echo "$manifestJson" | jq --raw-output '.schemaVersion')"
|
||||
case "$schemaVersion" in
|
||||
2)
|
||||
mediaType="$(echo "$manifestJson" | jq --raw-output '.mediaType')"
|
||||
|
||||
case "$mediaType" in
|
||||
application/vnd.docker.distribution.manifest.v2+json)
|
||||
configDigest="$(echo "$manifestJson" | jq --raw-output '.config.digest')"
|
||||
imageId="${configDigest#*:}" # strip off "sha256:"
|
||||
|
||||
configFile="$imageId.json"
|
||||
local configFile="$imageId.json"
|
||||
fetch_blob "$token" "$image" "$configDigest" "$dir/$configFile" -s
|
||||
|
||||
layersFs="$(echo "$manifestJson" | jq --raw-output --compact-output '.layers[]')"
|
||||
IFS="$newlineIFS"
|
||||
layers=( $layersFs )
|
||||
local layersFs="$(echo "$manifestJson" | jq --raw-output --compact-output '.layers[]')"
|
||||
local IFS="$newlineIFS"
|
||||
local layers=( $layersFs )
|
||||
unset IFS
|
||||
|
||||
echo "Downloading '$imageIdentifier' (${#layers[@]} layers)..."
|
||||
layerId=
|
||||
layerFiles=()
|
||||
local layerId=
|
||||
local layerFiles=()
|
||||
for i in "${!layers[@]}"; do
|
||||
layerMeta="${layers[$i]}"
|
||||
local layerMeta="${layers[$i]}"
|
||||
|
||||
layerMediaType="$(echo "$layerMeta" | jq --raw-output '.mediaType')"
|
||||
layerDigest="$(echo "$layerMeta" | jq --raw-output '.digest')"
|
||||
local layerMediaType="$(echo "$layerMeta" | jq --raw-output '.mediaType')"
|
||||
local layerDigest="$(echo "$layerMeta" | jq --raw-output '.digest')"
|
||||
|
||||
# save the previous layer's ID
|
||||
parentId="$layerId"
|
||||
local parentId="$layerId"
|
||||
# create a new fake layer ID based on this layer's digest and the previous layer's fake ID
|
||||
layerId="$(echo "$parentId"$'\n'"$layerDigest" | sha256sum | cut -d' ' -f1)"
|
||||
# this accounts for the possibility that an image contains the same layer twice (and thus has a duplicate digest value)
|
||||
|
@ -149,8 +113,8 @@ while [ $# -gt 0 ]; do
|
|||
echo '1.0' > "$dir/$layerId/VERSION"
|
||||
|
||||
if [ ! -s "$dir/$layerId/json" ]; then
|
||||
parentJson="$(printf ', parent: "%s"' "$parentId")"
|
||||
addJson="$(printf '{ id: "%s"%s }' "$layerId" "${parentId:+$parentJson}")"
|
||||
local parentJson="$(printf ', parent: "%s"' "$parentId")"
|
||||
local addJson="$(printf '{ id: "%s"%s }' "$layerId" "${parentId:+$parentJson}")"
|
||||
# this starter JSON is taken directly from Docker's own "docker save" output for unimportant layers
|
||||
jq "$addJson + ." > "$dir/$layerId/json" <<-'EOJSON'
|
||||
{
|
||||
|
@ -180,7 +144,7 @@ while [ $# -gt 0 ]; do
|
|||
|
||||
case "$layerMediaType" in
|
||||
application/vnd.docker.image.rootfs.diff.tar.gzip)
|
||||
layerTar="$layerId/layer.tar"
|
||||
local layerTar="$layerId/layer.tar"
|
||||
layerFiles=( "${layerFiles[@]}" "$layerTar" )
|
||||
# TODO figure out why "-C -" doesn't work here
|
||||
# "curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume."
|
||||
|
@ -190,7 +154,7 @@ while [ $# -gt 0 ]; do
|
|||
echo "skipping existing ${layerId:0:12}"
|
||||
continue
|
||||
fi
|
||||
token="$(curl -fsSL "$authBase/token?service=$authService&scope=repository:$image:pull" | jq --raw-output '.token')"
|
||||
local token="$(curl -fsSL "$authBase/token?service=$authService&scope=repository:$image:pull" | jq --raw-output '.token')"
|
||||
fetch_blob "$token" "$image" "$layerDigest" "$dir/$layerTar" --progress
|
||||
;;
|
||||
|
||||
|
@ -205,10 +169,10 @@ while [ $# -gt 0 ]; do
|
|||
imageId="$layerId"
|
||||
|
||||
# munge the top layer image manifest to have the appropriate image configuration for older daemons
|
||||
imageOldConfig="$(jq --raw-output --compact-output '{ id: .id } + if .parent then { parent: .parent } else {} end' "$dir/$imageId/json")"
|
||||
local imageOldConfig="$(jq --raw-output --compact-output '{ id: .id } + if .parent then { parent: .parent } else {} end' "$dir/$imageId/json")"
|
||||
jq --raw-output "$imageOldConfig + del(.history, .rootfs)" "$dir/$configFile" > "$dir/$imageId/json"
|
||||
|
||||
manifestJsonEntry="$(
|
||||
local manifestJsonEntry="$(
|
||||
echo '{}' | jq --raw-output '. + {
|
||||
Config: "'"$configFile"'",
|
||||
RepoTags: ["'"${image#library\/}:$tag"'"],
|
||||
|
@ -216,8 +180,82 @@ while [ $# -gt 0 ]; do
|
|||
}'
|
||||
)"
|
||||
manifestJsonEntries=( "${manifestJsonEntries[@]}" "$manifestJsonEntry" )
|
||||
;;
|
||||
}
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
imageTag="$1"
|
||||
shift
|
||||
image="${imageTag%%[:@]*}"
|
||||
imageTag="${imageTag#*:}"
|
||||
digest="${imageTag##*@}"
|
||||
tag="${imageTag%%@*}"
|
||||
|
||||
# add prefix library if passed official image
|
||||
if [[ "$image" != *"/"* ]]; then
|
||||
image="library/$image"
|
||||
fi
|
||||
|
||||
imageFile="${image//\//_}" # "/" can't be in filenames :)
|
||||
|
||||
token="$(curl -fsSL "$authBase/token?service=$authService&scope=repository:$image:pull" | jq --raw-output '.token')"
|
||||
|
||||
manifestJson="$(
|
||||
curl -fsSL \
|
||||
-H "Authorization: Bearer $token" \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.list.v2+json' \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.v1+json' \
|
||||
"$registryBase/v2/$image/manifests/$digest"
|
||||
)"
|
||||
if [ "${manifestJson:0:1}" != '{' ]; then
|
||||
echo >&2 "error: /v2/$image/manifests/$digest returned something unexpected:"
|
||||
echo >&2 " $manifestJson"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
imageIdentifier="$image:$tag@$digest"
|
||||
|
||||
schemaVersion="$(echo "$manifestJson" | jq --raw-output '.schemaVersion')"
|
||||
case "$schemaVersion" in
|
||||
2)
|
||||
mediaType="$(echo "$manifestJson" | jq --raw-output '.mediaType')"
|
||||
|
||||
case "$mediaType" in
|
||||
application/vnd.docker.distribution.manifest.v2+json)
|
||||
handle_single_manifest_v2 "$manifestJson"
|
||||
;;
|
||||
application/vnd.docker.distribution.manifest.list.v2+json)
|
||||
layersFs="$(echo "$manifestJson" | jq --raw-output --compact-output '.manifests[]')"
|
||||
IFS="$newlineIFS"
|
||||
layers=( $layersFs )
|
||||
unset IFS
|
||||
|
||||
found=""
|
||||
# parse first level multi-arch manifest
|
||||
for i in "${!layers[@]}"; do
|
||||
layerMeta="${layers[$i]}"
|
||||
maniArch="$(echo "$layerMeta" | jq --raw-output '.platform.architecture')"
|
||||
if [ "$maniArch" = "$(go env GOARCH)" ]; then
|
||||
digest="$(echo "$layerMeta" | jq --raw-output '.digest')"
|
||||
# get second level single manifest
|
||||
submanifestJson="$(
|
||||
curl -fsSL \
|
||||
-H "Authorization: Bearer $token" \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.list.v2+json' \
|
||||
-H 'Accept: application/vnd.docker.distribution.manifest.v1+json' \
|
||||
"$registryBase/v2/$image/manifests/$digest"
|
||||
)"
|
||||
handle_single_manifest_v2 "$submanifestJson"
|
||||
found="found"
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ -z "$found" ]; then
|
||||
echo >&2 "error: manifest for $maniArch is not found"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo >&2 "error: unknown manifest mediaType ($imageIdentifier): '$mediaType'"
|
||||
exit 1
|
||||
|
|
|
@ -64,7 +64,9 @@ case "$PACKAGE_ARCH" in
|
|||
;;
|
||||
*)
|
||||
DOCKERFILE="Dockerfile.$PACKAGE_ARCH"
|
||||
if [ "$PACKAGE_ARCH" != "aarch64" ]; then
|
||||
TEST_IMAGE_NAMESPACE="$PACKAGE_ARCH"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
export DOCKERFILE TEST_IMAGE_NAMESPACE
|
||||
|
|
Loading…
Reference in a new issue