Fix KubernetesPodOperator pod name length validation (#8829)

* Fix KubernetesPodOperator pod name length validation

* Add test, verify Exception is raised
This commit is contained in:
Daniel Saiz 2020-05-15 17:41:52 +02:00 коммит произвёл GitHub
Родитель 85bbab27db
Коммит f82ad452b0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 18 добавлений и 3 удалений

Просмотреть файл

@ -49,8 +49,8 @@ class KubernetesPodOperator(BaseOperator): # pylint: disable=too-many-instance-
:param image: Docker image you wish to launch. Defaults to hub.docker.com,
but fully qualified URLS will point to custom repositories.
:type image: str
:param name: name of the pod in which the task will run, will be used to
generate a pod id (DNS-1123 subdomain, containing only [a-z0-9.-]).
:param name: name of the pod in which the task will run, will be used (plus a random
suffix) to generate a pod id (DNS-1123 subdomain, containing only [a-z0-9.-]).
:type name: str
:param cmds: entrypoint of the container. (templated)
The docker images's entrypoint is used if this is not provided.
@ -322,5 +322,5 @@ class KubernetesPodOperator(BaseOperator): # pylint: disable=too-many-instance-
def _set_name(self, name):
if self.pod_template_file or self.full_pod_spec:
return None
validate_key(name, max_length=63)
validate_key(name, max_length=220)
return re.sub(r'[^a-z0-9.-]+', '-', name.lower())

Просмотреть файл

@ -819,5 +819,20 @@ class TestKubernetesPodOperator(unittest.TestCase):
self.expected_pod['spec']['priorityClassName'] = priority_class_name
self.assertEqual(self.expected_pod, actual_pod)
def test_pod_name(self):
pod_name_too_long = "a" * 221
with self.assertRaises(AirflowException):
KubernetesPodOperator(
namespace='default',
image="ubuntu:16.04",
cmds=["bash", "-cx"],
arguments=["echo 10"],
labels={"foo": "bar"},
name=pod_name_too_long,
task_id="task",
in_cluster=False,
do_xcom_push=False,
)
# pylint: enable=unused-argument