Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sherine-k committed Aug 9, 2023
1 parent 3abfde4 commit 7511209
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 56 deletions.
38 changes: 19 additions & 19 deletions pkg/cli/mirror/catalog_images.go
Expand Up @@ -32,12 +32,10 @@ import (
)

const (
opmCachePrefix = "/tmp/cache"
opmBinarySuffix = "opm"
opmBinaryDir = "usr/bin/registry"
cacheLocationPlaceholder = "cacheLocation.txt"
cacheFolderUID = 1001
cacheFolderGID = 0
opmCachePrefix = "/tmp/cache"
opmBinarySuffix = "opm"
cacheFolderUID = 1001
cacheFolderGID = 0
)

type NoCacheArgsErrorType struct{}
Expand Down Expand Up @@ -221,7 +219,7 @@ func (o *MirrorOptions) processCatalogRefs(ctx context.Context, catalogsByImage
layersToAdd := []v1.Layer{}
layersToDelete := []v1.Layer{}
withCacheRegeneration := true
_, err := os.Stat(filepath.Join(artifactDir, cacheLocationPlaceholder))
_, err := os.Stat(filepath.Join(artifactDir, config.OPMCacheLocationPlaceholder))
if errors.Is(err, os.ErrNotExist) {
withCacheRegeneration = false
} else if err != nil {
Expand All @@ -244,7 +242,7 @@ func (o *MirrorOptions) processCatalogRefs(ctx context.Context, catalogsByImage

if withCacheRegeneration {
// read the location of the cache
dat, err := os.ReadFile(filepath.Join(artifactDir, cacheLocationPlaceholder))
dat, err := os.ReadFile(filepath.Join(artifactDir, config.OPMCacheLocationPlaceholder))
if err != nil {
return fmt.Errorf("unable to determine location of cache for image %s. Cache generation failed: %v", ctlgRef, err)
}
Expand Down Expand Up @@ -301,7 +299,11 @@ func (o *MirrorOptions) processCatalogRefs(ctx context.Context, catalogsByImage
// Although it was prefered to keep the entrypoint and command as it was
// we couldnt guarantie that the cache-dir was /tmp/cache, and therefore
// we had to specify the command for the newly built catalog
if withCacheRegeneration {
cfg.Config.Cmd = []string{"serve", "/configs", "--cache-dir=/tmp/cache"}
} else { // this means that no cache was found in the original catalog (old catalog with opm < 1.25)
cfg.Config.Cmd = []string{"serve", "/configs"}
}
}
if err := imgBuilder.Run(ctx, refExact, layoutPath, update, layers...); err != nil {
return fmt.Errorf("error building catalog layers: %v", err)
Expand Down Expand Up @@ -355,10 +357,10 @@ func findOpmCmd(artifactDir string) (string, error) {

// extractOPMAndCache is usually called after rendering catalog's declarative config.
// it uses crane modules to pull the catalog image, select the manifest that corresponds to the
// current platform. It then extracts from that image any files that are suffixed `*opm` for later
// current platform architecture. It then extracts from that image any files that are suffixed `*opm` for later
// use upon rebuilding the catalog: This is because the opm binary can be called `opm` but also
// `darwin-amd64-opm` etc.
func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, outDir string, insecure bool) error {
func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, ctlgSrcDir string, insecure bool) error {
var img v1.Image
var err error
refExact := srcRef.Ref.Exact()
Expand All @@ -369,7 +371,7 @@ func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, o
return fmt.Errorf("unable to pull image from %s: %v", refExact, err)
}
} else {
img, err = getPtfImageFromOCIIndex(v1alpha2.TrimProtocol(srcRef.OCIFBCPath), runtime.GOARCH, runtime.GOOS)
img, err = getPlatformImageFromOCIIndex(v1alpha2.TrimProtocol(srcRef.OCIFBCPath), runtime.GOARCH, runtime.GOOS)
if err != nil {
return err
}
Expand All @@ -387,7 +389,7 @@ func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, o
}
}

cacheLocationFileName := filepath.Join(outDir, cacheLocationPlaceholder)
cacheLocationFileName := filepath.Join(ctlgSrcDir, config.OPMCacheLocationPlaceholder)

baseDir := filepath.Dir(cacheLocationFileName)
err = os.MkdirAll(baseDir, 0755)
Expand All @@ -396,17 +398,15 @@ func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, o
}

cfl, err := os.Create(cacheLocationFileName)
if err == nil {
defer cfl.Close()
} else {
if err != nil {
return err
}
defer cfl.Close()

_, err = cfl.Write([]byte(cachePath))
if err != nil {
return err
}
cfl.Close()
// cachePath exists, opm binary will be needed to regenerate it
tr := tar.NewReader(mutate.Extract(img))
opmBinaryExtracted := ""
Expand All @@ -429,7 +429,7 @@ func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, o
return err
}

targetFileName := filepath.Join(outDir, config.OpmBinDir, header.Name)
targetFileName := filepath.Join(ctlgSrcDir, config.OpmBinDir, header.Name)
bytes := buf.Bytes()

baseDir := filepath.Dir(targetFileName)
Expand Down Expand Up @@ -461,9 +461,9 @@ func extractOPMAndCache(ctx context.Context, srcRef image.TypedImageReference, o
return nil
}

// getPtfImageFromOCIIndex takes an oci local image located in `fbcPath` and finds the image that
// getPlatformImageFromOCIIndex takes an oci local image located in `fbcPath` and finds the image that
// corresponds to the current platform and OS within the manifestList or imageIndex
func getPtfImageFromOCIIndex(fbcPath string, architecture string, os string) (v1.Image, error) {
func getPlatformImageFromOCIIndex(fbcPath string, architecture string, os string) (v1.Image, error) {
var img v1.Image

// obtain the path to where the OCI image reference resides
Expand Down
88 changes: 51 additions & 37 deletions pkg/config/metadata.go
Expand Up @@ -7,43 +7,51 @@ import (
/*
Constants defined here refer to this workspace layout:
/tmp/cwd/oc-mirror-workspace
├── results-1675904745
└── src
├── catalogs
│ └── icr.io ─┐
│ └── cpopen ├─ catalog path (one per catalog)
│ └── ibm-zcon-zosconnect-catalog │
│ └── sha256:6f02ecef46020bcd21bdd24a01f435023d5fc3943972ef0d9769d5276e178e76 ─┘
│ ├── include-config.gob <—— this represents v1alpha2.IncludeConfig for associated with index/index.json
│ ├── index <—— this represents a decl config for "single architecture image" image
│ │ │ (i.e. no multi arch use case)
│ │ └── index.json <—— Declarative Config
│ └── layout <—— OCI layout is capable of holding “manifest list” and “single architecture" images
│ ├── blobs
│ │ └── sha256
│ │ ├── 01c6d5dcde3e9f2de758d794a77974600fe5e0b7a8c2ce2833eede2e8b25e7e5
│ │ ├── 1cd0595314a53d179ddaf68761c9f40c4d9d1bcd3f692d1c005938dac2993db6
│ │ ├── 1ff4ea896d6b958aa060e04eb090f70c563ad0650e6b362c1a1d67582acb3b8e
│ │ ├── 25d123725cf91c20b497ca9dae8e0a6e8dedd8fe64f83757f3b41f6ac447eac0
│ │ ├── 2d55550cefe39606cc933a2ed1d242b3fd9a72d85e92a2c6b52b9623a6f4fe6a
│ │ ├── 34e12aa195bcd8366fbab95a242e8214ae959259bd0a1119c28d124f5799f502
│ │ ├── 49a32e2e950732d5a638d5486968dcc3096f940a94811cfec99bd5a4f9e1ad49
│ │ ├── 561bc8bee264b124ba5673e73ad36589abbecf0b15fb845ed9aab4e640989fbc
│ │ ├── 6672e188b9c3f7274b7ebf4498b34f951bc20ea86a8d72367eab363f1722d2ed
│ │ ├── 7062267a99d0149e4129843a9e3257b882920fb8554ec2068a264b37539768bc
│ │ ├── ae475359a3fb8fe5e3dff0626f0c788b94340416eb5c453339abc884ac86b671
│ │ ├── b2dd6105dc025aa5c5e8b75e0b2d8a390951369801d049fc0de2586917a42772
│ │ ├── c03c8f94bb495320bbe862bc69349dbdf9f2a29b83d5b344b3930890aaf89d7d
│ │ ├── dc1b9846d7994450e74b9cde2e621f8c1d98cdf1debd591db291785dd3fc6446
│ │ └── f89d6e2463fc5fff8bba9e568ec28a6030076dbc412bd52dff6dbf2b5897a59d
│ ├── index.json
│ └── oci-layout
├── charts
├── publish
│ └── .metadata.json
├── release-signatures
└── v2
/tmp/cwd/oc-mirror-workspace
├── results-1675904745
└── src
├── catalogs
│ └── icr.io ─┐
│ └── cpopen ├─ catalog path (one per catalog)
│ └── ibm-zcon-zosconnect-catalog │
│ └── sha256:6f02ecef46020bcd21bdd24a01f435023d5fc3943972ef0d9769d5276e178e76 ─┘
│ ├── cacheLocation.txt <—— this file stores the location of the opm cache within the operators catalog container
│ ├── include-config.gob <—— this represents v1alpha2.IncludeConfig for associated with index/index.json
│ ├── index <—— this represents a decl config for "single architecture image" image
│ │ │ (i.e. no multi arch use case)
│ │ └── index.json <—— Declarative Config
│ ├── layout <—— OCI layout is capable of holding “manifest list” and “single architecture" images
│ │ ├── blobs
│ │ │ └── sha256
│ │ │ ├── 01c6d5dcde3e9f2de758d794a77974600fe5e0b7a8c2ce2833eede2e8b25e7e5
│ │ │ ├── 1cd0595314a53d179ddaf68761c9f40c4d9d1bcd3f692d1c005938dac2993db6
│ │ │ ├── 1ff4ea896d6b958aa060e04eb090f70c563ad0650e6b362c1a1d67582acb3b8e
│ │ │ ├── 25d123725cf91c20b497ca9dae8e0a6e8dedd8fe64f83757f3b41f6ac447eac0
│ │ │ ├── 2d55550cefe39606cc933a2ed1d242b3fd9a72d85e92a2c6b52b9623a6f4fe6a
│ │ │ ├── 34e12aa195bcd8366fbab95a242e8214ae959259bd0a1119c28d124f5799f502
│ │ │ ├── 49a32e2e950732d5a638d5486968dcc3096f940a94811cfec99bd5a4f9e1ad49
│ │ │ ├── 561bc8bee264b124ba5673e73ad36589abbecf0b15fb845ed9aab4e640989fbc
│ │ │ ├── 6672e188b9c3f7274b7ebf4498b34f951bc20ea86a8d72367eab363f1722d2ed
│ │ │ ├── 7062267a99d0149e4129843a9e3257b882920fb8554ec2068a264b37539768bc
│ │ │ ├── ae475359a3fb8fe5e3dff0626f0c788b94340416eb5c453339abc884ac86b671
│ │ │ ├── b2dd6105dc025aa5c5e8b75e0b2d8a390951369801d049fc0de2586917a42772
│ │ │ ├── c03c8f94bb495320bbe862bc69349dbdf9f2a29b83d5b344b3930890aaf89d7d
│ │ │ ├── dc1b9846d7994450e74b9cde2e621f8c1d98cdf1debd591db291785dd3fc6446
│ │ │ └── f89d6e2463fc5fff8bba9e568ec28a6030076dbc412bd52dff6dbf2b5897a59d
│ │ ├── index.json
│ │ └── oci-layout
│ └── tmp
│ └── cache <—— this folder contains the cache regenerated according to the declarative config
│ ├── cache <—— this folder is generated by `opm serve $DECL_CONFIG --cache-dir $CACHE_DIR --cache-only`
│ │ ├── namespace-configuration-operator_alpha_namespace-configuration-operator.v1.2.4.json
│ │ └── packages.json
│ └── digest <—— this file ensures the integrity of the cache when compared with the decl config
├── charts
├── publish
│ └── .metadata.json
├── release-signatures
└── v2
*/
const (
// DefaultWorkspaceName defines the default value for the workspace if not provided by the user
Expand Down Expand Up @@ -97,6 +105,12 @@ const (
// catalog include config data for incorporation
// into the metadata is located.
IncludeConfigFile = "include-config.gob"
// OPMCacheLocationPlaceholder is the file where
// the path to the catalog cache is stored during plan
// so that it is later used to rebuild the cache layer.
// The contents are taken from the catalog
// image command - like `serve /configs --cache-dir /tmp/cache`
OPMCacheLocationPlaceholder = "cacheLocation.txt"
)

// MetadataBasePath is the local path relative to the oc-mirror workspace
Expand Down

0 comments on commit 7511209

Please sign in to comment.