-
Notifications
You must be signed in to change notification settings - Fork 56
Fix pull-through distribution base paths #2496
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
Draft
gerrod3
wants to merge
1
commit into
pulp:main
Choose a base branch
from
gerrod3:cx/fix-pull-through-base-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 hidden or 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 @@ | ||
| Prevent container distributions from overlapping other distribution base paths. Pull-through distributions **NEED** to be fixed before upgrading pulpcore; run `pulpcore-manager container-repair-pull-through-distributions` to repair existing distributions. | ||
This file contains hidden or 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 hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
pulp_container/app/management/commands/container-repair-pull-through-distributions.py
This file contains hidden or 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,80 @@ | ||
| from gettext import gettext as _ | ||
| from uuid import uuid4 | ||
|
|
||
| from django.conf import settings | ||
| from django.core.management import BaseCommand | ||
| from django.db import transaction | ||
|
|
||
| from pulpcore.plugin.cache import SyncContentCache | ||
|
|
||
| from pulp_container.app.models import ContainerPullThroughDistribution | ||
| from pulp_container.app.pull_through import ( | ||
| PULL_THROUGH_DISTRIBUTION_MARKER, | ||
| find_container_distribution_overlaps, | ||
| ) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """Repair pull-through distributions whose registry path is stored as their base path.""" | ||
|
|
||
| help = _("Repair pull-through distribution base paths and report remaining overlaps.") | ||
|
|
||
| def handle(self, *args, **options): | ||
| """Move eligible pull-through base paths and report unresolved overlaps.""" | ||
| repaired = 0 | ||
| old_base_paths = [] | ||
| distributions = ContainerPullThroughDistribution.objects.exclude( | ||
| pulp_labels__contains=PULL_THROUGH_DISTRIBUTION_MARKER | ||
| ).select_related("pulp_domain") | ||
|
|
||
| for distribution in distributions.iterator(): | ||
| if distribution.name != distribution.base_path: | ||
| continue | ||
|
|
||
| old_base_paths.append(distribution.base_path) | ||
| distribution.base_path = str(uuid4()) | ||
| distribution.pulp_labels = { | ||
| **(distribution.pulp_labels or {}), | ||
| **PULL_THROUGH_DISTRIBUTION_MARKER, | ||
| } | ||
| with transaction.atomic(): | ||
| distribution.save(update_fields=["base_path", "pulp_labels"]) | ||
| repaired += 1 | ||
|
|
||
| self.stdout.write(self.style.SUCCESS(f"Repaired {repaired} pull-through distributions.")) | ||
|
|
||
| unresolved = ContainerPullThroughDistribution.objects.exclude( | ||
| pulp_labels__contains=PULL_THROUGH_DISTRIBUTION_MARKER | ||
| ).select_related("pulp_domain") | ||
| if unresolved: | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| "The following pull-through distributions require manual repair:" | ||
| ) | ||
| ) | ||
| for distribution in unresolved: | ||
| self.stdout.write(self._format_distribution(distribution)) | ||
|
|
||
| overlaps = find_container_distribution_overlaps() | ||
| if overlaps: | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| "The following container distributions have overlapping base paths:" | ||
| ) | ||
| ) | ||
| for distribution, other in overlaps: | ||
| self.stdout.write( | ||
| f" {self._format_distribution(distribution).strip()} overlaps " | ||
| f"{self._format_distribution(other).strip()}" | ||
| ) | ||
|
|
||
| if settings.CACHE_ENABLED and old_base_paths: | ||
| SyncContentCache().delete(base_key=old_base_paths) | ||
| self.stdout.write(self.style.SUCCESS("Flushed repaired distribution cache entries.")) | ||
|
|
||
| @staticmethod | ||
| def _format_distribution(distribution): | ||
| return ( | ||
| f" domain={distribution.pulp_domain.name!r} pk={distribution.pk} " | ||
| f"name={distribution.name!r} base_path={distribution.base_path!r}" | ||
| ) |
This file contains hidden or 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,77 @@ | ||
| from django.db.models import F, Q, TextField, Value | ||
| from django.db.models.functions import Concat | ||
|
|
||
| from pulpcore.plugin.models import Distribution | ||
|
|
||
| from pulp_container.app.models import ContainerDistribution, ContainerPullThroughDistribution | ||
| from pulp_container.constants import ( | ||
| PULL_THROUGH_DISTRIBUTION_LABEL, | ||
| PULL_THROUGH_DISTRIBUTION_LABEL_VALUE, | ||
| ) | ||
|
|
||
| PULL_THROUGH_DISTRIBUTION_MARKER = { | ||
| PULL_THROUGH_DISTRIBUTION_LABEL: PULL_THROUGH_DISTRIBUTION_LABEL_VALUE | ||
| } | ||
|
|
||
|
|
||
| def is_pull_through_distribution_marked(distribution): | ||
| """Return whether a pull-through distribution uses its name as its registry path.""" | ||
| return all( | ||
| (distribution.pulp_labels or {}).get(key) == value | ||
| for key, value in PULL_THROUGH_DISTRIBUTION_MARKER.items() | ||
| ) | ||
|
|
||
|
|
||
| def get_pull_through_distribution_path(distribution): | ||
| """Return the registry path represented by a pull-through distribution.""" | ||
| if is_pull_through_distribution_marked(distribution): | ||
| return distribution.name | ||
| return distribution.base_path | ||
|
|
||
|
|
||
| def _match_path(queryset, path, field): | ||
| """Return the longest segment-aware path match from a queryset.""" | ||
| prefix_field = f"{field}_prefix" | ||
| return ( | ||
| queryset.annotate( | ||
| requested_path=Value(path, output_field=TextField()), | ||
| **{prefix_field: Concat(F(field), Value("/"), output_field=TextField())}, | ||
| ) | ||
| .filter(Q(requested_path=F(field)) | Q(requested_path__startswith=F(prefix_field))) | ||
| .order_by(f"-{field}") | ||
| .first() | ||
| ) | ||
|
|
||
|
|
||
| def get_pull_through_distribution(path, domain): | ||
| """Find a pull-through distribution using the repaired scheme, then the legacy scheme.""" | ||
| distributions = ContainerPullThroughDistribution.objects.filter(pulp_domain=domain) | ||
| marked = distributions.filter(pulp_labels__contains=PULL_THROUGH_DISTRIBUTION_MARKER) | ||
| if distribution := _match_path(marked, path, "name"): | ||
| return distribution | ||
|
|
||
| legacy = distributions.exclude(pulp_labels__contains=PULL_THROUGH_DISTRIBUTION_MARKER) | ||
| return _match_path(legacy, path, "base_path") | ||
|
|
||
|
|
||
| def find_container_distribution_overlaps(): | ||
| """Return pairs where a container distribution overlaps any distribution.""" | ||
| container_ids = set(ContainerDistribution.objects.values_list("pk", flat=True)) | ||
| distributions = Distribution.objects.select_related("pulp_domain").order_by( | ||
| "pulp_domain_id", "base_path", "pk" | ||
| ) | ||
| by_domain_and_path = { | ||
| (distribution.pulp_domain_id, distribution.base_path): distribution | ||
| for distribution in distributions.iterator() | ||
| } | ||
| overlaps = [] | ||
|
|
||
| for distribution in by_domain_and_path.values(): | ||
| parts = distribution.base_path.split("/") | ||
| for index in range(1, len(parts)): | ||
| parent_path = "/".join(parts[:index]) | ||
| if parent := by_domain_and_path.get((distribution.pulp_domain_id, parent_path)): | ||
| if parent.pk in container_ids or distribution.pk in container_ids: | ||
| overlaps.append((parent, distribution)) | ||
|
|
||
| return overlaps |
This file contains hidden or 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.
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.
Given that the constraint is a trigger only looking for new base_paths being inserted, you might get away with a few 500 if you run the command after updating...
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.
Yeah, but this is meant to also be backported to older versions so I didn't want to include a forced migration.
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 was trying to say the actual situation is even better than the changelog states (and that is OK, so this is not a request to change.).
Even after upgrading, it's still better late than never to run the repair.