|
| 1 | +package declcfg |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/blang/semver/v4" |
| 7 | + "k8s.io/apimachinery/pkg/util/sets" |
| 8 | + |
| 9 | + "github.com/operator-framework/operator-registry/alpha/model" |
| 10 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 11 | +) |
| 12 | + |
| 13 | +func ConvertToModel(cfg DeclarativeConfig) (model.Model, error) { |
| 14 | + mpkgs := model.Model{} |
| 15 | + defaultChannels := map[string]string{} |
| 16 | + for _, p := range cfg.Packages { |
| 17 | + if p.Name == "" { |
| 18 | + return nil, fmt.Errorf("config contains package with no name") |
| 19 | + } |
| 20 | + |
| 21 | + if _, ok := mpkgs[p.Name]; ok { |
| 22 | + return nil, fmt.Errorf("duplicate package %q", p.Name) |
| 23 | + } |
| 24 | + |
| 25 | + mpkg := &model.Package{ |
| 26 | + Name: p.Name, |
| 27 | + Description: p.Description, |
| 28 | + Channels: map[string]*model.Channel{}, |
| 29 | + } |
| 30 | + if p.Icon != nil { |
| 31 | + mpkg.Icon = &model.Icon{ |
| 32 | + Data: p.Icon.Data, |
| 33 | + MediaType: p.Icon.MediaType, |
| 34 | + } |
| 35 | + } |
| 36 | + defaultChannels[p.Name] = p.DefaultChannel |
| 37 | + mpkgs[p.Name] = mpkg |
| 38 | + } |
| 39 | + |
| 40 | + channelDefinedEntries := map[string]sets.String{} |
| 41 | + for _, c := range cfg.Channels { |
| 42 | + mpkg, ok := mpkgs[c.Package] |
| 43 | + if !ok { |
| 44 | + return nil, fmt.Errorf("unknown package %q for channel %q", c.Package, c.Name) |
| 45 | + } |
| 46 | + |
| 47 | + if c.Name == "" { |
| 48 | + return nil, fmt.Errorf("package %q contains channel with no name", c.Package) |
| 49 | + } |
| 50 | + |
| 51 | + if _, ok := mpkg.Channels[c.Name]; ok { |
| 52 | + return nil, fmt.Errorf("package %q has duplicate channel %q", c.Package, c.Name) |
| 53 | + } |
| 54 | + |
| 55 | + mch := &model.Channel{ |
| 56 | + Package: mpkg, |
| 57 | + Name: c.Name, |
| 58 | + Bundles: map[string]*model.Bundle{}, |
| 59 | + // NOTICE: The field Properties of the type Channel is for internal use only. |
| 60 | + // DO NOT use it for any public-facing functionalities. |
| 61 | + // This API is in alpha stage and it is subject to change. |
| 62 | + Properties: c.Properties, |
| 63 | + } |
| 64 | + |
| 65 | + cde := sets.NewString() |
| 66 | + for _, entry := range c.Entries { |
| 67 | + if _, ok := mch.Bundles[entry.Name]; ok { |
| 68 | + return nil, fmt.Errorf("invalid package %q, channel %q: duplicate entry %q", c.Package, c.Name, entry.Name) |
| 69 | + } |
| 70 | + cde = cde.Insert(entry.Name) |
| 71 | + mch.Bundles[entry.Name] = &model.Bundle{ |
| 72 | + Package: mpkg, |
| 73 | + Channel: mch, |
| 74 | + Name: entry.Name, |
| 75 | + Replaces: entry.Replaces, |
| 76 | + Skips: entry.Skips, |
| 77 | + SkipRange: entry.SkipRange, |
| 78 | + } |
| 79 | + } |
| 80 | + channelDefinedEntries[c.Package] = cde |
| 81 | + |
| 82 | + mpkg.Channels[c.Name] = mch |
| 83 | + |
| 84 | + defaultChannelName := defaultChannels[c.Package] |
| 85 | + if defaultChannelName == c.Name { |
| 86 | + mpkg.DefaultChannel = mch |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // packageBundles tracks the set of bundle names for each package |
| 91 | + // and is used to detect duplicate bundles. |
| 92 | + packageBundles := map[string]sets.String{} |
| 93 | + |
| 94 | + for _, b := range cfg.Bundles { |
| 95 | + if b.Package == "" { |
| 96 | + return nil, fmt.Errorf("package name must be set for bundle %q", b.Name) |
| 97 | + } |
| 98 | + mpkg, ok := mpkgs[b.Package] |
| 99 | + if !ok { |
| 100 | + return nil, fmt.Errorf("unknown package %q for bundle %q", b.Package, b.Name) |
| 101 | + } |
| 102 | + |
| 103 | + bundles, ok := packageBundles[b.Package] |
| 104 | + if !ok { |
| 105 | + bundles = sets.NewString() |
| 106 | + } |
| 107 | + if bundles.Has(b.Name) { |
| 108 | + return nil, fmt.Errorf("package %q has duplicate bundle %q", b.Package, b.Name) |
| 109 | + } |
| 110 | + bundles.Insert(b.Name) |
| 111 | + packageBundles[b.Package] = bundles |
| 112 | + |
| 113 | + props, err := property.Parse(b.Properties) |
| 114 | + if err != nil { |
| 115 | + return nil, fmt.Errorf("parse properties for bundle %q: %v", b.Name, err) |
| 116 | + } |
| 117 | + |
| 118 | + if len(props.Packages) != 1 { |
| 119 | + return nil, fmt.Errorf("package %q bundle %q must have exactly 1 %q property, found %d", b.Package, b.Name, property.TypePackage, len(props.Packages)) |
| 120 | + } |
| 121 | + |
| 122 | + if b.Package != props.Packages[0].PackageName { |
| 123 | + return nil, fmt.Errorf("package %q does not match %q property %q", b.Package, property.TypePackage, props.Packages[0].PackageName) |
| 124 | + } |
| 125 | + |
| 126 | + // Parse version from the package property. |
| 127 | + rawVersion := props.Packages[0].Version |
| 128 | + ver, err := semver.Parse(rawVersion) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("error parsing bundle %q version %q: %v", b.Name, rawVersion, err) |
| 131 | + } |
| 132 | + |
| 133 | + channelDefinedEntries[b.Package] = channelDefinedEntries[b.Package].Delete(b.Name) |
| 134 | + found := false |
| 135 | + for _, mch := range mpkg.Channels { |
| 136 | + if mb, ok := mch.Bundles[b.Name]; ok { |
| 137 | + found = true |
| 138 | + mb.Image = b.Image |
| 139 | + mb.Properties = b.Properties |
| 140 | + mb.RelatedImages = relatedImagesToModelRelatedImages(b.RelatedImages) |
| 141 | + mb.CsvJSON = b.CsvJSON |
| 142 | + mb.Objects = b.Objects |
| 143 | + mb.PropertiesP = props |
| 144 | + mb.Version = ver |
| 145 | + } |
| 146 | + } |
| 147 | + if !found { |
| 148 | + return nil, fmt.Errorf("package %q, bundle %q not found in any channel entries", b.Package, b.Name) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + for pkg, entries := range channelDefinedEntries { |
| 153 | + if entries.Len() > 0 { |
| 154 | + return nil, fmt.Errorf("no olm.bundle blobs found in package %q for olm.channel entries %s", pkg, entries.List()) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + for _, mpkg := range mpkgs { |
| 159 | + defaultChannelName := defaultChannels[mpkg.Name] |
| 160 | + if defaultChannelName != "" && mpkg.DefaultChannel == nil { |
| 161 | + dch := &model.Channel{ |
| 162 | + Package: mpkg, |
| 163 | + Name: defaultChannelName, |
| 164 | + Bundles: map[string]*model.Bundle{}, |
| 165 | + } |
| 166 | + mpkg.DefaultChannel = dch |
| 167 | + mpkg.Channels[dch.Name] = dch |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if err := mpkgs.Validate(); err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + mpkgs.Normalize() |
| 175 | + return mpkgs, nil |
| 176 | +} |
| 177 | + |
| 178 | +func relatedImagesToModelRelatedImages(in []RelatedImage) []model.RelatedImage { |
| 179 | + var out []model.RelatedImage |
| 180 | + for _, p := range in { |
| 181 | + out = append(out, model.RelatedImage{ |
| 182 | + Name: p.Name, |
| 183 | + Image: p.Image, |
| 184 | + }) |
| 185 | + } |
| 186 | + return out |
| 187 | +} |
0 commit comments