-
Notifications
You must be signed in to change notification settings - Fork 39
AGENT-1193: Add --mirror-path flag to support pre-mirrored images #634
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
rwsu
wants to merge
1
commit into
openshift:master
Choose a base branch
from
rwsu:AGENT-1193-v2
base: master
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.
+258
−19
Open
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,6 +153,12 @@ pullSecret: pull-secret | |
| # [Optional] | ||
| # useBinary: %t | ||
|
|
||
| # Path to pre-mirrored images from oc-mirror workspace. | ||
| # When provided, skips image mirroring and uses the pre-mirrored registry data. | ||
| # The path should point to an oc-mirror workspace directory containing a 'data' subdirectory. | ||
| # [Optional] | ||
| # mirrorPath: /path/to/mirror/workspace | ||
|
|
||
| # Enable all default CatalogSources (on openshift-marketplace namespace). | ||
| # Should be disabled for disconnected environments. | ||
| # Default: false | ||
|
|
@@ -378,7 +384,7 @@ func (a *ApplianceConfig) GetRelease() (string, string, error) { | |
| return "", "", nil | ||
| } | ||
| releaseDigest = strings.Trim(releaseDigest, "'") | ||
| releaseImage = fmt.Sprintf("%s@%s", strings.Split(releaseImage, ":")[0], releaseDigest) | ||
| releaseImage = fmt.Sprintf("%s@%s", releaseImage, releaseDigest) | ||
|
Contributor
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. is this change needed? |
||
| } | ||
| logrus.Debugf("Release image: %s", releaseImage) | ||
| } | ||
|
|
@@ -430,6 +436,11 @@ func (a *ApplianceConfig) validateConfig(f asset.FileFetcher) field.ErrorList { | |
| } | ||
| } | ||
|
|
||
| // Validate mirrorPath | ||
| if err := a.validateMirrorPath(); err != nil { | ||
| allErrs = append(allErrs, err...) | ||
| } | ||
|
|
||
| return allErrs | ||
| } | ||
|
|
||
|
|
@@ -553,6 +564,41 @@ func (a *ApplianceConfig) validatePinnedImageSet() error { | |
| return nil | ||
| } | ||
|
|
||
| func (a *ApplianceConfig) validateMirrorPath() field.ErrorList { | ||
| allErrs := field.ErrorList{} | ||
|
|
||
| if a.Config.MirrorPath != nil { | ||
| mirrorPath := swag.StringValue(a.Config.MirrorPath) | ||
| if mirrorPath != "" { | ||
| // Validate mirror path exists and is a directory | ||
| info, err := os.Stat(mirrorPath) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| allErrs = append(allErrs, field.Invalid(field.NewPath("mirrorPath"), | ||
| mirrorPath, "mirror path does not exist")) | ||
| } else { | ||
| allErrs = append(allErrs, field.Invalid(field.NewPath("mirrorPath"), | ||
| mirrorPath, fmt.Sprintf("failed to access mirror path: %v", err))) | ||
| } | ||
| } else if !info.IsDir() { | ||
| allErrs = append(allErrs, field.Invalid(field.NewPath("mirrorPath"), | ||
| mirrorPath, "mirror path must be a directory")) | ||
| } else { | ||
| // Validate data subdirectory exists | ||
| dataDir := filepath.Join(mirrorPath, "data") | ||
| if _, err := os.Stat(dataDir); err != nil { | ||
| allErrs = append(allErrs, field.Invalid(field.NewPath("mirrorPath"), | ||
| mirrorPath, "mirror path must contain a 'data' subdirectory (expected oc-mirror workspace structure)")) | ||
| } | ||
| } | ||
|
|
||
| logrus.Infof("Using pre-mirrored images from: %s", mirrorPath) | ||
| } | ||
| } | ||
|
|
||
| return allErrs | ||
| } | ||
|
|
||
| func (a *ApplianceConfig) storePullSecret() error { | ||
| // Get home dir (~) | ||
| homeDir, err := os.UserHomeDir() | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,9 +99,54 @@ func (r *release) GetImageFromRelease(imageName string) (string, error) { | |
| return "", err | ||
| } | ||
|
|
||
| // Fix incomplete image references from local registries | ||
| image, err = r.fixImageReference(image, swag.StringValue(r.ApplianceConfig.Config.OcpRelease.URL)) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return image, nil | ||
| } | ||
|
|
||
| // fixImageReference repairs incomplete image references returned by oc adm release info | ||
| // when querying local registries with custom ports. The oc command may return references | ||
| // like "registry.example.com@sha256:..." which are missing the port and repository path. | ||
| // This function reconstructs the full reference from the release URL. | ||
| func (r *release) fixImageReference(imageRef, releaseURL string) (string, error) { | ||
| // Check if this looks like an incomplete reference (has @ but no / after the hostname) | ||
| // Example: "virthost.ostest.test.metalkube.org@sha256:abc123" | ||
| if strings.Contains(imageRef, "@") && !strings.Contains(strings.Split(imageRef, "@")[0], "/") { | ||
| logrus.Debugf("Detected incomplete image reference: %s", imageRef) | ||
|
|
||
| // Extract digest from the incomplete reference | ||
| parts := strings.SplitN(imageRef, "@", 2) | ||
| if len(parts) != 2 { | ||
| return imageRef, nil // Return as-is if we can't parse it | ||
| } | ||
| digest := parts[1] | ||
|
|
||
| // Extract registry/port/repo from release URL | ||
| // Example: "virthost.ostest.test.metalkube.org:5000/openshift/release-images:tag" | ||
| // We want: "virthost.ostest.test.metalkube.org:5000/openshift/release-images" | ||
| releaseRef := releaseURL | ||
|
|
||
| // Remove tag or digest from release URL | ||
| if idx := strings.LastIndex(releaseRef, ":"); idx > strings.LastIndex(releaseRef, "/") { | ||
| releaseRef = releaseRef[:idx] | ||
| } | ||
| if idx := strings.Index(releaseRef, "@"); idx != -1 { | ||
| releaseRef = releaseRef[:idx] | ||
| } | ||
|
|
||
| // Reconstruct full image reference | ||
| fixedRef := fmt.Sprintf("%s@%s", releaseRef, digest) | ||
| logrus.Debugf("Fixed image reference: %s -> %s", imageRef, fixedRef) | ||
| return fixedRef, nil | ||
| } | ||
|
|
||
| return imageRef, nil | ||
| } | ||
|
|
||
| func (r *release) extractFileFromImage(image, file, outputDir string) (string, error) { | ||
| cmd := fmt.Sprintf(templateImageExtract, file, outputDir, image) | ||
| logrus.Debugf("extracting %s to %s, %s", file, outputDir, cmd) | ||
|
|
@@ -138,35 +183,70 @@ func (r *release) execute(command string) (string, error) { | |
| } | ||
|
|
||
| func (r *release) mirrorImages(imageSetFile, blockedImages, additionalImages, operators string) error { | ||
| if err := templates.RenderTemplateFile( | ||
| imageSetFile, | ||
| templates.GetImageSetTemplateData(r.ApplianceConfig, blockedImages, additionalImages, operators), | ||
| r.EnvConfig.TempDir); err != nil { | ||
| var tempDir string | ||
|
|
||
| // If a mirror path is provided in appliance-config, use it directly instead of running oc-mirror | ||
| var mirrorPath string | ||
| if r.ApplianceConfig.Config.MirrorPath != nil { | ||
| mirrorPath = *r.ApplianceConfig.Config.MirrorPath | ||
| } | ||
|
|
||
| if mirrorPath != "" { | ||
| logrus.Infof("Using pre-mirrored images from: %s", mirrorPath) | ||
| tempDir = mirrorPath | ||
| } else { | ||
| // Normal mirroring flow - run oc-mirror | ||
| if err := templates.RenderTemplateFile( | ||
| imageSetFile, | ||
| templates.GetImageSetTemplateData(r.ApplianceConfig, blockedImages, additionalImages, operators), | ||
| r.EnvConfig.TempDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| imageSetFilePath, err := filepath.Abs(templates.GetFilePathByTemplate(imageSetFile, r.EnvConfig.TempDir)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tempDir = filepath.Join(r.EnvConfig.TempDir, "oc-mirror") | ||
| registryPort := swag.IntValue(r.ApplianceConfig.Config.ImageRegistry.Port) | ||
| cmd := fmt.Sprintf(ocMirror, imageSetFilePath, registryPort, tempDir) | ||
|
|
||
| logrus.Debugf("Fetching image from OCP release (%s)", cmd) | ||
| result, err := r.execute(cmd) | ||
| logrus.Debugf("mirroring result: %s", result) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Copy generated yaml files to cache dir (works for both mirror path and oc-mirror output) | ||
| if err := r.copyOutputYamls(tempDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| imageSetFilePath, err := filepath.Abs(templates.GetFilePathByTemplate(imageSetFile, r.EnvConfig.TempDir)) | ||
| if err != nil { | ||
| // Copy mapping file (works for both mirror path and oc-mirror output) | ||
| if err := r.copyMappingFile(tempDir); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tempDir := filepath.Join(r.EnvConfig.TempDir, "oc-mirror") | ||
| registryPort := swag.IntValue(r.ApplianceConfig.Config.ImageRegistry.Port) | ||
| cmd := fmt.Sprintf(ocMirror, imageSetFilePath, registryPort, tempDir) | ||
| return nil | ||
| } | ||
|
|
||
| logrus.Debugf("Fetching image from OCP release (%s)", cmd) | ||
| result, err := r.execute(cmd) | ||
| logrus.Debugf("mirroring result: %s", result) | ||
| func (r *release) copyMappingFile(ocMirrorDir string) error { | ||
| mappingFiles, err := filepath.Glob(filepath.Join(ocMirrorDir, fmt.Sprintf("results-*/%s", consts.OcMirrorMappingFileName))) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Copy generated yaml files to cache dir | ||
| if err = r.copyOutputYamls(tempDir); err != nil { | ||
| return err | ||
| // The slice returned from Glob will have a single filename when running the application, but it will be empty when running the unit-tests since they don't create the files "oc mirror" generates | ||
| for _, mappingFile := range mappingFiles { | ||
| if err := fileutil.CopyFile(mappingFile, filepath.Join(r.EnvConfig.CacheDir, consts.OcMirrorMappingFileName)); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return err | ||
| return nil | ||
| } | ||
|
|
||
| func (r *release) copyOutputYamls(ocMirrorDir string) error { | ||
|
|
@@ -186,6 +266,14 @@ func (r *release) copyOutputYamls(ocMirrorDir string) error { | |
| internalRegistryURI := fmt.Sprintf("%s:%d", registry.RegistryDomain, registry.RegistryPort) | ||
| newYaml := strings.ReplaceAll(string(yamlBytes), buildRegistryURI, internalRegistryURI) | ||
|
|
||
| // Add IDMS entry for local registry mirror if using a custom release URL | ||
| if filepath.Base(yamlPath) == "idms-oc-mirror.yaml" { | ||
|
Contributor
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. isn't it generated by oc-mirror? |
||
| newYaml, err = r.addLocalRegistryIDMS(newYaml, internalRegistryURI) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Write edited yamls to cache | ||
| if err = r.OSInterface.MkdirAll(filepath.Join(r.EnvConfig.CacheDir, consts.OcMirrorResourcesDir), os.ModePerm); err != nil { | ||
| return err | ||
|
|
@@ -198,6 +286,55 @@ func (r *release) copyOutputYamls(ocMirrorDir string) error { | |
| return nil | ||
| } | ||
|
|
||
| // addLocalRegistryIDMS adds an IDMS entry for the local registry mirror when using | ||
| // a custom release URL (not upstream quay.io). This ensures that pulls from the | ||
| // registry mirror are redirected to the appliance's internal registry. | ||
| func (r *release) addLocalRegistryIDMS(yamlContent, internalRegistryURI string) (string, error) { | ||
| releaseURL := swag.StringValue(r.ApplianceConfig.Config.OcpRelease.URL) | ||
|
|
||
| // Check if using a custom registry (not upstream quay.io) | ||
| if !strings.Contains(releaseURL, "quay.io") && !strings.Contains(releaseURL, "registry.ci.openshift.org") { | ||
| // Extract registry host:port from release URL | ||
| localRegistry := releaseURL | ||
| // Remove digest if present | ||
| if idx := strings.Index(localRegistry, "@"); idx != -1 { | ||
| localRegistry = localRegistry[:idx] | ||
| } | ||
| // Remove tag if present (after last colon that comes after last slash) | ||
| if lastSlash := strings.LastIndex(localRegistry, "/"); lastSlash != -1 { | ||
| if lastColon := strings.LastIndex(localRegistry[lastSlash:], ":"); lastColon != -1 { | ||
| localRegistry = localRegistry[:lastSlash+lastColon] | ||
| } | ||
| } | ||
|
|
||
| // Extract just the registry host:port (without repository path) | ||
| registryHost := localRegistry | ||
| if idx := strings.Index(localRegistry, "/"); idx != -1 { | ||
| registryHost = localRegistry[:idx] | ||
| } | ||
|
|
||
| logrus.Infof("Adding IDMS entry for registry mirror: %s -> %s", registryHost, internalRegistryURI) | ||
|
|
||
| // Append IDMS entry for registry mirror | ||
| // This maps all pulls from the registry mirror to the appliance's internal registry | ||
| additionalIDMS := fmt.Sprintf(`--- | ||
| apiVersion: config.openshift.io/v1 | ||
| kind: ImageDigestMirrorSet | ||
| metadata: | ||
| name: local-registry-mirror | ||
| spec: | ||
| imageDigestMirrors: | ||
| - mirrors: | ||
| - %s | ||
| source: %s | ||
| `, internalRegistryURI, registryHost) | ||
|
|
||
| return yamlContent + "\n" + additionalIDMS, nil | ||
| } | ||
|
|
||
| return yamlContent, nil | ||
| } | ||
|
|
||
| func (r *release) generateImagesList(images *[]types.Image) string { | ||
| if images == nil { | ||
| return "" | ||
|
|
||
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
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.
probably worth adding also to the user-guide
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.
Done.