Skip to content

Commit 2ddd741

Browse files
authored
Adds new catalogmetadata package (#393)
* Adds new `catalogmetadata` package At the moment it contains: * Types which embed `declcfg.Package`, `declcfg.Channel` and `declcfg.Bundle`. Later we will be adding more methods to them (for unmarshalling properties, for example) * A function to fetch metadata by scheme & catalog name * `Filter` function which can filter all these 3 types using a given predicate * `And`, `Or` and `Not` composite predicates to be used with `Filter` Later we might expand/change this package to be more convinient in use with variable sources. Signed-off-by: Mikalai Radchuk <[email protected]> * Adds predicates and tests for them Signed-off-by: Mikalai Radchuk <[email protected]> Signed-off-by: dtfranz <[email protected]> * Moves filter and predicates into a sub package Signed-off-by: Mikalai Radchuk <[email protected]> * Adds client to fetch bundles Signed-off-by: Mikalai Radchuk <[email protected]> * Adds sort subpackage to catalogmetadata Signed-off-by: Mikalai Radchuk <[email protected]> --------- Signed-off-by: Mikalai Radchuk <[email protected]> Signed-off-by: dtfranz <[email protected]>
1 parent 1ea812b commit 2ddd741

File tree

13 files changed

+1365
-2
lines changed

13 files changed

+1365
-2
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
9+
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
10+
"github.com/operator-framework/operator-registry/alpha/declcfg"
11+
12+
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
13+
)
14+
15+
func NewClient(cl client.Client) *Client {
16+
return &Client{cl: cl}
17+
}
18+
19+
// Client is reading catalog metadata
20+
type Client struct {
21+
// Note that eventually we will be reading from catalogd http API
22+
// instead of kube API server. We will need to swap this implementation.
23+
cl client.Client
24+
}
25+
26+
func (c *Client) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error) {
27+
var allBundles []*catalogmetadata.Bundle
28+
29+
var catalogList catalogd.CatalogList
30+
if err := c.cl.List(ctx, &catalogList); err != nil {
31+
return nil, err
32+
}
33+
for _, catalog := range catalogList.Items {
34+
channels, err := fetchCatalogMetadata[catalogmetadata.Channel](ctx, c.cl, catalog.Name, declcfg.SchemaChannel)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
bundles, err := fetchCatalogMetadata[catalogmetadata.Bundle](ctx, c.cl, catalog.Name, declcfg.SchemaBundle)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
bundles, err = populateExtraFields(catalog.Name, channels, bundles)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
allBundles = append(allBundles, bundles...)
50+
}
51+
52+
return allBundles, nil
53+
}
54+
55+
func fetchCatalogMetadata[T catalogmetadata.Schemas](ctx context.Context, cl client.Client, catalogName, schema string) ([]*T, error) {
56+
var cmList catalogd.CatalogMetadataList
57+
err := cl.List(ctx, &cmList, client.MatchingLabels{"catalog": catalogName, "schema": schema})
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
content, err := catalogmetadata.Unmarshal[T](cmList.Items)
63+
if err != nil {
64+
return nil, fmt.Errorf("error unmarshalling catalog metadata: %s", err)
65+
}
66+
67+
return content, nil
68+
}
69+
70+
func populateExtraFields(catalogName string, channels []*catalogmetadata.Channel, bundles []*catalogmetadata.Bundle) ([]*catalogmetadata.Bundle, error) {
71+
bundlesMap := map[string]*catalogmetadata.Bundle{}
72+
for i := range bundles {
73+
bundleKey := fmt.Sprintf("%s-%s", bundles[i].Package, bundles[i].Name)
74+
bundlesMap[bundleKey] = bundles[i]
75+
76+
bundles[i].CatalogName = catalogName
77+
}
78+
79+
for _, ch := range channels {
80+
for _, chEntry := range ch.Entries {
81+
bundleKey := fmt.Sprintf("%s-%s", ch.Package, chEntry.Name)
82+
bundle, ok := bundlesMap[bundleKey]
83+
if !ok {
84+
return nil, fmt.Errorf("bundle %q not found in catalog %q (package %q, channel %q)", chEntry.Name, catalogName, ch.Package, ch.Name)
85+
}
86+
87+
bundle.InChannels = append(bundle.InChannels, ch)
88+
}
89+
}
90+
91+
return bundles, nil
92+
}

0 commit comments

Comments
 (0)