Skip to content

Commit 8a386fc

Browse files
committedJul 7, 2020
fix(opm): refactor indexer
this refactors the indexer to make database extraction re-usable across indexer actions it also factors out a helper in the containerd registry to make future use of manifests simpler
1 parent af51ac0 commit 8a386fc

File tree

3 files changed

+47
-87
lines changed

3 files changed

+47
-87
lines changed
 

‎pkg/image/containerdregistry/registry.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"os"
1211

1312
"github.com/containerd/containerd/archive"
@@ -73,12 +72,7 @@ func (r *Registry) Unpack(ctx context.Context, ref image.Reference, dir string)
7372
// Set the default namespace if unset
7473
ctx = ensureNamespace(ctx)
7574

76-
img, err := r.Images().Get(ctx, ref.String())
77-
if err != nil {
78-
return err
79-
}
80-
81-
manifest, err := images.Manifest(ctx, r.Content(), img.Target, r.platform)
75+
manifest, err := r.getManifest(ctx, ref)
8276
if err != nil {
8377
return err
8478
}
@@ -101,12 +95,25 @@ func (r *Registry) Unpack(ctx context.Context, ref image.Reference, dir string)
10195
func (r *Registry) Labels(ctx context.Context, ref image.Reference) (map[string]string, error) {
10296
// Set the default namespace if unset
10397
ctx = ensureNamespace(ctx)
104-
tmpDir, err := ioutil.TempDir("./", "bundle_tmp")
98+
99+
manifest, err := r.getManifest(ctx, ref)
100+
if err != nil {
101+
return nil, err
102+
}
103+
imageConfig, err := r.getImage(ctx, *manifest)
105104
if err != nil {
106105
return nil, err
107106
}
108-
defer os.RemoveAll(tmpDir)
109107

108+
return imageConfig.Config.Labels, nil
109+
}
110+
111+
// Destroy cleans up the on-disk boltdb file and other cache files, unless preserve cache is true
112+
func (r *Registry) Destroy() (err error) {
113+
return r.destroy()
114+
}
115+
116+
func (r *Registry) getManifest(ctx context.Context, ref image.Reference) (*ocispec.Manifest, error) {
110117
img, err := r.Images().Get(ctx, ref.String())
111118
if err != nil {
112119
return nil, err
@@ -116,7 +123,10 @@ func (r *Registry) Labels(ctx context.Context, ref image.Reference) (map[string]
116123
if err != nil {
117124
return nil, err
118125
}
126+
return &manifest, nil
127+
}
119128

129+
func (r *Registry) getImage(ctx context.Context, manifest ocispec.Manifest) (*ocispec.Image, error) {
120130
ra, err := r.Content().ReaderAt(ctx, manifest.Config)
121131
if err != nil {
122132
return nil, err
@@ -138,14 +148,7 @@ func (r *Registry) Labels(ctx context.Context, ref image.Reference) (map[string]
138148
if err := json.Unmarshal(buf.Bytes(), &imageConfig); err != nil {
139149
return nil, err
140150
}
141-
142-
return imageConfig.Config.Labels, nil
143-
}
144-
145-
146-
// Destroy cleans up the on-disk boltdb file and other cache files, unless preserve cache is true
147-
func (r *Registry) Destroy() (err error) {
148-
return r.destroy()
151+
return &imageConfig, nil
149152
}
150153

151154
func (r *Registry) fetch(ctx context.Context, fetcher remotes.Fetcher, root ocispec.Descriptor) error {

‎pkg/lib/bundle/exporter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package bundle
22

33
import (
44
"context"
5-
"github.com/operator-framework/operator-registry/pkg/image"
6-
"github.com/operator-framework/operator-registry/pkg/image/execregistry"
75
"io/ioutil"
86
"os"
97
"path/filepath"
@@ -12,7 +10,9 @@ import (
1210
"github.com/sirupsen/logrus"
1311

1412
"github.com/operator-framework/operator-registry/pkg/containertools"
13+
"github.com/operator-framework/operator-registry/pkg/image"
1514
"github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
15+
"github.com/operator-framework/operator-registry/pkg/image/execregistry"
1616
)
1717

1818
// BundleExporter exports the manifests of a bundle image into a directory
@@ -22,7 +22,7 @@ type BundleExporter struct {
2222
containerTool containertools.ContainerTool
2323
}
2424

25-
func NewSQLExporterForBundle(image, directory string, containerTool containertools.ContainerTool) *BundleExporter {
25+
func NewExporterForBundle(image, directory string, containerTool containertools.ContainerTool) *BundleExporter {
2626
return &BundleExporter{
2727
image: image,
2828
directory: directory,

‎pkg/lib/indexer/indexer.go

+24-67
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,8 @@ func (i ImageIndexer) AddToIndex(request AddToIndexRequest) error {
6767
return err
6868
}
6969

70-
// set a temp directory for unpacking an image
71-
// this is in its own function context so that the deferred cleanup runs before we do a docker build
72-
// which prevents the full contents of the previous image from being in the build context
73-
var databasePath string
74-
if err := func() error {
75-
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
76-
if err != nil {
77-
78-
return err
79-
}
80-
defer os.RemoveAll(tmpDir)
81-
82-
databaseFile, err := i.getDatabaseFile(tmpDir, request.FromIndex)
83-
if err != nil {
84-
return err
85-
}
86-
// copy the index to the database folder in the build directory
87-
if databasePath, err = copyDatabaseTo(databaseFile, filepath.Join(buildDir, defaultDatabaseFolder)); err != nil {
88-
return err
89-
}
90-
return nil
91-
}(); err != nil {
70+
databasePath, err := i.extractDatabase(buildDir, request.FromIndex)
71+
if err != nil {
9272
return err
9373
}
9474

@@ -148,28 +128,8 @@ func (i ImageIndexer) DeleteFromIndex(request DeleteFromIndexRequest) error {
148128
return err
149129
}
150130

151-
// set a temp directory for unpacking an image
152-
// this is in its own function context so that the deferred cleanup runs before we do a docker build
153-
// which prevents the full contents of the previous image from being in the build context
154-
var databasePath string
155-
if err := func() error {
156-
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
157-
if err != nil {
158-
159-
return err
160-
}
161-
defer os.RemoveAll(tmpDir)
162-
163-
databaseFile, err := i.getDatabaseFile(tmpDir, request.FromIndex)
164-
if err != nil {
165-
return err
166-
}
167-
// copy the index to the database folder in the build directory
168-
if databasePath, err = copyDatabaseTo(databaseFile, filepath.Join(buildDir, defaultDatabaseFolder)); err != nil {
169-
return err
170-
}
171-
return nil
172-
}(); err != nil {
131+
databasePath, err := i.extractDatabase(buildDir, request.FromIndex)
132+
if err != nil {
173133
return err
174134
}
175135

@@ -224,28 +184,8 @@ func (i ImageIndexer) PruneFromIndex(request PruneFromIndexRequest) error {
224184
return err
225185
}
226186

227-
// set a temp directory for unpacking an image
228-
// this is in its own function context so that the deferred cleanup runs before we do a docker build
229-
// which prevents the full contents of the previous image from being in the build context
230-
var databasePath string
231-
if err := func() error {
232-
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
233-
if err != nil {
234-
235-
return err
236-
}
237-
defer os.RemoveAll(tmpDir)
238-
239-
databaseFile, err := i.getDatabaseFile(tmpDir, request.FromIndex)
240-
if err != nil {
241-
return err
242-
}
243-
// copy the index to the database folder in the build directory
244-
if databasePath, err = copyDatabaseTo(databaseFile, filepath.Join(buildDir, defaultDatabaseFolder)); err != nil {
245-
return err
246-
}
247-
return nil
248-
}(); err != nil {
187+
databasePath, err := i.extractDatabase(buildDir, request.FromIndex)
188+
if err != nil {
249189
return err
250190
}
251191

@@ -282,6 +222,23 @@ func (i ImageIndexer) PruneFromIndex(request PruneFromIndexRequest) error {
282222
return nil
283223
}
284224

225+
226+
// extractDatabase sets a temp directory for unpacking an image
227+
func (i ImageIndexer) extractDatabase(buildDir, fromIndex string) (string, error) {
228+
tmpDir, err := ioutil.TempDir("./", tmpDirPrefix)
229+
if err != nil {
230+
return "", err
231+
}
232+
defer os.RemoveAll(tmpDir)
233+
234+
databaseFile, err := i.getDatabaseFile(tmpDir, fromIndex)
235+
if err != nil {
236+
return "", err
237+
}
238+
// copy the index to the database folder in the build directory
239+
return copyDatabaseTo(databaseFile, filepath.Join(buildDir, defaultDatabaseFolder))
240+
}
241+
285242
func (i ImageIndexer) getDatabaseFile(workingDir, fromIndex string) (string, error) {
286243
if fromIndex == "" {
287244
return path.Join(workingDir, defaultDatabaseFile), nil
@@ -497,7 +454,7 @@ func (i ImageIndexer) ExportFromIndex(request ExportFromIndexRequest) error {
497454
// operator-registry does not care about the folder name
498455
folderName = bundleImage
499456
}
500-
exporter := bundle.NewSQLExporterForBundle(bundleImage, filepath.Join(request.DownloadPath, folderName), request.ContainerTool)
457+
exporter := bundle.NewExporterForBundle(bundleImage, filepath.Join(request.DownloadPath, folderName), request.ContainerTool)
501458
if err := exporter.Export(); err != nil {
502459
err = fmt.Errorf("error exporting bundle from image: %s", err)
503460
errs = append(errs, err)

0 commit comments

Comments
 (0)
Please sign in to comment.