Skip to content
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
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module github.com/operator-framework/operator-controller
go 1.19

require (
github.com/blang/semver/v4 v4.0.0
github.com/onsi/ginkgo/v2 v2.3.1
github.com/onsi/gomega v1.22.1
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999
github.com/operator-framework/operator-registry v1.26.2
k8s.io/apimachinery v0.25.0
k8s.io/client-go v0.25.0
sigs.k8s.io/controller-runtime v0.13.1
Expand Down Expand Up @@ -40,7 +42,7 @@ require (
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -58,7 +60,7 @@ require (
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/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -221,8 +223,9 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
Expand Down Expand Up @@ -285,6 +288,8 @@ github.com/onsi/gomega v1.22.1 h1:pY8O4lBfsHKZHM/6nrxkhVPUznOlIu3quZcKP/M20KI=
github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM=
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999 h1:3R+Bg57vgekyDqS0w9c7vHNsHck/bo6dkaby6U9wi/8=
github.com/operator-framework/deppy v0.0.0-20230102161649-36fa82370999/go.mod h1:JaF7sX6tn7mpXcOehYjSHiKM1Y0z09vEfC6dca4AVuo=
github.com/operator-framework/operator-registry v1.26.2 h1:kQToR/hPqdivljaRXM0olPllNIcc/GUk1VBoGwagJmk=
github.com/operator-framework/operator-registry v1.26.2/go.mod h1:Z7XIb/3ZkhBQCvMD/rJphyuY4LmU/eWpZS+o0Mm1WAk=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -363,8 +368,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 h1:iU7T1X1J6yxDr0rda54sWGkHgOp5XJrqm79gcNlC2VM=
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down
219 changes: 219 additions & 0 deletions internal/resolution/variable_sources/entity/bundle_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package entity

import (
"encoding/json"
"fmt"
"sync"

"github.com/blang/semver/v4"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/property"
)

const PropertyBundlePath = "olm.bundle.path"

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

type PackageRequired struct {
property.PackageRequired
SemverRange *semver.Range `json:"-"`
}

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)
}

type BundleEntity struct {
*input.Entity

// these properties are lazy loaded as they are requested
bundlePackage *property.Package
providedGVKs []GVK
requiredGVKs []GVKRequired
requiredPackages []PackageRequired
channelProperties *ChannelProperties
semVersion *semver.Version
bundlePath string
mu sync.RWMutex
}

func NewBundleEntity(entity *input.Entity) *BundleEntity {
return &BundleEntity{
Entity: entity,
mu: sync.RWMutex{},
}
}

func (b *BundleEntity) PackageName() (string, error) {
if err := b.loadPackage(); err != nil {
return "", err
}
return b.bundlePackage.PackageName, nil
}

func (b *BundleEntity) Version() (*semver.Version, error) {
if err := b.loadPackage(); err != nil {
return nil, err
}
return b.semVersion, nil
}

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

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

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

func (b *BundleEntity) ChannelName() (string, error) {
if err := b.loadChannelProperties(); err != nil {
return "", err
}
return b.channelProperties.ChannelName, nil
}

func (b *BundleEntity) ChannelProperties() (*ChannelProperties, error) {
if err := b.loadChannelProperties(); err != nil {
return nil, err
}
return b.channelProperties, nil
}

func (b *BundleEntity) BundlePath() (string, error) {
if err := b.loadBundlePath(); err != nil {
return "", err
}
return b.bundlePath, nil
}

func (b *BundleEntity) loadPackage() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.bundlePackage == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there no mutex lock required here unlike the other loadX funcs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!! Fixed

bundlePackage, err := loadFromEntity[property.Package](b.Entity, property.TypePackage)
if err != nil {
return fmt.Errorf("error determining package for entity '%s': %w", b.ID, err)
}
b.bundlePackage = &bundlePackage
if b.semVersion == nil {
semVer, err := semver.Parse(b.bundlePackage.Version)
if err != nil {
return fmt.Errorf("could not parse semver (%s) for entity '%s': %w", b.bundlePackage.Version, b.ID, err)
}
b.semVersion = &semVer
}
}
return nil
}

func (b *BundleEntity) loadProvidedGVKs() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.providedGVKs == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for these values to become out-of-date? If so, if the value in the map was ever updated, this would continue to return the old value right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great observation and question!!
This is read only and just serves to wrap around the Entity, which it doesn't expect to change during its lifetime.

Copy link
Member

@awgreene awgreene Jan 25, 2023

Choose a reason for hiding this comment

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

which it doesn't expect to change during its lifetime.

We may want to document any assumptions that we make. Do we prevent cache changes when deppy is solving?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it == BundleEntity. The corresponding entity can change, but only on the source of truth (e.g. catalog source). But from the time you pull the entity down from source until resolution, it shouldn't change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for answering! Makes perfect sense to me.

providedGVKs, err := loadFromEntity[[]GVK](b.Entity, property.TypeGVK)
if err != nil {
return fmt.Errorf("error determining bundle provided gvks for entity '%s': %w", b.ID, err)
}
b.providedGVKs = providedGVKs
}
return nil
}

func (b *BundleEntity) loadRequiredGVKs() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.requiredGVKs == nil {
requiredGVKs, err := loadFromEntity[[]GVKRequired](b.Entity, property.TypeGVKRequired)
if err != nil {
return fmt.Errorf("error determining bundle required gvks for entity '%s': %w", b.ID, err)
}
b.requiredGVKs = requiredGVKs
}
return nil
}

func (b *BundleEntity) loadRequiredPackages() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.requiredPackages == nil {
requiredPackages, err := loadFromEntity[[]PackageRequired](b.Entity, property.TypePackageRequired)
if err != nil {
return fmt.Errorf("error determining bundle required packages for entity '%s': %w", b.ID, err)
}
for _, requiredPackage := range requiredPackages {
semverRange, err := semver.ParseRange(requiredPackage.VersionRange)
if err != nil {
return fmt.Errorf("error determining bundle required package semver range for entity '%s': '%w'", b.ID, err)
}
requiredPackage.SemverRange = &semverRange
}
b.requiredPackages = requiredPackages
}
return nil
}

func (b *BundleEntity) loadChannelProperties() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.channelProperties == nil {
channel, err := loadFromEntity[ChannelProperties](b.Entity, property.TypeChannel)
if err != nil {
return fmt.Errorf("error determining bundle channel properties for entity '%s': %w", b.ID, err)
}
b.channelProperties = &channel
}
return nil
}

func (b *BundleEntity) loadBundlePath() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.bundlePath == "" {
bundlePath, err := loadFromEntity[string](b.Entity, PropertyBundlePath)
if err != nil {
return fmt.Errorf("error determining bundle path for entity '%s': %w", b.ID, err)
}
b.bundlePath = bundlePath
}
return nil
}

func loadFromEntity[T interface{}](entity *input.Entity, propertyName string) (T, error) {
deserializedProperty := *new(T)
propertyValue, ok := entity.Properties[propertyName]
if !ok {
return deserializedProperty, fmt.Errorf("property '%s' not found", propertyName)
}

if err := json.Unmarshal([]byte(propertyValue), &deserializedProperty); err != nil {
return deserializedProperty, fmt.Errorf("property '%s' ('%s') could not be parsed: %w", propertyName, propertyValue, err)
}
return deserializedProperty, nil
}
Loading