15
15
package bundle
16
16
17
17
import (
18
- "bytes"
19
18
"context"
20
- "errors"
21
19
"fmt"
22
20
"io/ioutil"
23
21
"os"
@@ -28,7 +26,6 @@ import (
28
26
29
27
"github.com/operator-framework/api/pkg/operators/v1alpha1"
30
28
"github.com/operator-framework/operator-registry/alpha/action"
31
- "github.com/operator-framework/operator-registry/alpha/declcfg"
32
29
declarativeconfig "github.com/operator-framework/operator-registry/alpha/declcfg"
33
30
"github.com/operator-framework/operator-registry/pkg/containertools"
34
31
registrybundle "github.com/operator-framework/operator-registry/pkg/lib/bundle"
@@ -37,18 +34,6 @@ import (
37
34
registryutil "github.com/operator-framework/operator-sdk/internal/registry"
38
35
)
39
36
40
- const (
41
- schemaChannel = "olm.channel"
42
- schemaPackage = "olm.package"
43
- defaultChannel = "operator-sdk-run"
44
- )
45
-
46
- type bundleDeclcfg struct {
47
- Package declcfg.Package
48
- Channel declcfg.Channel
49
- Bundle declcfg.Bundle
50
- }
51
-
52
37
type Install struct {
53
38
BundleImage string
54
39
@@ -58,13 +43,6 @@ type Install struct {
58
43
cfg * operator.Configuration
59
44
}
60
45
61
- type FBCContext struct {
62
- Package string
63
- ChannelName string
64
- Refs []string
65
- ChannelEntry declarativeconfig.ChannelEntry
66
- }
67
-
68
46
func NewInstall (cfg * operator.Configuration ) Install {
69
47
i := Install {
70
48
OperatorInstaller : registry .NewOperatorInstaller (cfg ),
@@ -126,6 +104,9 @@ func (i *Install) setup(ctx context.Context) error {
126
104
// set the field to true if FBC label is on the image or for a default index image.
127
105
if _ , hasFBCLabel := catalogLabels [containertools .ConfigsLocationLabel ]; hasFBCLabel || i .IndexImageCatalogCreator .IndexImage == registry .DefaultIndexImage {
128
106
i .IndexImageCatalogCreator .HasFBCLabel = true
107
+ if i .IndexImageCatalogCreator .BundleAddMode != "" {
108
+ return fmt .Errorf ("specifying the bundle add mode is not supported for File-Based Catalog bundles and index images" )
109
+ }
129
110
} else {
130
111
// index image is of the SQLite index format.
131
112
deprecationMsg := fmt .Sprintf ("%s is a SQLite index image. SQLite based index images are being deprecated and will be removed in a future release, please migrate your catalogs to the new File-Based Catalog format" , i .IndexImageCatalogCreator .IndexImage )
@@ -134,7 +115,7 @@ func (i *Install) setup(ctx context.Context) error {
134
115
135
116
if i .IndexImageCatalogCreator .HasFBCLabel {
136
117
// FBC variables
137
- f := & FBCContext {
118
+ f := & registry. FBCContext {
138
119
Package : labels [registrybundle .PackageLabel ],
139
120
Refs : []string {i .BundleImage },
140
121
ChannelEntry : declarativeconfig.ChannelEntry {
@@ -145,11 +126,11 @@ func (i *Install) setup(ctx context.Context) error {
145
126
if _ , hasChannelMetadata := labels [registrybundle .ChannelsLabel ]; hasChannelMetadata {
146
127
f .ChannelName = strings .Split (labels [registrybundle .ChannelsLabel ], "," )[0 ]
147
128
} else {
148
- f .ChannelName = defaultChannel
129
+ f .ChannelName = registry . DefaultChannel
149
130
}
150
131
151
132
// generate an fbc if an fbc specific label is found on the image or for a default index image.
152
- content , err := f . generateFBCContent (ctx , i .BundleImage , i .IndexImageCatalogCreator .IndexImage )
133
+ content , err := generateFBCContent (ctx , f , i .BundleImage , i .IndexImageCatalogCreator .IndexImage )
153
134
if err != nil {
154
135
return fmt .Errorf ("error generating File-Based Catalog with bundle %q: %v" , i .BundleImage , err )
155
136
}
@@ -169,10 +150,11 @@ func (i *Install) setup(ctx context.Context) error {
169
150
return nil
170
151
}
171
152
172
- func (f * FBCContext ) generateFBCContent (ctx context.Context , bundleImage , indexImage string ) (string , error ) {
153
+ // generateFBCContent creates a File-Based Catalog using the bundle image and index image from the run bundle command.
154
+ func generateFBCContent (ctx context.Context , f * registry.FBCContext , bundleImage , indexImage string ) (string , error ) {
173
155
log .Infof ("Creating a File-Based Catalog of the bundle %q" , bundleImage )
174
156
// generate a File-Based Catalog representation of the bundle image
175
- bundleDeclcfg , err := f .createFBC (ctx )
157
+ bundleDeclcfg , err := f .CreateFBC (ctx )
176
158
if err != nil {
177
159
return "" , fmt .Errorf ("error creating a File-Based Catalog with image %q: %v" , bundleImage , err )
178
160
}
@@ -192,28 +174,19 @@ func (f *FBCContext) generateFBCContent(ctx context.Context, bundleImage, indexI
192
174
}
193
175
}
194
176
195
- // validate the generated File-Based Catalog
196
- if err = validateFBC (declcfg ); err != nil {
197
- return "" , fmt .Errorf ("error validating the generated FBC: %v" , err )
198
- }
199
-
200
- // convert declarative config to string
201
- content , err := stringifyDecConfig (declcfg )
202
- if err != nil {
203
- return "" , fmt .Errorf ("error converting the declarative config to string: %v" , err )
204
- }
205
-
206
- if content == "" {
207
- return "" , errors .New ("file based catalog contents cannot be empty" )
177
+ // validate the declarative config and convert it to a string
178
+ var content string
179
+ if content , err = registry .ValidateAndStringify (declcfg ); err != nil {
180
+ return "" , fmt .Errorf ("error validating and converting the declarative config object to a string format: %v" , err )
208
181
}
209
182
210
183
log .Infof ("Generated a valid File-Based Catalog" )
211
184
212
185
return content , nil
213
186
}
214
187
215
- /// addBundleToIndexImage adds the bundle to an existing index image if the bundle is not already present in the index image.
216
- func addBundleToIndexImage (ctx context.Context , indexImage string , bundleDeclConfig bundleDeclcfg ) (* declarativeconfig.DeclarativeConfig , error ) {
188
+ // addBundleToIndexImage adds the bundle to an existing index image if the bundle is not already present in the index image.
189
+ func addBundleToIndexImage (ctx context.Context , indexImage string , bundleDeclConfig registry. BundleDeclcfg ) (* declarativeconfig.DeclarativeConfig , error ) {
217
190
log .Infof ("Rendering a File-Based Catalog of the Index Image %q" , indexImage )
218
191
log .SetOutput (ioutil .Discard )
219
192
render := action.Render {
@@ -257,67 +230,3 @@ func addBundleToIndexImage(ctx context.Context, indexImage string, bundleDeclCon
257
230
258
231
return imageDeclConfig , nil
259
232
}
260
-
261
- // createFBC generates an FBC by creating bundle, package and channel blobs.
262
- func (f * FBCContext ) createFBC (ctx context.Context ) (bundleDeclcfg , error ) {
263
- var bundleDC bundleDeclcfg
264
- // Rendering the bundle image into a declarative config format.
265
- log .SetOutput (ioutil .Discard )
266
- render := action.Render {
267
- Refs : f .Refs ,
268
- }
269
-
270
- // generate bundles by rendering the bundle objects.
271
- declcfg , err := render .Run (ctx )
272
- log .SetOutput (os .Stdout )
273
- if err != nil {
274
- return bundleDeclcfg {}, fmt .Errorf ("error rendering the bundle image: %v" , err )
275
- }
276
-
277
- // Ensuring a valid bundle size.
278
- if len (declcfg .Bundles ) != 1 {
279
- return bundleDeclcfg {}, fmt .Errorf ("bundle image should contain exactly one bundle blob" )
280
- }
281
-
282
- bundleDC .Bundle = declcfg .Bundles [0 ]
283
-
284
- // generate package.
285
- bundleDC .Package = declarativeconfig.Package {
286
- Schema : schemaPackage ,
287
- Name : f .Package ,
288
- DefaultChannel : f .ChannelName ,
289
- }
290
-
291
- // generate channel.
292
- bundleDC .Channel = declarativeconfig.Channel {
293
- Schema : schemaChannel ,
294
- Name : f .ChannelName ,
295
- Package : f .Package ,
296
- Entries : []declarativeconfig.ChannelEntry {f .ChannelEntry },
297
- }
298
-
299
- return bundleDC , nil
300
- }
301
-
302
- // stringifyDecConfig writes the generated declarative config to a string.
303
- func stringifyDecConfig (declcfg * declarativeconfig.DeclarativeConfig ) (string , error ) {
304
- var buf bytes.Buffer
305
- err := declarativeconfig .WriteYAML (* declcfg , & buf )
306
- if err != nil {
307
- return "" , fmt .Errorf ("error writing generated declarative config to JSON encoder: %v" , err )
308
- }
309
-
310
- return buf .String (), nil
311
- }
312
-
313
- // validateFBC converts the generated declarative config to a model and validates it.
314
- func validateFBC (declcfg * declarativeconfig.DeclarativeConfig ) error {
315
- // validates and converts declarative config to model
316
- _ , err := declarativeconfig .ConvertToModel (* declcfg )
317
- if err != nil {
318
- return fmt .Errorf ("error converting the declarative config to model: %v" , err )
319
-
320
- }
321
-
322
- return nil
323
- }
0 commit comments