Skip to content

Commit df88dab

Browse files
committed
poc for schema import
1 parent b137480 commit df88dab

File tree

6 files changed

+287
-44
lines changed

6 files changed

+287
-44
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: 30 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 []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,11 @@ func extractUniqueMetaKeys(blobMap map[string]any, m *Meta) error {
181196
}
182197
return nil
183198
}
199+
200+
func (destination *DeclarativeConfig) Merge(src *DeclarativeConfig) {
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+
destination.Deprecations = append(destination.Deprecations, src.Deprecations...)
206+
}

alpha/declcfg/load.go

Lines changed: 7 additions & 5 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

@@ -297,6 +293,12 @@ 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: %w", err)
300+
}
301+
cfg.Deprecations = append(cfg.Deprecations, d)
300302
case "":
301303
return fmt.Errorf("object '%s' is missing root schema field", string(in.Blob))
302304
default:

alpha/declcfg/load_test.go

Lines changed: 231 additions & 24 deletions
Large diffs are not rendered by default.

alpha/declcfg/write.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ func writeToEncoder(cfg DeclarativeConfig, enc encoder) error {
377377
pkgNames.Insert(pkgName)
378378
othersByPackage[pkgName] = append(othersByPackage[pkgName], o)
379379
}
380+
deprecationsByPackage := map[string][]Deprecation{}
381+
for _, d := range cfg.Deprecations {
382+
pkgName := d.Package
383+
pkgNames.Insert(pkgName)
384+
deprecationsByPackage[pkgName] = append(deprecationsByPackage[pkgName], d)
385+
}
380386

381387
for _, pName := range pkgNames.List() {
382388
if len(pName) == 0 {
@@ -418,13 +424,24 @@ func writeToEncoder(cfg DeclarativeConfig, enc encoder) error {
418424
return err
419425
}
420426
}
427+
428+
deprecations := deprecationsByPackage[pName]
429+
sort.SliceStable(deprecations, func(i, j int) bool {
430+
return deprecations[i].Name < deprecations[j].Name
431+
})
432+
for _, d := range deprecations {
433+
if err := enc.Encode(d); err != nil {
434+
return err
435+
}
436+
}
421437
}
422438

423439
for _, o := range othersByPackage[""] {
424440
if err := enc.Encode(o); err != nil {
425441
return err
426442
}
427443
}
444+
428445
return nil
429446
}
430447

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)