From 5c728312cdd8c8cfdfb23106fc3ef03e4fba02bb Mon Sep 17 00:00:00 2001 From: Mikalai Radchuk Date: Wed, 23 Aug 2023 12:13:54 +0100 Subject: [PATCH] fixup! Switch to catalogd's `CatalogMetadata` APIs Signed-off-by: Mikalai Radchuk --- cmd/resolutioncli/entity_source.go | 8 +- .../entitysources/catalogdsource.go | 125 +++++++++--------- 2 files changed, 60 insertions(+), 73 deletions(-) diff --git a/cmd/resolutioncli/entity_source.go b/cmd/resolutioncli/entity_source.go index 14fa3583e..664cf9943 100644 --- a/cmd/resolutioncli/entity_source.go +++ b/cmd/resolutioncli/entity_source.go @@ -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" ) @@ -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 } diff --git a/internal/resolution/entitysources/catalogdsource.go b/internal/resolution/entitysources/catalogdsource.go index 1a2a54012..68cab936f 100644 --- a/internal/resolution/entitysources/catalogdsource.go +++ b/internal/resolution/entitysources/catalogdsource.go @@ -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" @@ -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 } - catalogEntitiesList, err := ModelToEntities(model, catalog.Name) + catalogEntitiesList, err := MetadataToEntities(catalog.Name, channels, bundles) if err != nil { return nil, err } @@ -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) + } + + 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: 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 } 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 } - return model, nil + return channels, bundles, nil } type declcfgSchemas interface { @@ -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 -}