Skip to content

Commit ceef70d

Browse files
VenkatRamarajurashmigottipati
authored andcommitted
Implement File-Based Catalog (FBC) support for run bundle-upgrade (#5769)
* run bundle-upgrade implementation Signed-off-by: Venkat Ramaraju <[email protected]> Signed-off-by: rashmigottipati <[email protected]>
1 parent c57aa5a commit ceef70d

File tree

5 files changed

+426
-124
lines changed

5 files changed

+426
-124
lines changed

internal/olm/operator/bundle/install.go

Lines changed: 15 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
package bundle
1616

1717
import (
18-
"bytes"
1918
"context"
20-
"errors"
2119
"fmt"
2220
"io/ioutil"
2321
"os"
@@ -28,7 +26,6 @@ import (
2826

2927
"github.com/operator-framework/api/pkg/operators/v1alpha1"
3028
"github.com/operator-framework/operator-registry/alpha/action"
31-
"github.com/operator-framework/operator-registry/alpha/declcfg"
3229
declarativeconfig "github.com/operator-framework/operator-registry/alpha/declcfg"
3330
"github.com/operator-framework/operator-registry/pkg/containertools"
3431
registrybundle "github.com/operator-framework/operator-registry/pkg/lib/bundle"
@@ -37,18 +34,6 @@ import (
3734
registryutil "github.com/operator-framework/operator-sdk/internal/registry"
3835
)
3936

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-
5237
type Install struct {
5338
BundleImage string
5439

@@ -58,13 +43,6 @@ type Install struct {
5843
cfg *operator.Configuration
5944
}
6045

61-
type FBCContext struct {
62-
Package string
63-
ChannelName string
64-
Refs []string
65-
ChannelEntry declarativeconfig.ChannelEntry
66-
}
67-
6846
func NewInstall(cfg *operator.Configuration) Install {
6947
i := Install{
7048
OperatorInstaller: registry.NewOperatorInstaller(cfg),
@@ -126,6 +104,9 @@ func (i *Install) setup(ctx context.Context) error {
126104
// set the field to true if FBC label is on the image or for a default index image.
127105
if _, hasFBCLabel := catalogLabels[containertools.ConfigsLocationLabel]; hasFBCLabel || i.IndexImageCatalogCreator.IndexImage == registry.DefaultIndexImage {
128106
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+
}
129110
} else {
130111
// index image is of the SQLite index format.
131112
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 {
134115

135116
if i.IndexImageCatalogCreator.HasFBCLabel {
136117
// FBC variables
137-
f := &FBCContext{
118+
f := &registry.FBCContext{
138119
Package: labels[registrybundle.PackageLabel],
139120
Refs: []string{i.BundleImage},
140121
ChannelEntry: declarativeconfig.ChannelEntry{
@@ -145,11 +126,11 @@ func (i *Install) setup(ctx context.Context) error {
145126
if _, hasChannelMetadata := labels[registrybundle.ChannelsLabel]; hasChannelMetadata {
146127
f.ChannelName = strings.Split(labels[registrybundle.ChannelsLabel], ",")[0]
147128
} else {
148-
f.ChannelName = defaultChannel
129+
f.ChannelName = registry.DefaultChannel
149130
}
150131

151132
// 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)
153134
if err != nil {
154135
return fmt.Errorf("error generating File-Based Catalog with bundle %q: %v", i.BundleImage, err)
155136
}
@@ -169,10 +150,11 @@ func (i *Install) setup(ctx context.Context) error {
169150
return nil
170151
}
171152

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) {
173155
log.Infof("Creating a File-Based Catalog of the bundle %q", bundleImage)
174156
// generate a File-Based Catalog representation of the bundle image
175-
bundleDeclcfg, err := f.createFBC(ctx)
157+
bundleDeclcfg, err := f.CreateFBC(ctx)
176158
if err != nil {
177159
return "", fmt.Errorf("error creating a File-Based Catalog with image %q: %v", bundleImage, err)
178160
}
@@ -192,28 +174,19 @@ func (f *FBCContext) generateFBCContent(ctx context.Context, bundleImage, indexI
192174
}
193175
}
194176

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)
208181
}
209182

210183
log.Infof("Generated a valid File-Based Catalog")
211184

212185
return content, nil
213186
}
214187

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) {
217190
log.Infof("Rendering a File-Based Catalog of the Index Image %q", indexImage)
218191
log.SetOutput(ioutil.Discard)
219192
render := action.Render{
@@ -257,67 +230,3 @@ func addBundleToIndexImage(ctx context.Context, indexImage string, bundleDeclCon
257230

258231
return imageDeclConfig, nil
259232
}
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-
}

internal/olm/operator/bundleupgrade/upgrade.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020

2121
"github.com/operator-framework/api/pkg/operators/v1alpha1"
2222
registrybundle "github.com/operator-framework/operator-registry/pkg/lib/bundle"
23-
"github.com/spf13/pflag"
24-
2523
"github.com/operator-framework/operator-sdk/internal/olm/operator"
2624
"github.com/operator-framework/operator-sdk/internal/olm/operator/registry"
25+
"github.com/spf13/pflag"
2726
)
2827

2928
type Upgrade struct {
@@ -87,5 +86,11 @@ func (u *Upgrade) setup(ctx context.Context) error {
8786
u.IndexImageCatalogCreator.BundleImage = u.BundleImage
8887
u.IndexImageCatalogCreator.IndexImage = registry.DefaultIndexImage
8988

89+
if _, hasChannelMetadata := labels[registrybundle.ChannelsLabel]; hasChannelMetadata {
90+
u.IndexImageCatalogCreator.ChannelName = strings.Split(labels[registrybundle.ChannelsLabel], ",")[0]
91+
} else {
92+
u.IndexImageCatalogCreator.ChannelName = registry.DefaultChannel
93+
}
94+
9095
return nil
9196
}

internal/olm/operator/registry/catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ type CatalogCreator interface {
2525
}
2626

2727
type CatalogUpdater interface {
28-
UpdateCatalog(ctx context.Context, cs *v1alpha1.CatalogSource) error
28+
UpdateCatalog(ctx context.Context, cs *v1alpha1.CatalogSource, subscription *v1alpha1.Subscription) error
2929
}

0 commit comments

Comments
 (0)