|
8 | 8 | catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
|
9 | 9 | "github.com/operator-framework/deppy/pkg/deppy"
|
10 | 10 | "github.com/operator-framework/deppy/pkg/deppy/input"
|
| 11 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 12 | + "github.com/operator-framework/operator-registry/alpha/model" |
11 | 13 | "github.com/operator-framework/operator-registry/alpha/property"
|
12 | 14 | "sigs.k8s.io/controller-runtime/pkg/client"
|
13 | 15 |
|
@@ -71,70 +73,123 @@ func (es *CatalogdEntitySource) Iterate(ctx context.Context, fn input.IteratorFu
|
71 | 73 | return nil
|
72 | 74 | }
|
73 | 75 |
|
74 |
| -func getEntities(ctx context.Context, client client.Client) (input.EntityList, error) { |
75 |
| - entityList := input.EntityList{} |
76 |
| - bundleMetadatas, packageMetdatas, err := fetchMetadata(ctx, client) |
77 |
| - if err != nil { |
| 76 | +func getEntities(ctx context.Context, cl client.Client) (input.EntityList, error) { |
| 77 | + allEntitiesList := input.EntityList{} |
| 78 | + |
| 79 | + var catalogList catalogd.CatalogList |
| 80 | + if err := cl.List(ctx, &catalogList); err != nil { |
78 | 81 | return nil, err
|
79 | 82 | }
|
80 |
| - for _, bundle := range bundleMetadatas.Items { |
81 |
| - props := map[string]string{} |
82 |
| - |
83 |
| - // TODO: We should make sure all properties are forwarded |
84 |
| - // through and avoid a lossy translation from FBC --> entity |
85 |
| - for _, prop := range bundle.Spec.Properties { |
86 |
| - switch prop.Type { |
87 |
| - case property.TypePackage: |
88 |
| - // this is already a json marshalled object, so it doesn't need to be marshalled |
89 |
| - // like the other ones |
90 |
| - props[property.TypePackage] = string(prop.Value) |
91 |
| - case entities.PropertyBundleMediaType: |
92 |
| - props[entities.PropertyBundleMediaType] = string(prop.Value) |
93 |
| - } |
| 83 | + for _, catalog := range catalogList.Items { |
| 84 | + model, err := fetchCatalogModel(ctx, cl, catalog.Name) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
94 | 87 | }
|
95 | 88 |
|
96 |
| - imgValue, err := json.Marshal(bundle.Spec.Image) |
| 89 | + catalogEntitiesList, err := ModelToEntities(model, catalog.Name) |
97 | 90 | if err != nil {
|
98 | 91 | return nil, err
|
99 | 92 | }
|
100 |
| - props[entities.PropertyBundlePath] = string(imgValue) |
101 |
| - catalogScopedPkgName := fmt.Sprintf("%s-%s", bundle.Spec.Catalog.Name, bundle.Spec.Package) |
102 |
| - bundlePkg := packageMetdatas[catalogScopedPkgName] |
103 |
| - for _, ch := range bundlePkg.Spec.Channels { |
104 |
| - for _, b := range ch.Entries { |
105 |
| - catalogScopedEntryName := fmt.Sprintf("%s-%s", bundle.Spec.Catalog.Name, b.Name) |
106 |
| - if catalogScopedEntryName == bundle.Name { |
107 |
| - channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0}) |
108 |
| - props[property.TypeChannel] = string(channelValue) |
109 |
| - replacesValue, _ := json.Marshal(entities.ChannelEntry{ |
110 |
| - Name: b.Name, |
111 |
| - Replaces: b.Replaces, |
112 |
| - }) |
113 |
| - props[entities.PropertyBundleChannelEntry] = string(replacesValue) |
114 |
| - entity := input.Entity{ |
115 |
| - ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", bundle.Name, bundle.Spec.Package, ch.Name)), |
116 |
| - Properties: props, |
117 |
| - } |
118 |
| - entityList = append(entityList, entity) |
119 |
| - } |
120 |
| - } |
121 |
| - } |
| 93 | + |
| 94 | + allEntitiesList = append(allEntitiesList, catalogEntitiesList...) |
122 | 95 | }
|
123 |
| - return entityList, nil |
| 96 | + |
| 97 | + return allEntitiesList, nil |
124 | 98 | }
|
125 | 99 |
|
126 |
| -func fetchMetadata(ctx context.Context, client client.Client) (catalogd.BundleMetadataList, map[string]catalogd.Package, error) { |
127 |
| - packageMetdatas := catalogd.PackageList{} |
128 |
| - if err := client.List(ctx, &packageMetdatas); err != nil { |
129 |
| - return catalogd.BundleMetadataList{}, nil, err |
| 100 | +func fetchCatalogModel(ctx context.Context, cl client.Client, catalogName string) (model.Model, error) { |
| 101 | + packages, err := fetch[declcfg.Package](ctx, cl, declcfg.SchemaPackage, catalogName) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
130 | 104 | }
|
131 |
| - bundleMetadatas := catalogd.BundleMetadataList{} |
132 |
| - if err := client.List(ctx, &bundleMetadatas); err != nil { |
133 |
| - return catalogd.BundleMetadataList{}, nil, err |
| 105 | + channels, err := fetch[declcfg.Channel](ctx, cl, declcfg.SchemaChannel, catalogName) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
134 | 108 | }
|
135 |
| - packages := map[string]catalogd.Package{} |
136 |
| - for _, pkg := range packageMetdatas.Items { |
137 |
| - packages[pkg.Name] = pkg |
| 109 | + bundles, err := fetch[declcfg.Bundle](ctx, cl, declcfg.SchemaBundle, catalogName) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + cfg := &declcfg.DeclarativeConfig{ |
| 115 | + Packages: packages, |
| 116 | + Channels: channels, |
| 117 | + Bundles: bundles, |
138 | 118 | }
|
139 |
| - return bundleMetadatas, packages, nil |
| 119 | + model, err := declcfg.ConvertToModel(*cfg) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return model, nil |
| 125 | +} |
| 126 | + |
| 127 | +type declcfgSchemas interface { |
| 128 | + declcfg.Package | declcfg.Bundle | declcfg.Channel |
| 129 | +} |
| 130 | + |
| 131 | +// TODO: Cleanup once https://github.com/golang/go/issues/45380 implemented |
| 132 | +// We should be able to get rid of the schema arg and switch based on the type based to this generic |
| 133 | +func fetch[T declcfgSchemas](ctx context.Context, cl client.Client, schema, catalogName string) ([]T, error) { |
| 134 | + cmList := catalogd.CatalogMetadataList{} |
| 135 | + if err := cl.List(ctx, &cmList, client.MatchingLabels{"schema": schema, "catalog": catalogName}); err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + contents := []T{} |
| 140 | + for _, cm := range cmList.Items { |
| 141 | + var content T |
| 142 | + if err := json.Unmarshal(cm.Spec.Content, &content); err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + contents = append(contents, content) |
| 146 | + } |
| 147 | + |
| 148 | + return contents, nil |
| 149 | +} |
| 150 | + |
| 151 | +func ModelToEntities(model model.Model, catalogName string) (input.EntityList, error) { |
| 152 | + entityList := input.EntityList{} |
| 153 | + |
| 154 | + for _, pkg := range model { |
| 155 | + for _, ch := range pkg.Channels { |
| 156 | + for _, bundle := range ch.Bundles { |
| 157 | + props := map[string]string{} |
| 158 | + |
| 159 | + for _, prop := range bundle.Properties { |
| 160 | + switch prop.Type { |
| 161 | + case property.TypePackage: |
| 162 | + // this is already a json marshalled object, so it doesn't need to be marshalled |
| 163 | + // like the other ones |
| 164 | + props[property.TypePackage] = string(prop.Value) |
| 165 | + case entities.PropertyBundleMediaType: |
| 166 | + props[entities.PropertyBundleMediaType] = string(prop.Value) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + imgValue, err := json.Marshal(bundle.Image) |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + props[entities.PropertyBundlePath] = string(imgValue) |
| 175 | + |
| 176 | + channelValue, _ := json.Marshal(property.Channel{ChannelName: ch.Name, Priority: 0}) |
| 177 | + props[property.TypeChannel] = string(channelValue) |
| 178 | + replacesValue, _ := json.Marshal(entities.ChannelEntry{ |
| 179 | + Name: bundle.Name, |
| 180 | + Replaces: bundle.Replaces, |
| 181 | + }) |
| 182 | + props[entities.PropertyBundleChannelEntry] = string(replacesValue) |
| 183 | + |
| 184 | + catalogScopedEntryName := fmt.Sprintf("%s-%s", catalogName, bundle.Name) |
| 185 | + entity := input.Entity{ |
| 186 | + ID: deppy.IdentifierFromString(fmt.Sprintf("%s%s%s", catalogScopedEntryName, bundle.Package.Name, ch.Name)), |
| 187 | + Properties: props, |
| 188 | + } |
| 189 | + entityList = append(entityList, entity) |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return entityList, nil |
140 | 195 | }
|
0 commit comments