From 48b954ab9670a22f457ffa7a8e729592ca6b2dc2 Mon Sep 17 00:00:00 2001 From: Mathew Wicks Date: Fri, 4 Mar 2022 16:59:37 +1100 Subject: [PATCH] Sanitize KFP input/output params in kwargs --- elyra/pipeline/kfp/processor_kfp.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/elyra/pipeline/kfp/processor_kfp.py b/elyra/pipeline/kfp/processor_kfp.py index b3814d8a9..9e87c4931 100644 --- a/elyra/pipeline/kfp/processor_kfp.py +++ b/elyra/pipeline/kfp/processor_kfp.py @@ -619,7 +619,11 @@ def _cc_pipeline(self, operation.component_params_as_dict.pop(parameter, None) # Create ContainerOp instance and assign appropriate user-provided name - container_op = factory_function(**operation.component_params_as_dict) + sanitized_component_params = { + self._sanitize_param_name(name): value + for name, value in operation.component_params_as_dict.items() + } + container_op = factory_function(**sanitized_component_params) container_op.set_display_name(operation.name) if operation.doc: @@ -692,6 +696,29 @@ def _sanitize_operation_name(name: str) -> str: """ return re.sub('-+', '-', re.sub('[^-_0-9A-Za-z ]+', '-', name)).lstrip('-').rstrip('-') + @staticmethod + def _sanitize_param_name(name: str) -> str: + """ + Sanitize a component parameter name. + + Behavior is mirrored from how Kubeflow 1.X sanitizes identifier names: + - https://github.com/kubeflow/pipelines/blob/1.8.1/sdk/python/kfp/components/_naming.py#L32-L42 + - https://github.com/kubeflow/pipelines/blob/1.8.1/sdk/python/kfp/components/_naming.py#L49-L50 + """ + normalized_name = name.lower() + + # remove non-word characters + normalized_name = re.sub(r'[\W_]', ' ', normalized_name) + + # no double spaces, leading or trailing spaces + normalized_name = re.sub(' +', ' ', normalized_name).strip() + + # no leading digits + if re.match(r'\d', normalized_name): + normalized_name = 'n' + normalized_name + + return normalized_name.replace(' ', '_') + class KfpPipelineProcessorResponse(PipelineProcessorResponse): _type = RuntimeProcessorType.KUBEFLOW_PIPELINES