-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Deferrable mode for EksCreateFargateProfileOperator and EksDeleteFargateProfileOperator #31657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
o-nikolas
merged 5 commits into
apache:main
from
aws-mwaa:syedahsn/deferrable-eks-fargate-profile
Jun 14, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5208255
Deferrable mode for EksCreateFargateProfileOperator and EksDeleteFarg…
syedahsn 93dce0d
Remove hook caching in Trigger
syedahsn 5fc68f0
Add EKS trigger to provider.yaml
syedahsn a0d0713
Add comment on operator timeout
syedahsn fa86447
Fix integration name for EKS for triggers in provider.yaml
syedahsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| # 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 | ||
| # | ||
| # 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. | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from typing import Any | ||
|
|
||
| from botocore.exceptions import WaiterError | ||
|
|
||
| from airflow.exceptions import AirflowException | ||
| from airflow.providers.amazon.aws.hooks.eks import EksHook | ||
| from airflow.triggers.base import BaseTrigger, TriggerEvent | ||
|
|
||
|
|
||
| class EksCreateFargateProfileTrigger(BaseTrigger): | ||
| """ | ||
| Trigger for EksCreateFargateProfileOperator. | ||
| The trigger will asynchronously wait for the fargate profile to be created. | ||
|
|
||
| :param cluster_name: The name of the EKS cluster | ||
| :param fargate_profile_name: The name of the fargate profile | ||
| :param poll_interval: The amount of time in seconds to wait between attempts. | ||
| :param max_attempts: The maximum number of attempts to be made. | ||
| :param aws_conn_id: The Airflow connection used for AWS credentials. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| cluster_name: str, | ||
| fargate_profile_name: str, | ||
| poll_interval: int, | ||
| max_attempts: int, | ||
| aws_conn_id: str, | ||
| ): | ||
| self.cluster_name = cluster_name | ||
| self.fargate_profile_name = fargate_profile_name | ||
| self.poll_interval = poll_interval | ||
| self.max_attempts = max_attempts | ||
| self.aws_conn_id = aws_conn_id | ||
|
|
||
| def serialize(self) -> tuple[str, dict[str, Any]]: | ||
| return ( | ||
| self.__class__.__module__ + "." + self.__class__.__qualname__, | ||
syedahsn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| "cluster_name": self.cluster_name, | ||
| "fargate_profile_name": self.fargate_profile_name, | ||
| "poll_interval": str(self.poll_interval), | ||
| "max_attempts": str(self.max_attempts), | ||
| "aws_conn_id": self.aws_conn_id, | ||
| }, | ||
| ) | ||
|
|
||
| async def run(self): | ||
| self.hook = EksHook(aws_conn_id=self.aws_conn_id) | ||
| async with self.hook.async_conn as client: | ||
| attempt = 0 | ||
| waiter = client.get_waiter("fargate_profile_active") | ||
| while attempt < int(self.max_attempts): | ||
| attempt += 1 | ||
| try: | ||
| await waiter.wait( | ||
| clusterName=self.cluster_name, | ||
| fargateProfileName=self.fargate_profile_name, | ||
| WaiterConfig={"Delay": int(self.poll_interval), "MaxAttempts": 1}, | ||
| ) | ||
| break | ||
| except WaiterError as error: | ||
| if "terminal failure" in str(error): | ||
| raise AirflowException(f"Create Fargate Profile failed: {error}") | ||
| self.log.info( | ||
| "Status of fargate profile is %s", error.last_response["fargateProfile"]["status"] | ||
| ) | ||
| await asyncio.sleep(int(self.poll_interval)) | ||
| if attempt >= int(self.max_attempts): | ||
| raise AirflowException( | ||
| f"Create Fargate Profile failed - max attempts reached: {self.max_attempts}" | ||
| ) | ||
| else: | ||
| yield TriggerEvent({"status": "success", "message": "Fargate Profile Created"}) | ||
|
|
||
|
|
||
| class EksDeleteFargateProfileTrigger(BaseTrigger): | ||
syedahsn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Trigger for EksDeleteFargateProfileOperator. | ||
| The trigger will asynchronously wait for the fargate profile to be deleted. | ||
|
|
||
| :param cluster_name: The name of the EKS cluster | ||
| :param fargate_profile_name: The name of the fargate profile | ||
| :param poll_interval: The amount of time in seconds to wait between attempts. | ||
| :param max_attempts: The maximum number of attempts to be made. | ||
| :param aws_conn_id: The Airflow connection used for AWS credentials. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| cluster_name: str, | ||
| fargate_profile_name: str, | ||
| poll_interval: int, | ||
| max_attempts: int, | ||
| aws_conn_id: str, | ||
| ): | ||
| self.cluster_name = cluster_name | ||
| self.fargate_profile_name = fargate_profile_name | ||
| self.poll_interval = poll_interval | ||
| self.max_attempts = max_attempts | ||
| self.aws_conn_id = aws_conn_id | ||
|
|
||
| def serialize(self) -> tuple[str, dict[str, Any]]: | ||
| return ( | ||
| self.__class__.__module__ + "." + self.__class__.__qualname__, | ||
| { | ||
| "cluster_name": self.cluster_name, | ||
| "fargate_profile_name": self.fargate_profile_name, | ||
| "poll_interval": str(self.poll_interval), | ||
| "max_attempts": str(self.max_attempts), | ||
| "aws_conn_id": self.aws_conn_id, | ||
| }, | ||
| ) | ||
|
|
||
| async def run(self): | ||
| self.hook = EksHook(aws_conn_id=self.aws_conn_id) | ||
| async with self.hook.async_conn as client: | ||
| attempt = 0 | ||
| waiter = client.get_waiter("fargate_profile_deleted") | ||
| while attempt < int(self.max_attempts): | ||
| attempt += 1 | ||
| try: | ||
| await waiter.wait( | ||
| clusterName=self.cluster_name, | ||
| fargateProfileName=self.fargate_profile_name, | ||
| WaiterConfig={"Delay": int(self.poll_interval), "MaxAttempts": 1}, | ||
| ) | ||
| break | ||
| except WaiterError as error: | ||
| if "terminal failure" in str(error): | ||
| raise AirflowException(f"Delete Fargate Profile failed: {error}") | ||
| self.log.info( | ||
| "Status of fargate profile is %s", error.last_response["fargateProfile"]["status"] | ||
| ) | ||
| await asyncio.sleep(int(self.poll_interval)) | ||
| if attempt >= int(self.max_attempts): | ||
| raise AirflowException( | ||
| f"Delete Fargate Profile failed - max attempts reached: {self.max_attempts}" | ||
| ) | ||
| else: | ||
| yield TriggerEvent({"status": "success", "message": "Fargate Profile Deleted"}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.