Skip to content

Commit f2285a2

Browse files
authored
poc for schema import (#1151)
Signed-off-by: Jordan Keister <[email protected]>
1 parent 5d951b8 commit f2285a2

File tree

6 files changed

+145
-59
lines changed

6 files changed

+145
-59
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: 89 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ func TestLoadReader(t *testing.T) {
9797

9898
func TestWalkMetasFS(t *testing.T) {
9999
type spec struct {
100-
name string
101-
fsys fs.FS
102-
assertion require.ErrorAssertionFunc
103-
expectNumPackages int
104-
expectNumChannels int
105-
expectNumBundles int
106-
expectNumOthers int
100+
name string
101+
fsys fs.FS
102+
assertion require.ErrorAssertionFunc
103+
expectNumPackages int
104+
expectNumChannels int
105+
expectNumBundles int
106+
expectNumDeprecations int
107+
expectNumOthers int
107108
}
108109
specs := []spec{
109110
{
@@ -122,19 +123,20 @@ func TestWalkMetasFS(t *testing.T) {
122123
assertion: require.Error,
123124
},
124125
{
125-
name: "Success/ValidDir",
126-
fsys: validFS,
127-
assertion: require.NoError,
128-
expectNumPackages: 3,
129-
expectNumChannels: 0,
130-
expectNumBundles: 12,
131-
expectNumOthers: 1,
126+
name: "Success/ValidDir",
127+
fsys: validFS,
128+
assertion: require.NoError,
129+
expectNumPackages: 3,
130+
expectNumChannels: 0,
131+
expectNumBundles: 12,
132+
expectNumDeprecations: 1,
133+
expectNumOthers: 1,
132134
},
133135
}
134136

135137
for _, s := range specs {
136138
t.Run(s.name, func(t *testing.T) {
137-
numPackages, numChannels, numBundles, numOthers := 0, 0, 0, 0
139+
numPackages, numChannels, numBundles, numDeprecations, numOthers := 0, 0, 0, 0, 0
138140
err := WalkMetasFS(s.fsys, func(path string, meta *Meta, err error) error {
139141
if err != nil {
140142
return err
@@ -146,6 +148,8 @@ func TestWalkMetasFS(t *testing.T) {
146148
numChannels++
147149
case SchemaBundle:
148150
numBundles++
151+
case SchemaDeprecation:
152+
numDeprecations++
149153
default:
150154
numOthers++
151155
}
@@ -332,6 +336,18 @@ func TestLoadFS(t *testing.T) {
332336
Schema: "olm.bundle",
333337
},
334338
},
339+
Deprecations: []Deprecation{
340+
{
341+
Schema: "olm.catalog.deprecation",
342+
Package: "kiali",
343+
Name: "bobs-discount-name",
344+
Deprecations: []DeprecationEntry{
345+
{Schema: "olm.bundle", Name: "kiali-operator.v1.68.0", Message: json.RawMessage(`"kiali-operator.v1.68.0 is deprecated. Uninstall and install kiali-operator.v1.72.0 for support.\n"`)},
346+
{Schema: "olm.package", Name: "kiali", Message: json.RawMessage(`"package kiali is end of life. Please use 'kiali-new' package for support.\n"`)},
347+
{Schema: "olm.channel", Name: "alpha", Message: json.RawMessage(`"channel alpha is no longer supported. Please switch to channel 'stable'.\n"`)},
348+
},
349+
},
350+
},
335351
Others: []Meta{
336352
{Schema: "unexpected", Package: "", Blob: json.RawMessage(`{ "schema": "unexpected" }`)},
337353
},
@@ -881,6 +897,25 @@ present in the .indexignore file.`),
881897
unrecognizedSchema = &fstest.MapFile{
882898
Data: []byte(`{"schema":"olm.package"}{"schema":"unexpected"}{"schema":"olm.bundle"}`),
883899
}
900+
deprecations = &fstest.MapFile{
901+
Data: []byte(`---
902+
schema: olm.catalog.deprecation
903+
package: kiali
904+
name: bobs-discount-name
905+
deprecations:
906+
- schema: olm.bundle
907+
name: kiali-operator.v1.68.0
908+
message: |
909+
kiali-operator.v1.68.0 is deprecated. Uninstall and install kiali-operator.v1.72.0 for support.
910+
- schema: olm.package
911+
name: kiali
912+
message: |
913+
package kiali is end of life. Please use 'kiali-new' package for support.
914+
- schema: olm.channel
915+
name: alpha
916+
message: |
917+
channel alpha is no longer supported. Please switch to channel 'stable'.`),
918+
}
884919

885920
validFS = fstest.MapFS{
886921
".indexignore": indexIgnore,
@@ -889,18 +924,19 @@ present in the .indexignore file.`),
889924
"etcdoperator.v0.6.1.clusterserviceversion.yaml": etcdCSV,
890925
"README.md": readme,
891926
"unrecognized-schema.json": unrecognizedSchema,
927+
"deprecations.yaml": deprecations,
892928
}
893929
)
894930

931+
type EvaluationFunc func(*testing.T, *DeclarativeConfig)
932+
895933
func TestLoadFile(t *testing.T) {
896934
type spec struct {
897-
name string
898-
fsys fs.FS
899-
path string
900-
assertion require.ErrorAssertionFunc
901-
expectNumPackages int
902-
expectNumBundles int
903-
expectNumOthers int
935+
name string
936+
fsys fs.FS
937+
path string
938+
assertion require.ErrorAssertionFunc
939+
expect EvaluationFunc
904940
}
905941
specs := []spec{
906942
{
@@ -944,22 +980,38 @@ func TestLoadFile(t *testing.T) {
944980
assertion: require.Error,
945981
},
946982
{
947-
name: "Success/UnrecognizedSchema",
948-
fsys: validFS,
949-
path: "unrecognized-schema.json",
950-
assertion: require.NoError,
951-
expectNumPackages: 1,
952-
expectNumBundles: 1,
953-
expectNumOthers: 1,
983+
name: "Success/UnrecognizedSchema",
984+
fsys: validFS,
985+
path: "unrecognized-schema.json",
986+
assertion: require.NoError,
987+
expect: func(t *testing.T, d *DeclarativeConfig) {
988+
require.Equal(t, 1, len(d.Packages))
989+
require.Equal(t, 1, len(d.Bundles))
990+
require.Equal(t, 1, len(d.Others))
991+
},
954992
},
955993
{
956-
name: "Success/ValidFile",
957-
fsys: validFS,
958-
path: "etcd.yaml",
959-
assertion: require.NoError,
960-
expectNumPackages: 1,
961-
expectNumBundles: 6,
962-
expectNumOthers: 0,
994+
name: "Success/ValidFile",
995+
fsys: validFS,
996+
path: "etcd.yaml",
997+
assertion: require.NoError,
998+
expect: func(t *testing.T, d *DeclarativeConfig) {
999+
require.Equal(t, 1, len(d.Packages))
1000+
require.Equal(t, 6, len(d.Bundles))
1001+
require.Equal(t, 0, len(d.Others))
1002+
},
1003+
},
1004+
{
1005+
name: "Success/ValidFile/Deprecations",
1006+
fsys: validFS,
1007+
path: "deprecations.yaml",
1008+
assertion: require.NoError,
1009+
expect: func(t *testing.T, d *DeclarativeConfig) {
1010+
require.Equal(t, 0, len(d.Packages))
1011+
require.Equal(t, 0, len(d.Bundles))
1012+
require.Equal(t, 0, len(d.Others))
1013+
require.Equal(t, 1, len(d.Deprecations))
1014+
},
9631015
},
9641016
}
9651017

@@ -969,9 +1021,7 @@ func TestLoadFile(t *testing.T) {
9691021
s.assertion(t, err)
9701022
if err == nil {
9711023
require.NotNil(t, cfg)
972-
assert.Equal(t, len(cfg.Packages), s.expectNumPackages, "unexpected package count")
973-
assert.Equal(t, len(cfg.Bundles), s.expectNumBundles, "unexpected bundle count")
974-
assert.Equal(t, len(cfg.Others), s.expectNumOthers, "unexpected others count")
1024+
s.expect(t, cfg)
9751025
}
9761026
})
9771027
}

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)