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 30, 2024
1 parent f80b5ba commit 294bfa8
Show file tree
Hide file tree
Showing 20 changed files with 494 additions and 104 deletions.
33 changes: 25 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,24 @@ 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.expression) || (has(self.key) && has(self.operator))'
- messageExpression: '''operator '' + self.operator + '' is
not supported'''
rule: '!has(self.expression) || (self.operator in [''In'',
''NotIn'', ''Exists'', ''DoesNotExist''])'
- messageExpression: '''value must be empty when using the ''
+ self.operator + '' operator'''
rule: '!has(self.expression) || !(self.operator in [''In'',
''NotIn'']) || !has(self.value)'
- messageExpression: '''value must not be empty when using the
'' + self.operator + '' operator'''
rule: '!has(self.expression) || !(self.operator in [''Exists'',
''DoesNotExist'']) || has(self.value)'
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"`
}
Loading

0 comments on commit 294bfa8

Please sign in to comment.