Skip to content

Commit

Permalink
wip: olm.package.v2, olm.bundle.v2, olm.package.icon
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <[email protected]>
  • Loading branch information
joelanford committed Dec 12, 2024
1 parent c8307ca commit 1c1b37c
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 80 deletions.
140 changes: 140 additions & 0 deletions alpha/action/migrations/001_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package migrations

import (
"encoding/json"
"fmt"

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

func v2(cfg *declcfg.DeclarativeConfig) error {
uniqueBundles := map[string]*model.Bundle{}
m, err := declcfg.ConvertToModel(*cfg)
if err != nil {
return err
}
cfg.Packages = nil
cfg.Bundles = nil

for _, pkg := range m {
head, err := pkg.DefaultChannel.Head()
if err != nil {
return err
}

if head.PropertiesP == nil || len(head.PropertiesP.CSVMetadatas) == 0 {
return fmt.Errorf("no CSV metadata defined for package %s", pkg.Name)
}
csvMetadata := head.PropertiesP.CSVMetadatas[0]

packageAnnotations := map[string]string{
"operators.openshift.io/capabilities": csvMetadata.Annotations["capabilities"],
"operators.openshift.io/categories": csvMetadata.Annotations["categories"],
"operators.openshift.io/infrastructure-features": csvMetadata.Annotations["operators.openshift.io/infrastructure-features"],
"operators.openshift.io/valid-subscription": csvMetadata.Annotations["operators.openshift.io/valid-subscription"],
}

v2p := declcfg.PackageV2{
Schema: "olm.package.v2",
Package: pkg.Name,
DisplayName: csvMetadata.DisplayName,
ShortDescription: csvMetadata.Annotations["description"],
LongDescription: csvMetadata.Description,
Keywords: csvMetadata.Keywords,
Links: csvMetadata.Links,
Provider: csvMetadata.Provider,
Maintainers: csvMetadata.Maintainers,
Annotations: packageAnnotations,
}
cfg.PackageV2s = append(cfg.PackageV2s, v2p)

if pkg.Icon != nil {
v2i := declcfg.PackageIcon{
Schema: "olm.package.icon",
Package: pkg.Name,
Data: pkg.Icon.Data,
MediaType: pkg.Icon.MediaType,
}
cfg.PackageIcons = append(cfg.PackageIcons, v2i)
}
for _, ch := range pkg.Channels {
for _, b := range ch.Bundles {
uniqueBundles[b.Name] = b
}
}
}

for _, b := range uniqueBundles {
v2b := declcfg.BundleV2{
Schema: "olm.bundle.v2",
Package: b.Package.Name,
Name: b.Name,

Version: b.Version.String(),
Release: 0,
Reference: fmt.Sprintf("docker://%s", b.Image),
RelatedReferences: make([]string, 0, len(b.RelatedImages)),

Constraints: map[string][]json.RawMessage{},
Properties: map[string][]json.RawMessage{},
}

for _, ri := range b.RelatedImages {
v2b.RelatedReferences = append(v2b.RelatedReferences, fmt.Sprintf("docker://%s", ri.Image))
}

for _, p := range b.Properties {
if p.Type == property.TypePackage || p.Type == property.TypeCSVMetadata || p.Type == property.TypeBundleObject {
continue
}
if isContraint(p) {
v2b.Constraints[p.Type] = append(v2b.Constraints[p.Type], p.Value)
} else {
v2b.Properties[p.Type] = append(v2b.Properties[p.Type], p.Value)
}
}

if b.PropertiesP != nil && len(b.PropertiesP.CSVMetadatas) > 0 {
csvMetadata := b.PropertiesP.CSVMetadatas[0]

desiredAnnotations := map[string]string{
"createdAt": "operators.openshift.io/creationTimestamp",
"repository": "operators.openshift.io/repository",
"support": "operators.openshift.io/support",
"containerImage": "operators.openshift.io/image",
}

bundleAnnotations := map[string]string{}
for fromKey, toKey := range desiredAnnotations {
if value, ok := csvMetadata.Annotations[fromKey]; ok {
bundleAnnotations[toKey] = value
}
}
v2b.Annotations = bundleAnnotations
}
cfg.BundleV2s = append(cfg.BundleV2s, v2b)
}

for depIdx := range cfg.Deprecations {
for entryIdx := range cfg.Deprecations[depIdx].Entries {
e := &cfg.Deprecations[depIdx].Entries[entryIdx]
switch e.Reference.Schema {
case declcfg.SchemaPackage:
e.Reference.Schema = declcfg.SchemaPackageV2
case declcfg.SchemaBundle:
e.Reference.Schema = declcfg.SchemaBundleV2
}
}
}
return nil
}

func isContraint(p property.Property) bool {
switch p.Type {
case property.TypeConstraint, property.TypeGVKRequired, property.TypePackageRequired:
return true
}
return false
}
1 change: 1 addition & 0 deletions alpha/action/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ 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("v2", `migrate catalog to olm.package.v2, olm.bundle.v2, and olm.package.icon`, v2),
}

