Source code for

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

from datetime import datetime

from airflow.models.baseoperator import chain
from airflow.models.dag import DAG
from import ClusterStates, FargateProfileStates
from import (
from import EksClusterStateSensor, EksFargateProfileStateSensor
from airflow.utils.trigger_rule import TriggerRule
from import ENV_ID_KEY, SystemTestContextBuilder
from import get_describe_pod_operator

[docs]DAG_ID = "example_eks_with_fargate_in_one_step"
# Externally fetched variables # See
# See
[docs]sys_test_context_task = ( SystemTestContextBuilder() .add_variable(CLUSTER_ROLE_ARN_KEY) .add_variable(FARGATE_POD_ROLE_ARN_KEY) .add_variable(SUBNETS_KEY, split_string=True) .build() )
with DAG( dag_id=DAG_ID, schedule="@once", start_date=datetime(2021, 1, 1), tags=["example"], catchup=False, ) as dag:
[docs] test_context = sys_test_context_task()
env_id = test_context[ENV_ID_KEY] cluster_role_arn = test_context[CLUSTER_ROLE_ARN_KEY] fargate_pod_role_arn = test_context[FARGATE_POD_ROLE_ARN_KEY] subnets = test_context[SUBNETS_KEY] cluster_name = f"{env_id}-cluster" fargate_profile_name = f"{env_id}-profile" test_name = f"{env_id}_{DAG_ID}" # [START howto_operator_eks_create_cluster_with_fargate_profile] # Create an Amazon EKS cluster control plane and an AWS Fargate compute platform in one step. create_cluster_and_fargate_profile = EksCreateClusterOperator( task_id="create_eks_cluster_and_fargate_profile", cluster_name=cluster_name, cluster_role_arn=cluster_role_arn, resources_vpc_config={ "subnetIds": subnets, "endpointPublicAccess": True, "endpointPrivateAccess": False, }, compute="fargate", fargate_profile_name=fargate_profile_name, # Opting to use the same ARN for the cluster and the pod here, # but a different ARN could be configured and passed if desired. fargate_pod_execution_role_arn=fargate_pod_role_arn, ) # [END howto_operator_eks_create_cluster_with_fargate_profile] await_create_fargate_profile = EksFargateProfileStateSensor( task_id="await_create_fargate_profile", cluster_name=cluster_name, fargate_profile_name=fargate_profile_name, target_state=FargateProfileStates.ACTIVE, ) start_pod = EksPodOperator( task_id="run_pod", pod_name="run_pod", cluster_name=cluster_name, image="amazon/aws-cli:latest", cmds=["sh", "-c", "echo Test Airflow; date"], labels={"demo": "hello_world"}, get_logs=True, startup_timeout_seconds=600, # Keep the pod alive, so we can describe it in case of trouble. It's deleted with the cluster anyway. on_finish_action="keep_pod", ) describe_pod = get_describe_pod_operator( cluster_name, pod_name="{{ ti.xcom_pull(key='pod_name', task_ids='run_pod') }}" ) # only describe the pod if the task above failed, to help diagnose describe_pod.trigger_rule = TriggerRule.ONE_FAILED # An Amazon EKS cluster can not be deleted with attached resources such as nodegroups or Fargate profiles. # Setting the `force` to `True` will delete any attached resources before deleting the cluster. delete_cluster_and_fargate_profile = EksDeleteClusterOperator( task_id="delete_fargate_profile_and_cluster", trigger_rule=TriggerRule.ALL_DONE, cluster_name=cluster_name, force_delete_compute=True, ) await_delete_cluster = EksClusterStateSensor( task_id="await_delete_cluster", trigger_rule=TriggerRule.ALL_DONE, cluster_name=cluster_name, target_state=ClusterStates.NONEXISTENT, poke_interval=10, ) chain( # TEST SETUP test_context, # TEST BODY create_cluster_and_fargate_profile, await_create_fargate_profile, start_pod, # TEST TEARDOWN describe_pod, delete_cluster_and_fargate_profile, await_delete_cluster, ) from tests.system.utils.watcher import watcher # This test needs watcher in order to properly mark success/failure # when "tearDown" task with trigger rule is part of the DAG list(dag.tasks) >> watcher() from tests.system.utils import get_test_run # noqa: E402 # Needed to run the example DAG with pytest (see: tests/system/
[docs]test_run = get_test_run(dag)

Was this entry helpful?