chore(sdk.v2): Error out on invalid pipeline name (#4851)

This commit is contained in:
Chen Sun 2020-12-01 14:36:51 -08:00 committed by GitHub
parent 9631eb93ae
commit 03904ebacd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 2 deletions

View File

@ -73,8 +73,7 @@ class Compiler(object):
Raises:
NotImplementedError if the argument is of unsupported types.
"""
if not pipeline.name:
raise ValueError('Pipeline name is required.')
compiler_utils.validate_pipeline_name(pipeline.name)
pipeline_spec = pipeline_spec_pb2.PipelineSpec(
runtime_parameters=compiler_utils.build_runtime_parameter_spec(args))

View File

@ -350,6 +350,19 @@ class CompilerTest(unittest.TestCase):
- {inputPath: data}
""")
def test_compile_pipeline_with_invalid_name_should_raise_error(self):
def my_pipeline():
pass
with self.assertRaisesRegex(
ValueError,
'Invalid pipeline name: .*\nPlease specify a pipeline name that matches'
):
compiler.Compiler().compile(
pipeline_func=my_pipeline,
pipeline_root='dummy',
output_path='output.json')
if __name__ == '__main__':
unittest.main()

View File

@ -13,7 +13,9 @@
# limitations under the License.
"""KFP v2 DSL compiler utility functions."""
import re
from typing import Any, List, Mapping, Optional, Union
from kfp.v2 import dsl
from kfp.pipeline_spec import pipeline_spec_pb2
@ -91,3 +93,22 @@ def build_runtime_config_spec(
return pipeline_spec_pb2.PipelineJob.RuntimeConfig(
gcs_output_directory=pipeline_root,
parameters={k: _get_value(v) for k, v in parameter_values.items()})
def validate_pipeline_name(name: str) -> None:
"""Validate pipeline name.
A valid pipeline name should match ^[a-z0-9][a-z0-9-]{0,127}$.
Args:
name: The pipeline name.
Raises:
ValueError if the pipeline name doesn't conform to the regular expression.
"""
pattern = re.compile(r'^[a-z0-9][a-z0-9-]{0,127}$')
if not pattern.match(name):
raise ValueError('Invalid pipeline name: %s.\n'
'Please specify a pipeline name that matches the regular '
'expression "^[a-z0-9][a-z0-9-]{0,127}$" using '
'`dsl.pipeline(name=...)` decorator.' % name)

View File

@ -88,6 +88,22 @@ class CompilerUtilsTest(unittest.TestCase):
'gs://path', {'input1': 'test'})
self.assertEqual(expected_spec, runtime_config)
def test_validate_pipeline_name(self):
compiler_utils.validate_pipeline_name('my-pipeline')
compiler_utils.validate_pipeline_name('p' * 128)
with self.assertRaisesRegex(ValueError, 'Invalid pipeline name: '):
compiler_utils.validate_pipeline_name('my_pipeline')
with self.assertRaisesRegex(ValueError, 'Invalid pipeline name: '):
compiler_utils.validate_pipeline_name('My pipeline')
with self.assertRaisesRegex(ValueError, 'Invalid pipeline name: '):
compiler_utils.validate_pipeline_name('-my-pipeline')
with self.assertRaisesRegex(ValueError, 'Invalid pipeline name: '):
compiler_utils.validate_pipeline_name('p' * 129)
if __name__ == '__main__':
unittest.main()