Skip to content

Commit e754348

Browse files
committed
Kfp support for pip trusted host
Signed-off-by: Diego Lovison <diegolovison@gmail.com>
1 parent 0d098db commit e754348

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

sdk/python/kfp/cli/component.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def __init__(
155155
self._base_image = None
156156
self._target_image = None
157157
self._pip_index_urls = None
158+
self._pip_trusted_hosts = None
158159
self._load_components()
159160

160161
def _load_components(self):
@@ -214,11 +215,16 @@ def _load_components(self):
214215
logging.info(f'Using target image: {self._target_image}')
215216

216217
pip_index_urls = []
218+
pip_trusted_hosts = []
217219
for comp in self._components:
218220
if comp.pip_index_urls is not None:
219221
pip_index_urls.extend(comp.pip_index_urls)
222+
if comp.pip_trusted_hosts is not None:
223+
pip_trusted_hosts.extend(comp.pip_trusted_hosts)
220224
if pip_index_urls:
221225
self._pip_index_urls = list(dict.fromkeys(pip_index_urls))
226+
if pip_trusted_hosts:
227+
self._pip_trusted_hosts = list(dict.fromkeys(pip_trusted_hosts))
222228

223229
def _maybe_write_file(self,
224230
filename: str,
@@ -277,7 +283,7 @@ def generate_kfp_config(self):
277283

278284
def maybe_generate_dockerfile(self, overwrite_dockerfile: bool = False):
279285
index_urls_options = component_factory.make_index_url_options(
280-
self._pip_index_urls)
286+
self._pip_index_urls, self._pip_trusted_hosts)
281287
dockerfile_contents = _DOCKERFILE_TEMPLATE.format(
282288
base_image=self._base_image,
283289
maybe_copy_kfp_package=self._maybe_copy_kfp_package,

sdk/python/kfp/cli/component_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def _make_component(
3737
packages_to_install: Optional[List[str]] = None,
3838
output_component_file: Optional[str] = None,
3939
pip_index_urls: Optional[List[str]] = None,
40+
pip_trusted_hosts: Optional[List[str]] = None,
4041
) -> str:
4142
return textwrap.dedent('''
4243
from kfp.dsl import *
@@ -46,7 +47,8 @@ def _make_component(
4647
target_image={target_image},
4748
packages_to_install={packages_to_install},
4849
output_component_file={output_component_file},
49-
pip_index_urls={pip_index_urls})
50+
pip_index_urls={pip_index_urls},
51+
pip_trusted_hosts={pip_trusted_hosts})
5052
def {func_name}():
5153
pass
5254
''').format(
@@ -55,7 +57,8 @@ def {func_name}():
5557
packages_to_install=repr(packages_to_install),
5658
output_component_file=repr(output_component_file),
5759
pip_index_urls=repr(pip_index_urls),
58-
func_name=func_name)
60+
func_name=func_name,
61+
pip_trusted_hosts=repr(pip_trusted_hosts))
5962

6063

6164
def _write_file(filename: str, file_contents: str):

sdk/python/kfp/dsl/component_decorator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def component(func: Optional[Callable] = None,
2727
pip_index_urls: Optional[List[str]] = None,
2828
output_component_file: Optional[str] = None,
2929
install_kfp_package: bool = True,
30-
kfp_package_path: Optional[str] = None):
30+
kfp_package_path: Optional[str] = None,
31+
pip_trusted_hosts: Optional[List[str]] = None,):
3132
"""Decorator for Python-function based components.
3233
3334
A KFP component can either be a lightweight component or a containerized
@@ -114,7 +115,8 @@ def pipeline():
114115
pip_index_urls=pip_index_urls,
115116
output_component_file=output_component_file,
116117
install_kfp_package=install_kfp_package,
117-
kfp_package_path=kfp_package_path)
118+
kfp_package_path=kfp_package_path,
119+
pip_trusted_hosts=pip_trusted_hosts)
118120

119121
return component_factory.create_component_from_func(
120122
func,
@@ -124,4 +126,5 @@ def pipeline():
124126
pip_index_urls=pip_index_urls,
125127
output_component_file=output_component_file,
126128
install_kfp_package=install_kfp_package,
127-
kfp_package_path=kfp_package_path)
129+
kfp_package_path=kfp_package_path,
130+
pip_trusted_hosts=pip_trusted_hosts)

sdk/python/kfp/dsl/component_factory.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ComponentInfo():
5656
base_image: str = _DEFAULT_BASE_IMAGE
5757
packages_to_install: Optional[List[str]] = None
5858
pip_index_urls: Optional[List[str]] = None
59+
pip_trusted_hosts: Optional[List[str]] = None
5960

6061

6162
# A map from function_name to components. This is always populated when a
@@ -69,22 +70,33 @@ def _python_function_name_to_component_name(name):
6970
return name_with_spaces[0].upper() + name_with_spaces[1:]
7071

7172

72-
def make_index_url_options(pip_index_urls: Optional[List[str]]) -> str:
73+
def make_index_url_options(pip_index_urls: Optional[List[str]], pip_trusted_hosts: Optional[List[str]]) -> str:
7374
"""Generates index url options for pip install command based on provided
7475
pip_index_urls.
7576
7677
Args:
7778
pip_index_urls: Optional list of pip index urls
79+
pip_trusted_hosts: Optional list of pip trust hosts
7880
7981
Returns:
8082
- Empty string if pip_index_urls is empty/None.
81-
- '--index-url url --trusted-host url ' if pip_index_urls contains 1
83+
- '--index-url url ' if pip_index_urls contains 1
8284
url
83-
- the above followed by '--extra-index-url url --trusted-host url '
85+
- the above followed by '--extra-index-url url '
8486
for
8587
each next url in pip_index_urls if pip_index_urls contains more than 1
8688
url
8789
90+
- pip_trusted_hosts was added later. In this case, if pip_trusted_hosts is None or empty
91+
- the above followed by '--trusted-host url '
92+
for
93+
each url in pip_index_urls
94+
95+
- if pip_trusted_hosts is greater than 0
96+
- the above followed by '--trusted-host url '
97+
for
98+
each url in pip_trusted_hosts
99+
88100
Note: In case pip_index_urls is not empty, the returned string will
89101
contain space at the end.
90102
"""
@@ -94,11 +106,21 @@ def make_index_url_options(pip_index_urls: Optional[List[str]]) -> str:
94106
index_url = pip_index_urls[0]
95107
extra_index_urls = pip_index_urls[1:]
96108

97-
options = [f'--index-url {index_url} --trusted-host {index_url}']
109+
options = [f'--index-url {index_url}']
98110
options.extend(
99-
f'--extra-index-url {extra_index_url} --trusted-host {extra_index_url}'
111+
f'--extra-index-url {extra_index_url}'
100112
for extra_index_url in extra_index_urls)
101113

114+
if pip_trusted_hosts is None or len(pip_trusted_hosts) == 0:
115+
options = [f'--trusted-host {index_url}']
116+
options.extend(
117+
f'--trusted-host {extra_index_url}'
118+
for extra_index_url in extra_index_urls)
119+
else:
120+
options.extend(
121+
f'--trusted-host {trusted_host}'
122+
for trusted_host in pip_trusted_hosts)
123+
102124
return ' '.join(options) + ' '
103125

104126

@@ -126,6 +148,7 @@ def _get_packages_to_install_command(
126148
packages_to_install: Optional[List[str]] = None,
127149
install_kfp_package: bool = True,
128150
target_image: Optional[str] = None,
151+
pip_trusted_hosts: Optional[List[str]] = None,
129152
) -> List[str]:
130153
packages_to_install = packages_to_install or []
131154
kfp_in_user_pkgs = any(pkg.startswith('kfp') for pkg in packages_to_install)
@@ -136,7 +159,7 @@ def _get_packages_to_install_command(
136159
if not inject_kfp_install and not packages_to_install:
137160
return []
138161
pip_install_strings = []
139-
index_url_options = make_index_url_options(pip_index_urls)
162+
index_url_options = make_index_url_options(pip_index_urls, pip_trusted_hosts)
140163

141164
if inject_kfp_install:
142165
if kfp_package_path:
@@ -517,6 +540,7 @@ def create_component_from_func(
517540
output_component_file: Optional[str] = None,
518541
install_kfp_package: bool = True,
519542
kfp_package_path: Optional[str] = None,
543+
pip_trusted_hosts: Optional[List[str]] = None,
520544
) -> python_component.PythonComponent:
521545
"""Implementation for the @component decorator.
522546
@@ -530,6 +554,7 @@ def create_component_from_func(
530554
kfp_package_path=kfp_package_path,
531555
packages_to_install=packages_to_install,
532556
pip_index_urls=pip_index_urls,
557+
pip_trusted_hosts=pip_trusted_hosts,
533558
)
534559

535560
command = []
@@ -575,7 +600,8 @@ def create_component_from_func(
575600
output_component_file=output_component_file,
576601
base_image=base_image,
577602
packages_to_install=packages_to_install,
578-
pip_index_urls=pip_index_urls)
603+
pip_index_urls=pip_index_urls,
604+
pip_trusted_hosts=pip_trusted_hosts)
579605

580606
if REGISTERED_MODULES is not None:
581607
REGISTERED_MODULES[component_name] = component_info

0 commit comments

Comments
 (0)