99 "github.com/operator-framework/deppy/pkg/deppy"
1010 "github.com/operator-framework/deppy/pkg/deppy/input"
1111 "github.com/operator-framework/operator-registry/alpha/declcfg"
12- "github.com/operator-framework/operator-registry/alpha/model"
1312 "github.com/operator-framework/operator-registry/alpha/property"
1413 "sigs.k8s.io/controller-runtime/pkg/client"
1514
@@ -81,12 +80,12 @@ func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error
8180 return nil , err
8281 }
8382 for _ , catalog := range catalogList .Items {
84- model , err := fetchCatalogModel (ctx , cl , catalog .Name )
83+ channels , bundles , err := fetchMetadata (ctx , cl , catalog .Name )
8584 if err != nil {
8685 return nil , err
8786 }
8887
89- catalogEntitiesList , err := ModelToEntities ( model , catalog .Name )
88+ catalogEntitiesList , err := MetadataToEntities ( catalog .Name , channels , bundles )
9089 if err != nil {
9190 return nil , err
9291 }
@@ -97,31 +96,71 @@ func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error
9796 return allEntitiesList , nil
9897}
9998
100- func fetchCatalogModel (ctx context.Context , cl client.Client , catalogName string ) (model.Model , error ) {
101- packages , err := fetch [declcfg.Package ](ctx , cl , declcfg .SchemaPackage , catalogName )
102- if err != nil {
103- return nil , err
99+ func MetadataToEntities (catalogName string , channels []declcfg.Channel , bundles []declcfg.Bundle ) (input.EntityList , error ) {
100+ entityList := input.EntityList {}
101+
102+ bundlesMap := map [string ]* declcfg.Bundle {}
103+ for i := range bundles {
104+ bundlesMap [bundles [i ].Name ] = & bundles [i ]
105+ }
106+
107+ for _ , ch := range channels {
108+ for _ , chEntry := range ch .Entries {
109+ bundle , ok := bundlesMap [chEntry .Name ]
110+ if ! ok {
111+ return nil , fmt .Errorf ("package %s channel %s contains a bundle %s, but it does not exist" , ch .Package , ch .Name , chEntry .Name )
112+ }
113+
114+ props := map [string ]string {}
115+
116+ for _ , prop := range bundle .Properties {
117+ switch prop .Type {
118+ case property .TypePackage :
119+ // this is already a json marshalled object, so it doesn't need to be marshalled
120+ // like the other ones
121+ props [property .TypePackage ] = string (prop .Value )
122+ case entities .PropertyBundleMediaType :
123+ props [entities .PropertyBundleMediaType ] = string (prop .Value )
124+ }
125+ }
126+
127+ imgValue , err := json .Marshal (bundle .Image )
128+ if err != nil {
129+ return nil , err
130+ }
131+ props [entities .PropertyBundlePath ] = string (imgValue )
132+
133+ channelValue , _ := json .Marshal (property.Channel {ChannelName : ch .Name , Priority : 0 })
134+ props [property .TypeChannel ] = string (channelValue )
135+ replacesValue , _ := json .Marshal (entities.ChannelEntry {
136+ Name : bundle .Name ,
137+ Replaces : chEntry .Replaces ,
138+ })
139+ props [entities .PropertyBundleChannelEntry ] = string (replacesValue )
140+
141+ catalogScopedEntryName := fmt .Sprintf ("%s-%s" , catalogName , bundle .Name )
142+ entity := input.Entity {
143+ ID : deppy .IdentifierFromString (fmt .Sprintf ("%s%s%s" , catalogScopedEntryName , bundle .Package , ch .Name )),
144+ Properties : props ,
145+ }
146+ entityList = append (entityList , entity )
147+ }
104148 }
149+
150+ return entityList , nil
151+ }
152+
153+ func fetchMetadata (ctx context.Context , cl client.Client , catalogName string ) ([]declcfg.Channel , []declcfg.Bundle , error ) {
105154 channels , err := fetch [declcfg.Channel ](ctx , cl , declcfg .SchemaChannel , catalogName )
106155 if err != nil {
107- return nil , err
156+ return nil , nil , err
108157 }
109158 bundles , err := fetch [declcfg.Bundle ](ctx , cl , declcfg .SchemaBundle , catalogName )
110159 if err != nil {
111- return nil , err
112- }
113-
114- cfg := & declcfg.DeclarativeConfig {
115- Packages : packages ,
116- Channels : channels ,
117- Bundles : bundles ,
118- }
119- model , err := declcfg .ConvertToModel (* cfg )
120- if err != nil {
121- return nil , err
160+ return nil , nil , err
122161 }
123162
124- return model , nil
163+ return channels , bundles , nil
125164}
126165
127166type declcfgSchemas interface {
@@ -147,49 +186,3 @@ func fetch[T declcfgSchemas](ctx context.Context, cl client.Client, schema, cata
147186
148187 return contents , nil
149188}
150-
151- func ModelToEntities (model model.Model , catalogName string ) (input.EntityList , error ) {
152- entityList := input.EntityList {}
153-
154- for _ , pkg := range model {
155- for _ , ch := range pkg .Channels {
156- for _ , bundle := range ch .Bundles {
157- props := map [string ]string {}
158-
159- for _ , prop := range bundle .Properties {
160- switch prop .Type {
161- case property .TypePackage :
162- // this is already a json marshalled object, so it doesn't need to be marshalled
163- // like the other ones
164- props [property .TypePackage ] = string (prop .Value )
165- case entities .PropertyBundleMediaType :
166- props [entities .PropertyBundleMediaType ] = string (prop .Value )
167- }
168- }
169-
170- imgValue , err := json .Marshal (bundle .Image )
171- if err != nil {
172- return nil , err
173- }
174- props [entities .PropertyBundlePath ] = string (imgValue )
175-
176- channelValue , _ := json .Marshal (property.Channel {ChannelName : ch .Name , Priority : 0 })
177- props [property .TypeChannel ] = string (channelValue )
178- replacesValue , _ := json .Marshal (entities.ChannelEntry {
179- Name : bundle .Name ,
180- Replaces : bundle .Replaces ,
181- })
182- props [entities .PropertyBundleChannelEntry ] = string (replacesValue )
183-
184- catalogScopedEntryName := fmt .Sprintf ("%s-%s" , catalogName , bundle .Name )
185- entity := input.Entity {
186- ID : deppy .IdentifierFromString (fmt .Sprintf ("%s%s%s" , catalogScopedEntryName , bundle .Package .Name , ch .Name )),
187- Properties : props ,
188- }
189- entityList = append (entityList , entity )
190- }
191- }
192- }
193-
194- return entityList , nil
195- }
0 commit comments