Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,70 @@ go 1.19
require (
github.com/blang/semver/v4 v4.0.0
github.com/go-air/gini v1.0.4
github.com/onsi/ginkgo/v2 v2.3.1
github.com/onsi/gomega v1.22.1
github.com/go-logr/logr v1.2.3
github.com/onsi/ginkgo/v2 v2.4.0
github.com/onsi/gomega v1.23.0
github.com/operator-framework/api v0.17.2
github.com/operator-framework/operator-registry v1.26.2
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.0
github.com/tidwall/gjson v1.14.3
golang.org/x/net v0.3.1-0.20221206200815-1e63c2f08a10
google.golang.org/grpc v1.47.0
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
sigs.k8s.io/controller-runtime v0.13.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/h2non/filetype v1.1.1 // indirect
github.com/h2non/go-is-svg v0.0.0-20160927212452-35e8c4b0612c // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
562 changes: 549 additions & 13 deletions go.sum

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions pkg/deppy/input/catalogsource/catalogsource_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package catalogsource_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestDeppy(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "Deppy Suite")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be "RegistryQuerier Suite" or something like that?

}
161 changes: 161 additions & 0 deletions pkg/deppy/input/catalogsource/registry_bundle_to_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package catalogsource

import (
"fmt"
"sort"

"github.com/operator-framework/operator-registry/alpha/property"
catalogsourceapi "github.com/operator-framework/operator-registry/pkg/api"
"k8s.io/apimachinery/pkg/util/errors"

"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/deppy/pkg/lib/util"
)

type UpgradeEdge struct {
property.Channel
Replaces string `json:"replaces,omitempty"`
Skips []string `json:"skips,omitempty"`
SkipRange string `json:"skipRange,omitempty"`
Version string `json:"version,omitempty"`
}

type DefaultChannel struct {
DefaultChannel string `json:"defaultchannel"`
}

const (
TypeDefaultChannel = "olm.package.defaultChannel"
TypeBundleSource = "olm.bundle.path"
TypeLabel = "olm.label"
TypeLabelRequired = "olm.label.required"
)

func EntityFromBundle(catsrcID string, pkg *catalogsourceapi.Package, bundle *catalogsourceapi.Bundle) (*input.Entity, error) {
properties := map[string]string{}
var errs []error

// Multivalue properties
propsList := map[string]map[string]struct{}{}

setPropertyValue := func(key, value string) {
if _, ok := propsList[key]; !ok {
propsList[key] = map[string]struct{}{}
}
if _, ok := propsList[key][value]; !ok {
propsList[key][value] = struct{}{}
}
}

for _, prvAPI := range bundle.ProvidedApis {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to break these steps into their own functions to make this function a bit smaller and more readable. wdyt?

apiValue, err := util.JSONMarshal(prvAPI)
if err != nil {
errs = append(errs, err)
continue
}
setPropertyValue(property.TypeGVK, string(apiValue))
}

for _, reqAPI := range bundle.RequiredApis {
apiValue, err := util.JSONMarshal(reqAPI)
if err != nil {
errs = append(errs, err)
continue
}
setPropertyValue(property.TypeGVKRequired, string(apiValue))
}

for _, reqAPI := range bundle.Dependencies {
switch reqAPI.Type {
case property.TypeGVK:
setPropertyValue(property.TypeGVKRequired, reqAPI.Value)
case property.TypePackage:
setPropertyValue(property.TypePackageRequired, reqAPI.Value)
case TypeLabel: // legacy property
setPropertyValue(TypeLabelRequired, reqAPI.Value)
default:
setPropertyValue(reqAPI.Type, reqAPI.Value)
}
}

ignoredProperties := map[string]struct{}{
property.TypeBundleObject: {},
}

for _, p := range bundle.Properties {
if _, ok := ignoredProperties[p.Type]; ok {
continue
}
setPropertyValue(p.Type, p.Value)
}

for pType, pValues := range propsList {
var prop []interface{}
for pValue := range pValues {
var v interface{}
err := util.JSONUnmarshal([]byte(pValue), &v)
if err != nil {
errs = append(errs, err)
continue
}
prop = append(prop, v)
}
if len(prop) == 0 {
continue
}
if len(prop) > 1 {
sort.Slice(prop, func(i, j int) bool {
// enforce some ordering for deterministic properties. Possibly a neater way to do this.
return fmt.Sprintf("%v", prop[i]) < fmt.Sprintf("%v", prop[j])
})
}
pValue, err := util.JSONMarshal(prop)
if err != nil {
errs = append(errs, err)
continue
}
properties[pType] = string(pValue)
}

// Singleton properties.
// `olm.package`, `olm.channel`, `olm.defaultChannel`
pkgValue, err := util.JSONMarshal(property.Package{
PackageName: bundle.PackageName,
Version: bundle.Version,
})
if err != nil {
errs = append(errs, err)
} else {
properties[property.TypePackage] = string(pkgValue)
}

upValue, err := util.JSONMarshal(UpgradeEdge{
Channel: property.Channel{
ChannelName: bundle.ChannelName,
},
Replaces: bundle.Replaces,
Skips: bundle.Skips,
SkipRange: bundle.SkipRange,
// Version: bundle.Version,
})
if err != nil {
errs = append(errs, err)
} else {
properties[property.TypeChannel] = string(upValue)
}

properties[TypeDefaultChannel] = pkg.DefaultChannelName
properties[TypeBundleSource] = bundle.BundlePath

if len(errs) > 0 {
return nil, fmt.Errorf("failed to parse properties for bundle %s/%s in %s: %v", bundle.GetPackageName(), bundle.GetVersion(), catsrcID, errors.NewAggregate(errs))
}

// Since multiple instances of bundle may exist for different channels, entityID must include reference to channel
entityIDFromBundle := func(catsrcID string, bundle *catalogsourceapi.Bundle) deppy.Identifier {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just push this out into a proper function rather than an inline one?

return deppy.Identifier(fmt.Sprintf("%s/%s/%s/%s", catsrcID, bundle.PackageName, bundle.ChannelName, bundle.Version))
}

return input.NewEntity(entityIDFromBundle(catsrcID, bundle), properties), nil
}
118 changes: 118 additions & 0 deletions pkg/deppy/input/catalogsource/registry_bundle_to_entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package catalogsource_test

