|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + "unicode" |
| 8 | + "unicode/utf8" |
| 9 | + |
| 10 | + "github.com/Masterminds/semver/v3" |
| 11 | + |
| 12 | + "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 13 | + |
| 14 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 15 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 16 | +) |
| 17 | + |
| 18 | +func promotePackageMetadata(cfg *declcfg.DeclarativeConfig) error { |
| 19 | + metadataByPackage := map[string]promotedMetadata{} |
| 20 | + for i := range cfg.Bundles { |
| 21 | + b := &cfg.Bundles[i] |
| 22 | + |
| 23 | + csvMetadata, csvMetadataIdx, err := getCsvMetadata(b) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + // Skip objects that have no olm.csv.metadata property |
| 29 | + if csvMetadata == nil { |
| 30 | + continue |
| 31 | + } |
| 32 | + |
| 33 | + // Keep track of the metadata from the highest versioned bundle from each package. |
| 34 | + cur, ok := metadataByPackage[b.Package] |
| 35 | + if !ok || compareRegistryV1Semver(cur.version, b.Version) < 0 { |
| 36 | + metadataByPackage[b.Package] = promotedCSVMetadata(b.Version, csvMetadata) |
| 37 | + } |
| 38 | + |
| 39 | + // Delete the package-level metadata from the olm.csv.metadata object and |
| 40 | + // update the bundle properties to use the new slimmed-down revision of it. |
| 41 | + csvMetadata.DisplayName = "" |
| 42 | + delete(csvMetadata.Annotations, "description") |
| 43 | + csvMetadata.Provider = v1alpha1.AppLink{} |
| 44 | + csvMetadata.Maintainers = nil |
| 45 | + csvMetadata.Links = nil |
| 46 | + csvMetadata.Keywords = nil |
| 47 | + |
| 48 | + newCSVMetadata, err := json.Marshal(csvMetadata) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + b.Properties[csvMetadataIdx] = property.Property{ |
| 53 | + Type: property.TypeCSVMetadata, |
| 54 | + Value: newCSVMetadata, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Update each olm.package object to include the metadata we extracted from |
| 59 | + // bundles in the first loop. |
| 60 | + for i := range cfg.Packages { |
| 61 | + pkg := &cfg.Packages[i] |
| 62 | + metadata, ok := metadataByPackage[pkg.Name] |
| 63 | + if !ok { |
| 64 | + continue |
| 65 | + } |
| 66 | + pkg.DisplayName = metadata.displayName |
| 67 | + pkg.ShortDescription = shortenDescription(metadata.shortDescription) |
| 68 | + if metadata.provider.Name != "" || metadata.provider.URL != "" { |
| 69 | + pkg.Provider = &metadata.provider |
| 70 | + } |
| 71 | + pkg.Maintainers = metadata.maintainers |
| 72 | + pkg.Links = metadata.links |
| 73 | + pkg.Keywords = slices.DeleteFunc(metadata.keywords, func(s string) bool { |
| 74 | + // Delete keywords that are empty strings |
| 75 | + return s == "" |
| 76 | + }) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func getCsvMetadata(b *declcfg.Bundle) (*property.CSVMetadata, int, error) { |
| 82 | + for i, p := range b.Properties { |
| 83 | + if p.Type != property.TypeCSVMetadata { |
| 84 | + continue |
| 85 | + } |
| 86 | + var csvMetadata property.CSVMetadata |
| 87 | + if err := json.Unmarshal(p.Value, &csvMetadata); err != nil { |
| 88 | + return nil, -1, err |
| 89 | + } |
| 90 | + return &csvMetadata, i, nil |
| 91 | + } |
| 92 | + return nil, -1, nil |
| 93 | +} |
| 94 | + |
| 95 | +func compareRegistryV1Semver(a, b *semver.Version) int { |
| 96 | + if v := a.Compare(b); v != 0 { |
| 97 | + return v |
| 98 | + } |
| 99 | + aPre := semver.New(0, 0, 0, a.Metadata(), "") |
| 100 | + bPre := semver.New(0, 0, 0, b.Metadata(), "") |
| 101 | + return aPre.Compare(bPre) |
| 102 | +} |
| 103 | + |
| 104 | +type promotedMetadata struct { |
| 105 | + version *semver.Version |
| 106 | + |
| 107 | + displayName string |
| 108 | + shortDescription string |
| 109 | + provider v1alpha1.AppLink |
| 110 | + maintainers []v1alpha1.Maintainer |
| 111 | + links []v1alpha1.AppLink |
| 112 | + keywords []string |
| 113 | +} |
| 114 | + |
| 115 | +func promotedCSVMetadata(version *semver.Version, metadata *property.CSVMetadata) promotedMetadata { |
| 116 | + return promotedMetadata{ |
| 117 | + version: version, |
| 118 | + displayName: metadata.DisplayName, |
| 119 | + shortDescription: metadata.Annotations["description"], |
| 120 | + provider: metadata.Provider, |
| 121 | + maintainers: metadata.Maintainers, |
| 122 | + links: metadata.Links, |
| 123 | + keywords: metadata.Keywords, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func shortenDescription(input string) string { |
| 128 | + const maxLen = 256 |
| 129 | + input = strings.TrimSpace(input) |
| 130 | + |
| 131 | + // If the input is already under the limit return it. |
| 132 | + if utf8.RuneCountInString(input) <= maxLen { |
| 133 | + return input |
| 134 | + } |
| 135 | + |
| 136 | + // Chop off everything after the first paragraph. |
| 137 | + if idx := strings.Index(input, "\n\n"); idx != -1 { |
| 138 | + input = strings.TrimSpace(input[:idx]) |
| 139 | + } |
| 140 | + |
| 141 | + // If we're _now_ under the limit, return the first paragraph. |
| 142 | + if utf8.RuneCountInString(input) <= maxLen { |
| 143 | + return input |
| 144 | + } |
| 145 | + |
| 146 | + // If the first paragraph is still over the limit, we'll have to truncate. |
| 147 | + // We'll truncate at the last word boundary that still allows an ellipsis |
| 148 | + // to fit within the maximum length. But if there are no word boundaries |
| 149 | + // (veeeeery unlikely), we'll hard truncate mid-word. |
| 150 | + input = input[:maxLen-3] |
| 151 | + if truncatedIdx := strings.LastIndexFunc(input, unicode.IsSpace); truncatedIdx != -1 { |
| 152 | + return input[:truncatedIdx] + "..." |
| 153 | + } |
| 154 | + return input + "..." |
| 155 | +} |
0 commit comments