Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions python/src/main/python/yaml-template/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@


import argparse
import logging
import tempfile

import yaml

from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import WorkerOptions
from apache_beam.yaml import main

import ml_dependencies
Expand Down Expand Up @@ -46,6 +51,20 @@ def _get_runtime_dependencies(args):
return list(dict.fromkeys(dependencies))


def _uses_private_ips(args):
"""Returns whether the workers of a pipeline only use private IPs."""
try:
_, pipeline_args, _, pipeline_yaml = (
main._build_pipeline_yaml_from_argv(args))
pipeline_spec = yaml.safe_load(pipeline_yaml)
if not isinstance(pipeline_spec, dict):
pipeline_spec = {}
options = main._build_pipeline_options(pipeline_spec, pipeline_args)
except Exception: # pylint: disable=broad-except
options = PipelineOptions(args)
return options.view_as(WorkerOptions).use_public_ips is False


def run(argv=None):
parser = argparse.ArgumentParser()
_, args = parser.parse_known_args(argv)
Expand All @@ -56,7 +75,12 @@ def run(argv=None):
has_req_file = True
break

if not has_req_file:
if not has_req_file and _uses_private_ips(args):
# Without public IPs there is usually no access to PyPI.
logging.info(
'Workers only use private IPs, not adding the default runtime '
'dependencies.')
elif not has_req_file:
# Default dependencies, and any additional dependencies needed by ML
# transforms used by the pipeline.
dependencies = _get_runtime_dependencies(args)
Expand All @@ -72,6 +96,5 @@ def run(argv=None):


if __name__ == '__main__':
import logging
logging.getLogger().setLevel(logging.INFO)
run()
35 changes: 35 additions & 0 deletions python/src/test/python/yaml-template/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,41 @@ def test_run_does_not_add_ml_dependencies_if_requirements_file_present(self):

mock_run.assert_called_once_with(argv)

def test_run_does_not_add_requirements_file_for_private_ips(self):
argv = [
f'--yaml_pipeline={_ML_PIPELINE}',
'--yaml_experimental_features=ML',
'--no_use_public_ips',
]

with patch('main.main.run') as mock_run:
main.run(argv)

mock_run.assert_called_once_with(argv)

def test_run_does_not_add_requirements_file_for_private_ips_in_yaml(self):
pipeline = _ML_PIPELINE + '''
options:
use_public_ips: false
'''
argv = [f'--yaml_pipeline={pipeline}']

with patch('main.main.run') as mock_run:
main.run(argv)

mock_run.assert_called_once_with(argv)

def test_run_checks_private_ips_if_pipeline_cannot_be_parsed(self):
argv = [
'--yaml_pipeline_file=does/not/exist.yaml',
'--no_use_public_ips',
]

with patch('main.main.run') as mock_run:
main.run(argv)

mock_run.assert_called_once_with(argv)


if __name__ == '__main__':
unittest.main()