19
19
import logging
20
20
import secrets
21
21
import string
22
+ import warnings
22
23
from typing import TYPE_CHECKING
23
24
24
25
import pendulum
25
26
from slugify import slugify
26
27
27
28
from airflow .compat .functools import cache
28
29
from airflow .configuration import conf
30
+ from airflow .exceptions import AirflowProviderDeprecationWarning
29
31
30
32
if TYPE_CHECKING :
31
33
from airflow .models .taskinstancekey import TaskInstanceKey
@@ -45,6 +47,18 @@ def rand_str(num):
45
47
return "" .join (secrets .choice (alphanum_lower ) for _ in range (num ))
46
48
47
49
50
+ def add_unique_suffix (* , name : str , rand_len : int = 8 , max_len : int = POD_NAME_MAX_LENGTH ) -> str :
51
+ """Add random string to pod or job name while staying under max length.
52
+
53
+ :param name: name of the pod or job
54
+ :param rand_len: length of the random string to append
55
+ :param max_len: maximum length of the pod name
56
+ :meta private:
57
+ """
58
+ suffix = "-" + rand_str (rand_len )
59
+ return name [: max_len - len (suffix )].strip ("-." ) + suffix
60
+
61
+
48
62
def add_pod_suffix (* , pod_name : str , rand_len : int = 8 , max_len : int = POD_NAME_MAX_LENGTH ) -> str :
49
63
"""Add random string to pod name while staying under max length.
50
64
@@ -53,10 +67,48 @@ def add_pod_suffix(*, pod_name: str, rand_len: int = 8, max_len: int = POD_NAME_
53
67
:param max_len: maximum length of the pod name
54
68
:meta private:
55
69
"""
70
+ warnings .warn (
71
+ "This function is deprecated. Please use `add_unique_suffix`." ,
72
+ AirflowProviderDeprecationWarning ,
73
+ stacklevel = 2 ,
74
+ )
75
+
56
76
suffix = "-" + rand_str (rand_len )
57
77
return pod_name [: max_len - len (suffix )].strip ("-." ) + suffix
58
78
59
79
80
+ def create_unique_id (
81
+ dag_id : str | None = None ,
82
+ task_id : str | None = None ,
83
+ * ,
84
+ max_length : int = POD_NAME_MAX_LENGTH ,
85
+ unique : bool = True ,
86
+ ) -> str :
87
+ """
88
+ Generate unique pod or job ID given a dag_id and / or task_id.
89
+
90
+ :param dag_id: DAG ID
91
+ :param task_id: Task ID
92
+ :param max_length: max number of characters
93
+ :param unique: whether a random string suffix should be added
94
+ :return: A valid identifier for a kubernetes pod name
95
+ """
96
+ if not (dag_id or task_id ):
97
+ raise ValueError ("Must supply either dag_id or task_id." )
98
+ name = ""
99
+ if dag_id :
100
+ name += dag_id
101
+ if task_id :
102
+ if name :
103
+ name += "-"
104
+ name += task_id
105
+ base_name = slugify (name , lowercase = True )[:max_length ].strip (".-" )
106
+ if unique :
107
+ return add_pod_suffix (pod_name = base_name , rand_len = 8 , max_len = max_length )
108
+ else :
109
+ return base_name
110
+
111
+
60
112
def create_pod_id (
61
113
dag_id : str | None = None ,
62
114
task_id : str | None = None ,
@@ -73,6 +125,12 @@ def create_pod_id(
73
125
:param unique: whether a random string suffix should be added
74
126
:return: A valid identifier for a kubernetes pod name
75
127
"""
128
+ warnings .warn (
129
+ "This function is deprecated. Please use `create_unique_id`." ,
130
+ AirflowProviderDeprecationWarning ,
131
+ stacklevel = 2 ,
132
+ )
133
+
76
134
if not (dag_id or task_id ):
77
135
raise ValueError ("Must supply either dag_id or task_id." )
78
136
name = ""
0 commit comments