-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[codex] clean up Kubernetes snapshot registry images #1426
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
Open
GodBlf
wants to merge
9
commits into
opensandbox-group:main
Choose a base branch
from
GodBlf:fix/k8s-snapshot-registry-cleanup-1179
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.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1bdd4c3
fix(kubernetes): clean up snapshot registry images
GodBlf acf2fdf
fix(kubernetes): address snapshot cleanup review feedback
GodBlf 5ebb181
test(kubernetes): enable registry deletes in pause e2e
GodBlf 47782ff
Merge branch 'main' into fix/k8s-snapshot-registry-cleanup-1179
Pangjiping ca0354f
fix(kubernetes): fall back to registry digest on cleanup miss
GodBlf 179cae6
test(kubernetes): bound registry fallback assertions
GodBlf 85cb70a
fix(kubernetes): verify registry digest after cleanup
GodBlf b0d17af
test(kubernetes): complete registry manifest response fixture
GodBlf 4cd7bfc
test(kubernetes): bound pause e2e teardown
GodBlf 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
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
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
128 changes: 128 additions & 0 deletions
128
kubernetes/internal/controller/registry_image_deleter.go
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,128 @@ | ||
| // Copyright 2025 Alibaba Group Holding Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/authn" | ||
| "github.com/google/go-containerregistry/pkg/name" | ||
| "github.com/google/go-containerregistry/pkg/v1/remote" | ||
| "github.com/google/go-containerregistry/pkg/v1/remote/transport" | ||
| corev1 "k8s.io/api/core/v1" | ||
| ) | ||
|
|
||
| type registryImageDeleter interface { | ||
| Delete(ctx context.Context, imageReference string, registrySecret *corev1.Secret, insecure bool) error | ||
| } | ||
|
|
||
| type remoteRegistryImageDeleter struct{} | ||
|
|
||
| func (remoteRegistryImageDeleter) Delete( | ||
| ctx context.Context, | ||
| imageReference string, | ||
| registrySecret *corev1.Secret, | ||
| insecure bool, | ||
| ) error { | ||
| nameOptions := []name.Option{name.StrictValidation} | ||
| if insecure { | ||
| nameOptions = append(nameOptions, name.Insecure) | ||
| } | ||
|
|
||
| ref, err := name.ParseReference(imageReference, nameOptions...) | ||
| if err != nil { | ||
| return fmt.Errorf("parse image reference %q: %w", imageReference, err) | ||
| } | ||
|
|
||
| authenticator, err := registryAuthenticator(registrySecret, ref.Context().RegistryStr()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| remoteOptions := []remote.Option{remote.WithContext(ctx), remote.WithAuth(authenticator)} | ||
|
|
||
| // Registry implementations commonly require deletion by digest. Resolve | ||
| // legacy snapshots that do not have imageDigest recorded before deleting. | ||
| if _, ok := ref.(name.Digest); !ok { | ||
|
Pangjiping marked this conversation as resolved.
Outdated
|
||
| descriptor, headErr := remote.Head(ref, remoteOptions...) | ||
| if isRegistryNotFound(headErr) { | ||
| return nil | ||
| } | ||
| if headErr != nil { | ||
| return fmt.Errorf("resolve image digest for %q: %w", imageReference, headErr) | ||
| } | ||
| ref = ref.Context().Digest(descriptor.Digest.String()) | ||
| } | ||
|
|
||
| if err := remote.Delete(ref, remoteOptions...); err != nil && !isRegistryNotFound(err) { | ||
| return fmt.Errorf("delete image %q: %w", imageReference, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func registryAuthenticator(secret *corev1.Secret, registry string) (authn.Authenticator, error) { | ||
| if secret == nil { | ||
| return authn.Anonymous, nil | ||
| } | ||
|
|
||
| var auths map[string]authn.AuthConfig | ||
| switch { | ||
| case len(secret.Data[corev1.DockerConfigJsonKey]) > 0: | ||
|
Pangjiping marked this conversation as resolved.
|
||
| var config struct { | ||
| Auths map[string]authn.AuthConfig `json:"auths"` | ||
| } | ||
| if err := json.Unmarshal(secret.Data[corev1.DockerConfigJsonKey], &config); err != nil { | ||
| return nil, fmt.Errorf("parse registry secret %s/%s: %w", secret.Namespace, secret.Name, err) | ||
| } | ||
| auths = config.Auths | ||
| case len(secret.Data[corev1.DockerConfigKey]) > 0: | ||
| if err := json.Unmarshal(secret.Data[corev1.DockerConfigKey], &auths); err != nil { | ||
| return nil, fmt.Errorf("parse registry secret %s/%s: %w", secret.Namespace, secret.Name, err) | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("registry secret %s/%s has neither %s nor %s", secret.Namespace, secret.Name, corev1.DockerConfigJsonKey, corev1.DockerConfigKey) | ||
| } | ||
|
|
||
| for server, config := range auths { | ||
| if normalizeRegistry(server) == normalizeRegistry(registry) { | ||
| return authn.FromConfig(config), nil | ||
| } | ||
| } | ||
| return nil, fmt.Errorf("registry secret %s/%s has no credentials for %s", secret.Namespace, secret.Name, registry) | ||
| } | ||
|
|
||
| func normalizeRegistry(registry string) string { | ||
| registry = strings.TrimPrefix(registry, "https://") | ||
| registry = strings.TrimPrefix(registry, "http://") | ||
| registry = strings.TrimSuffix(registry, "/") | ||
| registry = strings.TrimSuffix(registry, "/v1") | ||
| registry = strings.TrimSuffix(registry, "/v2") | ||
| if registry == "index.docker.io" { | ||
| return name.DefaultRegistry | ||
| } | ||
| return registry | ||
| } | ||
|
|
||
| func isRegistryNotFound(err error) bool { | ||
| if err == nil { | ||
| return false | ||
| } | ||
| var transportErr *transport.Error | ||
| return errors.As(err, &transportErr) && transportErr.StatusCode == http.StatusNotFound | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.