Skip to content

Commit 8840220

Browse files
committed
fbc: promote package-level metdata from olm.csv.metadata to olm.package blob
Signed-off-by: Joe Lanford <[email protected]>
1 parent 9ac8e60 commit 8840220

File tree

6 files changed

+154
-20
lines changed

6 files changed

+154
-20
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package migrations
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/Masterminds/semver/v3"
7+
8+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
9+
10+
"github.com/operator-framework/operator-registry/alpha/declcfg"
11+
"github.com/operator-framework/operator-registry/alpha/property"
12+
)
13+
14+
func promotePackageMetadata(cfg *declcfg.DeclarativeConfig) error {
15+
metadataByPackage := map[string]promotedMetadata{}
16+
for i := range cfg.Bundles {
17+
b := &cfg.Bundles[i]
18+
19+
csvMetadata, csvMetadataIdx, err := getCsvMetadata(b)
20+
if err != nil {
21+
return err
22+
}
23+
if csvMetadata == nil {
24+
continue
25+
}
26+
27+
cur, ok := metadataByPackage[b.Package]
28+
if !ok || compareRegistryV1Semver(cur.version, b.Version) < 0 {
29+
metadataByPackage[b.Package] = promotedCSVMetadata(b.Version, csvMetadata)
30+
}
31+
32+
csvMetadata.DisplayName = ""
33+
delete(csvMetadata.Annotations, "description")
34+
csvMetadata.Provider = v1alpha1.AppLink{}
35+
csvMetadata.Maintainers = nil
36+
csvMetadata.Links = nil
37+
csvMetadata.Keywords = nil
38+
39+
newCSVMetadata, err := json.Marshal(csvMetadata)
40+
if err != nil {
41+
return err
42+
}
43+
b.Properties[csvMetadataIdx] = property.Property{
44+
Type: property.TypeCSVMetadata,
45+
Value: newCSVMetadata,
46+
}
47+
}
48+
49+
for i := range cfg.Packages {
50+
pkg := &cfg.Packages[i]
51+
metadata, ok := metadataByPackage[pkg.Name]
52+
if !ok {
53+
continue
54+
}
55+
pkg.DisplayName = metadata.displayName
56+
pkg.ShortDescription = metadata.shortDescription
57+
pkg.Provider = metadata.provider
58+
pkg.Maintainers = metadata.maintainers
59+
pkg.Links = metadata.links
60+
pkg.Keywords = metadata.keywords
61+
}
62+
return nil
63+
}
64+
65+
func getCsvMetadata(b *declcfg.Bundle) (*property.CSVMetadata, int, error) {
66+
for i, p := range b.Properties {
67+
if p.Type != property.TypeCSVMetadata {
68+
continue
69+
}
70+
var csvMetadata property.CSVMetadata
71+
if err := json.Unmarshal(p.Value, &csvMetadata); err != nil {
72+
return nil, -1, err
73+
}
74+
return &csvMetadata, i, nil
75+
}
76+
return nil, -1, nil
77+
}
78+
79+
func compareRegistryV1Semver(a, b *semver.Version) int {
80+
if v := a.Compare(b); v != 0 {
81+
return v
82+
}
83+
aPre := semver.New(0, 0, 0, a.Metadata(), "")
84+
bPre := semver.New(0, 0, 0, b.Metadata(), "")
85+
return aPre.Compare(bPre)
86+
}
87+
88+
type promotedMetadata struct {
89+
version *semver.Version
90+
91+
displayName string
92+
shortDescription string
93+
provider v1alpha1.AppLink
94+
maintainers []v1alpha1.Maintainer
95+
links []v1alpha1.AppLink
96+
keywords []string
97+
}
98+
99+
func promotedCSVMetadata(version *semver.Version, metadata *property.CSVMetadata) promotedMetadata {
100+
return promotedMetadata{
101+
version: version,
102+
displayName: metadata.DisplayName,
103+
shortDescription: metadata.Annotations["description"],
104+
provider: metadata.Provider,
105+
maintainers: metadata.Maintainers,
106+
links: metadata.Links,
107+
keywords: metadata.Keywords,
108+
}
109+
}

alpha/action/migrations/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var allMigrations = []Migration{
5656
newMigration("bundle-object-to-csv-metadata", `migrates bundles' "olm.bundle.object" to "olm.csv.metadata"`, bundleObjectToCSVMetadata),
5757
newMigration("split-icon", `split package icon out into separate "olm.icon" blob`, splitIcon),
5858
newMigration("promote-bundle-version", `promote bundle version into first-class bundle field, remove olm.package properties`, promoteBundleVersion),
59+
newMigration("promote-package-metadata", `promote package metadata from "olm.csv.metadata" properties to "olm.package" blob`, promotePackageMetadata),
5960
}
6061

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

