| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2022 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: eks_fargate_profile |
| 10 |
version_added: 4.0.0 |
| 11 |
short_description: Manage EKS Fargate Profile |
| 12 |
description: |
| 13 |
- Manage EKS Fargate Profile. |
| 14 |
author: |
| 15 |
- Tiago Jarra (@tjarra) |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: Name of EKS Fargate Profile. |
| 19 |
required: True |
| 20 |
type: str |
| 21 |
cluster_name: |
| 22 |
description: Name of EKS Cluster. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
role_arn: |
| 26 |
description: |
| 27 |
- ARN of IAM role used by the EKS cluster. |
| 28 |
- Required when I(state=present). |
| 29 |
type: str |
| 30 |
subnets: |
| 31 |
description: |
| 32 |
- list of subnet IDs for the Kubernetes cluster. |
| 33 |
- Required when I(state=present). |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
selectors: |
| 37 |
description: |
| 38 |
- A list of selectors to use in fargate profile. |
| 39 |
- Required when I(state=present). |
| 40 |
type: list |
| 41 |
elements: dict |
| 42 |
suboptions: |
| 43 |
namespace: |
| 44 |
description: A namespace used in fargate profile. |
| 45 |
type: str |
| 46 |
labels: |
| 47 |
description: A dictionary of labels used in fargate profile. |
| 48 |
type: dict |
| 49 |
default: {} |
| 50 |
state: |
| 51 |
description: Create or delete the Fargate Profile. |
| 52 |
choices: |
| 53 |
- absent |
| 54 |
- present |
| 55 |
default: present |
| 56 |
type: str |
| 57 |
wait: |
| 58 |
description: >- |
| 59 |
Specifies whether the module waits until the profile is created or deleted before moving on. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
wait_timeout: |
| 63 |
description: >- |
| 64 |
The duration in seconds to wait for the cluster to become active. Defaults |
| 65 |
to 1200 seconds (20 minutes). |
| 66 |
default: 1200 |
| 67 |
type: int |
| 68 |
extends_documentation_fragment: |
| 69 |
- amazon.aws.common.modules |
| 70 |
- amazon.aws.region.modules |
| 71 |
- amazon.aws.tags |
| 72 |
- amazon.aws.boto3 |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 77 |
|
| 78 |
- name: Create an EKS Fargate Profile |
| 79 |
community.aws.eks_fargate_profile: |
| 80 |
name: test_fargate |
| 81 |
cluster_name: test_cluster |
| 82 |
role_arn: my_eks_role |
| 83 |
subnets: |
| 84 |
- subnet-aaaa1111 |
| 85 |
selectors: |
| 86 |
- namespace: nm-test |
| 87 |
labels: |
| 88 |
- label1: test |
| 89 |
state: present |
| 90 |
wait: true |
| 91 |
|
| 92 |
- name: Remove an EKS Fargate Profile |
| 93 |
community.aws.eks_fargate_profile: |
| 94 |
name: test_fargate |
| 95 |
cluster_name: test_cluster |
| 96 |
wait: true |
| 97 |
state: absent |
| 98 |
""" |
| 99 |
|
| 100 |
RETURN = r""" |
| 101 |
fargate_profile_name: |
| 102 |
description: Name of Fargate Profile. |
| 103 |
returned: when state is present |
| 104 |
type: str |
| 105 |
sample: test_profile |
| 106 |
fargate_profile_arn: |
| 107 |
description: ARN of the Fargate Profile. |
| 108 |
returned: when state is present |
| 109 |
type: str |
| 110 |
sample: arn:aws:eks:us-east-1:1231231123:safd |
| 111 |
cluster_name: |
| 112 |
description: Name of EKS Cluster. |
| 113 |
returned: when state is present |
| 114 |
type: str |
| 115 |
sample: test-cluster |
| 116 |
created_at: |
| 117 |
description: Fargate Profile creation date and time. |
| 118 |
returned: when state is present |
| 119 |
type: str |
| 120 |
sample: '2022-01-18T20:00:00.111000+00:00' |
| 121 |
pod_execution_role_arn: |
| 122 |
description: ARN of the IAM Role used by Fargate Profile. |
| 123 |
returned: when state is present |
| 124 |
type: str |
| 125 |
sample: arn:aws:eks:us-east-1:1231231123:role/asdf |
| 126 |
subnets: |
| 127 |
description: List of subnets used in Fargate Profile. |
| 128 |
returned: when state is present |
| 129 |
type: list |
| 130 |
sample: |
| 131 |
- subnet-qwerty123 |
| 132 |
- subnet-asdfg456 |
| 133 |
selectors: |
| 134 |
description: Selector configuration. |
| 135 |
returned: when state is present |
| 136 |
type: complex |
| 137 |
contains: |
| 138 |
namespace: |
| 139 |
description: Name of the kubernetes namespace used in profile. |
| 140 |
returned: when state is present |
| 141 |
type: str |
| 142 |
sample: nm-test |
| 143 |
labels: |
| 144 |
description: List of kubernetes labels used in profile. |
| 145 |
returned: when state is present |
| 146 |
type: list |
| 147 |
sample: |
| 148 |
- label1: test1 |
| 149 |
- label2: test2 |
| 150 |
tags: |
| 151 |
description: A dictionary of resource tags. |
| 152 |
returned: when state is present |
| 153 |
type: dict |
| 154 |
sample: |
| 155 |
foo: bar |
| 156 |
env: test |
| 157 |
status: |
| 158 |
description: status of the EKS Fargate Profile. |
| 159 |
returned: when state is present |
| 160 |
type: str |
| 161 |
sample: |
| 162 |
- CREATING |
| 163 |
- ACTIVE |
| 164 |
""" |
| 165 |
|
| 166 |
try: |
| 167 |
import botocore |
| 168 |
except ImportError: |
| 169 |
pass |
| 170 |
|
| 171 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 172 |
|
| 173 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 174 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 175 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter |
| 176 |
|
| 177 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 178 |
|
| 179 |
|
| 180 |
def validate_tags(client, module, fargate_profile): |
| 181 |
changed = False |
| 182 |
|
| 183 |
desired_tags = module.params.get("tags") |
| 184 |
if desired_tags is None: |
| 185 |
return False |
| 186 |
|
| 187 |
try: |
| 188 |
existing_tags = client.list_tags_for_resource(resourceArn=fargate_profile["fargateProfileArn"])["tags"] |
| 189 |
tags_to_add, tags_to_remove = compare_aws_tags(existing_tags, desired_tags, module.params.get("purge_tags")) |
| 190 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 191 |
module.fail_json_aws(e, msg=f"Unable to list or compare tags for Fargate Profile {module.params.get('name')}") |
| 192 |
|
| 193 |
if tags_to_remove: |
| 194 |
changed = True |
| 195 |
if not module.check_mode: |
| 196 |
try: |
| 197 |
client.untag_resource(resourceArn=fargate_profile["fargateProfileArn"], tagKeys=tags_to_remove) |
| 198 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 199 |
module.fail_json_aws(e, msg=f"Unable to set tags for Fargate Profile {module.params.get('name')}") |
| 200 |
|
| 201 |
if tags_to_add: |
| 202 |
changed = True |
| 203 |
if not module.check_mode: |
| 204 |
try: |
| 205 |
client.tag_resource(resourceArn=fargate_profile["fargateProfileArn"], tags=tags_to_add) |
| 206 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 207 |
module.fail_json_aws(e, msg=f"Unable to set tags for Fargate Profile {module.params.get('name')}") |
| 208 |
|
| 209 |
return changed |
| 210 |
|
| 211 |
|
| 212 |
def create_or_update_fargate_profile(client, module): |
| 213 |
name = module.params.get("name") |
| 214 |
subnets = module.params["subnets"] |
| 215 |
role_arn = module.params["role_arn"] |
| 216 |
cluster_name = module.params["cluster_name"] |
| 217 |
selectors = module.params["selectors"] |
| 218 |
tags = module.params["tags"] or {} |
| 219 |
wait = module.params.get("wait") |
| 220 |
fargate_profile = get_fargate_profile(client, module, name, cluster_name) |
| 221 |
|
| 222 |
if fargate_profile: |
| 223 |
changed = False |
| 224 |
if set(fargate_profile["podExecutionRoleArn"]) != set(role_arn): |
| 225 |
module.fail_json(msg="Cannot modify Execution Role") |
| 226 |
if set(fargate_profile["subnets"]) != set(subnets): |
| 227 |
module.fail_json(msg="Cannot modify Subnets") |
| 228 |
if fargate_profile["selectors"] != selectors: |
| 229 |
module.fail_json(msg="Cannot modify Selectors") |
| 230 |
|
| 231 |
changed = validate_tags(client, module, fargate_profile) |
| 232 |
|
| 233 |
if wait: |
| 234 |
wait_until(client, module, "fargate_profile_active", name, cluster_name) |
| 235 |
fargate_profile = get_fargate_profile(client, module, name, cluster_name) |
| 236 |
|
| 237 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(fargate_profile)) |
| 238 |
|
| 239 |
if module.check_mode: |
| 240 |
module.exit_json(changed=True) |
| 241 |
|
| 242 |
check_profiles_status(client, module, cluster_name) |
| 243 |
|
| 244 |
try: |
| 245 |
params = dict( |
| 246 |
fargateProfileName=name, |
| 247 |
podExecutionRoleArn=role_arn, |
| 248 |
subnets=subnets, |
| 249 |
clusterName=cluster_name, |
| 250 |
selectors=selectors, |
| 251 |
tags=tags, |
| 252 |
) |
| 253 |
fargate_profile = client.create_fargate_profile(**params) |
| 254 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 255 |
module.fail_json_aws(e, msg=f"Couldn't create fargate profile {name}") |
| 256 |
|
| 257 |
if wait: |
| 258 |
wait_until(client, module, "fargate_profile_active", name, cluster_name) |
| 259 |
fargate_profile = get_fargate_profile(client, module, name, cluster_name) |
| 260 |
|
| 261 |
module.exit_json(changed=True, **camel_dict_to_snake_dict(fargate_profile)) |
| 262 |
|
| 263 |
|
| 264 |
def delete_fargate_profile(client, module): |
| 265 |
name = module.params.get("name") |
| 266 |
cluster_name = module.params["cluster_name"] |
| 267 |
existing = get_fargate_profile(client, module, name, cluster_name) |
| 268 |
wait = module.params.get("wait") |
| 269 |
if not existing or existing["status"] == "DELETING": |
| 270 |
module.exit_json(changed=False) |
| 271 |
|
| 272 |
if not module.check_mode: |
| 273 |
check_profiles_status(client, module, cluster_name) |
| 274 |
try: |
| 275 |
client.delete_fargate_profile(clusterName=cluster_name, fargateProfileName=name) |
| 276 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 277 |
module.fail_json_aws(e, msg=f"Couldn't delete fargate profile {name}") |
| 278 |
|
| 279 |
if wait: |
| 280 |
wait_until(client, module, "fargate_profile_deleted", name, cluster_name) |
| 281 |
|
| 282 |
module.exit_json(changed=True) |
| 283 |
|
| 284 |
|
| 285 |
def get_fargate_profile(client, module, name, cluster_name): |
| 286 |
try: |
| 287 |
return client.describe_fargate_profile(clusterName=cluster_name, fargateProfileName=name)["fargateProfile"] |
| 288 |
except is_boto3_error_code("ResourceNotFoundException"): |
| 289 |
return None |
| 290 |
except ( |
| 291 |
botocore.exceptions.BotoCoreError, |
| 292 |
botocore.exceptions.ClientError, |
| 293 |
) as e: # pylint: disable=duplicate-except |
| 294 |
module.fail_json_aws(e, msg="Couldn't get fargate profile") |
| 295 |
|
| 296 |
|
| 297 |
# Check if any fargate profiles is in changing states, if so, wait for the end |
| 298 |
def check_profiles_status(client, module, cluster_name): |
| 299 |
try: |
| 300 |
list_profiles = client.list_fargate_profiles(clusterName=cluster_name) |
| 301 |
|
| 302 |
for name in list_profiles["fargateProfileNames"]: |
| 303 |
fargate_profile = get_fargate_profile(client, module, name, cluster_name) |
| 304 |
if fargate_profile["status"] == "CREATING": |
| 305 |
wait_until( |
| 306 |
client, module, "fargate_profile_active", fargate_profile["fargateProfileName"], cluster_name |
| 307 |
) |
| 308 |
elif fargate_profile["status"] == "DELETING": |
| 309 |
wait_until( |
| 310 |
client, module, "fargate_profile_deleted", fargate_profile["fargateProfileName"], cluster_name |
| 311 |
) |
| 312 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 313 |
module.fail_json_aws(e, msg="Couldn't not find EKS cluster") |
| 314 |
|
| 315 |
|
| 316 |
def wait_until(client, module, waiter_name, name, cluster_name): |
| 317 |
wait_timeout = module.params.get("wait_timeout") |
| 318 |
waiter = get_waiter(client, waiter_name) |
| 319 |
attempts = 1 + int(wait_timeout / waiter.config.delay) |
| 320 |
try: |
| 321 |
waiter.wait(clusterName=cluster_name, fargateProfileName=name, WaiterConfig={"MaxAttempts": attempts}) |
| 322 |
except botocore.exceptions.WaiterError as e: |
| 323 |
module.fail_json_aws(e, msg="An error occurred waiting") |
| 324 |
|
| 325 |
|
| 326 |
def main(): |
| 327 |
argument_spec = dict( |
| 328 |
name=dict(required=True), |
| 329 |
cluster_name=dict(required=True), |
| 330 |
role_arn=dict(), |
| 331 |
subnets=dict(type="list", elements="str"), |
| 332 |
selectors=dict( |
| 333 |
type="list", |
| 334 |
elements="dict", |
| 335 |
options=dict( |
| 336 |
namespace=dict(type="str"), |
| 337 |
labels=dict(type="dict", default={}), |
| 338 |
), |
| 339 |
), |
| 340 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 341 |
purge_tags=dict(type="bool", default=True), |
| 342 |
state=dict(choices=["absent", "present"], default="present"), |
| 343 |
wait=dict(default=False, type="bool"), |
| 344 |
wait_timeout=dict(default=1200, type="int"), |
| 345 |
) |
| 346 |
|
| 347 |
module = AnsibleAWSModule( |
| 348 |
argument_spec=argument_spec, |
| 349 |
required_if=[["state", "present", ["role_arn", "subnets", "selectors"]]], |
| 350 |
supports_check_mode=True, |
| 351 |
) |
| 352 |
|
| 353 |
try: |
| 354 |
client = module.client("eks") |
| 355 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 356 |
module.fail_json_aws(e, msg="Couldn't connect to AWS") |
| 357 |
|
| 358 |
if module.params.get("state") == "present": |
| 359 |
create_or_update_fargate_profile(client, module) |
| 360 |
else: |
| 361 |
delete_fargate_profile(client, module) |
| 362 |
|
| 363 |
|
| 364 |
if __name__ == "__main__": |
| 365 |
main() |