Repository navigation
TRT-1286: Extension binaries should be able to provide and consume mirrored images #31946
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
openshift-trt-agent
wants to merge
4
commits into
openshift:main
Choose a base branch
from
openshift-trt:fix/TRT-1286
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d11c04
pass mirrored image mappings to extension binaries during test execution
7326d55
address review feedback on mirrored image mappings
b68b86e
handle cleanup errors in mirrored image file operations
178ffb5
return error when mirrored images file write fails
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,150 @@ | ||
| package extensions | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/openshift-eng/openshift-tests-extension/pkg/extension" | ||
| "github.com/openshift/library-go/pkg/image/reference" | ||
| "github.com/sirupsen/logrus" | ||
| k8simage "k8s.io/kubernetes/test/utils/image" | ||
| ) | ||
|
|
||
| const MirroredImagesEnvVar = "EXTENSION_MIRRORED_IMAGES" | ||
|
|
||
| // ComputeMirroredImages builds a mapping from each extension image's original | ||
| // pull spec to its mirrored pull spec at fromRepository. Extensions report | ||
| // their images through the info command; each Image carries the upstream | ||
| // registry/name/version and an optional Mapped hint for the mirrored tag. | ||
| func ComputeMirroredImages(extensionsInfo []*Extension, fromRepository string) (map[string]string, error) { | ||
| if fromRepository == "" { | ||
| return nil, nil | ||
| } | ||
|
|
||
| ref, err := reference.Parse(fromRepository) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid from-repository %q: %v", fromRepository, err) | ||
| } | ||
| if !strings.Contains(ref.Exact(), "/") { | ||
| return nil, fmt.Errorf("invalid from-repository %q: expected a registry and repository, such as registry.example.com/repo", fromRepository) | ||
| } | ||
|
|
||
| mapping := make(map[string]string) | ||
|
|
||
| for _, ext := range extensionsInfo { | ||
| if ext == nil || ext.Extension == nil { | ||
| continue | ||
| } | ||
| for _, img := range ext.Images { | ||
| original, mapped := mirroredPullSpecs(img, ref) | ||
| if original == mapped { | ||
| continue | ||
| } | ||
| mapping[original] = mapped | ||
| } | ||
| } | ||
|
|
||
| if len(mapping) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| logrus.Infof("Computed mirrored image mappings for %d images", len(mapping)) | ||
| return mapping, nil | ||
| } | ||
|
|
||
| // mirroredPullSpecs returns the original and mirrored pull specs for an image. | ||
| // The logic mirrors imageSetsForImage in the images command. | ||
| func mirroredPullSpecs(img extension.Image, ref reference.DockerImageReference) (original, mirrored string) { | ||
| original = fmt.Sprintf("%s/%s:%s", img.Registry, img.Name, img.Version) | ||
|
|
||
| if img.Mapped == nil { | ||
| id := k8simage.ImageID(img.Index) | ||
| config := k8simage.Config{} | ||
| config.SetName(img.Name) | ||
| config.SetVersion(img.Version) | ||
| config.SetRegistry(img.Registry) | ||
| originalSet := map[k8simage.ImageID]k8simage.Config{id: config} | ||
| mappedSet := k8simage.GetMappedImageConfigs(originalSet, ref.Exact()) | ||
| mappedConfig := mappedSet[id] | ||
| mirrored = mappedConfig.GetE2EImage() | ||
| } else { | ||
| mirrored = fmt.Sprintf("%s/%s:%s", ref.Registry, ref.RepositoryName(), img.Mapped.Version) | ||
| } | ||
|
|
||
| return original, mirrored | ||
| } | ||
|
|
||
| // WriteMirroredImagesFile writes the mapping to a temporary JSON file and | ||
| // returns the path. The caller is responsible for removing the file. | ||
| func WriteMirroredImagesFile(mapping map[string]string) (string, error) { | ||
| if len(mapping) == 0 { | ||
| return "", nil | ||
| } | ||
|
|
||
| data, err := json.Marshal(mapping) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to marshal mirrored images: %v", err) | ||
| } | ||
|
|
||
| f, err := os.CreateTemp("", "mirrored-images-*.json") | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to create mirrored images file: %v", err) | ||
| } | ||
|
|
||
| if _, err := f.Write(data); err != nil { | ||
| if closeErr := f.Close(); closeErr != nil { | ||
| logrus.Warnf("Failed to close temp file during cleanup: %v", closeErr) | ||
| } | ||
| if removeErr := os.Remove(f.Name()); removeErr != nil { | ||
| logrus.Warnf("Failed to remove temp file during cleanup: %v", removeErr) | ||
| } | ||
| return "", fmt.Errorf("failed to write mirrored images file: %v", err) | ||
| } | ||
|
|
||
| path := f.Name() | ||
| if err := f.Close(); err != nil { | ||
| if removeErr := os.Remove(path); removeErr != nil { | ||
| logrus.Warnf("Failed to remove temp file during cleanup: %v", removeErr) | ||
| } | ||
| return "", fmt.Errorf("failed to close mirrored images file: %v", err) | ||
| } | ||
|
|
||
| return path, nil | ||
| } | ||
|
|
||
| // ReadMirroredImagesFile reads a mirrored images mapping from a JSON file. | ||
| func ReadMirroredImagesFile(path string) (map[string]string, error) { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read mirrored images file: %v", err) | ||
| } | ||
|
|
||
| mapping := make(map[string]string) | ||
| if err := json.Unmarshal(data, &mapping); err != nil { | ||
| return nil, fmt.Errorf("failed to parse mirrored images file: %v", err) | ||
| } | ||
|
|
||
| return mapping, nil | ||
| } | ||
|
|
||
| // MirroredLocationFor returns the mirrored pull spec for an image if one | ||
| // exists in the mapping, or the original pull spec unchanged. | ||
| func MirroredLocationFor(mapping map[string]string, original string) string { | ||
| if mapped, ok := mapping[original]; ok { | ||
| return mapped | ||
| } | ||
| return original | ||
| } | ||
|
|
||
| // LoadMirroredImagesFromEnv reads the mirrored images mapping from the file | ||
| // referenced by the EXTENSION_MIRRORED_IMAGES environment variable. Returns | ||
| // nil without error when the variable is unset. | ||
| func LoadMirroredImagesFromEnv() (map[string]string, error) { | ||
| path := os.Getenv(MirroredImagesEnvVar) | ||
| if path == "" { | ||
| return nil, nil | ||
| } | ||
| return ReadMirroredImagesFile(path) | ||
| } | ||
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.