4
4
"context"
5
5
"database/sql"
6
6
"fmt"
7
+ "github.com/operator-framework/operator-registry/pkg/image"
8
+ "github.com/operator-framework/operator-registry/pkg/image/containerdregistry"
9
+ "github.com/operator-framework/operator-registry/pkg/image/execregistry"
7
10
"io"
8
11
"io/ioutil"
9
12
"os"
@@ -210,19 +213,43 @@ func (i ImageIndexer) getDatabaseFile(workingDir, fromIndex string) (string, err
210
213
// Pull the fromIndex
211
214
i .Logger .Infof ("Pulling previous image %s to get metadata" , fromIndex )
212
215
216
+ var reg image.Registry
217
+ var rerr error
218
+ switch i .ContainerTool {
219
+ case containertools .NoneTool :
220
+ reg , rerr = containerdregistry .NewRegistry (containerdregistry .WithLog (i .Logger ))
221
+ case containertools .PodmanTool :
222
+ fallthrough
223
+ case containertools .DockerTool :
224
+ reg , rerr = execregistry .NewRegistry (i .ContainerTool , i .Logger )
225
+ }
226
+ if rerr != nil {
227
+ return "" , rerr
228
+ }
229
+ defer func () {
230
+ if err := reg .Destroy (); err != nil {
231
+ i .Logger .WithError (err ).Warn ("error destroying local cache" )
232
+ }
233
+ }()
234
+
235
+ imageRef := image .SimpleReference (fromIndex )
236
+
237
+ if err := reg .Pull (context .TODO (), imageRef ); err != nil {
238
+ return "" , err
239
+ }
240
+
213
241
// Get the old index image's dbLocationLabel to find this path
214
- labels , err := i . LabelReader . GetLabelsFromImage ( fromIndex )
242
+ labels , err := reg . Labels ( context . TODO (), imageRef )
215
243
if err != nil {
216
244
return "" , err
217
245
}
246
+
218
247
dbLocation , ok := labels [containertools .DbLocationLabel ]
219
248
if ! ok {
220
- return "" , fmt .Errorf ("Index image %s missing label %s" , fromIndex , containertools .DbLocationLabel )
249
+ return "" , fmt .Errorf ("index image %s missing label %s" , fromIndex , containertools .DbLocationLabel )
221
250
}
222
251
223
- // extract the database to the working directory
224
- err = i .ImageReader .GetImageData (fromIndex , workingDir )
225
- if err != nil {
252
+ if err := reg .Unpack (context .TODO (), imageRef , workingDir ); err != nil {
226
253
return "" , err
227
254
}
228
255
@@ -341,7 +368,7 @@ type ExportFromIndexRequest struct {
341
368
Index string
342
369
Package string
343
370
DownloadPath string
344
- ContainerTool string
371
+ ContainerTool containertools. ContainerTool
345
372
}
346
373
347
374
// ExportFromIndex is an aggregate API used to specify operators from
@@ -355,7 +382,7 @@ func (i ImageIndexer) ExportFromIndex(request ExportFromIndexRequest) error {
355
382
defer os .RemoveAll (workingDir )
356
383
357
384
// extract the index database to the file
358
- databaseFile , err := i .WriteIndexDBFile ( request . Index , workingDir , defaultDatabaseFile )
385
+ databaseFile , err := i .getDatabaseFile ( workingDir , request . Index )
359
386
if err != nil {
360
387
return err
361
388
}
@@ -411,32 +438,6 @@ func (i ImageIndexer) ExportFromIndex(request ExportFromIndexRequest) error {
411
438
return utilerrors .NewAggregate (errs )
412
439
}
413
440
414
- func (i ImageIndexer ) WriteIndexDBFile (index , workingDir , databaseFile string ) (string , error ) {
415
- if index != "" {
416
- i .Logger .Infof ("Pulling previous image %s to get metadata" , index )
417
-
418
- // Get the old index image's dbLocationLabel to find this path
419
- labels , err := i .LabelReader .GetLabelsFromImage (index )
420
- if err != nil {
421
- return "" , err
422
- }
423
- if dbLocation , ok := labels [containertools .DbLocationLabel ]; ok {
424
- i .Logger .Infof ("Previous db location %s" , dbLocation )
425
-
426
- // extract the database to the file
427
- err = i .ImageReader .GetImageData (index , workingDir )
428
- if err != nil {
429
- return "" , err
430
- }
431
-
432
- databaseFile = path .Join (workingDir , dbLocation )
433
- }
434
- } else {
435
- databaseFile = path .Join (workingDir , databaseFile )
436
- }
437
- return databaseFile , nil
438
- }
439
-
440
441
func getBundlesToExport (dbQuerier pregistry.Query , packageName string ) ([]string , error ) {
441
442
bundles , err := dbQuerier .GetBundlePathsForPackage (context .TODO (), packageName )
442
443
if err != nil {
0 commit comments