44 "context"
55 "database/sql"
66 "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"
710 "io"
811 "io/ioutil"
912 "os"
@@ -210,19 +213,43 @@ func (i ImageIndexer) getDatabaseFile(workingDir, fromIndex string) (string, err
210213 // Pull the fromIndex
211214 i .Logger .Infof ("Pulling previous image %s to get metadata" , fromIndex )
212215
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+
213241 // 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 )
215243 if err != nil {
216244 return "" , err
217245 }
246+
218247 dbLocation , ok := labels [containertools .DbLocationLabel ]
219248 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 )
221250 }
222251
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 {
226253 return "" , err
227254 }
228255
@@ -341,7 +368,7 @@ type ExportFromIndexRequest struct {
341368 Index string
342369 Package string
343370 DownloadPath string
344- ContainerTool string
371+ ContainerTool containertools. ContainerTool
345372}
346373
347374// ExportFromIndex is an aggregate API used to specify operators from
@@ -355,7 +382,7 @@ func (i ImageIndexer) ExportFromIndex(request ExportFromIndexRequest) error {
355382 defer os .RemoveAll (workingDir )
356383
357384 // extract the index database to the file
358- databaseFile , err := i .WriteIndexDBFile ( request . Index , workingDir , defaultDatabaseFile )
385+ databaseFile , err := i .getDatabaseFile ( workingDir , request . Index )
359386 if err != nil {
360387 return err
361388 }
@@ -411,32 +438,6 @@ func (i ImageIndexer) ExportFromIndex(request ExportFromIndexRequest) error {
411438 return utilerrors .NewAggregate (errs )
412439}
413440
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-
440441func getBundlesToExport (dbQuerier pregistry.Query , packageName string ) ([]string , error ) {
441442 bundles , err := dbQuerier .GetBundlePathsForPackage (context .TODO (), packageName )
442443 if err != nil {
0 commit comments