From 5e6918c621db6744482b51a92ad933de09d0a508 Mon Sep 17 00:00:00 2001 From: Alexander Streed Date: Tue, 20 Jun 2023 12:47:12 -0500 Subject: [PATCH] Fix for `generateName` bug (#76) * Adds fix for generate name bug * Updates test * Adds changelog entry --- CHANGELOG.md | 8 ++++++++ prefect_kubernetes/worker.py | 11 ++++++++++- tests/test_worker.py | 7 ++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c0e592..e45009e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security +## 0.2.9 + +Released June 20th, 2023. + +### Fixed + +- Fixed issue where `generateName` was not populating correctly for some flow runs submitted by `KubernetesWorker` - [#76](https://github.com/PrefectHQ/prefect-kubernetes/pull/76) + ## 0.2.8 Released May 25th, 2023. diff --git a/prefect_kubernetes/worker.py b/prefect_kubernetes/worker.py index 0999d94..d437517 100644 --- a/prefect_kubernetes/worker.py +++ b/prefect_kubernetes/worker.py @@ -389,7 +389,16 @@ def _populate_generate_name_if_not_present(self): """Ensures that the generateName is present in the job manifest.""" manifest_generate_name = self.job_manifest["metadata"].get("generateName", "") has_placeholder = len(find_placeholders(manifest_generate_name)) > 0 - if not manifest_generate_name or has_placeholder: + # if name wasn't present during template rendering, generateName will be + # just a hyphen + manifest_generate_name_templated_with_empty_string = ( + manifest_generate_name == "-" + ) + if ( + not manifest_generate_name + or has_placeholder + or manifest_generate_name_templated_with_empty_string + ): generate_name = None if self.name: generate_name = _slugify_name(self.name) diff --git a/tests/test_worker.py b/tests/test_worker.py index 1a1608c..c06461d 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -1,4 +1,5 @@ import re +import uuid from contextlib import contextmanager from time import monotonic, sleep from unittest import mock @@ -154,7 +155,7 @@ def _mock_pods_stream_that_returns_running_pod(*args, **kwargs): "kind": "Job", "metadata": { "namespace": "default", - "generateName": "{{ name }}-", + "generateName": "-", "labels": {}, }, "spec": { @@ -685,7 +686,7 @@ def _mock_pods_stream_that_returns_running_pod(*args, **kwargs): class TestKubernetesWorkerJobConfiguration: @pytest.fixture def flow_run(self): - return FlowRun(name="my-flow-run-name") + return FlowRun(flow_id=uuid.uuid4(), name="my-flow-run-name") @pytest.fixture def deployment(self): @@ -918,7 +919,7 @@ async def default_configuration(self): @pytest.fixture def flow_run(self): - return FlowRun(name="my-flow-run-name") + return FlowRun(flow_id=uuid.uuid4(), name="my-flow-run-name") async def test_creates_job_by_building_a_manifest( self,