Skip to content

Commit b55c375

Browse files
author
jordi
committed
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 b55c375

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-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

+35
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,31 @@ def get_operator_cls(cls) -> operators.BinaryOp:
100100

101101

102102
class AndParser(BinaryOpParser):
103+
"""
104+
Deprecated. Use "all" instead
105+
"""
103106
@classmethod
104107
def get_name(cls) -> str:
105108
return "and"
106109

107110
@classmethod
108111
def get_operator_cls(cls) -> operators.BinaryOp:
109112
return operators.And
113+
114+
115+
class AllParser(AndParser):
116+
"""
117+
Just an alias for "and". In the future, we'll deprecate "and" in favour of "all"
118+
"""
119+
@classmethod
120+
def get_name(cls) -> str:
121+
return "all"
110122

111123

112124
class OrParser(BinaryOpParser):
125+
"""
126+
Deprecated. Use "any" instead
127+
"""
113128
@classmethod
114129
def get_name(cls) -> str:
115130
return "or"
@@ -118,6 +133,14 @@ def get_name(cls) -> str:
118133
def get_operator_cls(cls) -> operators.BinaryOp:
119134
return operators.Or
120135

136+
class AnyParser(OrParser):
137+
"""
138+
Just an alias for "or". In the future, we'll deprecate "or" in favour of "all"
139+
"""
140+
@classmethod
141+
def get_name(cls) -> str:
142+
return "any"
143+
121144

122145
class EqualParser(BinaryOpParser):
123146
@classmethod
@@ -180,6 +203,9 @@ def parse(self, op_inputs: dict | list, path_op: str) -> operators.List:
180203

181204

182205
class ForEachParser(OperatorParser):
206+
"""
207+
Deprecated. Use "map" instead of "forEach"
208+
"""
183209
@classmethod
184210
def get_name(cls) -> str:
185211
return "forEach"
@@ -195,6 +221,15 @@ def parse(self, op_inputs: dict | list, path_op: str) -> operators.ForEach:
195221
return operators.ForEach(elements, op)
196222
except TypeError as e:
197223
raise ParsingException(f"Error when parsing {path_op}") from e
224+
225+
226+
class MapParser(ForEachParser):
227+
"""
228+
It's an alias of ForEachParser. In the future, we'll deprecate "forEach" in favour of "map"
229+
"""
230+
@classmethod
231+
def get_name(cls) -> str:
232+
return "map"
198233

199234

200235
class ContainParser(OperatorParser):

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)