@@ -43,18 +43,31 @@ type Client struct {
43
43
fetcher Fetcher
44
44
}
45
45
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
+ }
48
51
52
+ func (c * Client ) CatalogContents (ctx context.Context ) (* Contents , error ) {
49
53
var catalogList catalogd.CatalogList
50
54
if err := c .cl .List (ctx , & catalogList ); err != nil {
51
55
return nil , err
52
56
}
57
+
58
+ contents := & Contents {
59
+ Packages : []* catalogmetadata.Package {},
60
+ Channels : []* catalogmetadata.Channel {},
61
+ Bundles : []* catalogmetadata.Bundle {},
62
+ }
63
+
53
64
for _ , catalog := range catalogList .Items {
54
65
// if the catalog has not been successfully unpacked, skip it
55
66
if ! meta .IsStatusConditionPresentAndEqual (catalog .Status .Conditions , catalogd .TypeUnpacked , metav1 .ConditionTrue ) {
56
67
continue
57
68
}
69
+
70
+ packages := []* catalogmetadata.Package {}
58
71
channels := []* catalogmetadata.Channel {}
59
72
bundles := []* catalogmetadata.Bundle {}
60
73
deprecations := []* catalogmetadata.Deprecation {}
@@ -70,93 +83,81 @@ func (c *Client) Bundles(ctx context.Context) ([]*catalogmetadata.Bundle, error)
70
83
return fmt .Errorf ("error was provided to the WalkMetasReaderFunc: %s" , err )
71
84
}
72
85
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 )
73
93
case declcfg .SchemaChannel :
74
94
var content catalogmetadata.Channel
75
95
if err := json .Unmarshal (meta .Blob , & content ); err != nil {
76
96
return fmt .Errorf ("error unmarshalling channel from catalog metadata: %s" , err )
77
97
}
98
+ content .Catalog = catalog .Name
78
99
channels = append (channels , & content )
79
100
case declcfg .SchemaBundle :
80
101
var content catalogmetadata.Bundle
81
102
if err := json .Unmarshal (meta .Blob , & content ); err != nil {
82
103
return fmt .Errorf ("error unmarshalling bundle from catalog metadata: %s" , err )
83
104
}
105
+ content .Catalog = catalog .Name
84
106
bundles = append (bundles , & content )
85
107
case declcfg .SchemaDeprecation :
86
108
var content catalogmetadata.Deprecation
87
109
if err := json .Unmarshal (meta .Blob , & content ); err != nil {
88
110
return fmt .Errorf ("error unmarshalling deprecation from catalog metadata: %s" , err )
89
111
}
112
+ content .Catalog = catalog .Name
90
113
deprecations = append (deprecations , & content )
91
114
}
92
115
93
116
return nil
94
117
})
95
- if err != nil {
96
- return nil , fmt .Errorf ("error processing response: %s" , err )
97
- }
98
118
99
- bundles , err = PopulateExtraFields (catalog .Name , channels , bundles , deprecations )
100
119
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 )
128
121
}
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
- }
138
122
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 {
142
125
switch entry .Reference .Schema {
143
126
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
+ }
145
135
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
+ }
150
142
}
151
143
}
152
144
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
+ }
155
152
}
156
153
}
157
154
}
158
155
}
156
+
157
+ contents .Packages = append (contents .Packages , packages ... )
158
+ contents .Channels = append (contents .Channels , channels ... )
159
+ contents .Bundles = append (contents .Bundles , bundles ... )
159
160
}
160
161
161
- return bundles , nil
162
+ return contents , nil
162
163
}
0 commit comments