Skip to content

Commit

Permalink
fixup! Switch to catalogd's CatalogMetadata APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Aug 23, 2023
1 parent 72952e3 commit 5c72831
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 73 deletions.
8 changes: 1 addition & 7 deletions cmd/resolutioncli/entity_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/action"
"github.com/operator-framework/operator-registry/alpha/declcfg"

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

model, err := declcfg.ConvertToModel(*cfg)
if err != nil {
return nil, err
}

// TODO: update empty catalog name string to be catalog name once we support multiple catalogs in CLI
entities, err := entitysources.ModelToEntities(model, "")
entities, err := entitysources.MetadataToEntities("", cfg.Channels, cfg.Bundles)
if err != nil {
return nil, err
}
Expand Down
125 changes: 59 additions & 66 deletions internal/resolution/entitysources/catalogdsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/model"
"github.com/operator-framework/operator-registry/alpha/property"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -81,12 +80,12 @@ func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error
return nil, err
}
for _, catalog := range catalogList.Items {
model, err := fetchCatalogModel(ctx, cl, catalog.Name)
channels, bundles, err := fetchMetadata(ctx, cl, catalog.Name)
if err != nil {
return nil, err

Check warning on line 85 in internal/resolution/entitysources/catalogdsource.go

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L85

Added line #L85 was not covered by tests
}

catalogEntitiesList, err := ModelToEntities(model, catalog.Name)
catalogEntitiesList, err := MetadataToEntities(catalog.Name, channels, bundles)
if err != nil {
return nil, err
}
Expand All @@ -97,31 +96,71 @@ func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error
return allEntitiesList, nil
}

func fetchCatalogModel(ctx context.Context, cl client.Client, catalogName string) (model.Model, error) {
packages, err := fetch[declcfg.Package](ctx, cl, declcfg.SchemaPackage, catalogName)
if err != nil {
return nil, err
func MetadataToEntities(catalogName string, channels []declcfg.Channel, bundles []declcfg.Bundle) (input.EntityList, error) {
entityList := input.EntityList{}

bundlesMap := map[string]*declcfg.Bundle{}
for i := range bundles {
bundlesMap[bundles[i].Name] = &bundles[i]
}

for _, ch := range channels {
for _, chEntry := range ch.Entries {
bundle, ok := bundlesMap[chEntry.Name]
if !ok {
return nil, fmt.Errorf("package %s channel %s contains a bundle %s, but it does not exist", ch.Package, ch.Name, chEntry.Name)

Check warning on line 111 in internal/resolution/entitysources/catalogdsource.go

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L111

Added line #L111 was not covered by tests
}

props := map[string]string{}

for _, prop := range bundle.Properties {
switch prop.Type {
case property.TypePackage:
// this is already a json marshalled object, so it doesn't need to be marshalled
// like the other ones
props[property.TypePackage] = string(prop.Value)
case entities.PropertyBundleMediaType:
props[entities.PropertyBundleMediaType] = string(prop.Value)
}
}

imgValue, err := json.Marshal(bundle.Image)
if err != nil {
return nil, err

Check warning on line 129 in internal/resolution/entitysources/catalogdsource.go

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L129

Added line #L129 was not covered by tests
}
props[entities.PropertyBundlePath] = string(imgValue)

channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0})
props[property.TypeChannel] = string(channelValue)
replacesValue, _ := json.Marshal(entities.ChannelEntry{
Name: bundle.Name,
Replaces: chEntry.Replaces,
})
props[entities.PropertyBundleChannelEntry] = string(replacesValue)

catalogScopedEntryName := fmt.Sprintf("%s-%s", catalogName, bundle.Name)
entity := input.Entity{
ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", catalogScopedEntryName, bundle.Package, ch.Name)),
Properties: props,
}
entityList = append(entityList, entity)
}
}

return entityList, nil
}

func fetchMetadata(ctx context.Context, cl client.Client, catalogName string) ([]declcfg.Channel, []declcfg.Bundle, error) {
channels, err := fetch[declcfg.Channel](ctx, cl, declcfg.SchemaChannel, catalogName)
if err != nil {
return nil, err
return nil, nil, err

Check warning on line 156 in internal/resolution/entitysources/catalogdsource.go

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L156

Added line #L156 was not covered by tests
}
bundles, err := fetch[declcfg.Bundle](ctx, cl, declcfg.SchemaBundle, catalogName)
if err != nil {
return nil, err
}

cfg := &declcfg.DeclarativeConfig{
Packages: packages,
Channels: channels,
Bundles: bundles,
}
model, err := declcfg.ConvertToModel(*cfg)
if err != nil {
return nil, err
return nil, nil, err

Check warning on line 160 in internal/resolution/entitysources/catalogdsource.go

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L160

Added line #L160 was not covered by tests
}

return model, nil
return channels, bundles, nil
}

type declcfgSchemas interface {
Expand All @@ -147,49 +186,3 @@ func fetch[T declcfgSchemas](ctx context.Context, cl client.Client, schema, cata

return contents, nil
}

func ModelToEntities(model model.Model, catalogName string) (input.EntityList, error) {
entityList := input.EntityList{}

for _, pkg := range model {
for _, ch := range pkg.Channels {
for _, bundle := range ch.Bundles {
props := map[string]string{}

for _, prop := range bundle.Properties {
switch prop.Type {
case property.TypePackage:
// this is already a json marshalled object, so it doesn't need to be marshalled
// like the other ones
props[property.TypePackage] = string(prop.Value)
case entities.PropertyBundleMediaType:
props[entities.PropertyBundleMediaType] = string(prop.Value)
}
}

imgValue, err := json.Marshal(bundle.Image)
if err != nil {
return nil, err
}
props[entities.PropertyBundlePath] = string(imgValue)

channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0})
props[property.TypeChannel] = string(channelValue)
replacesValue, _ := json.Marshal(entities.ChannelEntry{
Name: bundle.Name,
Replaces: bundle.Replaces,
})
props[entities.PropertyBundleChannelEntry] = string(replacesValue)

catalogScopedEntryName := fmt.Sprintf("%s-%s", catalogName, bundle.Name)
entity := input.Entity{
ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", catalogScopedEntryName, bundle.Package.Name, ch.Name)),
Properties: props,
}
entityList = append(entityList, entity)
}
}
}

return entityList, nil
}

0 comments on commit 5c72831

Please sign in to comment.