func NewMigrations(name string) (*Migrations, error) {
Expand Down
57 changes: 53 additions & 4 deletions alpha/declcfg/declcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@ import (
"errors"
"fmt"

prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"

"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"
SchemaChannel = "olm.channel"
SchemaBundle = "olm.bundle"
SchemaDeprecation = "olm.deprecations"
SchemaPackageV2 = "olm.package.v2"
SchemaBundleV2 = "olm.bundle.v2"
SchemaPackageIcon = "olm.package.icon"
)

type DeclarativeConfig struct {
Packages []Package
Channels []Channel
Bundles []Bundle
PackageV2s []PackageV2
PackageIcons []PackageIcon

Channels []Channel
Bundles []Bundle
BundleV2s []BundleV2

Deprecations []Deprecation
Others []Meta
}
Expand Down Expand Up @@ -92,6 +101,43 @@ type RelatedImage struct {
Image string `json:"image"`
}

type PackageV2 struct {
Schema string `json:"schema"`
Package string `json:"package"`
Annotations map[string]string `json:"annotations,omitempty"`

DisplayName string `json:"displayName,omitempty"`
ShortDescription string `json:"shortDescription,omitempty"`
LongDescription string `json:"longDescription,omitempty"`
Keywords []string `json:"keywords,omitempty"`
Links []v1alpha1.AppLink `json:"links,omitempty"`
Provider v1alpha1.AppLink `json:"provider,omitempty"`
Maintainers []v1alpha1.Maintainer `json:"maintainers,omitempty"`
}

type PackageIcon struct {
Schema string `json:"schema"`
Package string `json:"package"`
Data []byte `json:"data"`
MediaType string `json:"mediaType"`
}

type BundleV2 struct {
Schema string `json:"schema"`
Package string `json:"package"`
Name string `json:"name"`
Annotations map[string]string `json:"annotations,omitempty"`

Version string `json:"version"`
Release uint32 `json:"release"`

Reference string `json:"ref"`
RelatedReferences []string `json:"relatedReferences,omitempty"`

Properties map[string][]json.RawMessage `json:"properties,omitempty"`
Constraints map[string][]json.RawMessage `json:"constraints,omitempty"`
}

type Deprecation struct {
Schema string `json:"schema"`
Package string `json:"package"`
Expand Down Expand Up @@ -202,8 +248,11 @@ func extractUniqueMetaKeys(blobMap map[string]any, m *Meta) error {

func (destination *DeclarativeConfig) Merge(src *DeclarativeConfig) {
destination.Packages = append(destination.Packages, src.Packages...)
destination.PackageV2s = append(destination.PackageV2s, src.PackageV2s...)
destination.PackageIcons = append(destination.PackageIcons, src.PackageIcons...)
destination.Channels = append(destination.Channels, src.Channels...)
destination.Bundles = append(destination.Bundles, src.Bundles...)
destination.BundleV2s = append(destination.BundleV2s, src.BundleV2s...)
destination.Others = append(destination.Others, src.Others...)
destination.Deprecations = append(destination.Deprecations, src.Deprecations...)
}
Loading

0 comments on commit 1c1b37c

Please sign in to comment.