Skip to content

Commit

Permalink
update entitysources/catalogdsource to use status url
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Palmer <[email protected]>
  • Loading branch information
everettraven committed Sep 20, 2023
1 parent 3196932 commit 229963c
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions internal/resolution/entitysources/catalogdsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,26 @@ import (
"github.com/operator-framework/operator-controller/internal/resolution/entities"
)

const catalogdOnClusterBaseURL = "http://catalogd-catalogserver.catalogd-system.svc"

// CatalogdEntitySource is a source for(/collection of) deppy defined input.Entity, built from content
// made accessible on-cluster by https://github.com/operator-framework/catalogd.
// It is an implementation of deppy defined input.EntitySource
type CatalogdEntitySource struct {
client client.Client
baseURL string
client client.Client
}

type CatalogdEntitySourceOpt func(es *CatalogdEntitySource)

func WithBaseURL(baseURL string) CatalogdEntitySourceOpt {
return func(es *CatalogdEntitySource) {
es.baseURL = baseURL
func NewCatalogdEntitySource(client client.Client) *CatalogdEntitySource {
return &CatalogdEntitySource{
client: client,
}
}

func NewCatalogdEntitySource(client client.Client, opts ...CatalogdEntitySourceOpt) *CatalogdEntitySource {
ces := &CatalogdEntitySource{
client: client,
baseURL: catalogdOnClusterBaseURL,
}

for _, opt := range opts {
opt(ces)
}

return ces
}

func (es *CatalogdEntitySource) Get(_ context.Context, _ deppy.Identifier) (*input.Entity, error) {
panic("not implemented")
}

func (es *CatalogdEntitySource) Filter(ctx context.Context, filter input.Predicate) (input.EntityList, error) {
resultSet := input.EntityList{}
entities, err := getEntities(ctx, es.client, es.baseURL)
entities, err := getEntities(ctx, es.client)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +48,7 @@ func (es *CatalogdEntitySource) Filter(ctx context.Context, filter input.Predica
}

func (es *CatalogdEntitySource) GroupBy(ctx context.Context, fn input.GroupByFunction) (input.EntityListMap, error) {
entities, err := getEntities(ctx, es.client, es.baseURL)
entities, err := getEntities(ctx, es.client)
if err != nil {
return nil, err
}
Expand All @@ -81,7 +63,7 @@ func (es *CatalogdEntitySource) GroupBy(ctx context.Context, fn input.GroupByFun
}

func (es *CatalogdEntitySource) Iterate(ctx context.Context, fn input.IteratorFunction) error {
entities, err := getEntities(ctx, es.client, es.baseURL)
entities, err := getEntities(ctx, es.client)
if err != nil {
return err
}
Expand All @@ -93,15 +75,15 @@ func (es *CatalogdEntitySource) Iterate(ctx context.Context, fn input.IteratorFu
return nil
}

func getEntities(ctx context.Context, cl client.Client, baseURL string) (input.EntityList, error) {
func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error) {
allEntitiesList := input.EntityList{}

var catalogList catalogd.CatalogList
if err := cl.List(ctx, &catalogList); err != nil {
return nil, err
}
for _, catalog := range catalogList.Items {
channels, bundles, err := fetchCatalogMetadata(ctx, baseURL, catalog.Name)
channels, bundles, err := fetchCatalogMetadata(ctx, catalog.Status.ContentURL, catalog.Name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,13 +172,11 @@ type declcfgSchema interface {
declcfg.Package | declcfg.Bundle | declcfg.Channel
}

const catalogdOnClusterURLTemplate = "%s/catalogs/%s/all.json"

// 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, baseURL, schema, catalogName string) ([]T, error) {
func fetchCatalogMetadataByScheme[T declcfgSchema](ctx context.Context, url, schema, catalogName string) ([]T, error) {
contents := []T{}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf(catalogdOnClusterURLTemplate, baseURL, catalogName), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error forming request: %s", err)
}
Expand Down

0 comments on commit 229963c

Please sign in to comment.