Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #94 from google/upstream-1534278087
Browse files Browse the repository at this point in the history
Fix bug in foreign layer handling
  • Loading branch information
jonjohnsonjr authored Aug 14, 2018
2 parents bf22fb4 + 2acf471 commit 2f1cfd3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
13 changes: 5 additions & 8 deletions client/v2_2/docker_image_.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,11 @@ def _populate_manifest(self):
# Manifest files found in tar files are actually a json list.
# This code iterates through that collection and appends any foreign
# layers described in the order found in the config file.
foreign_layers_list = json.loads(self._foreign_layers_manifest)
for foreign_layers in foreign_layers_list:
if 'LayerSources' in foreign_layers:
config = json.loads(self._config)
layer_sources = foreign_layers['LayerSources']
for diff_id in config['rootfs']['diff_ids']:
if diff_id in layer_sources:
base_layers.append(layer_sources[diff_id])
manifest = json.loads(self._foreign_layers_manifest)
if 'layers' in manifest:
for layer in manifest['layers']:
if layer['mediaType'] == docker_http.FOREIGN_LAYER_MIME:
base_layers.append(layer)

# TODO(user): Update mimes here for oci_compat.
self._manifest = json.dumps(
Expand Down
18 changes: 11 additions & 7 deletions client/v2_2/save_.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from containerregistry.client.v1 import save as v1_save
from containerregistry.client.v2 import v1_compat
from containerregistry.client.v2_2 import docker_digest
from containerregistry.client.v2_2 import docker_http
from containerregistry.client.v2_2 import docker_image as v2_2_image
from containerregistry.client.v2_2 import v2_compat

Expand All @@ -38,11 +39,10 @@

def _diff_id(v1_img, blob):
try:
return v1_img.diff_id(blob)
except ValueError:
unzipped = v1_img.uncompressed_layer(blob)
return docker_digest.SHA256(unzipped)
except IOError:
# For foreign layers, we do not have the layer.tar
return v1_img.diff_id(blob)


def multi_image_tarball(
Expand Down Expand Up @@ -76,8 +76,6 @@ def add_file(filename, contents):
# ancestry ordering.
# - RepoTags: the list of tags to apply to this image once it
# is loaded.
# - LayerSources: optionally declare foreign layers declared in
# the base image
manifests = []

for (tag, image) in six.iteritems(tag_to_image):
Expand Down Expand Up @@ -108,10 +106,16 @@ def add_file(filename, contents):
'RepoTags': [str(tag)]
}

layer_sources = {}
input_manifest = json.loads(image.manifest())
input_layers = input_manifest['layers']

for i, diff_id in enumerate(diffs):
if input_layers[i]['mediaType'] == docker_http.FOREIGN_LAYER_MIME:
layer_sources[diff_id] = input_layers[i]

if 'LayerSources' in input_manifest:
manifest['LayerSources'] = input_manifest['LayerSources']
if layer_sources:
manifest['LayerSources'] = layer_sources

manifests.append(manifest)

Expand Down
14 changes: 13 additions & 1 deletion tools/fast_pusher_.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
action='store',
help='The path to the file storing the image config.')

parser.add_argument(
'--manifest',
action='store',
required=False,
help='The path to the file storing the image manifest.')

parser.add_argument(
'--digest',
action='append',
Expand Down Expand Up @@ -124,6 +130,7 @@ def main():
# If config is specified, use that. Otherwise, fallback on reading
# the config from the tarball.
config = args.config
manifest = args.manifest
if args.config:
logging.info('Reading config from %r', args.config)
with open(args.config, 'r') as reader:
Expand All @@ -133,6 +140,10 @@ def main():
with v2_2_image.FromTarball(args.tarball) as base:
config = base.config_file()

if args.manifest:
with open(args.manifest, 'r') as reader:
manifest = reader.read()

if len(args.digest or []) != len(args.layer or []):
logging.fatal('--digest and --layer must have matching lengths.')
sys.exit(1)
Expand All @@ -145,7 +156,8 @@ def main():
with v2_2_image.FromDisk(
config,
list(zip(args.digest or [], args.layer or [])),
legacy_base=args.tarball) as v2_2_img:
legacy_base=args.tarball,
foreign_layers_manifest=manifest) as v2_2_img:
# Resolve the appropriate credential to use based on the standard Docker
# client logic.
try:
Expand Down

0 comments on commit 2f1cfd3

Please sign in to comment.