Skip to content

Commit

Permalink
[draft/wip] image push
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hyagi committed Mar 21, 2024
1 parent 1241be5 commit 0978d7d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.10 on 2024-03-19 15:16
# Generated by Django 4.2.10 on 2024-03-21 19:41

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -7,6 +7,7 @@
class Migration(migrations.Migration):

dependencies = [
('core', '0117_task_unblocked_at'),
('container', '0038_add_manifest_metadata_fields'),
]

Expand Down Expand Up @@ -34,6 +35,16 @@ class Migration(migrations.Migration):
},
bases=('core.content',),
),
migrations.AddField(
model_name='containerpushrepository',
name='pending_config_blobs',
field=models.ManyToManyField(to='container.configblob'),
),
migrations.AddField(
model_name='containerrepository',
name='pending_config_blobs',
field=models.ManyToManyField(to='container.configblob'),
),
migrations.AlterField(
model_name='manifest',
name='config_blob',
Expand Down
2 changes: 2 additions & 0 deletions pulp_container/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ class ContainerRepository(
ManifestSigningService, on_delete=models.SET_NULL, null=True
)
pending_blobs = models.ManyToManyField(Blob)
pending_config_blobs = models.ManyToManyField(ConfigBlob)
pending_manifests = models.ManyToManyField(Manifest)

class Meta:
Expand Down Expand Up @@ -702,6 +703,7 @@ class ContainerPushRepository(Repository, AutoAddObjPermsMixin):
ManifestSigningService, on_delete=models.SET_NULL, null=True
)
pending_blobs = models.ManyToManyField(Blob)
pending_config_blobs = models.ManyToManyField(ConfigBlob)
pending_manifests = models.ManyToManyField(Manifest)

class Meta:
Expand Down
3 changes: 2 additions & 1 deletion pulp_container/app/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,9 @@ async def run_pipeline(self, saved_artifact):
async def init_pending_content(self, digest, manifest_data, media_type, artifact):
if config := manifest_data.get("config", None):
config_digest = config["digest"]

config_blob = await self.save_config_blob(config_digest)
await sync_to_async(self.repository.pending_blobs.add)(config_blob)
await sync_to_async(self.repository.pending_config_blobs.add)(config_blob)
else:
config_blob = None

Expand Down
69 changes: 53 additions & 16 deletions pulp_container/app/registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,6 @@ def create_single_chunk_artifact(self, chunk):
artifact.touch()
return artifact

### CHECK IF THIS WILL NEED TO BE MODIFIED
### CHECK IF THIS WILL NEED TO BE MODIFIED
### CHECK IF THIS WILL NEED TO BE MODIFIED
### CHECK IF THIS WILL NEED TO BE MODIFIED
def create_blob(self, artifact, digest):
with transaction.atomic():
try:
Expand Down Expand Up @@ -872,6 +868,38 @@ def partial_update(self, request, path, pk=None):

return UploadResponse(upload=upload, path=path, request=request, status=204)


def is_config_blob(self,blob):
required_fields = { "os", "rootfs", "architecture"}
if all(config_key in required_fields for config_key in blob):
return True
return False


def create_config_blob(self, manifest,raw_manifest, digest):
with transaction.atomic():
try:
blob = models.ConfigBlob(
data = raw_manifest,
digest = digest,
architecture=manifest.get("architecture"),
os=manifest.get("os"),
created=manifest.get("created"),
author=manifest.get("author", ""),
os_version=manifest.get("os_version", ""),
os_features=manifest.get("os_features", ""),
variant=manifest.get("variant", ""),
rootfs=manifest.get("rootfs", {}),
config=manifest.get("config", {}),
history=manifest.get("history", {}),
)
blob.save()
except IntegrityError:
blob = models.ConfigBlob.objects.get(digest=digest)
blob.touch()
return blob


def put(self, request, path, pk=None):
"""
Create a blob from uploaded chunks.
Expand Down Expand Up @@ -917,10 +945,18 @@ def put(self, request, path, pk=None):
artifact = Artifact.objects.get(sha256=artifact.sha256)
artifact.touch()

blob = self.create_blob(artifact, digest)
blob = None
try:
raw_manifest = artifact.file.read().decode("utf-8")
manifest = json.loads(raw_manifest)
blob = self.create_config_blob(manifest, raw_manifest, digest)
repository.pending_config_blobs.add(blob)
except UnicodeDecodeError:
blob = self.create_blob(artifact, digest)
repository.pending_blobs.add(blob)
upload.delete()

repository.pending_blobs.add(blob)

return BlobResponse(blob, path, 201, request)


Expand Down Expand Up @@ -983,18 +1019,18 @@ def handle_safe_method(self, request, path, pk):
blob = models.ConfigBlob.objects.get(digest=pk)
return redirects.redirect_to_content_app("config-blobs",pk)
except:
pass
pass ###### WIP PENDING FIX THIS
repository = repository.cast()
try:
blob = repository.pending_blobs.get(digest=pk)
blob.touch()
except models.Blob.DoesNotExist:
raise BlobNotFound(digest=pk)
#try:
# blob = models.ConfigBlob.objects.get(digest=pk,pk__in=repository_version.content)
#except:
# raise BlobNotFound(digest=pk)
# # pass
try:
blob = repository.pending_config_blobs.get(digest=pk)
blob.touch()
except:
raise BlobNotFound(digest=pk)
# pass

# try:
# config_blob = models.ConfigBlob.get(digest=pk,pk__in=repository_version.content)
Expand Down Expand Up @@ -1167,7 +1203,8 @@ def put(self, request, path, pk=None):
latest_version_content_pks = repository.latest_version().content.values_list("pk")
manifests_pks = repository.pending_manifests.values_list("pk")
blobs_pks = repository.pending_blobs.values_list("pk")
content_pks = latest_version_content_pks.union(manifests_pks).union(blobs_pks)
config_blobs_pks = repository.pending_config_blobs.values_list("pk")
content_pks = latest_version_content_pks.union(manifests_pks).union(blobs_pks).union(config_blobs_pks)

found_manifests = models.Manifest.objects.none()

Expand Down Expand Up @@ -1215,7 +1252,7 @@ def put(self, request, path, pk=None):
digest__in=found_manifests.values_list("blobs__digest"),
pk__in=content_pks,
)
found_config_blobs = models.Blob.objects.filter(
found_config_blobs = models.ConfigBlob.objects.filter(
digest__in=found_manifests.values_list("config_blob__digest"),
pk__in=content_pks,
)
Expand All @@ -1229,7 +1266,7 @@ def put(self, request, path, pk=None):
)

config_digest = config_layer.get("digest")
found_config_blobs = models.Blob.objects.filter(
found_config_blobs = models.ConfigBlob.objects.filter(
digest=config_digest, pk__in=content_pks
)
if not found_config_blobs.exists():
Expand Down

0 comments on commit 0978d7d

Please sign in to comment.