Skip to content

Commit

Permalink
fix: 修复推送镜像时, Manifest 中的 size 大小异常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
shabbywu committed Dec 29, 2021
1 parent e32e507 commit a1d531d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion moby_distribution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from moby_distribution.spec.image_json import ImageJSON
from moby_distribution.spec.manifest import ManifestSchema1, ManifestSchema2, OCIManifestSchema1

__version__ = "0.3.1"
__version__ = "0.3.2"
__ALL__ = [
"DockerRegistryV2Client",
"Blob",
Expand Down
6 changes: 3 additions & 3 deletions moby_distribution/registry/resources/blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def stat(self, digest: Optional[str] = None) -> Descriptor:
return Descriptor(
# Content-Type: application/octet-stream
mediaType=headers["Content-Type"],
# The Content-Length in headers is the `Descriptor Message` size, but not the `Blob` itself.
size=headers["Content-Length"],
digest=headers.get("Docker-Content-Digest", digest),
urls=[headers.get("Location", url)],
Expand All @@ -70,11 +71,10 @@ def upload(self) -> Descriptor:
blob = BlobWriter(uuid, location, client=self.client)
with self.accessor.open(mode="rb") as fh:
signer = HashSignWrapper(fh=blob)
shutil.copyfileobj(fsrc=fh, fdst=signer, length=1024 * 1024 * 4)
shutil.copyfileobj(fsrc=fh, fdst=signer, length=1024 * 1024 * 64)

digest = signer.digest()
blob.commit(digest)

self.digest = digest
return self.stat()

Expand Down Expand Up @@ -175,7 +175,7 @@ def write(self, buffer: Union[bytes, bytearray]) -> int:

start_s, end_s = resp.headers["range"].split("-", 1)
start, end = int(start_s), int(end_s)
size = end - start + 1
size = end - start + 1 - self._offset

self.uuid = resp.headers["docker-upload-uuid"]
self.location = resp.headers["location"]
Expand Down
16 changes: 12 additions & 4 deletions moby_distribution/registry/resources/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from moby_distribution.registry.resources.blobs import Blob, HashSignWrapper
from moby_distribution.registry.resources.manifests import ManifestRef
from moby_distribution.registry.utils import generate_temp_dir
from moby_distribution.spec.image_json import ImageJSON
from moby_distribution.spec.image_json import History, ImageJSON
from moby_distribution.spec.manifest import (
DockerManifestConfigDescriptor,
DockerManifestLayerDescriptor,
Expand Down Expand Up @@ -57,6 +57,7 @@ def __init__(
self._dirty = False
# diff id is the digest of uncompressed tarball
self._append_diff_ids: List[str] = []
self._append_historys: List[History] = []

@classmethod
def from_image(
Expand Down Expand Up @@ -137,10 +138,13 @@ def push_v2(self) -> ManifestSchema2:

# Step 3.: upload the manifest
manifest = ManifestSchema2(config=config_descriptor, layers=layer_descriptors)
ManifestRef(repo=self.repo, reference=self.reference, client=self.client).put(manifest)
ref = ManifestRef(repo=self.repo, reference=self.reference, client=self.client)
ref.put(manifest)
if self._dirty:
return ref.get(media_type=ManifestSchema2.content_type())
return manifest

def add_layer(self, layer: LayerRef) -> DockerManifestLayerDescriptor:
def add_layer(self, layer: LayerRef, history: Optional[History] = None) -> DockerManifestLayerDescriptor:
"""Add a layer to this image.
Step:
Expand Down Expand Up @@ -198,7 +202,9 @@ def add_layer(self, layer: LayerRef) -> DockerManifestLayerDescriptor:

self._dirty = True
self._append_diff_ids.append(uncompressed_tarball_signer.digest())
self._append_historys.append(history or History(comment="add by moby-distribution"))
self.layers.append(layer)

return DockerManifestLayerDescriptor(
digest=gzipped_signer.digest(),
size=size,
Expand All @@ -210,6 +216,7 @@ def image_json(self) -> ImageJSON:
if not self._dirty:
return base
base.rootfs.diff_ids.extend(self._append_diff_ids)
base.history.extend(self._append_historys)
return base

@property
Expand Down Expand Up @@ -253,8 +260,9 @@ def _upload_layer(self, layer: LayerRef) -> DockerManifestLayerDescriptor:
descriptor = blob.upload()
else:
descriptor = Blob(repo=self.repo, client=self.client).stat(layer.digest)

return DockerManifestLayerDescriptor(
size=descriptor.size,
size=layer.size,
digest=descriptor.digest,
urls=descriptor.urls,
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "moby-distribution"
version = "0.3.1"
version = "0.3.2"
description = "Yet another moby(docker) distribution implement by python."
authors = ["shabbywu <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit a1d531d

Please sign in to comment.