Skip to content

Commit

Permalink
draft/wip
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hyagi committed Apr 23, 2024
1 parent bfd4944 commit 587c154
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 180 deletions.
2 changes: 2 additions & 0 deletions pulp_container/app/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

DEFAULT_EXPIRES_TTL = settings.CACHE_SETTINGS["EXPIRES_TTL"]


class RegistryCache:
"""A class that overrides the default key specs."""

Expand Down Expand Up @@ -116,6 +117,7 @@ def make_entry(self, key, base_key, handler, args, kwargs, expires=DEFAULT_EXPIR
self.set(key, json.dumps(entry), expires, base_key=base_key)
return response


def find_base_path_cached(request, cached):
"""
Returns the base-path to use for the base-key in the cache
Expand Down
7 changes: 2 additions & 5 deletions pulp_container/app/modelresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ class BlobResource(BaseContentResource):
Resource for import/export of blob entities
"""

data = fields.Field(
column_name="data",
attribute="data",
widget=BinaryWidget()
)
data = fields.Field(column_name="data", attribute="data", widget=BinaryWidget())

def set_up_queryset(self):
"""
:return: Blobs specific to a specified repo-version.
Expand Down
4 changes: 2 additions & 2 deletions pulp_container/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class Blob(Content):
TYPE = "blob"

digest = models.TextField(db_index=True)
#data = models.TextField(blank=True)
data = models.BinaryField(blank=True)

class Meta:
Expand Down Expand Up @@ -143,9 +142,10 @@ def init_labels(self):
if self.config_blob:
try:
# if config-blob is stored in the database
config = json.loads(self.config_blob.config_blob.instance.data)['config']
config = json.loads(self.config_blob.config_blob.instance.data)["config"]
if "Labels" in config.keys():
self.labels = config["Labels"] or {}
# TODO: DO NOT USE BARE 'EXCEPT'
except:
# if not, we'll retrieve the config from file
config_artifact = self.config_blob._artifacts.get()
Expand Down
23 changes: 0 additions & 23 deletions pulp_container/app/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
log = logging.getLogger(__name__)


#try:
# import debugpy
# debugpy.listen(('0.0.0.0',5679))
# debugpy.wait_for_client()
#except:
# pass

class Registry(Handler):
"""
A set of handlers for the Container Registry v2 API.
Expand Down Expand Up @@ -284,8 +277,6 @@ async def get_by_digest(self, request):
}
except ObjectDoesNotExist:
distribution = await distribution.acast()
# "/pulp/container/{path:.+}/{content:(blobs|manifests)}/sha256:{digest:.+}"
#content_type = request.match_info["content"]
if distribution.remote_id and distribution.pull_through_distribution_id:
pull_downloader = await PullThroughDownloader.create(
distribution, repository_version, path, digest
Expand All @@ -310,16 +301,6 @@ async def get_by_digest(self, request):
return await self._stream_content_artifact(request, web.StreamResponse(), ca)
else:
raise RuntimeError("Only blobs or manifests are supported by the parser.")
#elif content_type == "blobs":
# # for configblobs no distribution and artifacts are created
# media_type = MEDIA_TYPE.CONFIG_BLOB_OCI
# headers = {
# "Content-Type": media_type,
# "Docker-Content-Digest": digest,
# "Docker-Distribution-API-Version": "registry/2.0",
# }
# blob = await Blob.objects.aget(digest=digest)
# return web.Response(text=blob.data,headers=headers)
else:
raise PathNotResolved(path)
else:
Expand Down Expand Up @@ -480,10 +461,6 @@ async def save_blob(self, digest, manifest):
with suppress(IntegrityError):
await bm_rel.asave()

# if this is a ConfigBlob there is no Artifact, so we return earlier
#if blob.data != '':
# return blob

ca = ContentArtifact(
content=blob,
artifact=None,
Expand Down
45 changes: 15 additions & 30 deletions pulp_container/app/registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import hashlib
import re

from aiohttp import web
from aiohttp.client_exceptions import ClientResponseError
from itertools import chain
from urllib.parse import urljoin, urlparse, urlunparse, parse_qs, urlencode
Expand Down Expand Up @@ -106,13 +105,6 @@
]


#try:
# import debugpy
# debugpy.listen(('0.0.0.0',5678))
# debugpy.wait_for_client()
#except:
# pass

class ContentRenderer(BaseRenderer):
"""
Rendered class for rendering Manifest and Blob responses.
Expand Down Expand Up @@ -618,7 +610,7 @@ def get(self, request):
tag.name, tag.tagged_manifest, req_oss, req_architectures, manifests
)
for manifest, tagged in manifests.items():
if manifest.config_blob.data != '':
if manifest.config_blob.data != "":
raw_data = manifest.config_blob.data
else:
with storage.open(manifest.config_blob._artifacts.get().file.name) as file:
Expand Down Expand Up @@ -801,29 +793,22 @@ def create_blob(self, artifact, digest):

blob_size = artifact.file.size
if blob_size > CONFIG_BLOB_SIZE:
self._store_layer_blob(artifact,blob,digest)
self._create_blob_content_artifact(artifact, blob, digest)
else:
with storage.open(artifact.file.name) as artifact_file:
raw_data = artifact_file.read()
blob.data = raw_data
blob.save()
#try:
# blob.data = raw_data.decode("utf-8")
# #blob.data = raw_data
# blob.save()
#except UnicodeDecodeError:
# # if we tried to decode a layer blob it will raise the UnicodeDecodeError, so
# # we will need to handle it as a blob this time
# self._store_layer_blob(artifact,blob,digest)


return blob

def _store_layer_blob(self,artifact,blob,digest):
def _create_blob_content_artifact(self, artifact, blob, digest):
"""
Blobs with size > CONFIG_BLOB_SIZE will be considered layer blobs and
will be stored as artifacts.
"""
try:
blob_artifact = ContentArtifact(
artifact=artifact, content=blob, relative_path=digest
)
blob_artifact = ContentArtifact(artifact=artifact, content=blob, relative_path=digest)
blob_artifact.save()
except IntegrityError:
# re-upload artifact in case it was previously removed.
Expand Down Expand Up @@ -1019,7 +1004,7 @@ def handle_safe_method(self, request, path, pk):
blob = repository.pending_blobs.get(digest=pk)
blob.touch()
except models.Blob.DoesNotExist:
#try:
# try:
# # configblobs do not have a repository_version nor artifact
# blob = models.Blob.objects.get(digest=pk)
# media_type = MEDIA_TYPE.CONFIG_BLOB_OCI
Expand All @@ -1031,17 +1016,17 @@ def handle_safe_method(self, request, path, pk):
# #return web.Response(text=blob.data,headers=headers)
# #return UploadResponse(upload=blob, path=path, request=request)
# return Response(data=blob.data)
#except models.Blob.DoesNotExist:
# except models.Blob.DoesNotExist:
raise BlobNotFound(digest=pk)

if blob.data:
media_type = MEDIA_TYPE.CONFIG_BLOB_OCI
headers = {
"Content-Type": media_type,
"Docker-Content-Digest": pk,
"Docker-Distribution-API-Version": "registry/2.0",
}
return Response(data=blob.data,headers=headers)
"Content-Type": media_type,
"Docker-Content-Digest": pk,
"Docker-Distribution-API-Version": "registry/2.0",
}
return Response(data=blob.data, headers=headers)

return redirects.issue_blob_redirect(blob)

Expand Down
19 changes: 1 addition & 18 deletions pulp_container/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re

from django.core.validators import URLValidator
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers

from pulpcore.app.serializers import fields
Expand Down Expand Up @@ -122,31 +121,15 @@ class Meta:
model = models.Manifest


class ConfigBlobSerializer(NoArtifactContentSerializer):
"""
Serializer for Blobs without artifacts (ConfigBlob).
"""

digest = serializers.CharField(help_text="sha256 of the Blob file")

class Meta:
fields = NoArtifactContentSerializer.Meta.fields + ("digest",)
model = models.Blob

class BlobSerializer(SingleArtifactContentSerializer):
"""
Serializer for Blobs.
"""

digest = serializers.CharField(help_text="sha256 of the Blob file")
artifact = fields.SingleContentArtifactField(
help_text=_("Artifact file representing the physical content"),
required = False
help_text=_("Artifact file representing the physical content"), required=False
)

#@extend_schema_field(ConfigBlobSerializer)
#def get_artifact(self, object):
# return object

class Meta:
fields = SingleArtifactContentSerializer.Meta.fields + ("digest",)
Expand Down
4 changes: 2 additions & 2 deletions pulp_container/app/tasks/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_or_create_blob(layer_json, manifest, path):
if layer_json["mediaType"] in [MEDIA_TYPE.CONFIG_BLOB_OCI, MEDIA_TYPE.CONFIG_BLOB]:
with open(layer_file_name, "r") as content_file:
raw_data = content_file.read()
blob.data = raw_data.encode('utf-8')
blob.data = raw_data.encode("utf-8")
blob.save()
else:
layer_artifact = Artifact.init_and_validate(layer_file_name)
Expand All @@ -48,7 +48,7 @@ def get_or_create_blob(layer_json, manifest, path):
artifact=layer_artifact, content=blob, relative_path=layer_json["digest"]
).save()
BlobManifest(manifest=manifest, manifest_blob=blob).save()

return blob


Expand Down
6 changes: 2 additions & 4 deletions pulp_container/app/tasks/sync_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ async def resolve_flush(self):
for manifest_dc in self.manifest_dcs:
config_blob_dc = manifest_dc.extra_data.get("config_blob_dc")
if config_blob_dc:
#manifest_dc.content.config_blob = await config_blob_dc.resolution()
manifest_dc.content.config_blob = await config_blob_dc.resolution()
await sync_to_async(manifest_dc.content.init_labels)()
manifest_dc.content.init_image_nature()
Expand Down Expand Up @@ -547,7 +546,7 @@ def create_blob(self, blob_data, deferred_download=True):

return blob_dc

async def create_config_blob(self,blob_data):
async def create_config_blob(self, blob_data):
digest = blob_data.get("digest")
relative_url = "/v2/{name}/blobs/{digest}".format(
name=self.remote.namespaced_upstream_name, digest=digest
Expand All @@ -558,12 +557,11 @@ async def create_config_blob(self,blob_data):
response = await downloader.run(extra_data={"headers": V2_ACCEPT_HEADERS})
with open(response.path, "rb") as content_file:
raw_data = content_file.read()
config_blob = Blob(digest=digest,data=raw_data)
config_blob = Blob(digest=digest, data=raw_data)
config_blob_dc = DeclarativeContent(content=config_blob)

return config_blob_dc


async def create_signatures(self, man_dc, signature_source):
"""
Create signature declarative contents.
Expand Down
Loading

0 comments on commit 587c154

Please sign in to comment.