Skip to content

Commit

Permalink
Fix Pod creation from Job in Kubernetes mock
Browse files Browse the repository at this point in the history
Avoid reusing the same metadata object when creating a Pod from a
Job. Previous versions modified the spec part of the Job when adding
additional metadata to the child Pod.
  • Loading branch information
rra committed Nov 16, 2023
1 parent abbff4d commit e9c85a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20231116_110515_rra_DM_41708a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Bug fixes

- Avoid reusing the same metadata object when creating a `Pod` from a `Job`. Previous versions modified the `spec` part of the `Job` when adding additional metadata to the child `Pod`.
18 changes: 13 additions & 5 deletions src/safir/testing/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,11 +1346,19 @@ async def create_namespaced_job(
self._store_object(namespace, "Job", name, body)

# Normally, Kubernetes will immediately spawn a Pod using the
# specification in the Job. Simulate that here.
pod = V1Pod(
metadata=body.spec.template.metadata or V1ObjectMeta(),
spec=body.spec.template.spec,
)
# specification in the Job. Simulate that here. We have to copy the
# components of the spec metadata so that we don't modify the Job when
# we flesh out the metadata of the Pod.
metadata = V1ObjectMeta()
if body.spec.template.metadata:
source = body.spec.template.metadata
metadata.name = source.name
metadata.generate_name = source.generate_name
if source.labels:
metadata.labels = source.labels.copy()
if source.annotations:
metadata.annnotations = source.annotations.copy()
pod = V1Pod(metadata=metadata, spec=body.spec.template.spec)
if not pod.metadata.name:
if not pod.metadata.generate_name:
pod.metadata.generate_name = f"{name}-"
Expand Down

0 comments on commit e9c85a4

Please sign in to comment.