Skip to content

Commit

Permalink
SDK/DSL: Fix PipelineVolume name length
Browse files Browse the repository at this point in the history
Volume name must be no more than 63 characters

Signed-off-by: Ilias Katsakioris <elikatsis@arrikto.com>
  • Loading branch information
elikatsis committed Dec 16, 2019
1 parent d39ef6e commit 22c69b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions sdk/python/kfp/dsl/_pipeline_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def __init__(self,
for attr in self.attribute_map.keys()}
else:
if "name" in kwargs:
if len(kwargs["name"]) > 63:
raise ValueError("PipelineVolume name must be no more than"
" 63 characters")
init_volume = {"name": kwargs.pop("name")}
else:
name_provided = False
Expand All @@ -69,9 +72,16 @@ def __init__(self,
init_volume["persistent_volume_claim"] = pvc_volume_source
super().__init__(**init_volume, **kwargs)
if not name_provided:
self.name = "pvolume-%s" % hashlib.sha256(
bytes(json.dumps(self.to_dict(), sort_keys=True), "utf-8")
).hexdigest()
hash_value = hashlib.sha256(bytes(json.dumps(self.to_dict(),
sort_keys=True),
"utf-8")).hexdigest()
name_prefix = "pvolume-"
# Name must be no more than 63 characters, so we will keep the last
# chars of the hash value
hash_len = 63 - len(name_prefix)
self.name = (name_prefix + hash_value[len(hash_value)-hash_len:]
if len(hash_value) > hash_len
else name_prefix + hash_value)
self.dependent_names = []

def after(self, *ops):
Expand Down
4 changes: 2 additions & 2 deletions sdk/python/tests/dsl/pipeline_volume_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def test_omitting_name(self):
def my_pipeline(param='foo'):
vol1 = PipelineVolume(pvc="foo")
vol2 = PipelineVolume(name="provided", pvc="foo")
name1 = ("pvolume-127ac63cf2013e9b95c192eb6a2c7d5a023ebeb51f6a114486e3"
"1216e083a563")
name1 = ("pvolume-2013e9b95c192eb6a2c7d5a023ebeb51f6a114486e31216e"
"083a563")
name2 = "provided"
self.assertEqual(vol1.name, name1)
self.assertEqual(vol2.name, name2)
Expand Down

0 comments on commit 22c69b2

Please sign in to comment.