Skip to content

Commit 1c1b37c

Browse files
committed
wip: olm.package.v2, olm.bundle.v2, olm.package.icon
Signed-off-by: Joe Lanford <[email protected]>
1 parent c8307ca commit 1c1b37c

File tree

4 files changed

+264
-80
lines changed

4 files changed

+264
-80
lines changed

alpha/action/migrations/001_v2.go

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package migrations
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/operator-framework/operator-registry/alpha/declcfg"
8+
"github.com/operator-framework/operator-registry/alpha/model"
9+
"github.com/operator-framework/operator-registry/alpha/property"
10+
)
11+
12+
func v2(cfg *declcfg.DeclarativeConfig) error {
13+
uniqueBundles := map[string]*model.Bundle{}
14+
m, err := declcfg.ConvertToModel(*cfg)
15+
if err != nil {
16+
return err
17+
}
18+
cfg.Packages = nil
19+
cfg.Bundles = nil
20+
21+
for _, pkg := range m {
22+
head, err := pkg.DefaultChannel.Head()
23+
if err != nil {
24+
return err
25+
}
26+
27+
if head.PropertiesP == nil || len(head.PropertiesP.CSVMetadatas) == 0 {
28+
return fmt.Errorf("no CSV metadata defined for package %s", pkg.Name)
29+
}
30+
csvMetadata := head.PropertiesP.CSVMetadatas[0]
31+
32+
packageAnnotations := map[string]string{
33+
"operators.openshift.io/capabilities": csvMetadata.Annotations["capabilities"],
34+
"operators.openshift.io/categories": csvMetadata.Annotations["categories"],
35+
"operators.openshift.io/infrastructure-features": csvMetadata.Annotations["operators.openshift.io/infrastructure-features"],
36+
"operators.openshift.io/valid-subscription": csvMetadata.Annotations["operators.openshift.io/valid-subscription"],
37+
}
38+
39+
v2p := declcfg.PackageV2{
40+
Schema: "olm.package.v2",
41+
Package: pkg.Name,
42+
DisplayName: csvMetadata.DisplayName,
43+
ShortDescription: csvMetadata.Annotations["description"],
44+
LongDescription: csvMetadata.Description,
45+
Keywords: csvMetadata.Keywords,
46+
Links: csvMetadata.Links,
47+
Provider: csvMetadata.Provider,
48+
Maintainers: csvMetadata.Maintainers,
49+
Annotations: packageAnnotations,
50+
}
51+
cfg.PackageV2s = append(cfg.PackageV2s, v2p)
52+
53+
if pkg.Icon != nil {
54+
v2i := declcfg.PackageIcon{
55+
Schema: "olm.package.icon",
56+
Package: pkg.Name,
57+
Data: pkg.Icon.Data,
58+
MediaType: pkg.Icon.MediaType,
59+
}
60+
cfg.PackageIcons = append(cfg.PackageIcons, v2i)
61+
}
62+
for _, ch := range pkg.Channels {
63+
for _, b := range ch.Bundles {
64+
uniqueBundles[b.Name] = b
65+
}
66+
}
67+
}
68+
69+
for _, b := range uniqueBundles {
70+
v2b := declcfg.BundleV2{
71+
Schema: "olm.bundle.v2",
72+
Package: b.Package.Name,
73+
Name: b.Name,
74+
75+
Version: b.Version.String(),
76+
Release: 0,
77+
Reference: fmt.Sprintf("docker://%s", b.Image),
78+
RelatedReferences: make([]string, 0, len(b.RelatedImages)),
79+
80+
Constraints: map[string][]json.RawMessage{},
81+
Properties: map[string][]json.RawMessage{},
82+
}
83+
84+
for _, ri := range b.RelatedImages {
85+
v2b.RelatedReferences = append(v2b.RelatedReferences, fmt.Sprintf("docker://%s", ri.Image))
86+
}
87+
88+
for _, p := range b.Properties {
89+
if p.Type == property.TypePackage || p.Type == property.TypeCSVMetadata || p.Type == property.TypeBundleObject {
90+
continue
91+
}
92+
if isContraint(p) {
93+
v2b.Constraints[p.Type] = append(v2b.Constraints[p.Type], p.Value)
94+
} else {
95+
v2b.Properties[p.Type] = append(v2b.Properties[p.Type], p.Value)
96+
}
97+
}
98+
99+
if b.PropertiesP != nil && len(b.PropertiesP.CSVMetadatas) > 0 {
100+
csvMetadata := b.PropertiesP.CSVMetadatas[0]
101+
102+
desiredAnnotations := map[string]string{
103+
"createdAt": "operators.openshift.io/creationTimestamp",
104+
"repository": "operators.openshift.io/repository",
105+
"support": "operators.openshift.io/support",
106+
"containerImage": "operators.openshift.io/image",
107+
}
108+
109+
bundleAnnotations := map[string]string{}
110+
for fromKey, toKey := range desiredAnnotations {
111+
if value, ok := csvMetadata.Annotations[fromKey]; ok {
112+
bundleAnnotations[toKey] = value
113+
}
114+
}
115+
v2b.Annotations = bundleAnnotations
116+
}
117+
cfg.BundleV2s = append(cfg.BundleV2s, v2b)
118+
}
119+
120+
for depIdx := range cfg.Deprecations {
121+
for entryIdx := range cfg.Deprecations[depIdx].Entries {
122+
e := &cfg.Deprecations[depIdx].Entries[entryIdx]
123+
switch e.Reference.Schema {
124+
case declcfg.SchemaPackage:
125+
e.Reference.Schema = declcfg.SchemaPackageV2
126+
case declcfg.SchemaBundle:
127+
e.Reference.Schema = declcfg.SchemaBundleV2
128+
}
129+
}
130+
}
131+
return nil
132+
}
133+
134+
func isContraint(p property.Property) bool {
135+
switch p.Type {
136+
case property.TypeConstraint, property.TypeGVKRequired, property.TypePackageRequired:
137+
return true
138+
}
139+
return false
140+
}

