@@ -2,19 +2,15 @@ package action
22
33import (
44 "context"
5- "database/sql"
65 "encoding/json"
76 "errors"
87 "fmt"
98 "os"
109 "path/filepath"
1110 "sort"
1211 "strings"
13- "sync"
1412 "text/template"
1513
16- "github.com/h2non/filetype"
17- "github.com/h2non/filetype/matchers"
1814 "k8s.io/apimachinery/pkg/util/sets"
1915
2016 "github.com/operator-framework/operator-registry/alpha/action/migrations"
@@ -25,17 +21,12 @@ import (
2521 "github.com/operator-framework/operator-registry/pkg/image/containersimageregistry"
2622 "github.com/operator-framework/operator-registry/pkg/lib/bundle"
2723 "github.com/operator-framework/operator-registry/pkg/registry"
28- "github.com/operator-framework/operator-registry/pkg/sqlite"
2924)
3025
31- var logDeprecationMessage sync.Once
32-
3326type RefType uint
3427
3528const (
3629 RefBundleImage RefType = 1 << iota
37- RefSqliteImage
38- RefSqliteFile
3930 RefDCImage
4031 RefDCDir
4132 RefBundleDir
@@ -55,15 +46,9 @@ type Render struct {
5546 AllowedRefMask RefType
5647 ImageRefTemplate * template.Template
5748 Migrations * migrations.Migrations
58-
59- skipSqliteDeprecationLog bool
6049}
6150
6251func (r Render ) Run (ctx context.Context ) (* declcfg.DeclarativeConfig , error ) {
63- if r .skipSqliteDeprecationLog {
64- // exhaust once with a no-op function.
65- logDeprecationMessage .Do (func () {})
66- }
6752 if r .Registry == nil {
6853 reg , err := containersimageregistry .NewDefault ()
6954 if err != nil {
@@ -125,21 +110,8 @@ func (r Render) renderReference(ctx context.Context, ref string) (*declcfg.Decla
125110 }
126111 return declcfg .LoadFS (ctx , os .DirFS (ref ))
127112 }
128- // The only supported file type is an sqlite DB file,
129- // since declarative configs will be in a directory.
130- if err := checkDBFile (ref ); err != nil {
131- return nil , err
132- }
133- if ! r .AllowedRefMask .Allowed (RefSqliteFile ) {
134- return nil , fmt .Errorf ("cannot render sqlite file: %w" , ErrNotAllowed )
135- }
136-
137- db , err := sqlite .Open (ref )
138- if err != nil {
139- return nil , err
140- }
141- defer db .Close ()
142- return sqliteToDeclcfg (ctx , db )
113+ // Only directories are supported for file-based catalogs and bundles.
114+ return nil , fmt .Errorf ("ref %q is not a directory" , ref )
143115}
144116
145117func (r Render ) imageToDeclcfg (ctx context.Context , imageRef string ) (* declcfg.DeclarativeConfig , error ) {
@@ -161,21 +133,7 @@ func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.D
161133 }
162134
163135 var cfg * declcfg.DeclarativeConfig
164- // nolint:nestif
165- if dbFile , ok := labels [containertools .DbLocationLabel ]; ok {
166- if ! r .AllowedRefMask .Allowed (RefSqliteImage ) {
167- return nil , fmt .Errorf ("cannot render sqlite image: %w" , ErrNotAllowed )
168- }
169- db , err := sqlite .Open (filepath .Join (tmpDir , dbFile ))
170- if err != nil {
171- return nil , err
172- }
173- defer db .Close ()
174- cfg , err = sqliteToDeclcfg (ctx , db )
175- if err != nil {
176- return nil , err
177- }
178- } else if configsDir , ok := labels [containertools .ConfigsLocationLabel ]; ok {
136+ if configsDir , ok := labels [containertools .ConfigsLocationLabel ]; ok {
179137 if ! r .AllowedRefMask .Allowed (RefDCImage ) {
180138 return nil , fmt .Errorf ("cannot render declarative config image: %w" , ErrNotAllowed )
181139 }
@@ -213,93 +171,6 @@ func (r Render) imageToDeclcfg(ctx context.Context, imageRef string) (*declcfg.D
213171}
214172
215173// checkDBFile returns an error if ref is not an sqlite3 database.
216- func checkDBFile (ref string ) error {
217- typ , err := filetype .MatchFile (ref )
218- if err != nil {
219- return err
220- }
221- if typ != matchers .TypeSqlite {
222- return fmt .Errorf ("ref %q has unsupported file type: %s" , ref , typ )
223- }
224- return nil
225- }
226-
227- func sqliteToDeclcfg (ctx context.Context , db * sql.DB ) (* declcfg.DeclarativeConfig , error ) {
228- logDeprecationMessage .Do (func () {
229- sqlite .LogSqliteDeprecation ()
230- })
231-
232- migrator , err := sqlite .NewSQLLiteMigrator (db )
233- if err != nil {
234- return nil , err
235- }
236- if migrator == nil {
237- return nil , fmt .Errorf ("failed to load migrator" )
238- }
239-
240- if err := migrator .Migrate (ctx ); err != nil {
241- return nil , err
242- }
243-
244- q := sqlite .NewSQLLiteQuerierFromDb (db )
245- m , err := sqlite .ToModel (ctx , q )
246- if err != nil {
247- return nil , err
248- }
249-
250- cfg := declcfg .ConvertFromModel (m )
251-
252- if err := populateDBRelatedImages (ctx , & cfg , db ); err != nil {
253- return nil , err
254- }
255-
256- return & cfg , nil
257- }
258-
259- func populateDBRelatedImages (ctx context.Context , cfg * declcfg.DeclarativeConfig , db * sql.DB ) error {
260- rows , err := db .QueryContext (ctx , "SELECT image, operatorbundle_name FROM related_image" )
261- if err != nil {
262- return err
263- }
264- defer rows .Close ()
265-
266- // nolint:staticcheck
267- images := map [string ]sets.String {}
268- for rows .Next () {
269- var (
270- img sql.NullString
271- bundleName sql.NullString
272- )
273- if err := rows .Scan (& img , & bundleName ); err != nil {
274- return err
275- }
276- if ! img .Valid || ! bundleName .Valid {
277- continue
278- }
279- m , ok := images [bundleName .String ]
280- if ! ok {
281- m = sets .NewString ()
282- }
283- m .Insert (img .String )
284- images [bundleName .String ] = m
285- }
286-
287- for i , b := range cfg .Bundles {
288- ris , ok := images [b .Name ]
289- if ! ok {
290- continue
291- }
292- for _ , ri := range b .RelatedImages {
293- if ris .Has (ri .Image ) {
294- ris .Delete (ri .Image )
295- }
296- }
297- for ri := range ris {
298- cfg .Bundles [i ].RelatedImages = append (cfg .Bundles [i ].RelatedImages , declcfg.RelatedImage {Image : ri })
299- }
300- }
301- return nil
302- }
303174
304175func bundleToDeclcfg (bundle * registry.Bundle ) (* declcfg.Bundle , error ) {
305176 objs , props , err := registry .ObjectsAndPropertiesFromBundle (bundle )
0 commit comments