-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add repository_version param as a building context #1687
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
The `build_image` endpoint has been refactored to accept `build_context` | ||
(i.e., a file repository version) instead of raw artifacts. The same applies to Containerfile." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
from gettext import gettext as _ | ||
import os | ||
import re | ||
|
||
from django.core.validators import URLValidator | ||
from rest_framework import serializers | ||
|
||
from pulpcore.plugin.models import ( | ||
Artifact, | ||
ContentRedirectContentGuard, | ||
Remote, | ||
Repository, | ||
|
@@ -30,9 +28,11 @@ | |
ValidateFieldsMixin, | ||
) | ||
|
||
from pulp_file.app.models import FileContent | ||
from pulp_container.app import models | ||
from pulp_container.constants import SIGNATURE_TYPE | ||
|
||
|
||
VALID_SIGNATURE_NAME_REGEX = r"^sha256:[0-9a-f]{64}@[0-9a-f]{32}$" | ||
VALID_TAG_REGEX = r"^[A-Za-z0-9][A-Za-z0-9._-]*$" | ||
VALID_BASE_PATH_REGEX_COMPILED = re.compile(r"^[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9])*$") | ||
|
@@ -758,13 +758,12 @@ class OCIBuildImageSerializer(ValidateFieldsMixin, serializers.Serializer): | |
A repository must be specified, to which the container image content will be added. | ||
""" | ||
|
||
containerfile_artifact = RelatedField( | ||
many=False, | ||
lookup_field="pk", | ||
view_name="artifacts-detail", | ||
queryset=Artifact.objects.all(), | ||
containerfile_name = serializers.CharField( | ||
required=False, | ||
allow_blank=True, | ||
help_text=_( | ||
"Artifact representing the Containerfile that should be used to run podman-build." | ||
"Name of the Containerfile, from build_context, that should be used to run " | ||
"podman-build." | ||
), | ||
) | ||
containerfile = serializers.FileField( | ||
|
@@ -774,65 +773,77 @@ class OCIBuildImageSerializer(ValidateFieldsMixin, serializers.Serializer): | |
tag = serializers.CharField( | ||
git-hyagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
required=False, default="latest", help_text="A tag name for the new image being built." | ||
) | ||
artifacts = serializers.JSONField( | ||
build_context = RepositoryVersionRelatedField( | ||
required=False, | ||
help_text="A JSON string where each key is an artifact href and the value is it's " | ||
"relative path (name) inside the /pulp_working_directory of the build container " | ||
"executing the Containerfile.", | ||
help_text=_("RepositoryVersion to be used as the build context for container images."), | ||
allow_null=True, | ||
lubosmj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
queryset=RepositoryVersion.objects.filter(repository__pulp_type="file.file"), | ||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
"""Initializer for OCIBuildImageSerializer.""" | ||
super().__init__(*args, **kwargs) | ||
self.fields["containerfile_artifact"].required = False | ||
|
||
def validate(self, data): | ||
"""Validates that all the fields make sense.""" | ||
data = super().validate(data) | ||
|
||
if "containerfile" in data: | ||
if "containerfile_artifact" in data: | ||
raise serializers.ValidationError( | ||
_("Only one of 'containerfile' and 'containerfile_artifact' may be specified.") | ||
) | ||
data["containerfile_artifact"] = Artifact.init_and_validate(data.pop("containerfile")) | ||
elif "containerfile_artifact" in data: | ||
data["containerfile_artifact"].touch() | ||
else: | ||
if bool(data.get("containerfile", None)) == bool(data.get("containerfile_name", None)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Poor mans xor... 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤣 |
||
raise serializers.ValidationError( | ||
_("Exactly one of 'containerfile' or 'containerfile_name' must be specified.") | ||
) | ||
|
||
if "containerfile_name" in data and "build_context" not in data: | ||
raise serializers.ValidationError( | ||
_("'containerfile' or 'containerfile_artifact' must " "be specified.") | ||
_("A 'build_context' must be specified when 'containerfile_name' is present.") | ||
) | ||
artifacts = {} | ||
if "artifacts" in data: | ||
for url, relative_path in data["artifacts"].items(): | ||
if os.path.isabs(relative_path): | ||
|
||
# TODO: this can be removed after https://github.com/pulp/pulpcore/issues/5786 | ||
if data.get("build_context", None): | ||
data["repository_version"] = data["build_context"] | ||
git-hyagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return data | ||
|
||
def deferred_files_validation(self, data): | ||
""" | ||
Defer the validation of on_demand_artifacts and the `Containerfile` to avoid rerunning | ||
unnecessary database queries when checking permissions (DRF Access Policy). | ||
""" | ||
if build_context := data.get("build_context", None): | ||
|
||
# check if the on_demand_artifacts exist | ||
for on_demand_artifact in build_context.on_demand_artifacts.iterator(): | ||
if not on_demand_artifact.content_artifact.artifact: | ||
raise serializers.ValidationError( | ||
_("Relative path cannot start with '/'. " "{0}").format(relative_path) | ||
_( | ||
"It is not possible to use File content synced with on-demand " | ||
"policy without pulling the data first." | ||
) | ||
) | ||
|
||
# check if the containerfile_name exists in the build_context (File Repository) | ||
if ( | ||
data.get("containerfile_name", None) | ||
and not FileContent.objects.filter( | ||
repositories__in=[build_context.repository.pk], | ||
relative_path=data["containerfile_name"], | ||
).exists() | ||
): | ||
raise serializers.ValidationError( | ||
_( | ||
'Could not find the Containerfile "' | ||
+ data["containerfile_name"] | ||
+ '" in the build_context provided' | ||
) | ||
artifactfield = RelatedField( | ||
view_name="artifacts-detail", | ||
queryset=Artifact.objects.all(), | ||
source="*", | ||
initial=url, | ||
) | ||
try: | ||
artifact = artifactfield.run_validation(data=url) | ||
artifact.touch() | ||
artifacts[str(artifact.pk)] = relative_path | ||
except serializers.ValidationError as e: | ||
# Append the URL of missing Artifact to the error message | ||
e.detail[0] = "%s %s" % (e.detail[0], url) | ||
raise e | ||
data["artifacts"] = artifacts | ||
|
||
data["build_context_pk"] = build_context.repository.pk | ||
|
||
return data | ||
|
||
class Meta: | ||
fields = ( | ||
"containerfile_artifact", | ||
"containerfile_name", | ||
"containerfile", | ||
"repository", | ||
"tag", | ||
"artifacts", | ||
"build_context", | ||
) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we intend to lift that limitation some day?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a good improvement.
We could “force” (or automate) the download by Pulp if the artifacts were not found, or maybe do not allow the usage of on-demand repositories for the build machinery.