Skip to content

Commit

Permalink
airflow/providers/docker/operators/docker: extra_hosts support added
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzgaloff committed Aug 27, 2020
1 parent 0957db1 commit 58dbd8e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions airflow/providers/docker/operators/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def __init__(
shm_size: Optional[int] = None,
tty: Optional[bool] = False,
cap_add: Optional[Iterable[str]] = None,
extra_hosts: Optional[Dict[str, str]] = None,
**kwargs,
) -> None:

Expand Down Expand Up @@ -200,6 +201,7 @@ def __init__(
self.shm_size = shm_size
self.tty = tty
self.cap_add = cap_add
self.extra_hosts = extra_hosts
if kwargs.get('xcom_push') is not None:
raise AirflowException("'xcom_push' was deprecated, use 'BaseOperator.do_xcom_push' instead")

Expand Down Expand Up @@ -244,6 +246,7 @@ def _run_image(self) -> Optional[str]:
cpu_shares=int(round(self.cpus * 1024)),
mem_limit=self.mem_limit,
cap_add=self.cap_add,
extra_hosts=self.extra_hosts,
),
image=self.image,
user=self.user,
Expand Down
15 changes: 15 additions & 0 deletions tests/providers/docker/operators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,18 @@ def test_execute_xcom_behavior(self):

self.assertEqual(xcom_push_result, b'container log')
self.assertIs(no_xcom_push_result, None)

def test_extra_hosts(self):
hosts_obj = mock.Mock()
operator = DockerOperator(task_id='test', image='test', extra_hosts=hosts_obj)
operator.execute(None)
self.client_mock.create_container.assert_called_once()
self.assertIn(
'host_config', self.client_mock.create_container.call_args.kwargs,
)
self.assertIn(
'extra_hosts', self.client_mock.create_host_config.call_args.kwargs,
)
self.assertIs(
hosts_obj, self.client_mock.create_host_config.call_args.kwargs['extra_hosts'],
)

0 comments on commit 58dbd8e

Please sign in to comment.