Skip to content

Commit

Permalink
poc for schema import
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Keister <[email protected]>
  • Loading branch information
grokspawn committed Oct 30, 2023
1 parent b137480 commit de0479f
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 44 deletions.
5 changes: 1 addition & 4 deletions alpha/action/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,7 @@ BundleLoop:
func combineConfigs(cfgs []declcfg.DeclarativeConfig) *declcfg.DeclarativeConfig {
out := &declcfg.DeclarativeConfig{}
for _, in := range cfgs {
out.Packages = append(out.Packages, in.Packages...)
out.Channels = append(out.Channels, in.Channels...)
out.Bundles = append(out.Bundles, in.Bundles...)
out.Others = append(out.Others, in.Others...)
out.Merge(&in)
}
return out
}
37 changes: 30 additions & 7 deletions alpha/declcfg/declcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import (
)

const (
SchemaPackage = "olm.package"
SchemaChannel = "olm.channel"
SchemaBundle = "olm.bundle"
SchemaPackage = "olm.package"
SchemaChannel = "olm.channel"
SchemaBundle = "olm.bundle"
SchemaDeprecation = "olm.catalog.deprecation"
)

type DeclarativeConfig struct {
Packages []Package
Channels []Channel
Bundles []Bundle
Others []Meta
Packages []Package
Channels []Channel
Bundles []Bundle
Deprecations []Deprecation
Others []Meta
}

type Package struct {
Expand Down Expand Up @@ -90,6 +92,19 @@ type RelatedImage struct {
Image string `json:"image"`
}

type Deprecation struct {
Schema string `json:"schema"`
Package string `json:"package"`
Name string `json:"name,omitempty"`
Deprecations []DeprecationEntry `json:"deprecations"`
}

type DeprecationEntry struct {
Schema string `json:"schema"`
Name string `json:"name,omitempty"`
Message json.RawMessage `json:"message"`
}

type Meta struct {
Schema string
Package string
Expand Down Expand Up @@ -181,3 +196,11 @@ func extractUniqueMetaKeys(blobMap map[string]any, m *Meta) error {
}
return nil
}

func (destination *DeclarativeConfig) Merge(src *DeclarativeConfig) {
destination.Packages = append(destination.Packages, src.Packages...)
destination.Channels = append(destination.Channels, src.Channels...)
destination.Bundles = append(destination.Bundles, src.Bundles...)
destination.Others = append(destination.Others, src.Others...)
destination.Deprecations = append(destination.Deprecations, src.Deprecations...)
}
12 changes: 7 additions & 5 deletions alpha/declcfg/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,8 @@ func mergeCfgs(ctx context.Context, cfgChan <-chan *DeclarativeConfig, fcfg *Dec
if !ok {
return nil
}
fcfg.Packages = append(fcfg.Packages, cfg.Packages...)
fcfg.Channels = append(fcfg.Channels, cfg.Channels...)
fcfg.Bundles = append(fcfg.Bundles, cfg.Bundles...)
fcfg.Others = append(fcfg.Others, cfg.Others...)
fcfg.Merge(cfg)
}

}
}

Expand Down Expand Up @@ -297,6 +293,12 @@ func LoadReader(r io.Reader) (*DeclarativeConfig, error) {
return fmt.Errorf("parse bundle: %v", err)
}
cfg.Bundles = append(cfg.Bundles, b)
case SchemaDeprecation:
var d Deprecation
if err := json.Unmarshal(in.Blob, &d); err != nil {
return fmt.Errorf("parse deprecation: %w", err)
}
cfg.Deprecations = append(cfg.Deprecations, d)
case "":
return fmt.Errorf("object '%s' is missing root schema field", string(in.Blob))
default:
Expand Down
255 changes: 231 additions & 24 deletions alpha/declcfg/load_test.go

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions alpha/declcfg/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ func writeToEncoder(cfg DeclarativeConfig, enc encoder) error {
pkgNames.Insert(pkgName)
othersByPackage[pkgName] = append(othersByPackage[pkgName], o)
}
deprecationsByPackage := map[string][]Deprecation{}
for _, d := range cfg.Deprecations {
pkgName := d.Package
pkgNames.Insert(pkgName)
deprecationsByPackage[pkgName] = append(deprecationsByPackage[pkgName], d)
}

for _, pName := range pkgNames.List() {
if len(pName) == 0 {
Expand Down Expand Up @@ -418,13 +424,24 @@ func writeToEncoder(cfg DeclarativeConfig, enc encoder) error {
return err
}
}

deprecations := deprecationsByPackage[pName]
sort.SliceStable(deprecations, func(i, j int) bool {
return deprecations[i].Name < deprecations[j].Name
})
for _, d := range deprecations {
if err := enc.Encode(d); err != nil {
return err
}
}
}

for _, o := range othersByPackage[""] {
if err := enc.Encode(o); err != nil {
return err
}
}

return nil
}

Expand Down
5 changes: 1 addition & 4 deletions alpha/template/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,7 @@ func newChannel(pkgName string, chName string) *declcfg.Channel {
func combineConfigs(cfgs []declcfg.DeclarativeConfig) *declcfg.DeclarativeConfig {
out := &declcfg.DeclarativeConfig{}
for _, in := range cfgs {
out.Packages = append(out.Packages, in.Packages...)
out.Channels = append(out.Channels, in.Channels...)
out.Bundles = append(out.Bundles, in.Bundles...)
out.Others = append(out.Others, in.Others...)
out.Merge(&in)
}
return out
}
Expand Down

0 comments on commit de0479f

Please sign in to comment.