Skip to content

Commit bee57f0

Browse files
jordijordipiqueselles
jordi
authored andcommitted
Create alias for certain operations
This commit improves the name of certain operations. This change has effect on v1beta1. However, to be able to consume configs written with v1alph1, we're keeping the old names. The operations that now have a new alias are: - "and" -> "all" - "or" -> "any" - "forEach" -> "map" Appart from that, we're changing the behaviour of "or" (and "any) when they have no operands. Before, the result was true, but now it's false. This makes this operation consistent with `any([])` in Python. The intention is to make it's output more intuitive for Python users.
1 parent 55f29e7 commit bee57f0

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

generic_k8s_webhook/config_parser/entrypoint.py

+3
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ def _parse_v1beta1(self, raw_list_webhook_config: dict) -> list[Webhook]:
100100
meta_op_parser=op_parser.MetaOperatorParser(
101101
list_op_parser_classes=[
102102
op_parser.AndParser,
103+
op_parser.AllParser,
103104
op_parser.OrParser,
105+
op_parser.AnyParser,
104106
op_parser.EqualParser,
105107
op_parser.SumParser,
106108
op_parser.NotParser,
107109
op_parser.ListParser,
108110
op_parser.ForEachParser,
111+
op_parser.MapParser,
109112
op_parser.ContainParser,
110113
op_parser.ConstParser,
111114
op_parser.GetValueParser,

generic_k8s_webhook/config_parser/operator_parser.py

+42
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ def get_operator_cls(cls) -> operators.BinaryOp:
100100

101101

102102
class AndParser(BinaryOpParser):
103+
"""
104+
Deprecated. Use "all" instead
105+
"""
106+
103107
@classmethod
104108
def get_name(cls) -> str:
105109
return "and"
@@ -109,7 +113,21 @@ def get_operator_cls(cls) -> operators.BinaryOp:
109113
return operators.And
110114

111115

116+
class AllParser(AndParser):
117+
"""
118+
Just an alias for "and". In the future, we'll deprecate "and" in favour of "all"
119+
"""
120+
121+
@classmethod
122+
def get_name(cls) -> str:
123+
return "all"
124+
125+
112126
class OrParser(BinaryOpParser):
127+
"""
128+
Deprecated. Use "any" instead
129+
"""
130+
113131
@classmethod
114132
def get_name(cls) -> str:
115133
return "or"
@@ -119,6 +137,16 @@ def get_operator_cls(cls) -> operators.BinaryOp:
119137
return operators.Or
120138

121139

140+
class AnyParser(OrParser):
141+
"""
142+
Just an alias for "or". In the future, we'll deprecate "or" in favour of "all"
143+
"""
144+
145+
@classmethod
146+
def get_name(cls) -> str:
147+
return "any"
148+
149+
122150
class EqualParser(BinaryOpParser):
123151
@classmethod
124152
def get_name(cls) -> str:
@@ -180,6 +208,10 @@ def parse(self, op_inputs: dict | list, path_op: str) -> operators.List:
180208

181209

182210
class ForEachParser(OperatorParser):
211+
"""
212+
Deprecated. Use "map" instead of "forEach"
213+
"""
214+
183215
@classmethod
184216
def get_name(cls) -> str:
185217
return "forEach"
@@ -197,6 +229,16 @@ def parse(self, op_inputs: dict | list, path_op: str) -> operators.ForEach:
197229
raise ParsingException(f"Error when parsing {path_op}") from e
198230

199231

232+
class MapParser(ForEachParser):
233+
"""
234+
It's an alias of ForEachParser. In the future, we'll deprecate "forEach" in favour of "map"
235+
"""
236+
237+
@classmethod
238+
def get_name(cls) -> str:
239+
return "map"
240+
241+
200242
class ContainParser(OperatorParser):
201243
@classmethod
202244
def get_name(cls) -> str:

generic_k8s_webhook/operators.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def _op(self, lhs, rhs):
110110
return lhs and rhs
111111

112112
def _zero_args_result(self):
113+
# This follows the default behaviour in Python when executing `all([])`
113114
return True
114115

115116

@@ -118,7 +119,8 @@ def _op(self, lhs, rhs):
118119
return lhs or rhs
119120

120121
def _zero_args_result(self):
121-
return True
122+
# This follows the default behaviour in Python when executing `any([])`
123+
return False
122124

123125

124126
class ArithOp(BinaryOp):

tests/conditions_test.yaml

+37
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ test_suites:
3131
- condition:
3232
and: []
3333
expected_result: true
34+
# Just check we can parse "all", since it's the same as "and"
35+
- name: ALL
36+
tests:
37+
- schemas: [v1beta1]
38+
cases:
39+
- condition:
40+
all:
41+
- const: true
42+
- const: true
43+
expected_result: true
3444
- name: OR
3545
tests:
3646
- schemas: [v1alpha1]
@@ -49,6 +59,19 @@ test_suites:
4959
or:
5060
- const: true
5161
expected_result: true
62+
- condition:
63+
or: []
64+
expected_result: false
65+
# Just check we can parse "any", since it's the same as "or"
66+
- name: ANY
67+
tests:
68+
- schemas: [v1beta1]
69+
cases:
70+
- condition:
71+
any:
72+
- const: false
73+
- const: false
74+
expected_result: false
5275
- name: NOT
5376
tests:
5477
- schemas: [v1alpha1]
@@ -143,6 +166,20 @@ test_suites:
143166
- maxCPU: 1
144167
- maxCPU: 2
145168
expected_result: [2, 3]
169+
# Just check we can parse "map", since it's the same as "forEach"
170+
- name: MAP
171+
tests:
172+
- schemas: [v1beta1]
173+
cases:
174+
- condition:
175+
map:
176+
elements:
177+
const: [1, 2]
178+
op:
179+
sum:
180+
- const: 10
181+
- getValue: "."
182+
expected_result: [11, 12]
146183
- name: CONTAIN
147184
tests:
148185
- schemas: [v1alpha1]

0 commit comments

Comments
 (0)