-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathdagbuilder.py
More file actions
1371 lines (1128 loc) · 63.5 KB
/
Copy pathdagbuilder.py
File metadata and controls
1371 lines (1128 loc) · 63.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Module contains code for generating tasks and constructing a DAG"""
from __future__ import annotations
import ast
import inspect
import logging
import os
import re
import warnings
from copy import deepcopy
from datetime import datetime, timedelta
from functools import partial, reduce
from typing import Any, Callable, Dict, List, Tuple, Union
from airflow import configuration
from packaging import version
from dagfactory.utils import check_dict_key
try:
from airflow.sdk.bases.operator import BaseOperator
from airflow.sdk.definitions.dag import DAG
from airflow.sdk.definitions.variable import Variable
except ImportError:
from airflow.models import BaseOperator, Variable
from airflow.models.dag import DAG
from airflow.datasets import Dataset
from airflow.models import MappedOperator
from airflow.timetables.base import Timetable
from airflow.utils.module_loading import import_string
from airflow.utils.task_group import TaskGroup
from airflow.version import version as AIRFLOW_VERSION
try: # Try Airflow 3
from airflow.providers.standard.operators.python import BranchPythonOperator, PythonOperator
from airflow.providers.standard.sensors.python import PythonSensor
except ImportError:
from airflow.operators.python import BranchPythonOperator, PythonOperator
from airflow.sensors.python import PythonSensor
logger = logging.getLogger(__name__)
# Try to import HttpOperator and HttpSensor only if the package is installed
try:
from airflow.providers.http.operators.http import HttpOperator
from airflow.providers.http.sensors.http import HttpSensor
HTTP_OPERATOR_CLASS = HttpOperator
HTTP_SENSOR_CLASS = HttpSensor
except ImportError: # pragma: no cover
try:
# TODO: Remove this when apache-airflow-providers-http >= 5.0.0
from airflow.providers.http.operators.http import SimpleHttpOperator
from airflow.providers.http.sensors.http import HttpSensor
HTTP_OPERATOR_CLASS = SimpleHttpOperator
HTTP_SENSOR_CLASS = HttpSensor
except ImportError: # pragma: no cover
HTTP_OPERATOR_CLASS = None
HTTP_SENSOR_CLASS = None
logger.info("Package apache-airflow-providers-http is not installed.")
# Try to import SqlSensor only if the package is installed
try:
from airflow.providers.common.sql.sensors.sql import SqlSensor
SQL_SENSOR_CLASS = SqlSensor
except ImportError:
logger.info("Package apache-airflow-providers-common-sql is not installed.")
SQL_SENSOR_CLASS = None
# Try to import KubernetesPodOperator only if the package is installed
try:
from airflow.providers.cncf.kubernetes.operators.pod import KubernetesPodOperator
KUBERNETES_OPERATOR_CLASS = KubernetesPodOperator
except ImportError:
try:
# TODO: Remove this when apache-airflow-providers-cncf-kubernetes >= 10.0.0
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import KubernetesPodOperator
KUBERNETES_OPERATOR_CLASS = KubernetesPodOperator
except ImportError:
logger.info("Package apache-airflow-providers-cncf-kubernetes is not installed.")
KUBERNETES_OPERATOR_CLASS = None
try:
from airflow.providers.cncf.kubernetes import __version__
K8S_PROVIDER_VERSION = __version__
except ImportError: # pragma: no cover
try:
# TODO: Remove this when apache-airflow-providers-cncf-kubernetes >= 10.4.2
from airflow.providers.cncf.kubernetes import get_provider_info
K8S_PROVIDER_VERSION = get_provider_info.get_provider_info()["versions"][0]
except ImportError:
logger.info("Package apache-airflow-providers-cncf-kubernetes is not installed.")
K8S_PROVIDER_VERSION = "0"
try:
from airflow.providers.cncf.kubernetes.secret import Secret
except ImportError:
try:
# TODO: Remove this when apache-airflow-providers-cncf-kubernetes >= 5.0.0
from airflow.kubernetes.secret import Secret
except ImportError:
logger.info("Package apache-airflow-providers-cncf-kubernetes is not installed.")
try:
from kubernetes.client.models import (
V1Affinity,
V1Container,
V1ContainerPort as Port,
V1EnvFromSource,
V1EnvVar,
V1LocalObjectReference,
V1Pod,
V1PodSecurityContext,
V1Toleration,
V1Volume,
V1VolumeMount as VolumeMount,
)
except ImportError:
logger.info("Package apache-airflow-providers-cncf-kubernetes is not installed.")
from dagfactory import parsers, utils
from dagfactory.constants import AIRFLOW3_MAJOR_VERSION
from dagfactory.exceptions import DagFactoryConfigException, DagFactoryException
# these are params only used in the DAG factory, not in the tasks
SYSTEM_PARAMS: List[str] = ["operator", "dependencies", "task_group_name", "parent_group_name"]
INSTALLED_AIRFLOW_VERSION = version.parse(AIRFLOW_VERSION)
class DagBuilder:
"""
Generates tasks and a DAG from a config.
:param dag_name: the name of the DAG
:param dag_config: a dictionary containing configuration for the DAG
:param default_config: a dictionary containing defaults for all DAGs
in the YAML file
"""
def __init__(
self, dag_name: str, dag_config: Dict[str, Any], default_config: Dict[str, Any], yml_dag: str = ""
) -> None:
self.dag_name: str = dag_name
self.dag_config: Dict[str, Any] = deepcopy(dag_config)
self.default_config: Dict[str, Any] = deepcopy(default_config)
self._yml_dag = yml_dag
# pylint: disable=too-many-branches,too-many-statements
def get_dag_params(self) -> Dict[str, Any]:
"""
Merges default config with dag config, sets dag_id, and extropolates dag_start_date
:returns: dict of dag parameters
"""
try:
dag_params: Dict[str, Any] = utils.merge_configs(self.dag_config, self.default_config)
except Exception as err:
raise DagFactoryConfigException("Failed to merge config with default config") from err
dag_params["dag_id"]: str = self.dag_name
# If there are no default_args, add an empty dictionary
dag_params["default_args"] = {} if "default_args" not in dag_params else dag_params["default_args"]
if utils.check_dict_key(dag_params, "schedule_interval") and dag_params["schedule_interval"] == "None":
dag_params["schedule_interval"] = None
# Convert from 'dagrun_timeout_sec: int' to 'dagrun_timeout: timedelta'
if utils.check_dict_key(dag_params, "dagrun_timeout_sec"):
dag_params["dagrun_timeout"]: timedelta = timedelta(seconds=dag_params["dagrun_timeout_sec"])
del dag_params["dagrun_timeout_sec"]
if utils.check_dict_key(dag_params, "start_date"):
dag_params["start_date"]: datetime = utils.get_datetime(
date_value=dag_params["start_date"],
timezone=dag_params.get("timezone", "UTC"),
)
if utils.check_dict_key(dag_params, "end_date"):
dag_params["end_date"]: datetime = utils.get_datetime(
date_value=dag_params["end_date"],
timezone=dag_params.get("timezone", "UTC"),
)
# Convert from 'end_date: Union[str, datetime, date]' to 'end_date: datetime'
if utils.check_dict_key(dag_params["default_args"], "end_date"):
dag_params["default_args"]["end_date"]: datetime = utils.get_datetime(
date_value=dag_params["default_args"]["end_date"],
timezone=dag_params["default_args"].get("timezone", "UTC"),
)
if utils.check_dict_key(dag_params["default_args"], "retry_delay_sec"):
dag_params["default_args"]["retry_delay"]: timedelta = timedelta(
seconds=dag_params["default_args"]["retry_delay_sec"]
)
del dag_params["default_args"]["retry_delay_sec"]
if utils.check_dict_key(dag_params["default_args"], "sla_secs"):
dag_params["default_args"]["sla"]: timedelta = timedelta(seconds=dag_params["default_args"]["sla_secs"])
del dag_params["default_args"]["sla_secs"]
if utils.check_dict_key(dag_params["default_args"], "execution_timeout"):
if isinstance(dag_params["default_args"]["execution_timeout"], int):
dag_params["default_args"]["execution_timeout"]: timedelta = timedelta(
seconds=dag_params["default_args"]["execution_timeout"]
)
# Parse callbacks at the DAG-level and at the Task-level, configured in default_args. Note that the version
# check has gone into the set_callback method
for callback_type in [
"on_execute_callback",
"on_success_callback",
"on_failure_callback",
"on_retry_callback", # Not applicable at the DAG-level
"on_skipped_callback", # Not applicable at the DAG-level
"sla_miss_callback", # Not applicable at the default_args level
]:
# Here, we are parsing both the DAG-level params and default_args for callbacks. Previously, this was
# copy-and-pasted for each callback type and each configuration option (via a string import, function
# defined via YAML, or file path and name
# First, check at the DAG-level for just the single field (via a string or via a provider callback that
# takes parameters). Since "on_retry_callback" and "on_skipped_callback" is only applicable at the
# Task-level, we are skipping that callback type here.
if callback_type not in ("on_retry_callback", "on_skipped_callback"):
if utils.check_dict_key(dag_params, callback_type):
dag_params[callback_type]: Callable = self.set_callback(
parameters=dag_params, callback_type=callback_type
)
# Then, check at the DAG-level for a file path and name
if utils.check_dict_key(dag_params, f"{callback_type}_name") and utils.check_dict_key(
dag_params, f"{callback_type}_file"
):
dag_params[callback_type] = self.set_callback(
parameters=dag_params, callback_type=callback_type, has_name_and_file=True
)
# SLAs are defined at the DAG-level, and will be applied to every task.
# https://www.astronomer.io/docs/learn/error-notifications-in-airflow/. Here, we are not going to add
# callbacks for sla_miss_callback, or on_skipped_callback if the Airflow version is less than 2.7.0
if (callback_type != "sla_miss_callback") or not (
callback_type == "on_skipped_callback" and version.parse(AIRFLOW_VERSION) < version.parse("2.7.0")
):
# Next, check for a callback at the Task-level using default_args
if utils.check_dict_key(dag_params["default_args"], callback_type):
dag_params["default_args"][callback_type]: Callable = self.set_callback(
parameters=dag_params["default_args"], callback_type=callback_type
)
# Finally, check for file path and name at the Task-level using default_args
if utils.check_dict_key(dag_params["default_args"], f"{callback_type}_name") and utils.check_dict_key(
dag_params["default_args"], f"{callback_type}_file"
):
dag_params["default_args"][callback_type] = self.set_callback(
parameters=dag_params["default_args"], callback_type=callback_type, has_name_and_file=True
)
if utils.check_dict_key(dag_params, "template_searchpath"):
if isinstance(dag_params["template_searchpath"], (list, str)) and utils.check_template_searchpath(
dag_params["template_searchpath"]
):
dag_params["template_searchpath"]: Union[str, List[str]] = dag_params["template_searchpath"]
else:
raise DagFactoryException("template_searchpath is not valid!")
if utils.check_dict_key(dag_params, "render_template_as_native_obj"):
if isinstance(dag_params["render_template_as_native_obj"], bool):
dag_params["render_template_as_native_obj"]: bool = dag_params["render_template_as_native_obj"]
else:
raise DagFactoryException("render_template_as_native_obj should be bool type!")
if check_dict_key(dag_params["default_args"], "start_date"):
dag_params["default_args"]["start_date"]: datetime = utils.get_datetime(
date_value=dag_params["default_args"]["start_date"],
timezone=dag_params["default_args"].get("timezone", "UTC"),
)
return dag_params
@staticmethod
def make_timetable(timetable: str, timetable_params: Dict[str, Any]) -> Timetable:
"""
Takes a custom timetable and params and creates an instance of that timetable.
:returns instance of timetable object
"""
try:
# class is a Callable https://stackoverflow.com/a/34578836/3679900
timetable_obj: Callable[..., Timetable] = import_string(timetable)
except Exception as err:
raise DagFactoryException(f"Failed to import timetable {timetable} due to: {err}") from err
try:
schedule: Timetable = timetable_obj(**timetable_params)
except Exception as err: # pragma: no cover
raise DagFactoryException(f"Failed to create {timetable_obj} due to: {err}") from err
return schedule
@staticmethod
def _create_volume(vol):
volume = V1Volume(name=vol.get("name"))
for k, v in vol["configs"].items():
snake_key = utils.convert_to_snake_case(k)
if hasattr(volume, snake_key):
setattr(volume, snake_key, v)
else:
raise DagFactoryException(f"Volume for KubernetesPodOperator does not have attribute {k}")
return volume
@staticmethod
def _clean_kpo_task_params(task_params: dict) -> dict:
conversions = [
("ports", Port, "list"),
("volume_mounts", VolumeMount, "list"),
("env_vars", V1EnvVar, "list"),
("env_from", V1EnvFromSource, "list"),
("secrets", Secret, "list"),
("affinity", V1Affinity, "single"),
("image_pull_secrets", V1LocalObjectReference, "list"),
("tolerations", V1Toleration, "list"),
("security_context", V1PodSecurityContext, "single"),
("init_containers", V1Container, "list"),
("pod_runtime_info_envs", V1EnvVar, "list"),
("full_pod_spec", V1Pod, "single"),
]
# Conditional field based on version
if version.parse(K8S_PROVIDER_VERSION) >= version.parse("7.8.0"):
from kubernetes.client.models import V1HostAlias
conversions.append(("host_aliases", V1HostAlias, "list"))
if version.parse(K8S_PROVIDER_VERSION) >= version.parse("7.0.0"):
from kubernetes.client.models import V1PodDNSConfig
conversions.append(("dns_config", V1PodDNSConfig, "single"))
if version.parse(K8S_PROVIDER_VERSION) >= version.parse("5.0.0"):
from kubernetes.client.models import V1ResourceRequirements
conversions.append(("container_resources", V1ResourceRequirements, "single"))
if version.parse(K8S_PROVIDER_VERSION) >= version.parse("4.4.0"):
from kubernetes.client.models import V1SecurityContext
conversions.append(("container_security_context", V1SecurityContext, "single"))
for key, cls, conv_type in conversions:
if key in task_params and task_params[key] is not None:
if conv_type == "list":
task_params[key] = [cls(**v) for v in task_params[key]]
elif conv_type == "single":
task_params[key] = cls(task_params[key])
# Special case for volumes that uses a different constructor
if task_params.get("volumes") is not None:
task_params["volumes"] = [DagBuilder._create_volume(vol) for vol in task_params["volumes"]]
return task_params
@staticmethod
def _handle_http_sensor(operator_obj, task_params):
# Only handle if HttpOperator/HttpSensor are available
if HTTP_OPERATOR_CLASS and issubclass(operator_obj, HTTP_OPERATOR_CLASS):
headers = task_params.get("headers", {})
content_type = headers.get("Content-Type", "").lower()
if "data" in task_params and "application/json" in content_type:
task_params["data"]: Callable = utils.get_json_serialized_callable(task_params["data"])
if "Content-Type" not in headers:
headers["Content-Type"] = "application/json"
task_params["headers"] = headers
elif HTTP_SENSOR_CLASS and issubclass(operator_obj, HTTP_SENSOR_CLASS):
if not (
task_params.get("response_check_name") and task_params.get("response_check_file")
) and not task_params.get("response_check_lambda"):
raise DagFactoryException(
"Failed to create task. HttpSensor requires \
`response_check_name` and `response_check_file` parameters \
or `response_check_lambda` parameter."
)
# remove dag-factory specific parameters
# Airflow 2.0 doesn't allow these to
response_check_name = task_params.pop("response_check_name", None)
response_check_file = task_params.pop("response_check_file", None)
if response_check_name:
task_params["response_check"]: Callable = utils.get_python_callable(
response_check_name, response_check_file
)
else:
response_check_name = task_params.pop("response_check_lambda", None)
task_params["response_check"]: Callable = utils.get_python_callable_lambda(response_check_name)
return task_params
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
# pylint: disable=too-many-locals
@staticmethod
def make_task(operator: str, task_params: Dict[str, Any]) -> BaseOperator:
"""
Takes an operator and params and creates an instance of that operator.
:returns: instance of operator object
"""
try:
# class is a Callable https://stackoverflow.com/a/34578836/3679900
operator_obj: Callable[..., BaseOperator] = import_string(operator)
except Exception as err:
raise DagFactoryException(f"Failed to import operator: {operator}") from err
# pylint: disable=too-many-nested-blocks
try:
if issubclass(operator_obj, (PythonOperator, BranchPythonOperator, PythonSensor)):
if (
not task_params.get("python_callable")
and not task_params.get("python_callable_name")
and not task_params.get("python_callable_file")
):
# pylint: disable=line-too-long
raise DagFactoryException(
"Failed to create task. PythonOperator, BranchPythonOperator and PythonSensor requires \
`python_callable_name` and `python_callable_file` "
"parameters.\nOptionally you can load python_callable "
"from a file. with the special pyyaml notation:\n"
" python_callable_file: !!python/name:my_module.my_func"
)
if not task_params.get("python_callable"):
task_params["python_callable"]: Callable = utils.get_python_callable(
task_params["python_callable_name"], task_params["python_callable_file"]
)
# remove dag-factory specific parameters
# Airflow 2.0 doesn't allow these to be passed to operator
del task_params["python_callable_name"]
del task_params["python_callable_file"]
elif isinstance(task_params["python_callable"], str):
task_params["python_callable"]: Callable = import_string(task_params["python_callable"])
# Check for the custom success and failure callables in SqlSensor. These are considered
# optional, so no failures in case they aren't found. Note: there's no reason to
# declare both a callable file and a lambda function for success/failure parameter.
# If both are found the object will not throw and error, instead callable file will
# take precedence over the lambda function
if SQL_SENSOR_CLASS and issubclass(operator_obj, SQL_SENSOR_CLASS):
# Success checks
if task_params.get("success_check_file") and task_params.get("success_check_name"):
task_params["success"]: Callable = utils.get_python_callable(
task_params["success_check_name"], task_params["success_check_file"]
)
del task_params["success_check_name"]
del task_params["success_check_file"]
elif task_params.get("success_check_lambda"):
task_params["success"]: Callable = utils.get_python_callable_lambda(
task_params["success_check_lambda"]
)
del task_params["success_check_lambda"]
# Failure checks
if task_params.get("failure_check_file") and task_params.get("failure_check_name"):
task_params["failure"]: Callable = utils.get_python_callable(
task_params["failure_check_name"], task_params["failure_check_file"]
)
del task_params["failure_check_name"]
del task_params["failure_check_file"]
elif task_params.get("failure_check_lambda"):
task_params["failure"]: Callable = utils.get_python_callable_lambda(
task_params["failure_check_lambda"]
)
del task_params["failure_check_lambda"]
# Only handle HTTP operator/sensor if the package is installed
if (HTTP_OPERATOR_CLASS or HTTP_SENSOR_CLASS) and issubclass(
operator_obj, (HTTP_OPERATOR_CLASS, HTTP_SENSOR_CLASS)
):
task_params = DagBuilder._handle_http_sensor(operator_obj, task_params)
# Only handle KubernetesPodOperator if the package is installed
if KUBERNETES_OPERATOR_CLASS and issubclass(operator_obj, KUBERNETES_OPERATOR_CLASS):
task_params = DagBuilder._clean_kpo_task_params(task_params)
DagBuilder.adjust_general_task_params(task_params)
expand_kwargs: Dict[str, Union[Dict[str, Any], Any]] = {}
if utils.check_dict_key(task_params, "expand") or utils.check_dict_key(task_params, "partial"):
# Getting expand and partial kwargs from task_params
(task_params, expand_kwargs, partial_kwargs) = utils.get_expand_partial_kwargs(task_params)
# If there are partial_kwargs we should merge them with existing task_params
if partial_kwargs and not utils.is_partial_duplicated(partial_kwargs, task_params):
task_params.update(partial_kwargs)
task: Union[BaseOperator, MappedOperator] = (
operator_obj(**task_params)
if not expand_kwargs
else operator_obj.partial(**task_params).expand(**expand_kwargs)
)
except Exception as err:
raise DagFactoryException(f"Failed to create {operator_obj} task: {err}") from err
return task
@staticmethod
def make_task_groups(task_groups: Dict[str, Any], dag: DAG) -> Dict[str, "TaskGroup"]:
"""Takes a DAG and task group configurations. Creates TaskGroup instances.
:param task_groups: Task group configuration from the YAML configuration file.
:param dag: DAG instance that task groups to be added.
"""
task_groups_dict: Dict[str, "TaskGroup"] = {}
for task_group_name, task_group_conf in task_groups.items():
DagBuilder.make_nested_task_groups(
task_group_name, task_group_conf, task_groups_dict, task_groups, None, dag
)
return task_groups_dict
@staticmethod
def _init_task_group_callback_param(task_group_conf):
"""
_init_task_group_callback_param
Handle configuring callbacks for TaskGroups in this method in this helper-method
:param task_group_conf: dict containing the configuration of the TaskGroup
"""
# The Airflow version needs to be at least 2.2.0, and default args must be present. Basically saying here: if
# it's not the case that we're using at least Airflow 2.2.0 and default_args are present, then return the
# TaskGroup configuration without doing anything
if not (
version.parse(AIRFLOW_VERSION) >= version.parse("2.2.0")
and isinstance(task_group_conf.get("default_args"), dict)
):
return task_group_conf
# Check the callback types that can be in the default_args of the TaskGroup
for callback_type in [
"on_execute_callback",
"on_success_callback",
"on_failure_callback",
"on_retry_callback",
"on_skipped_callback", # This is only available AIRFLOW_VERSION >= 2.7.0
]:
# on_skipped_callback can only be added to the default_args of a TaskGroup for AIRFLOW_VERSION >= 2.7.0
if callback_type == "on_skipped_callback" and version.parse(AIRFLOW_VERSION) < version.parse("2.7.0"):
continue
# First, check for a str, str with params, or provider callback
if utils.check_dict_key(task_group_conf["default_args"], callback_type):
task_group_conf["default_args"][callback_type]: Callable = DagBuilder.set_callback(
parameters=task_group_conf["default_args"], callback_type=callback_type
)
# Then, check for a file path and name
if utils.check_dict_key(task_group_conf["default_args"], f"{callback_type}_name") and utils.check_dict_key(
task_group_conf["default_args"], f"{callback_type}_file"
):
task_group_conf["default_args"][callback_type] = DagBuilder.set_callback(
parameters=task_group_conf["default_args"],
callback_type=callback_type,
has_name_and_file=True,
)
return task_group_conf
@staticmethod
def make_nested_task_groups(
task_group_name: str,
task_group_conf: Any,
task_groups_dict: Dict[str, "TaskGroup"],
task_groups: Dict[str, Any],
circularity_check_queue: List[str] | None,
dag: DAG,
):
"""Takes a DAG and task group configurations. Creates nested TaskGroup instances.
:param task_group_name: The name of the task group to be created
:param task_group_conf: Configuration details for the task group, which may include parent group information.
:param task_groups_dict: A dictionary where the created TaskGroup instances are stored, keyed by task group name.
:param task_groups: Task group configuration from the YAML configuration file.
:param circularity_check_queue: A list used to track the task groups being processed to detect circular dependencies.
:param dag: DAG instance that task groups to be added.
"""
if task_group_name in task_groups_dict:
return
if circularity_check_queue is None:
circularity_check_queue = []
if task_group_name in circularity_check_queue:
error_string = "Circular dependency detected:\n"
index = circularity_check_queue.index(task_group_name)
while index < len(circularity_check_queue):
error_string += f"{circularity_check_queue[index]} depends on {task_group_name}\n"
index += 1
raise Exception(error_string)
circularity_check_queue.append(task_group_name)
if task_group_conf.get("parent_group_name"):
parent_group_name = task_group_conf["parent_group_name"]
parent_group_conf = task_groups[parent_group_name]
DagBuilder.make_nested_task_groups(
parent_group_name, parent_group_conf, task_groups_dict, task_groups, circularity_check_queue, dag
)
task_group_conf["parent_group"] = task_groups_dict[parent_group_name]
task_group_conf["group_id"] = task_group_name
task_group_conf["dag"] = dag
task_group_conf = DagBuilder._init_task_group_callback_param(task_group_conf)
task_group = TaskGroup(**{k: v for k, v in task_group_conf.items() if k not in SYSTEM_PARAMS})
task_groups_dict[task_group_name] = task_group
@staticmethod
def set_dependencies(
tasks_config: Dict[str, Dict[str, Any]],
operators_dict: Dict[str, BaseOperator],
task_groups_config: Dict[str, Dict[str, Any]],
task_groups_dict: Dict[str, "TaskGroup"],
):
"""Take the task configurations in YAML file and operator
instances, then set the dependencies between tasks.
:param tasks_config: Raw task configuration from YAML file
:param operators_dict: Dictionary for operator instances
:param task_groups_config: Raw task group configuration from YAML file
:param task_groups_dict: Dictionary for task group instances
"""
tasks_and_task_groups_config = {**tasks_config, **task_groups_config}
tasks_and_task_groups_instances = {**operators_dict, **task_groups_dict}
for name, conf in tasks_and_task_groups_config.items():
# if task is in a task group, group_id is prepended to its name
if conf.get("task_group"):
group_id = conf["task_group"].group_id
name = f"{group_id}.{name}"
if conf.get("dependencies"):
source: Union[BaseOperator, "TaskGroup"] = tasks_and_task_groups_instances[name]
for dep in conf["dependencies"]:
if tasks_and_task_groups_config[dep].get("task_group"):
group_id = tasks_and_task_groups_config[dep]["task_group"].group_id
dep = f"{group_id}.{dep}"
dep: Union[BaseOperator, "TaskGroup"] = tasks_and_task_groups_instances[dep]
source.set_upstream(dep)
@staticmethod
def replace_expand_values(task_conf: Dict, tasks_dict: Dict[str, BaseOperator]):
"""
Replaces any expand values in the task configuration with their corresponding XComArg value.
:param: task_conf: the configuration dictionary for the task.
:type: Dict
:param: tasks_dict: a dictionary containing the tasks for the current DAG run.
:type: Dict[str, BaseOperator]
:returns: updated conf dict with expanded values replaced with their XComArg values.
:type: Dict
"""
for expand_key, expand_value in task_conf["expand"].items():
if ".output" in expand_value:
task_id = expand_value.split(".output")[0]
if task_id in tasks_dict:
task_conf["expand"][expand_key] = tasks_dict[task_id].output
elif "XcomArg" in expand_value:
task_id = re.findall(r"\(+(.*?)\)", expand_value)[0]
if task_id in tasks_dict:
task_conf["expand"][expand_key] = tasks_dict[task_id].output
return task_conf
@staticmethod
def safe_eval(condition_string: str, dataset_map: dict) -> Any:
"""
Safely evaluates a condition string using the provided dataset map.
:param condition_string: A string representing the condition to evaluate.
Example: "(dataset_custom_1 & dataset_custom_2) | dataset_custom_3".
:type condition_string: str
:param dataset_map: A dictionary where keys are valid variable names (dataset aliases),
and values are Dataset objects.
:type dataset_map: dict
:returns: The result of evaluating the condition.
:rtype: Any
"""
tree = ast.parse(condition_string, mode="eval")
evaluator = parsers.SafeEvalVisitor(dataset_map)
return evaluator.evaluate(tree)
@staticmethod
def _extract_and_transform_datasets(datasets_conditions: str) -> Tuple[str, Dict[str, Any]]:
"""
Extracts dataset names and storage paths from the conditions string and transforms them into valid variable names.
:param datasets_conditions: A string of conditions dataset URIs to be evaluated in the condition.
:type datasets_conditions: str
:returns: A tuple containing the transformed conditions string and the dataset map.
:rtype: Tuple[str, Dict[str, Any]]
"""
dataset_map = {}
datasets_filter: List[str] = utils.extract_dataset_names(datasets_conditions) + utils.extract_storage_names(
datasets_conditions
)
for uri in datasets_filter:
valid_variable_name = utils.make_valid_variable_name(uri)
datasets_conditions = datasets_conditions.replace(uri, valid_variable_name)
dataset_map[valid_variable_name] = Dataset(uri)
return datasets_conditions, dataset_map
@staticmethod
def evaluate_condition_with_datasets(datasets_conditions: str) -> Any:
"""
Evaluates a condition using the dataset filter, transforming URIs into valid variable names.
:param datasets_conditions: A string of conditions dataset URIs to be evaluated in the condition.
:type datasets_conditions: str
:returns: The result of the logical condition evaluation with URIs replaced by valid variable names.
:rtype: Any
"""
datasets_conditions, dataset_map = DagBuilder._extract_and_transform_datasets(datasets_conditions)
evaluated_condition = DagBuilder.safe_eval(datasets_conditions, dataset_map)
return evaluated_condition
@staticmethod
def process_file_with_datasets(file: str, datasets_conditions: str) -> Any:
"""
Processes datasets from a file and evaluates conditions if provided.
:param file: The file path containing dataset information in a YAML or other structured format.
:type file: str
:param datasets_conditions: A string of dataset conditions to filter and process.
:type datasets_conditions: str
:returns: The result of the condition evaluation if `condition_string` is provided, otherwise a list of `Dataset` objects.
:rtype: Any
"""
is_airflow_version_at_least_2_9 = version.parse(AIRFLOW_VERSION) >= version.parse("2.9.0")
datasets_conditions, dataset_map = DagBuilder._extract_and_transform_datasets(datasets_conditions)
if is_airflow_version_at_least_2_9:
map_datasets = utils.get_datasets_map_uri_yaml_file(file, list(dataset_map.keys()))
dataset_map = {alias_dataset: Dataset(uri) for alias_dataset, uri in map_datasets.items()}
evaluated_condition = DagBuilder.safe_eval(datasets_conditions, dataset_map)
return evaluated_condition
else:
datasets_uri = utils.get_datasets_uri_yaml_file(file, list(dataset_map.keys()))
return [Dataset(uri) for uri in datasets_uri]
@staticmethod
def _init_watchers(watchers_data):
"""Initialize watcher objects from configuration."""
from dagfactory.utils import _import_from_string
watchers = []
for watcher in watchers_data:
watcher_class = _import_from_string(watcher["callable"])
trigger_data = watcher.get("trigger", {})
trigger_class = _import_from_string(trigger_data.get("callable"))
trigger_params = trigger_data.get("params", {})
watchers.append(watcher_class(name=watcher.get("name"), trigger=trigger_class(**trigger_params)))
return watchers
@staticmethod
def _combine_assets(assets, op: str):
"""Combine a list of Asset objects using logical operators."""
if op == "or":
return reduce(lambda a, b: a | b, assets)
elif op == "and":
return reduce(lambda a, b: a & b, assets)
else:
raise ValueError(f"Unknown operator: {op}")
@staticmethod
def _is_asset(d):
from airflow.sdk import Asset
if not isinstance(d, dict):
return False
for key, value in d.items():
if isinstance(value, Asset):
return True
elif isinstance(value, list):
if any(isinstance(item, Asset) for item in value):
return True
elif isinstance(value, dict):
if DagBuilder._is_asset(value):
return True
return False
@staticmethod
def _asset_schedule(value):
"""Recursively parse and construct assets or combinations of assets."""
from airflow.sdk import Asset
if isinstance(value, dict):
if "or" in value:
assets = [DagBuilder._asset_schedule(item) for item in value["or"]]
return DagBuilder._combine_assets(assets, "or")
elif "and" in value:
assets = [DagBuilder._asset_schedule(item) for item in value["and"]]
return DagBuilder._combine_assets(assets, "and")
elif isinstance(value, list):
return [asset for asset in value]
elif isinstance(value, Asset):
return value
else:
raise TypeError(f"Unexpected data type: {type(value)}")
@staticmethod
def configure_schedule(dag_params: Dict[str, Any], dag_kwargs: Dict[str, Any]) -> None:
"""
Configures the schedule for the DAG based on parameters and the Airflow version.
:param dag_params: A dictionary containing DAG parameters, including scheduling configuration.
Example: {"schedule": {"file": "datasets.yaml", "datasets": ["dataset_1"], "conditions": "dataset_1 & dataset_2"}}
:type dag_params: Dict[str, Any]
:param dag_kwargs: A dictionary for setting the resulting schedule configuration for the DAG.
:type dag_kwargs: Dict[str, Any]
:raises KeyError: If required keys like "schedule" or "datasets" are missing in the parameters.
:returns: None. The function updates `dag_kwargs` in-place.
"""
if INSTALLED_AIRFLOW_VERSION.major < AIRFLOW3_MAJOR_VERSION:
is_airflow_version_at_least_2_4 = version.parse(AIRFLOW_VERSION) >= version.parse("2.4.0")
is_airflow_version_at_least_2_9 = version.parse(AIRFLOW_VERSION) >= version.parse("2.9.0")
has_schedule_attr = utils.check_dict_key(dag_params, "schedule")
has_schedule_interval_attr = utils.check_dict_key(dag_params, "schedule_interval")
if has_schedule_attr and not has_schedule_interval_attr and is_airflow_version_at_least_2_4:
schedule: Dict[str, Any] = dag_params.get("schedule")
has_file_attr = utils.check_dict_key(schedule, "file")
has_datasets_attr = utils.check_dict_key(schedule, "datasets")
if has_file_attr and has_datasets_attr:
file = schedule.get("file")
datasets: Union[List[str], str] = schedule.get("datasets")
datasets_conditions: str = utils.parse_list_datasets(datasets)
dag_kwargs["schedule"] = DagBuilder.process_file_with_datasets(file, datasets_conditions)
elif has_datasets_attr and is_airflow_version_at_least_2_9:
datasets = schedule["datasets"]
datasets_conditions: str = utils.parse_list_datasets(datasets)
dag_kwargs["schedule"] = DagBuilder.evaluate_condition_with_datasets(datasets_conditions)
else:
dag_kwargs["schedule"] = [Dataset(uri) for uri in schedule]
if has_file_attr:
schedule.pop("file")
if has_datasets_attr:
schedule.pop("datasets")
else:
schedule = dag_params.get("schedule")
if DagBuilder._is_asset(schedule):
dag_kwargs["schedule"] = DagBuilder._asset_schedule(schedule)
else:
if (
utils.check_dict_key(dag_params, "schedule")
and isinstance(dag_params["schedule"], str)
and dag_params["schedule"].strip().lower() == "none"
):
dag_kwargs["schedule"] = None
else:
dag_kwargs["schedule"] = schedule
@staticmethod
def _normalise_tasks_config(tasks_cfg: Any) -> Dict[str, Dict[str, Any]]:
"""Ensure tasks configuration is in the canonical dict form.
Dag authors may provide tasks either as a mapping of ``task_id`` -> config
or as a *list* of configs each containing a ``task_id`` key. This helper
converts the latter to the former so that the rest of the builder logic
can operate on a single, predictable structure.
:param tasks_cfg: the raw ``tasks`` value from the YAML / dict config
"""
# Nothing provided – let the caller decide how to handle later.
if tasks_cfg is None:
return {}
# Already in the desired form
if isinstance(tasks_cfg, dict):
return tasks_cfg
if isinstance(tasks_cfg, list):
converted: Dict[str, Dict[str, Any]] = {}
for entry in tasks_cfg:
if not isinstance(entry, dict) or "task_id" not in entry:
raise DagFactoryConfigException(
"Each task definition in the list must be a mapping that contains a 'task_id' key"
)
task_id = entry["task_id"]
if task_id in converted:
raise DagFactoryConfigException(f"Duplicate task_id detected in tasks list: '{task_id}'")
# Exclude task_id from the configuration body – historically it
# is represented by the mapping key, not within the dict.
task_conf = {k: v for k, v in entry.items() if k != "task_id"}
converted[task_id] = task_conf
return converted
raise DagFactoryConfigException("'tasks' must be either a mapping or a list of task configs")
@staticmethod
def _normalise_task_groups_config(task_groups_cfg: Any) -> Dict[str, Dict[str, Any]]:
"""Convert a list-based task_groups definition into dict form.
Accepts either the canonical mapping of ``group_name`` -> config or a list where each item is a mapping
containing a ``group_name`` key. Performs duplicate detection and basic validation.
:param task_groups_cfg: the raw ``task_groups`` value from the YAML / dict config
"""
if task_groups_cfg is None:
return {}
if isinstance(task_groups_cfg, dict):
return task_groups_cfg
if isinstance(task_groups_cfg, list):
converted: Dict[str, Dict[str, Any]] = {}
for entry in task_groups_cfg:
if not isinstance(entry, dict) or "group_name" not in entry:
raise DagFactoryConfigException(
"Each task_group definition in the list must be a mapping that contains a 'group_name' key"
)
group_id = entry["group_name"]
if group_id in converted:
raise DagFactoryConfigException(f"Duplicate group_name detected in task_groups list: '{group_id}'")
group_conf = {k: v for k, v in entry.items() if k != "group_name"}
converted[group_id] = group_conf
return converted
raise DagFactoryConfigException("'task_groups' must be either a mapping or a list of group configs")
# pylint: disable=too-many-locals
def build(self) -> Dict[str, Union[str, DAG]]:
"""
Generates a DAG from the DAG parameters.
:returns: dict with dag_id and DAG object
:type: Dict[str, Union[str, DAG]]
"""
dag_params: Dict[str, Any] = self.get_dag_params()
dag_params["tasks"] = DagBuilder._normalise_tasks_config(dag_params.get("tasks"))
dag_params["task_groups"] = DagBuilder._normalise_task_groups_config(dag_params.get("task_groups"))
dag_kwargs: Dict[str, Any] = {}
dag_kwargs["dag_id"] = dag_params["dag_id"]
if version.parse(AIRFLOW_VERSION) >= version.parse("2.9.0"):
dag_kwargs["dag_display_name"] = dag_params.get("dag_display_name", dag_params["dag_id"])
dag_kwargs["description"] = dag_params.get("description", None)
if "concurrency" in dag_params:
warnings.warn(
"`concurrency` param is deprecated. Please use max_active_tasks.", category=DeprecationWarning
)
dag_kwargs["max_active_tasks"] = dag_params["concurrency"]
else:
dag_kwargs["max_active_tasks"] = dag_params.get(
"max_active_tasks", configuration.conf.getint("core", "max_active_tasks_per_dag")
)
if dag_params.get("timetable"):
timetable_args = dag_params.get("timetable")
dag_kwargs["timetable"] = DagBuilder.make_timetable(
timetable_args.get("callable"), timetable_args.get("params")
)
dag_kwargs["catchup"] = dag_params.get(
"catchup", configuration.conf.getboolean("scheduler", "catchup_by_default")
)
dag_kwargs["max_active_runs"] = dag_params.get(
"max_active_runs", configuration.conf.getint("core", "max_active_runs_per_dag")
)