Skip to content

Commit b9a2c05

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: serialize V1OwnerReference with wire names in GKE code executor
Pass V1OwnerReference directly in patch_body when patching the code ConfigMap so patch_namespaced_config_map serializes it with Kubernetes wire field names, and return a V1Job from the mock create_namespaced_job in unit tests. Previously, owner_reference.to_dict() omitted the wire apiVersion field, causing patch_namespaced_config_map to fail with a warning and leaving job-mode code-src-* ConfigMaps orphaned in the cluster. Users upgrading from earlier versions should clean up any leaked code-src-* ConfigMaps. Closes #7443 Closes #7444 PiperOrigin-RevId: 996083470
1 parent 5f45441 commit b9a2c05

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/google/adk/code_executors/gke_code_executor.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def _add_owner_reference(
475475
uid=owner_job.metadata.uid,
476476
controller=True,
477477
)
478-
patch_body = {"metadata": {"ownerReferences": [owner_reference.to_dict()]}}
478+
patch_body = {"metadata": {"ownerReferences": [owner_reference]}}
479479

480480
try:
481481
self._core_v1.patch_namespaced_config_map(

‎tests/unittests/code_executors/test_gke_code_executor.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def mock_k8s_clients():
5252
) as mock_client_class:
5353
mock_batch_v1 = MagicMock(spec=client.BatchV1Api)
5454
mock_core_v1 = MagicMock(spec=client.CoreV1Api)
55+
mock_batch_v1.create_namespaced_job.return_value = client.V1Job(
56+
api_version="batch/v1",
57+
kind="Job",
58+
metadata=client.V1ObjectMeta(name="test-job", uid="test-job-uid"),
59+
)
5560
mock_client_class.BatchV1Api.return_value = mock_batch_v1
5661
mock_client_class.CoreV1Api.return_value = mock_core_v1
5762
yield {
@@ -196,6 +201,33 @@ def test_execute_code_success(
196201
].create_namespaced_config_map.assert_called_once()
197202
mock_k8s_clients["batch_v1"].create_namespaced_job.assert_called_once()
198203
mock_k8s_clients["core_v1"].patch_namespaced_config_map.assert_called_once()
204+
body = mock_k8s_clients[
205+
"core_v1"
206+
].patch_namespaced_config_map.call_args.kwargs["body"]
207+
assert body == {
208+
"metadata": {
209+
"ownerReferences": [
210+
client.V1OwnerReference(
211+
api_version="batch/v1",
212+
kind="Job",
213+
name="test-job",
214+
uid="test-job-uid",
215+
controller=True,
216+
)
217+
]
218+
}
219+
}
220+
assert client.ApiClient().sanitize_for_serialization(body) == {
221+
"metadata": {
222+
"ownerReferences": [{
223+
"apiVersion": "batch/v1",
224+
"controller": True,
225+
"kind": "Job",
226+
"name": "test-job",
227+
"uid": "test-job-uid",
228+
}]
229+
}
230+
}
199231
mock_k8s_clients["core_v1"].read_namespaced_pod_log.assert_called_once()
200232

201233
@patch("google.adk.code_executors.gke_code_executor.Watch")

0 commit comments

Comments
 (0)