Skip to content

Commit 77a7c57

Browse files
committed
poc for schema import
1 parent b137480 commit 77a7c57

File tree

6 files changed

+255
-21
lines changed

6 files changed

+255
-21
lines changed

alpha/action/render.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,7 @@ BundleLoop:
450450
func combineConfigs(cfgs []declcfg.DeclarativeConfig) *declcfg.DeclarativeConfig {
451451
out := &declcfg.DeclarativeConfig{}
452452
for _, in := range cfgs {
453-
out.Packages = append(out.Packages, in.Packages...)
454-
out.Channels = append(out.Channels, in.Channels...)
455-
out.Bundles = append(out.Bundles, in.Bundles...)
456-
out.Others = append(out.Others, in.Others...)
453+
out.Merge(&in)
457454
}
458455
return out
459456
}

alpha/declcfg/declcfg.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ import (
1616
)
1717

1818
const (
19-
SchemaPackage = "olm.package"
20-
SchemaChannel = "olm.channel"
21-
SchemaBundle = "olm.bundle"
19+
SchemaPackage = "olm.package"
20+
SchemaChannel = "olm.channel"
21+
SchemaBundle = "olm.bundle"
22+
SchemaDeprecation = "olm.catalog.deprecation"
2223
)
2324

2425
type DeclarativeConfig struct {
25-
Packages []Package
26-
Channels []Channel
27-
Bundles []Bundle
28-
Others []Meta
26+
Packages []Package
27+
Channels []Channel
28+
Bundles []Bundle
29+
Deprecations map[string]Deprecation // mapping package name to deprecation
30+
Others []Meta
2931
}
3032

3133
type Package struct {
@@ -90,6 +92,19 @@ type RelatedImage struct {
9092
Image string `json:"image"`
9193
}
9294

95+
type Deprecation struct {
96+
Schema string `json:"schema"`
97+
Package string `json:"package"`
98+
Name string `json:"name,omitempty"`
99+
Deprecations []DeprecationEntry `json:"deprecations"`
100+
}
101+
102+
type DeprecationEntry struct {
103+
Schema string `json:"schema"`
104+
Name string `json:"name,omitempty"`
105+
Message json.RawMessage `json:"message"`
106+
}
107+
93108
type Meta struct {
94109
Schema string
95110
Package string
@@ -181,3 +196,27 @@ func extractUniqueMetaKeys(blobMap map[string]any, m *Meta) error {
181196
}
182197
return nil
183198
}
199+
200+
func (destination *DeclarativeConfig) Merge(src *DeclarativeConfig) error {
201+
destination.Packages = append(destination.Packages, src.Packages...)
202+
destination.Channels = append(destination.Channels, src.Channels...)
203+
destination.Bundles = append(destination.Bundles, src.Bundles...)
204+
destination.Others = append(destination.Others, src.Others...)
205+
if err := destination.mergeDeprecations(src); err != nil {
206+
return err
207+
}
208+
return nil
209+
}
210+
211+
func (destination *DeclarativeConfig) mergeDeprecations(src *DeclarativeConfig) error {
212+
for p, d := range src.Deprecations {
213+
if _, ok := destination.Deprecations[p]; ok {
214+
return fmt.Errorf("deprecation for package %q already exists", d.Package)
215+
}
216+
if destination.Deprecations == nil {
217+
destination.Deprecations = make(map[string]Deprecation)
218+
}
219+
destination.Deprecations[p] = d
220+
}
221+
return nil
222+
}

alpha/declcfg/load.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,8 @@ func mergeCfgs(ctx context.Context, cfgChan <-chan *DeclarativeConfig, fcfg *Dec
227227
if !ok {
228228
return nil
229229
}
230-
fcfg.Packages = append(fcfg.Packages, cfg.Packages...)
231-
fcfg.Channels = append(fcfg.Channels, cfg.Channels...)
232-
fcfg.Bundles = append(fcfg.Bundles, cfg.Bundles...)
233-
fcfg.Others = append(fcfg.Others, cfg.Others...)
230+
fcfg.Merge(cfg)
234231
}
235-
236232
}
237233
}
238234

@@ -272,7 +268,7 @@ func extractCSV(objs []string) string {
272268

273269
// LoadReader reads yaml or json from the passed in io.Reader and unmarshals it into a DeclarativeConfig struct.
274270
func LoadReader(r io.Reader) (*DeclarativeConfig, error) {
275-
cfg := &DeclarativeConfig{}
271+
cfg := &DeclarativeConfig{Deprecations: make(map[string]Deprecation)}
276272

277273
if err := WalkMetasReader(r, func(in *Meta, err error) error {
278274
if err != nil {
@@ -297,6 +293,15 @@ func LoadReader(r io.Reader) (*DeclarativeConfig, error) {
297293
return fmt.Errorf("parse bundle: %v", err)
298294
}
299295
cfg.Bundles = append(cfg.Bundles, b)
296+
case SchemaDeprecation:
297+
var d Deprecation
298+
if err := json.Unmarshal(in.Blob, &d); err != nil {
299+
return fmt.Errorf("parse deprecation: %v", err)
300+
}
301+
if _, ok := cfg.Deprecations[d.Package]; ok {
302+
return fmt.Errorf("deprecation for package %q already exists", d.Package)
303+
}
304+
cfg.Deprecations[d.Package] = d
300305
case "":
301306
return fmt.Errorf("object '%s' is missing root schema field", string(in.Blob))
302307
default:

alpha/declcfg/load_test.go

Lines changed: 189 additions & 0 deletions
Large diffs are not rendered by default.

alpha/declcfg/write.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,20 @@ func writeToEncoder(cfg DeclarativeConfig, enc encoder) error {
418418
return err
419419
}
420420
}
421+
422+
if _, ok := cfg.Deprecations[pName]; ok {
423+
if err := enc.Encode(cfg.Deprecations[pName]); err != nil {
424+
return err
425+
}
426+
}
421427
}
422428

423429
for _, o := range othersByPackage[""] {
424430
if err := enc.Encode(o); err != nil {
425431
return err
426432
}
427433
}
434+
428435
return nil
429436
}
430437

alpha/template/semver/semver.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,7 @@ func newChannel(pkgName string, chName string) *declcfg.Channel {
387387
func combineConfigs(cfgs []declcfg.DeclarativeConfig) *declcfg.DeclarativeConfig {
388388
out := &declcfg.DeclarativeConfig{}
389389
for _, in := range cfgs {
390-
out.Packages = append(out.Packages, in.Packages...)
391-
out.Channels = append(out.Channels, in.Channels...)
392-
out.Bundles = append(out.Bundles, in.Bundles...)
393-
out.Others = append(out.Others, in.Others...)
390+
out.Merge(&in)
394391
}
395392
return out
396393
}

0 commit comments

Comments
 (0)