Skip to content

Commit e107891

Browse files
authored
Extend init_containers defined in pod_override (#17537)
Needs to extend also the init_containers not just override (e.g. git sync and similar init_containers will stay) With that change if you define a pod_override and specifies the "init_containers" attribute it will extend the base init_containers with the new ones instead of overwriting them. That is to keep the dag git sync init_containers or other pre-defined init_containers based on the template or the configuration.
1 parent 35a6c30 commit e107891

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

airflow/kubernetes/pod_generator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ def reconcile_specs(
287287
client_spec.containers = PodGenerator.reconcile_containers(
288288
base_spec.containers, client_spec.containers
289289
)
290-
merged_spec = extend_object_field(base_spec, client_spec, 'volumes')
290+
merged_spec = extend_object_field(base_spec, client_spec, 'init_containers')
291+
merged_spec = extend_object_field(base_spec, merged_spec, 'volumes')
291292
return merge_objects(base_spec, merged_spec)
292293

293294
return None

docs/apache-airflow/executor/kubernetes.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ create a V1pod with a single container, and overwrite the fields as follows:
113113
:start-after: [START task_with_volume]
114114
:end-before: [END task_with_volume]
115115

116-
Note that volume mounts, environment variables, ports, and devices will all be extended instead of overwritten.
116+
Note that the following fields **will all be extended** instead of overwritten. From *spec*: volumes, and init_containers. From *container*: volume mounts, environment variables, ports, and devices.
117117

118118
To add a sidecar container to the launched pod, create a V1pod with an empty first container with the
119119
name ``base`` and a second container containing your desired sidecar.

tests/kubernetes/test_pod_generator.py

+8
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,14 @@ def test_reconcile_specs(self):
631631
client_spec.active_deadline_seconds = 100
632632
assert client_spec == res
633633

634+
def test_reconcile_specs_init_containers(self):
635+
base_spec = k8s.V1PodSpec(containers=[], init_containers=[k8s.V1Container(name='base_container1')])
636+
client_spec = k8s.V1PodSpec(
637+
containers=[], init_containers=[k8s.V1Container(name='client_container1')]
638+
)
639+
res = PodGenerator.reconcile_specs(base_spec, client_spec)
640+
assert res.init_containers == base_spec.init_containers + client_spec.init_containers
641+
634642
def test_deserialize_model_file(self):
635643
path = sys.path[0] + '/tests/kubernetes/pod.yaml'
636644
result = PodGenerator.deserialize_model_file(path)

0 commit comments

Comments
 (0)