-
Notifications
You must be signed in to change notification settings - Fork 560
feat(resolver): olm.constraint
property and compound constraints
#2418
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
Merged
openshift-merge-robot
merged 1 commit into
operator-framework:master
from
estroz:feat/compound-constraint-gated
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"github.com/sirupsen/logrus" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
||
"github.com/operator-framework/api/pkg/constraints" | ||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
v1alpha1listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1" | ||
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache" | ||
|
@@ -27,12 +28,14 @@ type OperatorResolver interface { | |
type SatResolver struct { | ||
cache cache.OperatorCacheProvider | ||
estroz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log logrus.FieldLogger | ||
pc *predicateConverter | ||
} | ||
|
||
func NewDefaultSatResolver(rcp cache.SourceProvider, catsrcLister v1alpha1listers.CatalogSourceLister, logger logrus.FieldLogger) *SatResolver { | ||
return &SatResolver{ | ||
cache: cache.New(rcp, cache.WithLogger(logger), cache.WithCatalogSourceLister(catsrcLister)), | ||
log: logger, | ||
pc: &predicateConverter{}, | ||
} | ||
} | ||
|
||
|
@@ -337,7 +340,7 @@ func (r *SatResolver) getBundleInstallables(preferredNamespace string, bundleSta | |
|
||
visited[bundle] = &bundleInstallable | ||
|
||
dependencyPredicates, err := DependencyPredicates(bundle.Properties) | ||
dependencyPredicates, err := r.pc.convertDependencyProperties(bundle.Properties) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
|
@@ -733,10 +736,14 @@ func sortChannel(bundles []*cache.Entry) ([]*cache.Entry, error) { | |
return chains[0], nil | ||
} | ||
|
||
func DependencyPredicates(properties []*api.Property) ([]cache.Predicate, error) { | ||
// predicateConverter configures olm.constraint value -> predicate conversion for the resolver. | ||
type predicateConverter struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will contain fields when CEL is added. |
||
|
||
// convertDependencyProperties converts all known constraint properties to predicates. | ||
func (pc *predicateConverter) convertDependencyProperties(properties []*api.Property) ([]cache.Predicate, error) { | ||
var predicates []cache.Predicate | ||
for _, property := range properties { | ||
predicate, err := predicateForProperty(property) | ||
predicate, err := pc.predicateForProperty(property) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -748,18 +755,80 @@ func DependencyPredicates(properties []*api.Property) ([]cache.Predicate, error) | |
return predicates, nil | ||
} | ||
|
||
func predicateForProperty(property *api.Property) (cache.Predicate, error) { | ||
func (pc *predicateConverter) predicateForProperty(property *api.Property) (cache.Predicate, error) { | ||
if property == nil { | ||
return nil, nil | ||
} | ||
p, ok := predicates[property.Type] | ||
|
||
// olm.constraint holds all constraint types except legacy types, | ||
// so defer error handling to its parser. | ||
if property.Type == constraints.OLMConstraintType { | ||
return pc.predicateForConstraintProperty(property.Value) | ||
} | ||
|
||
// Legacy behavior dictates that unknown properties are ignored. See enhancement for details: | ||
// https://github.com/operator-framework/enhancements/blob/master/enhancements/compound-bundle-constraints.md | ||
p, ok := legacyPredicateParsers[property.Type] | ||
if !ok { | ||
return nil, nil | ||
} | ||
return p(property.Value) | ||
} | ||
|
||
var predicates = map[string]func(string) (cache.Predicate, error){ | ||
func (pc *predicateConverter) predicateForConstraintProperty(value string) (cache.Predicate, error) { | ||
constraint, err := constraints.Parse(json.RawMessage([]byte(value))) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse olm.constraint: %v", err) | ||
} | ||
|
||
preds, err := pc.convertConstraints(constraint) | ||
if err != nil { | ||
return nil, fmt.Errorf("convert olm.constraint to resolver predicate: %v", err) | ||
} | ||
return preds[0], nil | ||
} | ||
|
||
// convertConstraints creates predicates from each element of constraints, recursing on compound constraints. | ||
// New constraint types added to the constraints package must be handled here. | ||
func (pc *predicateConverter) convertConstraints(constraints ...constraints.Constraint) ([]cache.Predicate, error) { | ||
|
||
preds := make([]cache.Predicate, len(constraints)) | ||
for i, constraint := range constraints { | ||
|
||
var err error | ||
switch { | ||
case constraint.GVK != nil: | ||
preds[i] = cache.ProvidingAPIPredicate(opregistry.APIKey{ | ||
Group: constraint.GVK.Group, | ||
Version: constraint.GVK.Version, | ||
Kind: constraint.GVK.Kind, | ||
}) | ||
case constraint.Package != nil: | ||
preds[i], err = newPackageRequiredPredicate(constraint.Package.PackageName, constraint.Package.VersionRange) | ||
case constraint.All != nil: | ||
subs, perr := pc.convertConstraints(constraint.All.Constraints...) | ||
preds[i], err = cache.And(subs...), perr | ||
case constraint.Any != nil: | ||
subs, perr := pc.convertConstraints(constraint.Any.Constraints...) | ||
preds[i], err = cache.Or(subs...), perr | ||
case constraint.None != nil: | ||
subs, perr := pc.convertConstraints(constraint.None.Constraints...) | ||
preds[i], err = cache.Not(subs...), perr | ||
default: | ||
// Unknown constraint types are handled by constraints.Parse(), | ||
// but parsed constraints may be empty. | ||
return nil, fmt.Errorf("constraint is empty") | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
} | ||
|
||
return preds, nil | ||
} | ||
|
||
var legacyPredicateParsers = map[string]func(string) (cache.Predicate, error){ | ||
"olm.gvk.required": predicateForRequiredGVKProperty, | ||
"olm.package.required": predicateForRequiredPackageProperty, | ||
"olm.label.required": predicateForRequiredLabelProperty, | ||
|
@@ -789,11 +858,15 @@ func predicateForRequiredPackageProperty(value string) (cache.Predicate, error) | |
if err := json.Unmarshal([]byte(value), &pkg); err != nil { | ||
return nil, err | ||
} | ||
ver, err := semver.ParseRange(pkg.VersionRange) | ||
return newPackageRequiredPredicate(pkg.PackageName, pkg.VersionRange) | ||
} | ||
|
||
func newPackageRequiredPredicate(name, verRange string) (cache.Predicate, error) { | ||
ver, err := semver.ParseRange(verRange) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cache.And(cache.PkgPredicate(pkg.PackageName), cache.VersionInRangePredicate(ver, pkg.VersionRange)), nil | ||
return cache.And(cache.PkgPredicate(name), cache.VersionInRangePredicate(ver, verRange)), nil | ||
} | ||
|
||
func predicateForRequiredLabelProperty(value string) (cache.Predicate, error) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.