alpha/declcfg/declcfg.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1212
"k8s.io/apimachinery/pkg/util/sets"
1313

14+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
15+
1416
"github.com/operator-framework/operator-registry/alpha/property"
1517
prettyunmarshaler "github.com/operator-framework/operator-registry/pkg/prettyunmarshaler"
1618
)
@@ -36,12 +38,12 @@ type Package struct {
3638
Schema string `json:"schema"`
3739
Name string `json:"name"`
3840

39-
//DisplayName string `json:"displayName,omitempty"`
40-
//ShortDescription string `json:"shortDescription,omitempty"`
41-
//Provider v1alpha1.AppLink `json:"provider,omitempty"`
42-
//Maintainers []v1alpha1.Maintainer `json:"maintainers,omitempty"`
43-
//Links []v1alpha1.AppLink `json:"links,omitempty"`
44-
//Keywords []string `json:"keywords,omitempty"`
41+
DisplayName string `json:"displayName,omitempty"`
42+
ShortDescription string `json:"shortDescription,omitempty"`
43+
Provider v1alpha1.AppLink `json:"provider,omitempty"`
44+
Maintainers []v1alpha1.Maintainer `json:"maintainers,omitempty"`
45+
Links []v1alpha1.AppLink `json:"links,omitempty"`
46+
Keywords []string `json:"keywords,omitempty"`
4547

4648
DefaultChannel string `json:"defaultChannel"`
4749
Description string `json:"description,omitempty"`

alpha/declcfg/declcfg_to_model.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ func ConvertToModel(cfg DeclarativeConfig) (model.Model, error) {
3232
Description: p.Description,
3333
Channels: map[string]*model.Channel{},
3434

35-
//DisplayName: p.DisplayName,
36-
//ShortDescription: p.ShortDescription,
37-
//Provider: p.Provider,
38-
//Maintainers: p.Maintainers,
39-
//Links: p.Links,
40-
//Keywords: p.Keywords,
35+
DisplayName: p.DisplayName,
36+
ShortDescription: p.ShortDescription,
37+
Provider: p.Provider,
38+
Maintainers: p.Maintainers,
39+
Links: p.Links,
40+
Keywords: p.Keywords,
4141
}
4242
if p.Icon != nil {
4343
mpkg.Icon = &model.Icon{

alpha/model/model.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package model
33
import (
44
"errors"
55
"fmt"
6+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
67
"sort"
78
"strings"
89

@@ -52,12 +53,12 @@ type Package struct {
5253
Channels map[string]*Channel
5354
Deprecation *Deprecation
5455

55-
//DisplayName string
56-
//ShortDescription string
57-
//Provider v1alpha1.AppLink
58-
//Maintainers []v1alpha1.Maintainer
59-
//Links []v1alpha1.AppLink
60-
//Keywords []string
56+
DisplayName string
57+
ShortDescription string
58+
Provider v1alpha1.AppLink
59+
Maintainers []v1alpha1.Maintainer
60+
Links []v1alpha1.AppLink
61+
Keywords []string
6162
}
6263

6364
func (m *Package) Validate() error {

pkg/api/model_to_api.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ConvertModelBundleToAPIBundle(b model.Bundle) (*Bundle, error) {
3030
MediaType: b.Package.Icon.MediaType,
3131
}}
3232
}
33-
csv := csvMetadataToCsv(props.CSVMetadatas[0])
33+
csv := bundleToCSV(b)
3434
csv.Name = b.Name
3535
csv.Spec.Icon = icons
3636
csv.Spec.InstallStrategy = v1alpha1.NamedInstallStrategy{
@@ -101,7 +101,28 @@ func parseProperties(in []property.Property) (*property.Properties, error) {
101101
return props, nil
102102
}
103103

104-
func csvMetadataToCsv(m property.CSVMetadata) v1alpha1.ClusterServiceVersion {
104+
func bundleToCSV(b model.Bundle) v1alpha1.ClusterServiceVersion {
105+
p := b.Package
106+
m := b.PropertiesP.CSVMetadatas[0]
107+
108+
if m.DisplayName == "" {
109+
m.DisplayName = p.DisplayName
110+
}
111+
if _, ok := m.Annotations["description"]; !ok {
112+
m.Annotations["description"] = p.ShortDescription
113+
}
114+
if m.Provider.Name == "" && m.Provider.URL == "" {
115+
m.Provider = p.Provider
116+
}
117+
if m.Maintainers == nil {
118+
m.Maintainers = p.Maintainers
119+
}
120+
if m.Links == nil {
121+
m.Links = p.Links
122+
}
123+
if m.Keywords == nil {
124+
m.Keywords = p.Keywords
125+
}
105126
return v1alpha1.ClusterServiceVersion{
106127
TypeMeta: metav1.TypeMeta{
107128
Kind: operators.ClusterServiceVersionKind,

0 commit comments

Comments
 (0)