-
Notifications
You must be signed in to change notification settings - Fork 49
T keithyao/batch purge Adds ABAC support for purge command with batched token requests #577
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
keithy1012
wants to merge
11
commits into
Azure:main
Choose a base branch
from
keithy1012:t-keithyao/batch-purge
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
02e4cf1
Implemented Per-Repository Token Refresh method
69c5fbb
Added new testing
e666217
Implemented option 2 batch processing for purge
d6779aa
Merge branch 'main' into t-keithyao/batch-purge
keithy1012 6b6b106
Refactored code
7db0aea
Added token refresh check
eb5ea3a
Merge origin/main and fix conflicts
b857681
Made config for abacBatchSize
8ad2ac8
Check abac from token before constructing acrcliclient
f9f0762
Changed scope string
6aa0c97
env var
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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 |
|---|---|---|
|
|
@@ -7,8 +7,10 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "runtime" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
|
|
@@ -228,34 +230,75 @@ func purge(ctx context.Context, | |
| dryRun bool, | ||
| includeLocked bool) (deletedTagsCount int, deletedManifestsCount int, err error) { | ||
|
|
||
| // In order to print a summary of the deleted tags/manifests the counters get updated everytime a repo is purged. | ||
| for repoName, tagRegex := range tagFilters { | ||
| var singleDeletedTagsCount int | ||
| var manifestToTagsCountMap map[string]int | ||
|
|
||
| // Handle tag deletion based on mode | ||
| if untaggedOnly { | ||
| // Initialize empty map for untagged-only mode (no tag deletion) | ||
| manifestToTagsCountMap = make(map[string]int) | ||
| } else { | ||
| // Standard mode: delete matching tags first | ||
| singleDeletedTagsCount, manifestToTagsCountMap, err = purgeTags(ctx, acrClient, repoParallelism, loginURL, repoName, agoDuration, tagRegex, keep, filterTimeout, dryRun, includeLocked) | ||
| if err != nil { | ||
| return deletedTagsCount, deletedManifestsCount, fmt.Errorf("failed to purge tags: %w", err) | ||
| // Load ABAC batch size from environment variable | ||
| abacBatchSize := 10 // default | ||
| if envVal, exists := os.LookupEnv("ABAC_BATCH_SIZE"); exists { | ||
| if parsed, err := strconv.Atoi(envVal); err == nil && parsed > 0 { | ||
| abacBatchSize = parsed | ||
| } | ||
| } | ||
|
|
||
| // Collect all repository names into a slice for batching | ||
| repos := make([]string, 0, len(tagFilters)) | ||
| for repoName := range tagFilters { | ||
| repos = append(repos, repoName) | ||
| } | ||
|
|
||
| // Process repositories in batches of abacBatchSize. | ||
| // For ABAC-enabled registries, we refresh the token per batch. | ||
| // For non-ABAC registries, the batching loop is harmless (no token refresh needed). | ||
| for i := 0; i < len(repos); i += abacBatchSize { | ||
| end := i + abacBatchSize | ||
| if end > len(repos) { | ||
| end = len(repos) | ||
| } | ||
| batch := repos[i:end] | ||
|
|
||
| // For ABAC registries, request a token that covers all repositories in this batch | ||
| if acrClient.IsAbac() { | ||
| if err := acrClient.RefreshTokenForAbac(ctx, batch); err != nil { | ||
| return deletedTagsCount, deletedManifestsCount, fmt.Errorf("failed to authorize ABAC repositories batch: %w", err) | ||
|
Member
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. can we output the repository names in the batch? |
||
| } | ||
| } | ||
|
|
||
| singleDeletedManifestsCount := 0 | ||
| // If the untagged flag is set or untagged-only mode is enabled, delete manifests | ||
| if removeUntaggedManifests { | ||
| singleDeletedManifestsCount, err = purgeDanglingManifests(ctx, acrClient, repoParallelism, loginURL, repoName, agoDuration, keep, manifestToTagsCountMap, dryRun, includeLocked) | ||
| if err != nil { | ||
| return deletedTagsCount, deletedManifestsCount, fmt.Errorf("failed to purge manifests: %w", err) | ||
| // Process all repositories in this batch | ||
| for j, repoName := range batch { | ||
| // For ABAC registries, check if token expired and refresh for remaining repos in batch | ||
| if acrClient.IsAbac() && acrClient.IsTokenExpired() { | ||
| remainingRepos := batch[j:] | ||
| fmt.Printf("ABAC token expired, refreshing for remaining repositories: %v\n", remainingRepos) | ||
| if err := acrClient.RefreshTokenForAbac(ctx, remainingRepos); err != nil { | ||
| return deletedTagsCount, deletedManifestsCount, fmt.Errorf("failed to refresh ABAC token: %w", err) | ||
| } | ||
| } | ||
| tagRegex := tagFilters[repoName] | ||
| var singleDeletedTagsCount int | ||
| var manifestToTagsCountMap map[string]int | ||
|
|
||
| // Handle tag deletion based on mode | ||
| if untaggedOnly { | ||
| // Initialize empty map for untagged-only mode (no tag deletion) | ||
| manifestToTagsCountMap = make(map[string]int) | ||
| } else { | ||
| // Standard mode: delete matching tags first | ||
| singleDeletedTagsCount, manifestToTagsCountMap, err = purgeTags(ctx, acrClient, repoParallelism, loginURL, repoName, agoDuration, tagRegex, keep, filterTimeout, dryRun, includeLocked) | ||
| if err != nil { | ||
| return deletedTagsCount, deletedManifestsCount, fmt.Errorf("failed to purge tags: %w", err) | ||
| } | ||
| } | ||
|
|
||
| singleDeletedManifestsCount := 0 | ||
| // If the untagged flag is set or untagged-only mode is enabled, delete manifests | ||
| if removeUntaggedManifests { | ||
| singleDeletedManifestsCount, err = purgeDanglingManifests(ctx, acrClient, repoParallelism, loginURL, repoName, agoDuration, keep, manifestToTagsCountMap, dryRun, includeLocked) | ||
| if err != nil { | ||
| return deletedTagsCount, deletedManifestsCount, fmt.Errorf("failed to purge manifests: %w", err) | ||
| } | ||
| } | ||
| // After every repository is purged the counters are updated. | ||
| deletedTagsCount += singleDeletedTagsCount | ||
| deletedManifestsCount += singleDeletedManifestsCount | ||
| } | ||
| // After every repository is purged the counters are updated. | ||
| deletedTagsCount += singleDeletedTagsCount | ||
| deletedManifestsCount += singleDeletedManifestsCount | ||
| } | ||
|
|
||
| return deletedTagsCount, deletedManifestsCount, nil | ||
|
|
||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
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.
Uh oh!
There was an error while loading. Please reload this page.