Skip to content

Commit e37bb1c

Browse files
committed
refactor catalogmetadata types used for resolution
Signed-off-by: everettraven <[email protected]>
1 parent ef06f40 commit e37bb1c

25 files changed

+727
-677
lines changed

cmd/manager/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func main() {
115115
}
116116

117117
if err = (&controllers.ClusterExtensionReconciler{
118-
Client: cl,
119-
BundleProvider: catalogClient,
120-
Scheme: mgr.GetScheme(),
121-
Resolver: resolver,
118+
Client: cl,
119+
CatalogProvider: catalogClient,
120+
Scheme: mgr.GetScheme(),
121+
Resolver: resolver,
122122
}).SetupWithManager(mgr); err != nil {
123123
setupLog.Error(err, "unable to create controller", "controller", "ClusterExtension")
124124
os.Exit(1)

cmd/resolutioncli/client.go

+61-21
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ import (
2020
"context"
2121

2222
"github.com/operator-framework/operator-registry/alpha/action"
23+
"github.com/operator-framework/operator-registry/alpha/declcfg"
2324

2425
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
2526
"github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
27+
"github.com/operator-framework/operator-controller/internal/controllers"
2628
)
2729

2830
type indexRefClient struct {
29-
renderer action.Render
30-
bundlesCache []*catalogmetadata.Bundle
31+
renderer action.Render
32+
bundlesCache []*catalogmetadata.Bundle
33+
channelsCache []*catalogmetadata.Channel
34+
packagesCache []*catalogmetadata.Package
3135
}
3236

37+
var _ controllers.CatalogProvider = &indexRefClient{}
38+
3339
func newIndexRefClient(indexRef string) *indexRefClient {
3440
return &indexRefClient{
3541
renderer: action.Render{
@@ -39,47 +45,81 @@ func newIndexRefClient(indexRef string) *indexRefClient {
3945
}
4046
}
4147

42-
func (c *indexRefClient) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error) {
43-
if c.bundlesCache == nil {
48+
func (c *indexRefClient) CatalogContents(ctx context.Context) (*client.Contents, error) {
49+
if c.bundlesCache == nil || c.channelsCache == nil || c.packagesCache == nil {
4450
cfg, err := c.renderer.Run(ctx)
4551
if err != nil {
4652
return nil, err
4753
}
4854

4955
var (
50-
channels []*catalogmetadata.Channel
51-
bundles []*catalogmetadata.Bundle
52-
deprecations []*catalogmetadata.Deprecation
56+
channels []*catalogmetadata.Channel
57+
bundles []*catalogmetadata.Bundle
58+
packages []*catalogmetadata.Package
5359
)
5460

61+
// TODO: update fake catalog name string to be catalog name once we support multiple catalogs in CLI
62+
catalogName := "offline-catalog"
63+
64+
for i := range cfg.Packages {
65+
packages = append(packages, &catalogmetadata.Package{
66+
Package: cfg.Packages[i],
67+
Catalog: catalogName,
68+
})
69+
}
70+
5571
for i := range cfg.Channels {
5672
channels = append(channels, &catalogmetadata.Channel{
5773
Channel: cfg.Channels[i],
74+
Catalog: catalogName,
5875
})
5976
}
6077

6178
for i := range cfg.Bundles {
6279
bundles = append(bundles, &catalogmetadata.Bundle{
63-
Bundle: cfg.Bundles[i],
80+
Bundle: cfg.Bundles[i],
81+
Catalog: catalogName,
6482
})
6583
}
6684

67-
for i := range cfg.Deprecations {
68-
deprecations = append(deprecations, &catalogmetadata.Deprecation{
69-
Deprecation: cfg.Deprecations[i],
70-
})
71-
}
72-
73-
// TODO: update fake catalog name string to be catalog name once we support multiple catalogs in CLI
74-
catalogName := "offline-catalog"
75-
76-
bundles, err = client.PopulateExtraFields(catalogName, channels, bundles, deprecations)
77-
if err != nil {
78-
return nil, err
85+
for _, deprecation := range cfg.Deprecations {
86+
for _, entry := range deprecation.Entries {
87+
switch entry.Reference.Schema {
88+
case declcfg.SchemaPackage:
89+
for _, pkg := range packages {
90+
if pkg.Name == deprecation.Package {
91+
pkg.Deprecation = &declcfg.DeprecationEntry{
92+
Reference: entry.Reference,
93+
Message: entry.Message,
94+
}
95+
}
96+
}
97+
case declcfg.SchemaChannel:
98+
for _, channel := range channels {
99+
if channel.Package == deprecation.Package && channel.Name == entry.Reference.Name {
100+
channel.Deprecation = &declcfg.DeprecationEntry{
101+
Reference: entry.Reference,
102+
Message: entry.Message,
103+
}
104+
}
105+
}
106+
case declcfg.SchemaBundle:
107+
for _, bundle := range bundles {
108+
if bundle.Package == deprecation.Package && bundle.Name == entry.Reference.Name {
109+
bundle.Deprecation = &declcfg.DeprecationEntry{
110+
Reference: entry.Reference,
111+
Message: entry.Message,
112+
}
113+
}
114+
}
115+
}
116+
}
79117
}
80118

81119
c.bundlesCache = bundles
120+
c.channelsCache = channels
121+
c.packagesCache = packages
82122
}
83123

84-
return c.bundlesCache, nil
124+
return &client.Contents{}, nil
85125
}

cmd/resolutioncli/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func run(ctx context.Context, packageName, packageChannel, packageVersionRange,
150150

151151
cl := clientBuilder.Build()
152152
catalogClient := newIndexRefClient(indexRef)
153-
allBundles, err := catalogClient.Bundles(ctx)
153+
contents, err := catalogClient.CatalogContents(ctx)
154154
if err != nil {
155155
return err
156156
}
@@ -162,7 +162,7 @@ func run(ctx context.Context, packageName, packageChannel, packageVersionRange,
162162
if err := cl.List(ctx, &bundleDeploymentList); err != nil {
163163
return err
164164
}
165-
variables, err := controllers.GenerateVariables(allBundles, clusterExtensionList.Items, bundleDeploymentList.Items)
165+
variables, err := controllers.GenerateVariables(contents, clusterExtensionList.Items, bundleDeploymentList.Items)
166166
if err != nil {
167167
return err
168168
}

internal/catalogmetadata/client/client.go

+54-53
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,31 @@ type Client struct {
4343
fetcher Fetcher
4444
}
4545

46-
func (c *Client) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error) {
47-
var allBundles []*catalogmetadata.Bundle
46+
type Contents struct {
47+
Packages []*catalogmetadata.Package
48+
Channels []*catalogmetadata.Channel
49+
Bundles []*catalogmetadata.Bundle
50+
}
4851

52+
func (c *Client) CatalogContents(ctx context.Context) (*Contents, error) {
4953
var catalogList catalogd.CatalogList
5054
if err := c.cl.List(ctx, &catalogList); err != nil {
5155
return nil, err
5256
}
57+
58+
contents := &Contents{
59+
Packages: []*catalogmetadata.Package{},
60+
Channels: []*catalogmetadata.Channel{},
61+
Bundles: []*catalogmetadata.Bundle{},
62+
}
63+
5364
for _, catalog := range catalogList.Items {
5465
// if the catalog has not been successfully unpacked, skip it
5566
if !meta.IsStatusConditionPresentAndEqual(catalog.Status.Conditions, catalogd.TypeUnpacked, metav1.ConditionTrue) {
5667
continue
5768
}
69+
70+
packages := []*catalogmetadata.Package{}
5871
channels := []*catalogmetadata.Channel{}
5972
bundles := []*catalogmetadata.Bundle{}
6073
deprecations := []*catalogmetadata.Deprecation{}
@@ -70,93 +83,81 @@ func (c *Client) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error)
7083
return fmt.Errorf("error was provided to the WalkMetasReaderFunc: %s", err)
7184
}
7285
switch meta.Schema {
86+
case declcfg.SchemaPackage:
87+
var content catalogmetadata.Package
88+
if err := json.Unmarshal(meta.Blob, &content); err != nil {
89+
return fmt.Errorf("error unmarshalling channel from catalog metadata: %s", err)
90+
}
91+
content.Catalog = catalog.Name
92+
packages = append(packages, &content)
7393
case declcfg.SchemaChannel:
7494
var content catalogmetadata.Channel
7595
if err := json.Unmarshal(meta.Blob, &content); err != nil {
7696
return fmt.Errorf("error unmarshalling channel from catalog metadata: %s", err)
7797
}
98+
content.Catalog = catalog.Name
7899
channels = append(channels, &content)
79100
case declcfg.SchemaBundle:
80101
var content catalogmetadata.Bundle
81102
if err := json.Unmarshal(meta.Blob, &content); err != nil {
82103
return fmt.Errorf("error unmarshalling bundle from catalog metadata: %s", err)
83104
}
105+
content.Catalog = catalog.Name
84106
bundles = append(bundles, &content)
85107
case declcfg.SchemaDeprecation:
86108
var content catalogmetadata.Deprecation
87109
if err := json.Unmarshal(meta.Blob, &content); err != nil {
88110
return fmt.Errorf("error unmarshalling deprecation from catalog metadata: %s", err)
89111
}
112+
content.Catalog = catalog.Name
90113
deprecations = append(deprecations, &content)
91114
}
92115

93116
return nil
94117
})
95-
if err != nil {
96-
return nil, fmt.Errorf("error processing response: %s", err)
97-
}
98118

99-
bundles, err = PopulateExtraFields(catalog.Name, channels, bundles, deprecations)
100119
if err != nil {
101-
return nil, err
102-
}
103-
104-
allBundles = append(allBundles, bundles...)
105-
}
106-
107-
return allBundles, nil
108-
}
109-
110-
func PopulateExtraFields(catalogName string, channels []*catalogmetadata.Channel, bundles []*catalogmetadata.Bundle, deprecations []*catalogmetadata.Deprecation) ([]*catalogmetadata.Bundle, error) {
111-
bundlesMap := map[string]*catalogmetadata.Bundle{}
112-
for i := range bundles {
113-
bundleKey := fmt.Sprintf("%s-%s", bundles[i].Package, bundles[i].Name)
114-
bundlesMap[bundleKey] = bundles[i]
115-
116-
bundles[i].CatalogName = catalogName
117-
}
118-
119-
for _, ch := range channels {
120-
for _, chEntry := range ch.Entries {
121-
bundleKey := fmt.Sprintf("%s-%s", ch.Package, chEntry.Name)
122-
bundle, ok := bundlesMap[bundleKey]
123-
if !ok {
124-
return nil, fmt.Errorf("bundle %q not found in catalog %q (package %q, channel %q)", chEntry.Name, catalogName, ch.Package, ch.Name)
125-
}
126-
127-
bundle.InChannels = append(bundle.InChannels, ch)
120+
return nil, fmt.Errorf("error processing response: %s", err)
128121
}
129-
}
130-
131-
// According to https://docs.google.com/document/d/1EzefSzoGZL2ipBt-eCQwqqNwlpOIt7wuwjG6_8ZCi5s/edit?usp=sharing
132-
// the olm.deprecations FBC object is only valid when either 0 or 1 instances exist
133-
// for any given package
134-
deprecationMap := make(map[string]*catalogmetadata.Deprecation, len(deprecations))
135-
for _, deprecation := range deprecations {
136-
deprecationMap[deprecation.Package] = deprecation
137-
}
138122

139-
for i := range bundles {
140-
if dep, ok := deprecationMap[bundles[i].Package]; ok {
141-
for _, entry := range dep.Entries {
123+
for _, deprecation := range deprecations {
124+
for _, entry := range deprecation.Entries {
142125
switch entry.Reference.Schema {
143126
case declcfg.SchemaPackage:
144-
bundles[i].Deprecations = append(bundles[i].Deprecations, entry)
127+
for _, pkg := range packages {
128+
if pkg.Name == deprecation.Package {
129+
pkg.Deprecation = &declcfg.DeprecationEntry{
130+
Reference: entry.Reference,
131+
Message: entry.Message,
132+
}
133+
}
134+
}
145135
case declcfg.SchemaChannel:
146-
for _, ch := range bundles[i].InChannels {
147-
if ch.Name == entry.Reference.Name {
148-
bundles[i].Deprecations = append(bundles[i].Deprecations, entry)
149-
break
136+
for _, channel := range channels {
137+
if channel.Package == deprecation.Package && channel.Name == entry.Reference.Name {
138+
channel.Deprecation = &declcfg.DeprecationEntry{
139+
Reference: entry.Reference,
140+
Message: entry.Message,
141+
}
150142
}
151143
}
152144
case declcfg.SchemaBundle:
153-
if bundles[i].Name == entry.Reference.Name {
154-
bundles[i].Deprecations = append(bundles[i].Deprecations, entry)
145+
for _, bundle := range bundles {
146+
if bundle.Package == deprecation.Package && bundle.Name == entry.Reference.Name {
147+
bundle.Deprecation = &declcfg.DeprecationEntry{
148+
Reference: entry.Reference,
149+
Message: entry.Message,
150+
}
151+
}
155152
}
156153
}
157154
}
158155
}
156+
157+
contents.Packages = append(contents.Packages, packages...)
158+
contents.Channels = append(contents.Channels, channels...)
159+
contents.Bundles = append(contents.Bundles, bundles...)
159160
}
160161

161-
return bundles, nil
162+
return contents, nil
162163
}

0 commit comments

Comments
 (0)