20
20
import types
21
21
from typing import List
22
22
23
+ from kfp .dsl import constants
24
+
23
25
COMPONENT_NAME_PREFIX = 'comp-'
24
26
_EXECUTOR_LABEL_PREFIX = 'exec-'
25
27
@@ -126,3 +128,78 @@ def validate_pipeline_name(name: str) -> None:
126
128
'Please specify a pipeline name that matches the regular '
127
129
'expression "^[a-z0-9][a-z0-9-]{0,127}$" using '
128
130
'`dsl.pipeline(name=...)` decorator.' % name )
131
+
132
+ def validate_cpu_request_limit_to_float (cpu : str ) -> float :
133
+ """Validates cpu request/limit string and converts to its numeric
134
+ float value.
135
+
136
+ Args:
137
+ cpu: CPU requests or limits. This string should be a number or a
138
+ number followed by an "m" to indicate millicores (1/1000). For
139
+ more information, see `Specify a CPU Request and a CPU Limit
140
+ <https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/#specify-a-cpu-request-and-a-cpu-limit>`_.
141
+
142
+ Raises:
143
+ ValueError if the cpu request/limit string value is invalid.
144
+
145
+ Returns:
146
+ The numeric value (float) of the cpu request/limit.
147
+ """
148
+ if re .match (r'([0-9]*[.])?[0-9]+m?$' , cpu ) is None :
149
+ raise ValueError (
150
+ 'Invalid cpu string. Should be float or integer, or integer'
151
+ ' followed by "m".' )
152
+
153
+ return float (cpu [:- 1 ]) / 1000 if cpu .endswith ('m' ) else float (cpu )
154
+
155
+ def validate_memory_request_limit_to_float (memory : str ) -> float :
156
+ """Validates memory request/limit string and converts to its numeric
157
+ value.
158
+
159
+ Args:
160
+ memory: Memory requests or limits. This string should be a number or
161
+ a number followed by one of "E", "Ei", "P", "Pi", "T", "Ti", "G",
162
+ "Gi", "M", "Mi", "K", or "Ki".
163
+
164
+ Raises:
165
+ ValueError if the memory request/limit string value is invalid.
166
+
167
+ Returns:
168
+ The numeric value (float) of the memory request/limit.
169
+ """
170
+ if re .match (r'^[0-9]+(E|Ei|P|Pi|T|Ti|G|Gi|M|Mi|K|Ki){0,1}$' ,
171
+ memory ) is None :
172
+ raise ValueError (
173
+ 'Invalid memory string. Should be a number or a number '
174
+ 'followed by one of "E", "Ei", "P", "Pi", "T", "Ti", "G", '
175
+ '"Gi", "M", "Mi", "K", "Ki".' )
176
+
177
+ if memory .endswith ('E' ):
178
+ memory = float (memory [:- 1 ]) * constants ._E / constants ._G
179
+ elif memory .endswith ('Ei' ):
180
+ memory = float (memory [:- 2 ]) * constants ._EI / constants ._G
181
+ elif memory .endswith ('P' ):
182
+ memory = float (memory [:- 1 ]) * constants ._P / constants ._G
183
+ elif memory .endswith ('Pi' ):
184
+ memory = float (memory [:- 2 ]) * constants ._PI / constants ._G
185
+ elif memory .endswith ('T' ):
186
+ memory = float (memory [:- 1 ]) * constants ._T / constants ._G
187
+ elif memory .endswith ('Ti' ):
188
+ memory = float (memory [:- 2 ]) * constants ._TI / constants ._G
189
+ elif memory .endswith ('G' ):
190
+ memory = float (memory [:- 1 ])
191
+ elif memory .endswith ('Gi' ):
192
+ memory = float (memory [:- 2 ]) * constants ._GI / constants ._G
193
+ elif memory .endswith ('M' ):
194
+ memory = float (memory [:- 1 ]) * constants ._M / constants ._G
195
+ elif memory .endswith ('Mi' ):
196
+ memory = float (memory [:- 2 ]) * constants ._MI / constants ._G
197
+ elif memory .endswith ('K' ):
198
+ memory = float (memory [:- 1 ]) * constants ._K / constants ._G
199
+ elif memory .endswith ('Ki' ):
200
+ memory = float (memory [:- 2 ]) * constants ._KI / constants ._G
201
+ else :
202
+ # By default interpret as a plain integer, in the unit of Bytes.
203
+ memory = float (memory ) / constants ._G
204
+
205
+ return memory
0 commit comments