Skip to content

Commit 5c72831

Browse files
author
Mikalai Radchuk
committed
fixup! Switch to catalogd's CatalogMetadata APIs
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent 72952e3 commit 5c72831

File tree

2 files changed

+60
-73
lines changed

2 files changed

+60
-73
lines changed

cmd/resolutioncli/entity_source.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/operator-framework/deppy/pkg/deppy"
2323
"github.com/operator-framework/deppy/pkg/deppy/input"
2424
"github.com/operator-framework/operator-registry/alpha/action"
25-
"github.com/operator-framework/operator-registry/alpha/declcfg"
2625

2726
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
2827
)
@@ -97,13 +96,8 @@ func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList,
9796
return nil, err
9897
}
9998

100-
model, err := declcfg.ConvertToModel(*cfg)
101-
if err != nil {
102-
return nil, err
103-
}
104-
10599
// TODO: update empty catalog name string to be catalog name once we support multiple catalogs in CLI
106-
entities, err := entitysources.ModelToEntities(model, "")
100+
entities, err := entitysources.MetadataToEntities("", cfg.Channels, cfg.Bundles)
107101
if err != nil {
108102
return nil, err
109103
}

internal/resolution/entitysources/catalogdsource.go

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
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

127166
type 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

Comments
 (0)