Skip to content

Commit

Permalink
Support CEL for matchExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Aug 6, 2024
1 parent da3756a commit 92122e2
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 77 deletions.
21 changes: 13 additions & 8 deletions kustomize/crd/bases/kwok.x-k8s.io_stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,16 @@ spec:
description: MatchExpressions is a list of label selector requirements.
The requirements are ANDed.
items:
description: |-
SelectorRequirement is a resource selector requirement is a selector that contains values, a key,
and an operator that relates the key and values.
description: SelectorExpression is a resource selector expression
is a set of requirements that must be true for a match.
properties:
expression:
description: Expression represents the expression which
will be evaluated by CEL.
type: string
key:
description: The name of the scope that the selector applies
to.
description: Key represents the expression which will be
evaluated by JQ.
type: string
operator:
description: Represents a scope's relationship to a set
Expand All @@ -252,10 +255,12 @@ spec:
items:
type: string
type: array
required:
- key
- operator
type: object
x-kubernetes-validations:
- message: expression and key are mutually exclusive
rule: has(self.expression) != has(self.key)
- message: key and operator must be set together
rule: has(self.key) && has(self.operator)
type: array
matchLabels:
additionalProperties:
Expand Down
23 changes: 23 additions & 0 deletions pkg/apis/internalversion/cel_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internalversion

// ExpressionCEL is the expression which will be evaluated by CEL.
type ExpressionCEL struct {
// Expression represents the expression which will be evaluated by CEL.
Expression string
}
14 changes: 10 additions & 4 deletions pkg/apis/internalversion/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,19 @@ type StageSelector struct {
// operator is "In", and the values array contains only "value". The requirements are ANDed.
MatchAnnotations map[string]string
// MatchExpressions is a list of label selector requirements. The requirements are ANDed.
MatchExpressions []SelectorRequirement
MatchExpressions []SelectorExpression
}

// SelectorRequirement is a resource selector requirement is a selector that contains values, a key,
// SelectorExpression is a resource selector expression is a set of requirements that must be true for a match.
type SelectorExpression struct {
*ExpressionCEL
*SelectorJQ
}

// SelectorJQ is a resource selector requirement is a selector that contains values, a key,
// and an operator that relates the key and values.
type SelectorRequirement struct {
// The name of the scope that the selector applies to.
type SelectorJQ struct {
// Key represents the expression which will be evaluated by JQ.
Key string
// Represents a scope's relationship to a set of values.
Operator SelectorOperator
Expand Down
90 changes: 76 additions & 14 deletions pkg/apis/internalversion/zz_generated.conversion.go

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

52 changes: 47 additions & 5 deletions pkg/apis/internalversion/zz_generated.deepcopy.go

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

23 changes: 23 additions & 0 deletions pkg/apis/v1alpha1/cel_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

// ExpressionCEL is the expression which will be evaluated by CEL.
type ExpressionCEL struct {
// Expression represents the expression which will be evaluated by CEL.
Expression string `json:"expression,omitempty"`
}
20 changes: 14 additions & 6 deletions pkg/apis/v1alpha1/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,24 @@ type StageSelector struct {
// operator is "In", and the values array contains only "value". The requirements are ANDed.
MatchAnnotations map[string]string `json:"matchAnnotations,omitempty"`
// MatchExpressions is a list of label selector requirements. The requirements are ANDed.
MatchExpressions []SelectorRequirement `json:"matchExpressions,omitempty"`
MatchExpressions []SelectorExpression `json:"matchExpressions,omitempty"`
}

// SelectorRequirement is a resource selector requirement is a selector that contains values, a key,
// SelectorExpression is a resource selector expression is a set of requirements that must be true for a match.
// +kubebuilder:validation:XValidation:rule="has(self.expression) != has(self.key)",message="expression and key are mutually exclusive"
// +kubebuilder:validation:XValidation:rule="has(self.key) && has(self.operator)",message="key and operator must be set together"
type SelectorExpression struct {
*ExpressionCEL `json:",inline"`
*SelectorJQ `json:",inline"`
}

// SelectorJQ is a resource selector requirement is a selector that contains values, a key,
// and an operator that relates the key and values.
type SelectorRequirement struct {
// The name of the scope that the selector applies to.
Key string `json:"key"`
type SelectorJQ struct {
// Key represents the expression which will be evaluated by JQ.
Key string `json:"key,omitempty"`
// Represents a scope's relationship to a set of values.
Operator SelectorOperator `json:"operator"`
Operator SelectorOperator `json:"operator,omitempty"`
// An array of string values.
// If the operator is In, NotIn, Intersection or NotIntersection, the values array must be non-empty.
// If the operator is Exists or DoesNotExist, the values array must be empty.
Expand Down
Loading

0 comments on commit 92122e2

Please sign in to comment.