Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove provides+required GVK and modify/remove associated tests #408

Merged
merged 2 commits into from
Sep 14, 2023
Merged
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
16 changes: 0 additions & 16 deletions internal/catalogmetadata/filter/bundle_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ func InChannel(channelName string) Predicate[catalogmetadata.Bundle] {
}
}

func ProvidesGVK(gvk *catalogmetadata.GVK) Predicate[catalogmetadata.Bundle] {
return func(bundle *catalogmetadata.Bundle) bool {
providedGVKs, err := bundle.ProvidedGVKs()
if err != nil {
return false
}
for i := 0; i < len(providedGVKs); i++ {
providedGVK := &providedGVKs[i]
if providedGVK.String() == gvk.String() {
return true
}
}
return false
}
}

func WithBundleImage(bundleImage string) Predicate[catalogmetadata.Bundle] {
return func(bundle *catalogmetadata.Bundle) bool {
return bundle.Image == bundleImage
Expand Down
28 changes: 0 additions & 28 deletions internal/catalogmetadata/filter/bundle_predicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,6 @@ func TestInChannel(t *testing.T) {
assert.False(t, f(b3))
}

func TestProvidesGVK(t *testing.T) {
b1 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{
Properties: []property.Property{
{
Type: property.TypeGVK,
Value: json.RawMessage(`[{"group":"foo.io","kind":"Foo","version":"v1"},{"group":"bar.io","kind":"Bar","version":"v1"}]`),
},
},
}}
b2 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{}}
f1 := filter.ProvidesGVK(&catalogmetadata.GVK{
Group: "foo.io",
Version: "v1",
Kind: "Foo",
})
f2 := filter.ProvidesGVK(&catalogmetadata.GVK{
Group: "baz.io",
Version: "v1alpha1",
Kind: "Baz",
})
// Filter with Bundle which provides the GVK should return true
assert.True(t, f1(b1))
// Filter with Bundle which does not provide the GVK should return false
assert.False(t, f2(b1))
// Filter with Bundle with no GVK should return false
assert.False(t, f1(b2))
}

func TestWithBundleImage(t *testing.T) {
b1 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{Image: "fake-image-uri-1"}}
b2 := &catalogmetadata.Bundle{Bundle: declcfg.Bundle{Image: "fake-image-uri-2"}}
Expand Down
80 changes: 44 additions & 36 deletions internal/catalogmetadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/operator-framework/operator-registry/alpha/property"
)

const (
MediaTypePlain = "plain+v0"
MediaTypeRegistry = "registry+v1"
PropertyBundleMediaType = "olm.bundle.mediatype"
)
Comment on lines +14 to +18
Copy link
Member

Choose a reason for hiding this comment

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

I was a bit confused since this (and PackageRequired an some other bits in this file) is not related to GVK removal. But we still need these changes for further entity source removal.

I'm ok to merge it like this.


type Schemas interface {
Package | Bundle | Channel
}
Expand All @@ -23,20 +29,9 @@ type Channel struct {
declcfg.Channel
}

type GVK property.GVK

func (g GVK) String() string {
return fmt.Sprintf(`group:"%s" version:"%s" kind:"%s"`, g.Group, g.Version, g.Kind)
}

type GVKRequired property.GVKRequired

func (g GVKRequired) String() string {
return fmt.Sprintf(`group:"%s" version:"%s" kind:"%s"`, g.Group, g.Version, g.Kind)
}

func (g GVKRequired) AsGVK() GVK {
return GVK(g)
type PackageRequired struct {
property.PackageRequired
SemverRange *bsemver.Range `json:"-"`
}

type Bundle struct {
Expand All @@ -46,11 +41,11 @@ type Bundle struct {

mu sync.RWMutex
// these properties are lazy loaded as they are requested
propertiesMap map[string]property.Property
bundlePackage *property.Package
semVersion *bsemver.Version
providedGVKs []GVK
requiredGVKs []GVKRequired
propertiesMap map[string]property.Property
bundlePackage *property.Package
semVersion *bsemver.Version
requiredPackages []PackageRequired
mediaType string
}

func (b *Bundle) Version() (*bsemver.Version, error) {
Expand All @@ -60,18 +55,19 @@ func (b *Bundle) Version() (*bsemver.Version, error) {
return b.semVersion, nil
}

func (b *Bundle) ProvidedGVKs() ([]GVK, error) {
if err := b.loadProvidedGVKs(); err != nil {
func (b *Bundle) RequiredPackages() ([]PackageRequired, error) {
if err := b.loadRequiredPackages(); err != nil {
return nil, err
}
return b.providedGVKs, nil
return b.requiredPackages, nil
}

func (b *Bundle) RequiredGVKs() ([]GVKRequired, error) {
if err := b.loadRequiredGVKs(); err != nil {
return nil, err
func (b *Bundle) MediaType() (string, error) {
if err := b.loadMediaType(); err != nil {
return "", err
}
return b.requiredGVKs, nil

return b.mediaType, nil
}

func (b *Bundle) loadPackage() error {
Expand All @@ -94,28 +90,40 @@ func (b *Bundle) loadPackage() error {
return nil
}

func (b *Bundle) loadProvidedGVKs() error {
func (b *Bundle) loadRequiredPackages() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.providedGVKs == nil {
providedGVKs, err := loadFromProps[[]GVK](b, property.TypeGVK, false)
if b.requiredPackages == nil {
requiredPackages, err := loadFromProps[[]PackageRequired](b, property.TypePackageRequired, false)
if err != nil {
return fmt.Errorf("error determining provided GVKs for bundle %q: %s", b.Name, err)
return fmt.Errorf("error determining bundle required packages for bundle %q: %s", b.Name, err)
}
for _, requiredPackage := range requiredPackages {
semverRange, err := bsemver.ParseRange(requiredPackage.VersionRange)
if err != nil {
return fmt.Errorf(
"error parsing bundle required package semver range for bundle %q (required package %q): %s",
b.Name,
requiredPackage.PackageName,
err,
)
}
requiredPackage.SemverRange = &semverRange
}
b.providedGVKs = providedGVKs
b.requiredPackages = requiredPackages
}
return nil
}

func (b *Bundle) loadRequiredGVKs() error {
func (b *Bundle) loadMediaType() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.requiredGVKs == nil {
requiredGVKs, err := loadFromProps[[]GVKRequired](b, property.TypeGVKRequired, false)
if b.mediaType == "" {
Copy link
Member

Choose a reason for hiding this comment

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

Hm. We should probably use a pointer here: if nil - try to unmarshal. If non-nil - then we already tried to load it (even if the string itself is empty).

Copy link
Member

Choose a reason for hiding this comment

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

Created #409 for this. Will rebase once this PR gets merged.

mediaType, err := loadFromProps[string](b, PropertyBundleMediaType, false)
if err != nil {
return fmt.Errorf("error determining required GVKs for bundle %q: %s", b.Name, err)
return fmt.Errorf("error determining bundle mediatype for bundle %q: %s", b.Name, err)
}
b.requiredGVKs = requiredGVKs
b.mediaType = mediaType
}
return nil
}
Expand Down
Loading