alpha/action/migrations/migrations.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Migrations struct {
5454
var allMigrations = []Migration{
5555
newMigration(NoMigrations, "do nothing", func(_ *declcfg.DeclarativeConfig) error { return nil }),
5656
newMigration("bundle-object-to-csv-metadata", `migrates bundles' "olm.bundle.object" to "olm.csv.metadata"`, bundleObjectToCSVMetadata),
57+
newMigration("v2", `migrate catalog to olm.package.v2, olm.bundle.v2, and olm.package.icon`, v2),
5758
}
5859

5960
func NewMigrations(name string) (*Migrations, error) {

alpha/declcfg/declcfg.go

+53-4
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,35 @@ import (
66
"errors"
77
"fmt"
88

9-
prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"
10-
119
"golang.org/x/text/cases"
1210
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1311
"k8s.io/apimachinery/pkg/util/sets"
1412

13+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
14+
1515
"github.com/operator-framework/operator-registry/alpha/property"
16+
prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"
1617
)
1718

1819
const (
1920
SchemaPackage = "olm.package"
2021
SchemaChannel = "olm.channel"
2122
SchemaBundle = "olm.bundle"
2223
SchemaDeprecation = "olm.deprecations"
24+
SchemaPackageV2 = "olm.package.v2"
25+
SchemaBundleV2 = "olm.bundle.v2"
26+
SchemaPackageIcon = "olm.package.icon"
2327
)
2428

2529
type DeclarativeConfig struct {
2630
Packages []Package
27-
Channels []Channel
28-
Bundles []Bundle
31+
PackageV2s []PackageV2
32+
PackageIcons []PackageIcon
33+
34+
Channels []Channel
35+
Bundles []Bundle
36+
BundleV2s []BundleV2
37+
2938
Deprecations []Deprecation
3039
Others []Meta
3140
}
@@ -92,6 +101,43 @@ type RelatedImage struct {
92101
Image string `json:"image"`
93102
}
94103

104+
type PackageV2 struct {
105+
Schema string `json:"schema"`
106+
Package string `json:"package"`
107+
Annotations map[string]string `json:"annotations,omitempty"`
108+
109+
DisplayName string `json:"displayName,omitempty"`
110+
ShortDescription string `json:"shortDescription,omitempty"`
111+
LongDescription string `json:"longDescription,omitempty"`
112+
Keywords []string `json:"keywords,omitempty"`
113+
Links []v1alpha1.AppLink `json:"links,omitempty"`
114+
Provider v1alpha1.AppLink `json:"provider,omitempty"`
115+
Maintainers []v1alpha1.Maintainer `json:"maintainers,omitempty"`
116+
}
117+
118+
type PackageIcon struct {
119+
Schema string `json:"schema"`
120+
Package string `json:"package"`
121+
Data []byte `json:"data"`
122+
MediaType string `json:"mediaType"`
123+
}
124+
125+
type BundleV2 struct {
126+
Schema string `json:"schema"`
127+
Package string `json:"package"`
128+
Name string `json:"name"`
129+
Annotations map[string]string `json:"annotations,omitempty"`
130+
131+
Version string `json:"version"`
132+
Release uint32 `json:"release"`
133+
134+
Reference string `json:"ref"`
135+
RelatedReferences []string `json:"relatedReferences,omitempty"`
136+
137+
Properties map[string][]json.RawMessage `json:"properties,omitempty"`
138+
Constraints map[string][]json.RawMessage `json:"constraints,omitempty"`
139+
}
140+
95141
type Deprecation struct {
96142
Schema string `json:"schema"`
97143
Package string `json:"package"`
@@ -202,8 +248,11 @@ func extractUniqueMetaKeys(blobMap map[string]any, m *Meta) error {
202248

203249
func (destination *DeclarativeConfig) Merge(src *DeclarativeConfig) {
204250
destination.Packages = append(destination.Packages, src.Packages...)
251+
destination.PackageV2s = append(destination.PackageV2s, src.PackageV2s...)
252+
destination.PackageIcons = append(destination.PackageIcons, src.PackageIcons...)
205253
destination.Channels = append(destination.Channels, src.Channels...)
206254
destination.Bundles = append(destination.Bundles, src.Bundles...)
255+
destination.BundleV2s = append(destination.BundleV2s, src.BundleV2s...)
207256
destination.Others = append(destination.Others, src.Others...)
208257
destination.Deprecations = append(destination.Deprecations, src.Deprecations...)
209258
}

0 commit comments

Comments
 (0)