Skip to content

Commit

Permalink
Merge pull request #248 from tylerslaton/sync-constraint-changes
Browse files Browse the repository at this point in the history
Bug 2034319: Sync constraint changes
  • Loading branch information
openshift-merge-robot committed Jan 28, 2022
2 parents d795a1d + fbfd54e commit 5863540
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 76 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -12,7 +12,7 @@ require (
github.com/mikefarah/yq/v3 v3.0.0-20201202084205-8846255d1c37
github.com/onsi/ginkgo v1.16.4
github.com/openshift/api v0.0.0-20200331152225-585af27e34fd
github.com/operator-framework/api v0.10.8-0.20211210205029-40cb9fd4036a
github.com/operator-framework/api v0.12.0
github.com/operator-framework/operator-lifecycle-manager v0.0.0-00010101000000-000000000000
github.com/operator-framework/operator-registry v1.17.5
github.com/sirupsen/logrus v1.8.1
Expand Down
13 changes: 7 additions & 6 deletions staging/api/pkg/constraints/constraint.go
Expand Up @@ -12,9 +12,9 @@ const OLMConstraintType = "olm.constraint"

// Constraint holds parsed, potentially nested dependency constraints.
type Constraint struct {
// Constraint message that surfaces in resolution
// Constraint failure message that surfaces in resolution
// This field is optional
Message string `json:"message,omitempty" yaml:"message,omitempty"`
FailureMessage string `json:"failureMessage,omitempty" yaml:"failureMessage,omitempty"`

// The cel struct that contraints CEL expression
Cel *Cel `json:"cel,omitempty" yaml:"cel,omitempty"`
Expand All @@ -25,14 +25,15 @@ type Constraint struct {
// GVK defines a constraint for a GVK.
GVK *GVKConstraint `json:"gvk,omitempty" yaml:"gvk,omitempty"`

// All, Any, and None are compound constraints. See this enhancement for details:
// All, Any, and Not are compound constraints. See this enhancement for details:
// https://github.com/operator-framework/enhancements/blob/master/enhancements/compound-bundle-constraints.md
All *CompoundConstraint `json:"all,omitempty" yaml:"all,omitempty"`
Any *CompoundConstraint `json:"any,omitempty" yaml:"any,omitempty"`
// A note on None: this constraint is not particularly useful by itself.
// A note on Not: this constraint isn't particularly useful by itself.
// It should be used within an All constraint alongside some other constraint type
// since saying "none of these GVKs/packages/etc." without an alternative doesn't make sense.
None *CompoundConstraint `json:"none,omitempty" yaml:"none,omitempty"`
// since saying "do not use any of these GVKs/packages/etc." without an alternative
// doesn't make sense.
Not *CompoundConstraint `json:"not,omitempty" yaml:"not,omitempty"`
}

// CompoundConstraint holds a list of potentially nested constraints
Expand Down
58 changes: 29 additions & 29 deletions staging/api/pkg/constraints/constraint_test.go
Expand Up @@ -22,28 +22,28 @@ func TestParse(t *testing.T) {
name: "Valid/BasicGVK",
input: json.RawMessage(inputBasicGVK),
expConstraint: Constraint{
Message: "blah",
GVK: &GVKConstraint{Group: "example.com", Version: "v1", Kind: "Foo"},
FailureMessage: "blah",
GVK: &GVKConstraint{Group: "example.com", Version: "v1", Kind: "Foo"},
},
},
{
name: "Valid/BasicPackage",
input: json.RawMessage(inputBasicPackage),
expConstraint: Constraint{
Message: "blah",
Package: &PackageConstraint{PackageName: "foo", VersionRange: ">=1.0.0"},
FailureMessage: "blah",
Package: &PackageConstraint{PackageName: "foo", VersionRange: ">=1.0.0"},
},
},
{
name: "Valid/BasicAll",
input: json.RawMessage(fmt.Sprintf(inputBasicCompoundTmpl, "all")),
expConstraint: Constraint{
Message: "blah",
FailureMessage: "blah",
All: &CompoundConstraint{
Constraints: []Constraint{
{
Message: "blah blah",
Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"},
FailureMessage: "blah blah",
Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"},
},
},
},
Expand All @@ -53,27 +53,27 @@ func TestParse(t *testing.T) {
name: "Valid/BasicAny",
input: json.RawMessage(fmt.Sprintf(inputBasicCompoundTmpl, "any")),
expConstraint: Constraint{
Message: "blah",
FailureMessage: "blah",
Any: &CompoundConstraint{
Constraints: []Constraint{
{
Message: "blah blah",
Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"},
FailureMessage: "blah blah",
Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"},
},
},
},
},
},
{
name: "Valid/BasicNone",
input: json.RawMessage(fmt.Sprintf(inputBasicCompoundTmpl, "none")),
name: "Valid/BasicNot",
input: json.RawMessage(fmt.Sprintf(inputBasicCompoundTmpl, "not")),
expConstraint: Constraint{
Message: "blah",
None: &CompoundConstraint{
FailureMessage: "blah",
Not: &CompoundConstraint{
Constraints: []Constraint{
{
Message: "blah blah",
Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"},
FailureMessage: "blah blah",
Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"},
},
},
},
Expand All @@ -83,13 +83,13 @@ func TestParse(t *testing.T) {
name: "Valid/Complex",
input: json.RawMessage(inputComplex),
expConstraint: Constraint{
Message: "blah",
FailureMessage: "blah",
All: &CompoundConstraint{
Constraints: []Constraint{
{Package: &PackageConstraint{PackageName: "fuz", VersionRange: ">=1.0.0"}},
{GVK: &GVKConstraint{Group: "fals.example.com", Kind: "Fal", Version: "v1"}},
{
Message: "foo and buf must be stable versions",
FailureMessage: "foo and buf must be stable versions",
All: &CompoundConstraint{
Constraints: []Constraint{
{Package: &PackageConstraint{PackageName: "foo", VersionRange: ">=1.0.0"}},
Expand All @@ -99,7 +99,7 @@ func TestParse(t *testing.T) {
},
},
{
Message: "blah blah",
FailureMessage: "blah blah",
Any: &CompoundConstraint{
Constraints: []Constraint{
{GVK: &GVKConstraint{Group: "foos.example.com", Kind: "Foo", Version: "v1beta1"}},
Expand All @@ -109,7 +109,7 @@ func TestParse(t *testing.T) {
},
},
{
None: &CompoundConstraint{
Not: &CompoundConstraint{
Constraints: []Constraint{
{GVK: &GVKConstraint{Group: "bazs.example.com", Kind: "Baz", Version: "v1alpha1"}},
},
Expand All @@ -132,7 +132,7 @@ func TestParse(t *testing.T) {
{
name: "Invalid/UnknownField",
input: json.RawMessage(
`{"message": "something", "arbitrary": {"key": "value"}}`,
`{"failureMessage": "something", "arbitrary": {"key": "value"}}`,
),
expError: `json: unknown field "arbitrary"`,
},
Expand All @@ -153,7 +153,7 @@ func TestParse(t *testing.T) {

const (
inputBasicGVK = `{
"message": "blah",
"failureMessage": "blah",
"gvk": {
"group": "example.com",
"version": "v1",
Expand All @@ -162,19 +162,19 @@ const (
}`

inputBasicPackage = `{
"message": "blah",
"failureMessage": "blah",
"package": {
"packageName": "foo",
"versionRange": ">=1.0.0"
}
}`

inputBasicCompoundTmpl = `{
"message": "blah",
"failureMessage": "blah",
"%s": {
"constraints": [
{
"message": "blah blah",
"failureMessage": "blah blah",
"package": {
"packageName": "fuz",
"versionRange": ">=1.0.0"
Expand All @@ -185,7 +185,7 @@ const (
`

inputComplex = `{
"message": "blah",
"failureMessage": "blah",
"all": {
"constraints": [
{
Expand All @@ -202,7 +202,7 @@ const (
}
},
{
"message": "foo and buf must be stable versions",
"failureMessage": "foo and buf must be stable versions",
"all": {
"constraints": [
{
Expand All @@ -228,7 +228,7 @@ const (
}
},
{
"message": "blah blah",
"failureMessage": "blah blah",
"any": {
"constraints": [
{
Expand Down Expand Up @@ -256,7 +256,7 @@ const (
}
},
{
"none": {
"not": {
"constraints": [
{
"gvk": {
Expand Down
2 changes: 1 addition & 1 deletion staging/operator-lifecycle-manager/go.mod
Expand Up @@ -25,7 +25,7 @@ require (
github.com/onsi/gomega v1.15.0
github.com/openshift/api v0.0.0-20200331152225-585af27e34fd
github.com/openshift/client-go v0.0.0-20200326155132-2a6cd50aedd0
github.com/operator-framework/api v0.10.8-0.20211210205029-40cb9fd4036a
github.com/operator-framework/api v0.12.0
github.com/operator-framework/operator-registry v1.17.5
github.com/otiai10/copy v1.2.0
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions staging/operator-lifecycle-manager/go.sum
Expand Up @@ -927,8 +927,8 @@ github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJ
github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/operator-framework/api v0.7.1/go.mod h1:L7IvLd/ckxJEJg/t4oTTlnHKAJIP/p51AvEslW3wYdY=
github.com/operator-framework/api v0.10.8-0.20211210205029-40cb9fd4036a h1:tX+zUHdTkn7NfLt9Y8zjsfmSeZkE+sKQEZRqYkVdHYo=
github.com/operator-framework/api v0.10.8-0.20211210205029-40cb9fd4036a/go.mod h1:FTiYGm11fZQ3cSX+EQHc/UWoGZAwkGfyeHU+wMJ8jmA=
github.com/operator-framework/api v0.12.0 h1:aHxHk50/Y1J4Ogdk2J6tYofgX+GEqyBPCMyun+JFqV4=
github.com/operator-framework/api v0.12.0/go.mod h1:FTiYGm11fZQ3cSX+EQHc/UWoGZAwkGfyeHU+wMJ8jmA=
github.com/operator-framework/operator-registry v1.17.5 h1:LR8m1rFz5Gcyje8WK6iYt+gIhtzqo52zMRALdmTYHT0=
github.com/operator-framework/operator-registry v1.17.5/go.mod h1:sRQIgDMZZdUcmHltzyCnM6RUoDF+WS8Arj1BQIARDS8=
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
Expand Down
Expand Up @@ -358,9 +358,9 @@ func CountingPredicate(p Predicate, n *int) Predicate {
}

type celPredicate struct {
program constraints.CelProgram
rule string
message string
program constraints.CelProgram
rule string
failureMessage string
}

func (cp *celPredicate) Test(entry *Entry) bool {
Expand All @@ -383,14 +383,14 @@ func (cp *celPredicate) Test(entry *Entry) bool {
return ok
}

func CreateCelPredicate(env *constraints.CelEnvironment, rule string, message string) (Predicate, error) {
func CreateCelPredicate(env *constraints.CelEnvironment, rule string, failureMessage string) (Predicate, error) {
prog, err := env.Validate(rule)
if err != nil {
return nil, err
}
return &celPredicate{program: prog, rule: rule, message: message}, nil
return &celPredicate{program: prog, rule: rule, failureMessage: failureMessage}, nil
}

func (cp *celPredicate) String() string {
return fmt.Sprintf("with constraint: %q and message: %q", cp.rule, cp.message)
return fmt.Sprintf("with constraint: %q and message: %q", cp.rule, cp.failureMessage)
}
Expand Up @@ -815,11 +815,11 @@ func (pc *predicateConverter) convertConstraints(constraints ...constraints.Cons
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...)
case constraint.Not != nil:
subs, perr := pc.convertConstraints(constraint.Not.Constraints...)
preds[i], err = cache.Not(subs...), perr
case constraint.Cel != nil:
preds[i], err = cache.CreateCelPredicate(pc.celEnv, constraint.Cel.Rule, constraint.Message)
preds[i], err = cache.CreateCelPredicate(pc.celEnv, constraint.Cel.Rule, constraint.FailureMessage)
default:
// Unknown constraint types are handled by constraints.Parse(),
// but parsed constraints may be empty.
Expand Down
Expand Up @@ -803,7 +803,7 @@ func TestSolveOperators_OLMConstraint_CompoundAll(t *testing.T) {
catName: csName, catNamespace: namespace,
properties: []*api.Property{{
Type: constraints.OLMConstraintType,
Value: `{"message": "all constraint",
Value: `{"failureMessage": "all constraint",
"all": {"constraints": [
{"package": {"packageName": "foo", "versionRange": ">=1.0.0"}},
{"gvk": {"group": "g1", "version": "v1", "kind": "k1"}},
Expand Down Expand Up @@ -885,7 +885,7 @@ func TestSolveOperators_OLMConstraint_CompoundAny(t *testing.T) {
catName: csName, catNamespace: namespace,
properties: []*api.Property{{
Type: constraints.OLMConstraintType,
Value: `{"message": "any constraint",
Value: `{"failureMessage": "any constraint",
"any": {"constraints": [
{"gvk": {"group": "g1", "version": "v1", "kind": "k1"}},
{"gvk": {"group": "g2", "version": "v2", "kind": "k2"}}
Expand Down Expand Up @@ -953,7 +953,7 @@ func TestSolveOperators_OLMConstraint_CompoundAny(t *testing.T) {
}
}

func TestSolveOperators_OLMConstraint_CompoundNone(t *testing.T) {
func TestSolveOperators_OLMConstraint_CompoundNot(t *testing.T) {
namespace := "olm"
csName := "community"
catalog := cache.SourceKey{Name: csName, Namespace: namespace}
Expand All @@ -965,10 +965,10 @@ func TestSolveOperators_OLMConstraint_CompoundNone(t *testing.T) {
properties: []*api.Property{
{
Type: constraints.OLMConstraintType,
Value: `{"message": "compound none constraint",
Value: `{"failureMessage": "compound not constraint",
"all": {"constraints": [
{"gvk": {"group": "g0", "version": "v0", "kind": "k0"}},
{"none": {"constraints": [
{"not": {"constraints": [
{"gvk": {"group": "g1", "version": "v1", "kind": "k1"}},
{"gvk": {"group": "g2", "version": "v2", "kind": "k2"}}
]}}
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func TestSolveOperators_OLMConstraint_Unknown(t *testing.T) {
catName: csName, catNamespace: namespace,
properties: []*api.Property{{
Type: constraints.OLMConstraintType,
Value: `{"message": "unknown constraint", "unknown": {"foo": "bar"}}`,
Value: `{"failureMessage": "unknown constraint", "unknown": {"foo": "bar"}}`,
}},
}}

Expand Down Expand Up @@ -2389,21 +2389,21 @@ func TestSolveOperators_GenericConstraint(t *testing.T) {
deps1 := []*api.Dependency{
{
Type: "olm.constraint",
Value: `{"message":"gvk-constraint",
Value: `{"failureMessage":"gvk-constraint",
"cel":{"rule":"properties.exists(p, p.type == 'olm.gvk' && p.value == {'group': 'g', 'version': 'v', 'kind': 'k'})"}}`,
},
}
deps2 := []*api.Dependency{
{
Type: "olm.constraint",
Value: `{"message":"gvk2-constraint",
Value: `{"failureMessage":"gvk2-constraint",
"cel":{"rule":"properties.exists(p, p.type == 'olm.gvk' && p.value == {'group': 'g2', 'version': 'v', 'kind': 'k'})"}}`,
},
}
deps3 := []*api.Dependency{
{
Type: "olm.constraint",
Value: `{"message":"package-constraint",
Value: `{"failureMessage":"package-constraint",
"cel":{"rule":"properties.exists(p, p.type == 'olm.package' && p.value.packageName == 'packageB' && (semver_compare(p.value.version, '1.0.1') == 0))"}}`,
},
}
Expand Down
Expand Up @@ -4,3 +4,8 @@ dependencies:
group: testapi.coreos.com
kind: testapi
version: v1
- type: olm.constraint
value:
failureMessage: 'require to have "certified"'
cel:
rule: 'properties.exists(p, p.type == "certified")'
2 changes: 1 addition & 1 deletion staging/operator-registry/go.mod
Expand Up @@ -36,7 +36,7 @@ require (
github.com/onsi/gomega v1.15.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.2-0.20190823105129-775207bd45b6
github.com/operator-framework/api v0.10.8-0.20211210002341-1eb6c0266cce
github.com/operator-framework/api v0.12.0
github.com/otiai10/copy v1.2.0
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions staging/operator-registry/go.sum
Expand Up @@ -645,8 +645,8 @@ github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700 h1:e
github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
github.com/operator-framework/api v0.10.8-0.20211210002341-1eb6c0266cce h1:+zEJrt6Kw4SnN115WzleIpmw5jAgscrWb9K0WgvjhOc=
github.com/operator-framework/api v0.10.8-0.20211210002341-1eb6c0266cce/go.mod h1:FTiYGm11fZQ3cSX+EQHc/UWoGZAwkGfyeHU+wMJ8jmA=
github.com/operator-framework/api v0.12.0 h1:aHxHk50/Y1J4Ogdk2J6tYofgX+GEqyBPCMyun+JFqV4=
github.com/operator-framework/api v0.12.0/go.mod h1:FTiYGm11fZQ3cSX+EQHc/UWoGZAwkGfyeHU+wMJ8jmA=
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
Expand Down

0 comments on commit 5863540

Please sign in to comment.