Skip to content

Commit

Permalink
feat(sdk): Add settings of the dnsConfig field. Fixes #4836 (#4837)
Browse files Browse the repository at this point in the history
* feat(sdk): Add settings of the dnsConfig field. Fixes #4836

* feat(sdk): Add dnsConfig example and sample.

* feat(sdk): Refactor dnsConfig param.

* feat(sdk): Refactor dnsConfig param.
  • Loading branch information
ohke authored Dec 15, 2020
1 parent 7a66414 commit 5a4b70e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
50 changes: 50 additions & 0 deletions samples/core/dns_config/dns_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import kfp
from kfp import dsl
from kubernetes.client.models import V1PodDNSConfig, V1PodDNSConfigOption


def echo_op():
return dsl.ContainerOp(
name='echo',
image='library/bash:4.4.23',
command=['sh', '-c'],
arguments=['echo "hello world"']
)


@dsl.pipeline(
name='dnsConfig setting',
description='Passes dnsConfig setting to workflow.'
)
def dns_config_pipeline():
echo_task = echo_op()


if __name__ == '__main__':
pipeline_conf = kfp.dsl.PipelineConf()
pipeline_conf.set_dns_config(dns_config=V1PodDNSConfig(
nameservers=["1.2.3.4"],
options=[V1PodDNSConfigOption(name="ndots", value="2")]
))

kfp.compiler.Compiler().compile(
dns_config_pipeline,
__file__ + '.yaml',
pipeline_conf=pipeline_conf
)
3 changes: 3 additions & 0 deletions sdk/python/kfp/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,9 @@ def _create_pipeline_workflow(self, parameter_defaults, pipeline, op_transformer
if pipeline_conf.default_pod_node_selector:
workflow['spec']['nodeSelector'] = pipeline_conf.default_pod_node_selector

if pipeline_conf.dns_config:
workflow['spec']['dnsConfig'] = convert_k8s_obj_to_json(pipeline_conf.dns_config)

if pipeline_conf.image_pull_policy != None:
if pipeline_conf.image_pull_policy in ["Always", "Never", "IfNotPresent"]:
for template in workflow["spec"]["templates"]:
Expand Down
23 changes: 23 additions & 0 deletions sdk/python/kfp/dsl/_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from typing import Union
from kubernetes.client.models import V1PodDNSConfig
from . import _container_op
from . import _resource_op
from . import _ops_group
Expand Down Expand Up @@ -66,6 +67,7 @@ def __init__(self):
self.image_pull_policy = None
self.parallelism = None
self._data_passing_method = None
self.dns_config = None

def set_image_pull_secrets(self, image_pull_secrets):
"""Configures the pipeline level imagepullsecret
Expand Down Expand Up @@ -149,6 +151,27 @@ def add_op_transformer(self, transformer):
"""
self.op_transformers.append(transformer)

def set_dns_config(self, dns_config: V1PodDNSConfig):
"""Set the dnsConfig to be given to each pod.
Args:
dns_config: Kubernetes V1PodDNSConfig
For detailed description, check Kubernetes V1PodDNSConfig definition
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1PodDNSConfig.md
Example:
::
import kfp
from kubernetes.client.models import V1PodDNSConfig, V1PodDNSConfigOption
pipeline_conf = kfp.dsl.PipelineConf()
pipeline_conf.set_dns_config(dns_config=V1PodDNSConfig(
nameservers=["1.2.3.4"],
options=[V1PodDNSConfigOption(name="ndots", value="2")]
))
"""
self.dns_config = dns_config

@property
def data_passing_method(self):
return self._data_passing_method
Expand Down
18 changes: 17 additions & 1 deletion sdk/python/tests/compiler/compiler_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from kfp.dsl import ContainerOp, pipeline
from kfp.dsl.types import Integer, InconsistentTypeException
from kubernetes.client import V1Toleration, V1Affinity, V1NodeSelector, V1NodeSelectorRequirement, V1NodeSelectorTerm, \
V1NodeAffinity
V1NodeAffinity, V1PodDNSConfig, V1PodDNSConfigOption


def some_op():
Expand Down Expand Up @@ -822,6 +822,22 @@ def some_pipeline():
workflow_dict = kfp.compiler.Compiler()._compile(some_pipeline)
self.assertEqual(workflow_dict['spec']['nodeSelector'], {"cloud.google.com/gke-accelerator":"nvidia-tesla-p4"})

def test_set_dns_config(self):
"""Test a pipeline with node selector."""
@dsl.pipeline()
def some_pipeline():
some_op()
dsl.get_pipeline_conf().set_dns_config(V1PodDNSConfig(
nameservers=["1.2.3.4"],
options=[V1PodDNSConfigOption(name="ndots", value="2")]
))

workflow_dict = kfp.compiler.Compiler()._compile(some_pipeline)
self.assertEqual(
workflow_dict['spec']['dnsConfig'],
{"nameservers": ["1.2.3.4"], "options": [{"name": "ndots", "value": "2"}]}
)

def test_container_op_output_error_when_no_or_multiple_outputs(self):

def no_outputs_pipeline():
Expand Down

0 comments on commit 5a4b70e

Please sign in to comment.