Skip to content

Commit

Permalink
Refactors entity sources to use catalogmetadata
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Sep 5, 2023
1 parent 3cd59a2 commit a39d3fc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
13 changes: 12 additions & 1 deletion cmd/resolutioncli/entity_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/action"

"github.com/operator-framework/operator-controller/internal/catalogmetadata"
"github.com/operator-framework/operator-controller/internal/resolution/entitysources"
)

Expand Down Expand Up @@ -96,8 +97,18 @@ func (es *indexRefEntitySource) entities(ctx context.Context) (input.EntityList,
return nil, err
}

var channels []*catalogmetadata.Channel
for i := range cfg.Channels {
channels = append(channels, &catalogmetadata.Channel{Channel: cfg.Channels[i]})
}

var bundles []*catalogmetadata.Bundle
for i := range cfg.Bundles {
bundles = append(bundles, &catalogmetadata.Bundle{Bundle: cfg.Bundles[i]})
}

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

"github.com/operator-framework/operator-controller/internal/catalogmetadata"
"github.com/operator-framework/operator-controller/internal/resolution/entities"
)

Expand Down Expand Up @@ -96,13 +97,13 @@ func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error
return allEntitiesList, nil
}

func MetadataToEntities(catalogName string, channels []declcfg.Channel, bundles []declcfg.Bundle) (input.EntityList, error) {
func MetadataToEntities(catalogName string, channels []*catalogmetadata.Channel, bundles []*catalogmetadata.Bundle) (input.EntityList, error) {
entityList := input.EntityList{}

bundlesMap := map[string]*declcfg.Bundle{}
bundlesMap := map[string]*catalogmetadata.Bundle{}
for i := range bundles {
bundleKey := fmt.Sprintf("%s-%s", bundles[i].Package, bundles[i].Name)
bundlesMap[bundleKey] = &bundles[i]
bundlesMap[bundleKey] = bundles[i]
}

for _, ch := range channels {
Expand Down Expand Up @@ -152,39 +153,27 @@ func MetadataToEntities(catalogName string, channels []declcfg.Channel, bundles
return entityList, nil
}

func fetchCatalogMetadata(ctx context.Context, cl client.Client, catalogName string) ([]declcfg.Channel, []declcfg.Bundle, error) {
channels, err := fetchCatalogMetadataByScheme[declcfg.Channel](ctx, cl, declcfg.SchemaChannel, catalogName)
func fetchCatalogMetadata(ctx context.Context, cl client.Client, catalogName string) ([]*catalogmetadata.Channel, []*catalogmetadata.Bundle, error) {
var cmList catalogd.CatalogMetadataList
err := cl.List(ctx, &cmList, client.MatchingLabels{"catalog": catalogName, "schema": declcfg.SchemaChannel})
if err != nil {
return nil, nil, err
}
bundles, err := fetchCatalogMetadataByScheme[declcfg.Bundle](ctx, cl, declcfg.SchemaBundle, catalogName)

channels, err := catalogmetadata.Unmarshal[catalogmetadata.Channel](cmList.Items)
if err != nil {
return nil, nil, err
}

return channels, bundles, nil
}

type declcfgSchema interface {
declcfg.Package | declcfg.Bundle | declcfg.Channel
}

// TODO: Cleanup once https://github.com/golang/go/issues/45380 implemented
// We should be able to get rid of the schema arg and switch based on the type passed to this generic
func fetchCatalogMetadataByScheme[T declcfgSchema](ctx context.Context, cl client.Client, schema, catalogName string) ([]T, error) {
cmList := catalogd.CatalogMetadataList{}
if err := cl.List(ctx, &cmList, client.MatchingLabels{"schema": schema, "catalog": catalogName}); err != nil {
return nil, err
err = cl.List(ctx, &cmList, client.MatchingLabels{"catalog": catalogName, "schema": declcfg.SchemaBundle})
if err != nil {
return nil, nil, err

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

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L170

Added line #L170 was not covered by tests
}

contents := []T{}
for _, cm := range cmList.Items {
var content T
if err := json.Unmarshal(cm.Spec.Content, &content); err != nil {
return nil, err
}
contents = append(contents, content)
bundles, err := catalogmetadata.Unmarshal[catalogmetadata.Bundle](cmList.Items)
if err != nil {
return nil, nil, err

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

View check run for this annotation

Codecov / codecov/patch

internal/resolution/entitysources/catalogdsource.go#L175

Added line #L175 was not covered by tests
}

return contents, nil
return channels, bundles, nil
}

0 comments on commit a39d3fc

Please sign in to comment.