|
| 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 | +} |
0 commit comments