Skip to content

Commit

Permalink
feat(constraint): Add constraint type validation to bundle validation
Browse files Browse the repository at this point in the history
A new type of dependency named constraint (olm.constraint) which uses
CEL expression is introduced in OLM. This new type is added to
dependencies.json file in the bundle just like gvk and package
dependencies. As a result, a new validation for constraint type
is added to ensure this type will be validated for bundle
dependencies.

Signed-off-by: Vu Dinh <vudinh@outlook.com>

Vendor the constraints pkg from api repo

Signed-off-by: Vu Dinh <vudinh@outlook.com>
  • Loading branch information
dinhxuanvu committed Dec 15, 2021
1 parent acaac87 commit c649224
Show file tree
Hide file tree
Showing 184 changed files with 45,975 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVK
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210826220005-b48c857c3a0e h1:GCzyKMDDjSGnlpl3clrdAK7I1AaVoaiKDOYkUzChZzg=
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210826220005-b48c857c3a0e/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -383,6 +384,7 @@ github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e/go.mod h1:0AA//
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
github.com/google/cel-go v0.9.0 h1:u1hg7lcZ/XWw2d3aV1jFS30ijQQ6q0/h1C2ZBeBD1gY=
github.com/google/cel-go v0.9.0/go.mod h1:U7ayypeSkw23szu4GaQTPJGx66c20mx8JklMSxrmI1w=
github.com/google/cel-spec v0.6.0/go.mod h1:Nwjgxy5CbjlPrtCWjeDjUyKMl8w41YBYGjsyDdqk0xA=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
Expand Down Expand Up @@ -765,6 +767,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 2 additions & 0 deletions pkg/lib/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func validateDependencies(dependenciesFile *registry.DependenciesFile) []error {
errs = dp.Validate()
case registry.LabelDependency:
errs = dp.Validate()
case registry.CelConstraint:
errs = dp.Validate()
default:
errs = append(errs, fmt.Errorf("unsupported dependency type %s", d.GetType()))
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/registry/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/blang/semver/v4"
"github.com/operator-framework/api/pkg/constraints"
)

var (
Expand Down Expand Up @@ -55,6 +56,7 @@ const (
DeprecatedType = "olm.deprecated"
LabelType = "olm.label"
PropertyKey = "olm.properties"
ConstraintType = "olm.constraint"
)

// APIKey stores GroupVersionKind for use as map keys
Expand Down Expand Up @@ -220,6 +222,16 @@ type LabelDependency struct {
Label string `json:"label" yaml:"label"`
}

type CelConstraint struct {
// Constraint message that surfaces in resolution
// This field is optional
Message string `json:"message" yaml:"message"`

// The cel struct that contraints CEL expression
// This field is required
Cel *constraints.Cel `json:"cel" yaml:"cel"`
}

type GVKProperty struct {
// The group of GVK based property
Group string `json:"group" yaml:"group"`
Expand Down Expand Up @@ -289,6 +301,25 @@ func (pd *PackageDependency) Validate() []error {
return errs
}

// Validate will validate constraint type and return error(s)
func (cc *CelConstraint) Validate() []error {
errs := []error{}
if cc.Cel == nil {
errs = append(errs, fmt.Errorf("The CEL field is missing"))
} else {
if cc.Cel.Rule == "" {
errs = append(errs, fmt.Errorf("The CEL expression is missing"))
return errs
}
validator := constraints.NewCelEnvironment()
_, err := validator.Validate(cc.Cel.Rule)
if err != nil {
errs = append(errs, fmt.Errorf("Invalid CEL expression: %s", err.Error()))
}
}
return errs
}

// GetDependencies returns the list of dependency
func (d *DependenciesFile) GetDependencies() []*Dependency {
var dependencies []*Dependency
Expand Down Expand Up @@ -329,6 +360,13 @@ func (e *Dependency) GetTypeValue() interface{} {
return nil
}
return dep
case ConstraintType:
dep := CelConstraint{}
err := json.Unmarshal([]byte(e.GetValue()), &dep)
if err != nil {
return nil
}
return dep
}
return nil
}
Expand Down
26 changes: 26 additions & 0 deletions vendor/github.com/antlr/antlr4/runtime/Go/antlr/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions vendor/github.com/antlr/antlr4/runtime/Go/antlr/atn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c649224

Please sign in to comment.