import (
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/deppy/pkg/deppy/input/catalogsource"

catalogsourceapi "github.com/operator-framework/operator-registry/pkg/api"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("RegistryBundleConverter", func() {
It("generates entity from bundle", func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may want to also poke at some of the error cases

// When converting a bundle to an entity, package, channel, defaultChannel and bundlePath
// must be created as special properties that are not overwritten
// The remaining entity properties must be aggregated from the bundle ProvidedAPIs, RequiredAPIs,
// Dependencies and Properties.
// Properties must be preserved except for `olm.bundle.object` properties, which are irrelevant to resolution.
// ProvidedAPIs, RequiredAPIs and Dependencies must be converted from their legacy format
// Values in the aggregated property set must not have duplicates.
b := &catalogsourceapi.Bundle{
CsvName: "test",
PackageName: "test",
ChannelName: "beta",
BundlePath: "path/to/bundle",
Version: "0.1.4",
SkipRange: "< 0.1.4",
Replaces: "test-operator.v0.0.1",
Skips: []string{"test-operator.v0.0.2", "test-operator.v0.0.3"},
ProvidedApis: []*catalogsourceapi.GroupVersionKind{{
Group: "foo",
Version: "v1",
Kind: "prov1",
}, {
Group: "foo",
Version: "v1",
Kind: "prov2",
}},
RequiredApis: []*catalogsourceapi.GroupVersionKind{{
Group: "foo",
Version: "v1",
Kind: "req1",
}, {
Group: "foo",
Version: "v1",
Kind: "req2",
}, {
Group: "foo",
Version: "v1",
Kind: "req3",
}, {
Group: "foo",
Version: "v1",
Kind: "req4",
}},
Dependencies: []*catalogsourceapi.Dependency{
// legacy constraint types
{
Type: "olm.gvk",
Value: `{"group":"foo","version":"v1","kind":"req1"}`,
}, {
Type: "olm.gvk",
Value: `{"group":"foo","version":"v1","kind":"req2"}`,
}, {
Type: "olm.package",
Value: `{"packageName":"dep1","version":"1.1.0"}`,
}, {
Type: "olm.package",
Value: `{"packageName":"dep2","version":"<1.1.0"}`,
}},
Properties: []*catalogsourceapi.Property{
{
Type: "olm.package",
Value: `{"packageName":"other","version":"0.1.4"}`,
}, {
Type: "olm.gvk",
Value: `{"group":"foo","version":"v1","kind":"prov1"}`,
}, {
Type: "olm.gvk.required",
Value: `{"group":"foo","version":"v1","kind":"req1"}`,
}, {
Type: "olm.gvk.required",
Value: `{"group":"foo","version":"v1","kind":"req3"}`,
}, {
Type: "olm.package.required",
Value: `{"packageName":"dep1","version":"1.1.0"}`,
}, {
Type: "olm.maxOpenShiftVersion",
Value: "4.12",
}, {
Type: "olm.deprecated",
Value: "{}",
}, {
//must be omitted
Type: "olm.bundle.object",
Value: `{"data": "eyJraW5kIjogIkN1c3RvbVJlc291cmNlRGVmaW5pdGlvbiIsICJhcGlWZXJzaW9uIjogImFwaWV4dGVuc2lvbnMuazhzLmlvL3YxIn0="}`,
}},
}
p := &catalogsourceapi.Package{DefaultChannelName: "stable"}
entity, err := catalogsource.EntityFromBundle("test-catalog", p, b)
Expect(err).To(BeNil())
Expect(entity).To(Equal(&input.Entity{
ID: "test-catalog/test/beta/0.1.4",
Properties: map[string]string{
"olm.package": `{"packageName":"test","version":"0.1.4"}`,
"olm.channel": `{"channelName":"beta","priority":0,"replaces":"test-operator.v0.0.1","skips":["test-operator.v0.0.2","test-operator.v0.0.3"],"skipRange":"< 0.1.4"}`,
"olm.package.defaultChannel": "stable",
"olm.bundle.path": "path/to/bundle",
"olm.maxOpenShiftVersion": "[4.12]",
"olm.deprecated": "[{}]",
"olm.package.required": `[{"packageName":"dep1","version":"1.1.0"},{"packageName":"dep2","version":"<1.1.0"}]`,
"olm.gvk": `[{"group":"foo","kind":"prov1","version":"v1"},{"group":"foo","kind":"prov2","version":"v1"}]`,
"olm.gvk.required": `[{"group":"foo","kind":"req1","version":"v1"},{"group":"foo","kind":"req2","version":"v1"},{"group":"foo","kind":"req3","version":"v1"},{"group":"foo","kind":"req4","version":"v1"}]`,
},
}))
})
})
Loading