Skip to content

WIP: FBC data model migrations #1679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alpha/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (i Init) Run() (*declcfg.Package, error) {
if iconType.MIME.Type != "image" {
return nil, fmt.Errorf("detected invalid type %q: not an image", iconType.MIME.Value)
}
pkg.Icon = &declcfg.Icon{
pkg.Icon = &declcfg.PackageIcon{ //nolint:staticcheck
Data: iconData,
MediaType: iconType.MIME.Value,
}
Expand Down
4 changes: 2 additions & 2 deletions alpha/action/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestInit(t *testing.T) {
},
expectPkg: &declcfg.Package{
Schema: "olm.package",
Icon: &declcfg.Icon{
Icon: &declcfg.PackageIcon{
Data: bytes.NewBufferString(svgIcon).Bytes(),
MediaType: "image/svg+xml",
},
Expand All @@ -93,7 +93,7 @@ func TestInit(t *testing.T) {
Name: "a",
DefaultChannel: "b",
Description: "c",
Icon: &declcfg.Icon{
Icon: &declcfg.PackageIcon{
Data: bytes.NewBufferString(svgIcon).Bytes(),
MediaType: "image/svg+xml",
},
Expand Down
32 changes: 32 additions & 0 deletions alpha/action/migrations/001_split_icon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package migrations

import (
"github.com/operator-framework/operator-registry/alpha/declcfg"
)

func splitIcon(cfg *declcfg.DeclarativeConfig) error {
splitIconFromPackage := func(p *declcfg.Package) error {
if p.Icon == nil { //nolint:staticcheck
return nil
}

// Make separate olm.icon object
cfg.Icons = append(cfg.Icons, declcfg.Icon{
Schema: declcfg.SchemaIcon,
Package: p.Name,
MediaType: p.Icon.MediaType, //nolint:staticcheck
Data: p.Icon.Data, //nolint:staticcheck
})

// Delete original icon from olm.package object
p.Icon = nil //nolint:staticcheck
return nil
}

for i := range cfg.Packages {
if err := splitIconFromPackage(&cfg.Packages[i]); err != nil {
return err
}
}
return nil
}
44 changes: 44 additions & 0 deletions alpha/action/migrations/002_promote_bundle_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package migrations

import (
"encoding/json"
"slices"

"github.com/Masterminds/semver/v3"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"
)

func promoteBundleVersion(cfg *declcfg.DeclarativeConfig) error {
promoteVersion := func(b *declcfg.Bundle) error {
// Promote the version from the olm.package property to the bundle field.
for _, p := range b.Properties {
if p.Type != property.TypePackage {
continue
}
var pkg property.Package
if err := json.Unmarshal(p.Value, &pkg); err != nil {
return err
}
version, err := semver.StrictNewVersion(pkg.Version)
if err != nil {
return err
}
b.Version = version
}

// Delete the olm.package properties
b.Properties = slices.DeleteFunc(b.Properties, func(p property.Property) bool {
return p.Type == property.TypePackage
})
return nil
}

for i := range cfg.Bundles {
if err := promoteVersion(&cfg.Bundles[i]); err != nil {
return err
}
}
return nil
}
155 changes: 155 additions & 0 deletions alpha/action/migrations/003_promote_package_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package migrations

import (
"encoding/json"
"slices"
"strings"
"unicode"
"unicode/utf8"

"github.com/Masterminds/semver/v3"

"github.com/operator-framework/api/pkg/operators/v1alpha1"

"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"
)

func promotePackageMetadata(cfg *declcfg.DeclarativeConfig) error {
metadataByPackage := map[string]promotedMetadata{}
for i := range cfg.Bundles {
b := &cfg.Bundles[i]

csvMetadata, csvMetadataIdx, err := getCsvMetadata(b)
if err != nil {
return err
}

// Skip objects that have no olm.csv.metadata property
if csvMetadata == nil {
continue
}

// Keep track of the metadata from the highest versioned bundle from each package.
cur, ok := metadataByPackage[b.Package]
if !ok || compareRegistryV1Semver(cur.version, b.Version) < 0 {
metadataByPackage[b.Package] = promotedCSVMetadata(b.Version, csvMetadata)
}

// Delete the package-level metadata from the olm.csv.metadata object and
// update the bundle properties to use the new slimmed-down revision of it.
csvMetadata.DisplayName = ""
delete(csvMetadata.Annotations, "description")
csvMetadata.Provider = v1alpha1.AppLink{}
csvMetadata.Maintainers = nil
csvMetadata.Links = nil
csvMetadata.Keywords = nil

newCSVMetadata, err := json.Marshal(csvMetadata)
if err != nil {
return err
}
b.Properties[csvMetadataIdx] = property.Property{
Type: property.TypeCSVMetadata,
Value: newCSVMetadata,
}
}

// Update each olm.package object to include the metadata we extracted from
// bundles in the first loop.
for i := range cfg.Packages {
pkg := &cfg.Packages[i]
metadata, ok := metadataByPackage[pkg.Name]
if !ok {
continue
}
pkg.DisplayName = metadata.displayName
pkg.ShortDescription = shortenDescription(metadata.shortDescription)
if metadata.provider.Name != "" || metadata.provider.URL != "" {
pkg.Provider = &metadata.provider
}
pkg.Maintainers = metadata.maintainers
pkg.Links = metadata.links
pkg.Keywords = slices.DeleteFunc(metadata.keywords, func(s string) bool {
// Delete keywords that are empty strings
return s == ""
})
}
return nil
}

func getCsvMetadata(b *declcfg.Bundle) (*property.CSVMetadata, int, error) {
for i, p := range b.Properties {
if p.Type != property.TypeCSVMetadata {
continue
}
var csvMetadata property.CSVMetadata
if err := json.Unmarshal(p.Value, &csvMetadata); err != nil {
return nil, -1, err
}
return &csvMetadata, i, nil
}
return nil, -1, nil
}

func compareRegistryV1Semver(a, b *semver.Version) int {
if v := a.Compare(b); v != 0 {
return v
}
aPre := semver.New(0, 0, 0, a.Metadata(), "")
bPre := semver.New(0, 0, 0, b.Metadata(), "")
return aPre.Compare(bPre)
}

type promotedMetadata struct {
version *semver.Version

displayName string
shortDescription string
provider v1alpha1.AppLink
maintainers []v1alpha1.Maintainer
links []v1alpha1.AppLink
keywords []string
}

func promotedCSVMetadata(version *semver.Version, metadata *property.CSVMetadata) promotedMetadata {
return promotedMetadata{
version: version,
displayName: metadata.DisplayName,
shortDescription: metadata.Annotations["description"],
provider: metadata.Provider,
maintainers: metadata.Maintainers,
links: metadata.Links,
keywords: metadata.Keywords,
}
}

func shortenDescription(input string) string {
const maxLen = 256
input = strings.TrimSpace(input)

// If the input is already under the limit return it.
if utf8.RuneCountInString(input) <= maxLen {
return input
}

// Chop off everything after the first paragraph.
if idx := strings.Index(input, "\n\n"); idx != -1 {
input = strings.TrimSpace(input[:idx])
}

// If we're _now_ under the limit, return the first paragraph.
if utf8.RuneCountInString(input) <= maxLen {
return input
}

// If the first paragraph is still over the limit, we'll have to truncate.
// We'll truncate at the last word boundary that still allows an ellipsis
// to fit within the maximum length. But if there are no word boundaries
// (veeeeery unlikely), we'll hard truncate mid-word.
input = input[:maxLen-3]
if truncatedIdx := strings.LastIndexFunc(input, unicode.IsSpace); truncatedIdx != -1 {
return input[:truncatedIdx] + "..."
}
return input + "..."
}
3 changes: 3 additions & 0 deletions alpha/action/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type Migrations struct {
var allMigrations = []Migration{
newMigration(NoMigrations, "do nothing", func(_ *declcfg.DeclarativeConfig) error { return nil }),
newMigration("bundle-object-to-csv-metadata", `migrates bundles' "olm.bundle.object" to "olm.csv.metadata"`, bundleObjectToCSVMetadata),
newMigration("split-icon", `split package icon out into separate "olm.icon" blob`, splitIcon),
newMigration("promote-bundle-version", `promote bundle version into first-class bundle field, remove olm.package properties`, promoteBundleVersion),
newMigration("promote-package-metadata", `promote package metadata from "olm.csv.metadata" properties to "olm.package" blob`, promotePackageMetadata),
}

func NewMigrations(name string) (*Migrations, error) {
Expand Down
3 changes: 3 additions & 0 deletions alpha/action/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func TestMigrations(t *testing.T) {
}
return nil
},
MigrationToken("split-icon"): func(d *declcfg.DeclarativeConfig) error { return nil },
MigrationToken("promote-bundle-version"): func(d *declcfg.DeclarativeConfig) error { return nil },
MigrationToken("promote-package-metadata"): func(d *declcfg.DeclarativeConfig) error { return nil },
}

tests := []struct {
Expand Down
41 changes: 33 additions & 8 deletions alpha/declcfg/declcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,62 @@ import (
"errors"
"fmt"

"github.com/Masterminds/semver/v3"
"golang.org/x/text/cases"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/operator-framework/api/pkg/operators/v1alpha1"

"github.com/operator-framework/operator-registry/alpha/property"
prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"
)

const (
SchemaPackage = "olm.package"
SchemaIcon = "olm.icon"
SchemaChannel = "olm.channel"
SchemaBundle = "olm.bundle"
SchemaDeprecation = "olm.deprecations"
)

type DeclarativeConfig struct {
Packages []Package
Icons []Icon
Channels []Channel
Bundles []Bundle
Deprecations []Deprecation
Others []Meta
}

type Package struct {
Schema string `json:"schema"`
Name string `json:"name"`
DefaultChannel string `json:"defaultChannel"`
Icon *Icon `json:"icon,omitempty"`
Description string `json:"description,omitempty"`
Properties []property.Property `json:"properties,omitempty" hash:"set"`
Schema string `json:"schema"`
Name string `json:"name"`
DisplayName string `json:"displayName,omitempty"`
ShortDescription string `json:"shortDescription,omitempty"`
Provider *v1alpha1.AppLink `json:"provider,omitempty"`
Maintainers []v1alpha1.Maintainer `json:"maintainers,omitempty"`
Links []v1alpha1.AppLink `json:"links,omitempty"`
Keywords []string `json:"keywords,omitempty"`
DefaultChannel string `json:"defaultChannel"`

// Deprecated: It is no longer recommended to embed an icon in the package.
// Instead, use separate a Icon item alongside the Package.
Icon *PackageIcon `json:"icon,omitempty"`

Description string `json:"description,omitempty"`
Properties []property.Property `json:"properties,omitempty" hash:"set"`
}

type Icon struct {
Schema string `json:"schema"`
Package string `json:"package"`

MediaType string `json:"mediaType"`
Data []byte `json:"data"`
}

type PackageIcon struct {
Data []byte `json:"base64data"`
MediaType string `json:"mediatype"`
}
Expand Down Expand Up @@ -69,8 +92,9 @@ type ChannelEntry struct {
// evaluation in bundlesEqual().
type Bundle struct {
Schema string `json:"schema"`
Name string `json:"name,omitempty"`
Package string `json:"package,omitempty"`
Name string `json:"name"`
Package string `json:"package"`
Version *semver.Version `json:"version,omitempty"`
Image string `json:"image"`
Properties []property.Property `json:"properties,omitempty" hash:"set"`
RelatedImages []RelatedImage `json:"relatedImages,omitempty" hash:"set"`
Expand Down Expand Up @@ -201,6 +225,7 @@ func extractUniqueMetaKeys(blobMap map[string]any, m *Meta) error {

func (destination *DeclarativeConfig) Merge(src *DeclarativeConfig) {
destination.Packages = append(destination.Packages, src.Packages...)
destination.Icons = append(destination.Icons, src.Icons...)
destination.Channels = append(destination.Channels, src.Channels...)
destination.Bundles = append(destination.Bundles, src.Bundles...)
destination.Others = append(destination.Others, src.Others...)
Expand Down
Loading
Loading