-
Notifications
You must be signed in to change notification settings - Fork 102
CLID-513: operator catalog pinned by digest in ISC #1333
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
adolfo-ab
wants to merge
1
commit into
openshift:main
Choose a base branch
from
adolfo-ab:pin-catalog
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.
+862
−32
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 |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "sigs.k8s.io/yaml" | ||
|
|
||
| "github.com/openshift/oc-mirror/v2/internal/pkg/api/v2alpha1" | ||
| "github.com/openshift/oc-mirror/v2/internal/pkg/image" | ||
| clog "github.com/openshift/oc-mirror/v2/internal/pkg/log" | ||
| ) | ||
|
|
||
| // copyISC creates a shallow copy of ImageSetConfiguration with a deep copy of the Operators slice. | ||
| // This ensures we don't mutate the original configuration when pinning catalog digests. | ||
| func copyISC(cfg v2alpha1.ImageSetConfiguration) v2alpha1.ImageSetConfiguration { | ||
| copied := cfg | ||
|
|
||
| // Deep copy operators slice (only part we modify) | ||
| if len(cfg.Mirror.Operators) > 0 { | ||
| copied.Mirror.Operators = make([]v2alpha1.Operator, len(cfg.Mirror.Operators)) | ||
| copy(copied.Mirror.Operators, cfg.Mirror.Operators) | ||
| } | ||
|
|
||
| return copied | ||
| } | ||
|
|
||
| // pinCatalogDigests creates a copy of ImageSetConfiguration with catalog references | ||
| // pinned to SHA256 digests instead of tags. | ||
| // | ||
| // For each operator catalog in cfg.Mirror.Operators: | ||
| // - Parses the catalog reference | ||
| // - Looks up the digest in catalogToFBCMap | ||
| // - Replaces the catalog field with digest format: {registry}/{path}@sha256:{digest} | ||
| // | ||
| // Returns a new ImageSetConfiguration with pinned catalogs and any errors encountered. | ||
| func pinCatalogDigests( | ||
| cfg v2alpha1.ImageSetConfiguration, | ||
| catalogToFBCMap map[string]v2alpha1.CatalogFilterResult, | ||
| log clog.PluggableLoggerInterface, | ||
| ) (v2alpha1.ImageSetConfiguration, error) { | ||
| // Create copy to avoid mutating original | ||
| pinnedCfg := copyISC(cfg) | ||
|
|
||
| // Iterate through operator catalogs by index to modify in place | ||
| for i := range pinnedCfg.Mirror.Operators { | ||
adolfo-ab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| op := &pinnedCfg.Mirror.Operators[i] | ||
|
|
||
| // Parse catalog reference | ||
| imgSpec, err := image.ParseRef(op.Catalog) | ||
| if err != nil { | ||
| return v2alpha1.ImageSetConfiguration{}, | ||
| fmt.Errorf("failed to parse catalog %s: %w", op.Catalog, err) | ||
| } | ||
|
|
||
| // Skip if already digest-pinned | ||
| if imgSpec.IsImageByDigest() { | ||
| log.Debug("Catalog %s is already digest-pinned, skipping", op.Catalog) | ||
| continue | ||
| } | ||
|
|
||
| // Look up digest in map | ||
| filterResult, ok := catalogToFBCMap[imgSpec.ReferenceWithTransport] | ||
| if !ok { | ||
| // Log warning but continue (non-fatal) | ||
| log.Warn("Catalog %s not found in CatalogToFBCMap, skipping pin", op.Catalog) | ||
| continue | ||
| } | ||
|
|
||
| // Check for empty digest | ||
| if filterResult.Digest == "" { | ||
| log.Warn("Empty digest for catalog %s, skipping pin", op.Catalog) | ||
| continue | ||
| } | ||
|
|
||
| // Build pinned reference: {registry}/{path}@sha256:{digest} | ||
| pinnedRef := fmt.Sprintf("%s@sha256:%s", imgSpec.Name, filterResult.Digest) | ||
|
|
||
| // Add transport prefix if non-docker (docker:// is default and can be omitted) | ||
| if imgSpec.Transport != "" && imgSpec.Transport != "docker://" { | ||
| pinnedRef = imgSpec.Transport + pinnedRef | ||
| } | ||
|
|
||
| log.Debug("Pinning catalog %s to %s", op.Catalog, pinnedRef) | ||
| op.Catalog = pinnedRef | ||
| } | ||
|
|
||
| return pinnedCfg, nil | ||
| } | ||
|
|
||
| // writeConfigToFile writes a config object (ISC or DISC) to a YAML file with timestamp naming. | ||
| // Returns the absolute path to the written file. | ||
| func writeConfigToFile(obj interface{}, configType, workingDir string) (string, error) { | ||
| filename := fmt.Sprintf("%s_pinned_%s.yaml", configType, time.Now().UTC().Format(time.RFC3339)) | ||
| filePath := filepath.Join(workingDir, filename) | ||
|
|
||
| yamlData, err := yaml.Marshal(obj) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to marshal %s to YAML: %w", strings.ToUpper(configType), err) | ||
| } | ||
|
|
||
| if err := os.WriteFile(filePath, yamlData, 0o600); err != nil { | ||
|
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. When writing the file should we allow others to read? Probably it is not mandatory that the user who wrote the file is the same who is going to read it in another phase. |
||
| return "", fmt.Errorf("failed to write pinned %s to %s: %w", strings.ToUpper(configType), filePath, err) | ||
| } | ||
|
|
||
| return filePath, nil | ||
| } | ||
|
|
||
| // writePinnedISC writes an ImageSetConfiguration to a YAML file with timestamp naming. | ||
| // | ||
| // The file is written to: {workingDir}/isc_pinned_{timestamp}.yaml | ||
| // e.g., isc_pinned_2025-12-31T11:37:17Z.yaml | ||
| // | ||
| // Returns the absolute path to the written file. | ||
| func writePinnedISC( | ||
| cfg v2alpha1.ImageSetConfiguration, | ||
| workingDir string, | ||
| ) (string, error) { | ||
| cfg.SetGroupVersionKind(v2alpha1.GroupVersion.WithKind(v2alpha1.ImageSetConfigurationKind)) | ||
| return writeConfigToFile(cfg, "isc", workingDir) | ||
| } | ||
|
|
||
| // createDISCFromISC creates a DeleteImageSetConfiguration from an already-pinned ImageSetConfiguration. | ||
| // It simply converts the Mirror section to a Delete section. | ||
| // | ||
| // The file is written to: {workingDir}/disc_pinned_{timestamp}.yaml | ||
| // e.g., disc_pinned_2025-12-31T11:37:17Z.yaml | ||
| // | ||
| // Returns the absolute path to the written pinned DISC file. | ||
| func createDISCFromISC( | ||
| pinnedISC v2alpha1.ImageSetConfiguration, | ||
| workingDir string, | ||
| ) (string, error) { | ||
| disc := v2alpha1.DeleteImageSetConfiguration{ | ||
| DeleteImageSetConfigurationSpec: v2alpha1.DeleteImageSetConfigurationSpec{ | ||
| Delete: v2alpha1.Delete{ | ||
| Platform: pinnedISC.Mirror.Platform, | ||
| Operators: pinnedISC.Mirror.Operators, | ||
| AdditionalImages: pinnedISC.Mirror.AdditionalImages, | ||
| Helm: pinnedISC.Mirror.Helm, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Set TypeMeta for DeleteImageSetConfiguration | ||
| disc.SetGroupVersionKind(v2alpha1.GroupVersion.WithKind(v2alpha1.DeleteImageSetConfigurationKind)) | ||
|
|
||
| return writeConfigToFile(disc, "disc", workingDir) | ||
| } | ||
|
|
||
| // PinAndWriteISCAndDSC pins catalogs and writes both ISC and DISC files. | ||
| // Returns paths to both written files (ISC path, DISC path). | ||
| func PinAndWriteISCAndDSC( | ||
| cfg v2alpha1.ImageSetConfiguration, | ||
| catalogToFBCMap map[string]v2alpha1.CatalogFilterResult, | ||
| workingDir string, | ||
| log clog.PluggableLoggerInterface, | ||
| ) (string, string, error) { | ||
| // Pin catalog digests | ||
| pinnedISC, err := pinCatalogDigests(cfg, catalogToFBCMap, log) | ||
| if err != nil { | ||
| return "", "", fmt.Errorf("failed to pin catalog digests: %w", err) | ||
| } | ||
|
|
||
| // Write pinned ISC | ||
| iscPath, err := writePinnedISC(pinnedISC, workingDir) | ||
| if err != nil { | ||
| return "", "", fmt.Errorf("failed to write pinned ISC: %w", err) | ||
| } | ||
|
|
||
| // Write pinned DISC | ||
| discPath, err := createDISCFromISC(pinnedISC, workingDir) | ||
| if err != nil { | ||
| return iscPath, "", fmt.Errorf("failed to create pinned DISC: %w", err) | ||
| } | ||
|
|
||
| return iscPath, discPath, nil | ||
| } | ||
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.