| 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_nodegroup |
| 10 |
version_added: 5.3.0 |
| 11 |
short_description: Manage EKS Nodegroup module |
| 12 |
description: |
| 13 |
- Manage EKS Nodegroup. |
| 14 |
author: |
| 15 |
- Tiago Jarra (@tjarra) |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: Name of EKS Nodegroup. |
| 19 |
required: True |
| 20 |
type: str |
| 21 |
cluster_name: |
| 22 |
description: Name of EKS Cluster. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
node_role: |
| 26 |
description: ARN of IAM role used by the EKS cluster Nodegroup. |
| 27 |
type: str |
| 28 |
subnets: |
| 29 |
description: list of subnet IDs for the Kubernetes cluster. |
| 30 |
type: list |
| 31 |
elements: str |
| 32 |
scaling_config: |
| 33 |
description: The scaling configuration details for the Auto Scaling group that is created for your node group. |
| 34 |
type: dict |
| 35 |
default: |
| 36 |
min_size: 1 |
| 37 |
max_size: 2 |
| 38 |
desired_size: 1 |
| 39 |
suboptions: |
| 40 |
min_size: |
| 41 |
description: The minimum number of nodes that the managed node group can scale in to. |
| 42 |
type: int |
| 43 |
max_size: |
| 44 |
description: The maximum number of nodes that the managed node group can scale out to. |
| 45 |
type: int |
| 46 |
desired_size: |
| 47 |
description: The current number of nodes that the managed node group should maintain. |
| 48 |
type: int |
| 49 |
disk_size: |
| 50 |
description: |
| 51 |
- Size of disk in nodegroup nodes. |
| 52 |
If you specify I(launch_template), then don't specify I(disk_size), or the node group deployment will fail. |
| 53 |
type: int |
| 54 |
instance_types: |
| 55 |
description: |
| 56 |
- Specify the instance types for a node group. |
| 57 |
If you specify I(launch_template), then don't specify I(instance_types), or the node group deployment will fail. |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
ami_type: |
| 61 |
description: The AMI type for your node group. |
| 62 |
type: str |
| 63 |
choices: |
| 64 |
- AL2_x86_64 |
| 65 |
- AL2_x86_64_GPU |
| 66 |
- AL2_ARM_64 |
| 67 |
- CUSTOM |
| 68 |
- BOTTLEROCKET_ARM_64 |
| 69 |
- BOTTLEROCKET_x86_64 |
| 70 |
remote_access: |
| 71 |
description: |
| 72 |
- The remote access (SSH) configuration to use with your node group. |
| 73 |
If you specify I(launch_template), then don't specify I(remote_access), or the node group deployment will fail. |
| 74 |
type: dict |
| 75 |
suboptions: |
| 76 |
ec2_ssh_key: |
| 77 |
description: The Amazon EC2 SSH key that provides access for SSH communication with the nodes in the managed node group. |
| 78 |
type: str |
| 79 |
source_sg: |
| 80 |
description: The security groups that are allowed SSH access (port 22) to the nodes. |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
update_config: |
| 84 |
description: The node group update configuration. |
| 85 |
type: dict |
| 86 |
default: |
| 87 |
max_unavailable: 1 |
| 88 |
suboptions: |
| 89 |
max_unavailable: |
| 90 |
description: The maximum number of nodes unavailable at once during a version update. |
| 91 |
type: int |
| 92 |
max_unavailable_percentage: |
| 93 |
description: The maximum percentage of nodes unavailable during a version update. |
| 94 |
type: int |
| 95 |
labels: |
| 96 |
description: The Kubernetes labels to be applied to the nodes in the node group when they are created. |
| 97 |
type: dict |
| 98 |
default: {} |
| 99 |
taints: |
| 100 |
description: The Kubernetes taints to be applied to the nodes in the node group. |
| 101 |
type: list |
| 102 |
elements: dict |
| 103 |
default: [] |
| 104 |
suboptions: |
| 105 |
key: |
| 106 |
description: The key of the taint. |
| 107 |
type: str |
| 108 |
value: |
| 109 |
description: The value of the taint. |
| 110 |
type: str |
| 111 |
effect: |
| 112 |
description: The effect of the taint. |
| 113 |
type: str |
| 114 |
choices: |
| 115 |
- NO_SCHEDULE |
| 116 |
- NO_EXECUTE |
| 117 |
- PREFER_NO_SCHEDULE |
| 118 |
launch_template: |
| 119 |
description: |
| 120 |
- An object representing a node group's launch template specification. |
| 121 |
- If specified, then do not specify I(instanceTypes), I(diskSize), or I(remoteAccess). |
| 122 |
type: dict |
| 123 |
suboptions: |
| 124 |
name: |
| 125 |
description: The name of the launch template. |
| 126 |
type: str |
| 127 |
version: |
| 128 |
description: |
| 129 |
- The version of the launch template to use. |
| 130 |
- If no version is specified, then the template's default version is used. |
| 131 |
type: str |
| 132 |
id: |
| 133 |
description: The ID of the launch template. |
| 134 |
type: str |
| 135 |
capacity_type: |
| 136 |
description: The capacity type for your node group. |
| 137 |
default: ON_DEMAND |
| 138 |
type: str |
| 139 |
choices: |
| 140 |
- ON_DEMAND |
| 141 |
- SPOT |
| 142 |
release_version: |
| 143 |
description: The AMI version of the Amazon EKS optimized AMI to use with your node group. |
| 144 |
type: str |
| 145 |
state: |
| 146 |
description: Create or delete the Nodegroup. |
| 147 |
choices: |
| 148 |
- absent |
| 149 |
- present |
| 150 |
default: present |
| 151 |
type: str |
| 152 |
tags: |
| 153 |
description: A dictionary of resource tags. |
| 154 |
type: dict |
| 155 |
aliases: ['resource_tags'] |
| 156 |
purge_tags: |
| 157 |
description: |
| 158 |
- Purge existing tags that are not found in the nodegroup. |
| 159 |
type: bool |
| 160 |
default: true |
| 161 |
wait: |
| 162 |
description: Specifies whether the module waits until the profile is created or deleted before moving on. |
| 163 |
type: bool |
| 164 |
default: false |
| 165 |
wait_timeout: |
| 166 |
description: The duration in seconds to wait for the nodegroup to become active. Defaults to C(1200) seconds. |
| 167 |
default: 1200 |
| 168 |
type: int |
| 169 |
extends_documentation_fragment: |
| 170 |
- amazon.aws.common.modules |
| 171 |
- amazon.aws.region.modules |
| 172 |
""" |
| 173 |
|
| 174 |
EXAMPLES = r""" |
| 175 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 176 |
|
| 177 |
- name: create nodegroup |
| 178 |
community.aws.eks_nodegroup: |
| 179 |
name: test_nodegroup |
| 180 |
state: present |
| 181 |
cluster_name: test_cluster |
| 182 |
node_role: arn:aws:eks:us-east-1:1231231123:role/asdf |
| 183 |
subnets: |
| 184 |
- subnet-qwerty123 |
| 185 |
- subnet-asdfg456 |
| 186 |
scaling_config: |
| 187 |
min_size: 1 |
| 188 |
max_size: 2 |
| 189 |
desired_size: 1 |
| 190 |
disk_size: 20 |
| 191 |
instance_types: 't3.micro' |
| 192 |
ami_type: 'AL2_x86_64' |
| 193 |
labels: |
| 194 |
'teste': 'test' |
| 195 |
taints: |
| 196 |
- key: 'test' |
| 197 |
value: 'test' |
| 198 |
effect: 'NO_SCHEDULE' |
| 199 |
capacity_type: 'ON_DEMAND' |
| 200 |
|
| 201 |
- name: Remove an EKS Nodegrop |
| 202 |
community.aws.eks_nodegroup: |
| 203 |
name: test_nodegroup |
| 204 |
cluster_name: test_cluster |
| 205 |
wait: true |
| 206 |
state: absent |
| 207 |
""" |
| 208 |
|
| 209 |
RETURN = r""" |
| 210 |
nodegroup_name: |
| 211 |
description: The name associated with an Amazon EKS managed node group. |
| 212 |
returned: when state is present |
| 213 |
type: str |
| 214 |
sample: test_cluster |
| 215 |
nodegroup_arn: |
| 216 |
description: The Amazon Resource Name (ARN) associated with the managed node group. |
| 217 |
returned: when state is present |
| 218 |
type: str |
| 219 |
sample: arn:aws:eks:us-east-1:1231231123:safd |
| 220 |
cluster_name: |
| 221 |
description: Name of EKS Cluster |
| 222 |
returned: when state is present |
| 223 |
type: str |
| 224 |
sample: test_cluster |
| 225 |
version: |
| 226 |
description: The Kubernetes version of the managed node group. |
| 227 |
returned: when state is present |
| 228 |
type: str |
| 229 |
sample: need_validate |
| 230 |
release_version: |
| 231 |
description: This is the version of the Amazon EKS optimized AMI that the node group was deployed with. |
| 232 |
returned: when state is present |
| 233 |
type: str |
| 234 |
sample: need_validate |
| 235 |
created_at: |
| 236 |
description: Nodegroup creation date and time. |
| 237 |
returned: when state is present |
| 238 |
type: str |
| 239 |
sample: '2022-01-18T20:00:00.111000+00:00' |
| 240 |
modified_at: |
| 241 |
description: Nodegroup modified date and time. |
| 242 |
returned: when state is present |
| 243 |
type: str |
| 244 |
sample: '2022-01-18T20:00:00.111000+00:00' |
| 245 |
status: |
| 246 |
description: status of the EKS Nodegroup. |
| 247 |
returned: when state is present |
| 248 |
type: str |
| 249 |
sample: |
| 250 |
- CREATING |
| 251 |
- ACTIVE |
| 252 |
capacity_type: |
| 253 |
description: The capacity type of your managed node group. |
| 254 |
returned: when state is present |
| 255 |
type: str |
| 256 |
sample: need_validate |
| 257 |
scaling_config: |
| 258 |
description: The scaling configuration details for the Auto Scaling group that is associated with your node group. |
| 259 |
returned: when state is present |
| 260 |
type: dict |
| 261 |
sample: need_validate |
| 262 |
instance_types: |
| 263 |
description: This is the instance type that is associated with the node group. |
| 264 |
returned: when state is present |
| 265 |
type: list |
| 266 |
sample: need_validate |
| 267 |
subnets: |
| 268 |
description: List of subnets used in Fargate Profile. |
| 269 |
returned: when state is present |
| 270 |
type: list |
| 271 |
sample: |
| 272 |
- subnet-qwerty123 |
| 273 |
- subnet-asdfg456 |
| 274 |
remote_access: |
| 275 |
description: This is the remote access configuration that is associated with the node group. |
| 276 |
returned: when state is present |
| 277 |
type: dict |
| 278 |
sample: need_validate |
| 279 |
ami_type: |
| 280 |
description: This is the AMI type that was specified in the node group configuration. |
| 281 |
returned: when state is present |
| 282 |
type: str |
| 283 |
sample: need_validate |
| 284 |
node_role: |
| 285 |
description: ARN of the IAM Role used by Nodegroup. |
| 286 |
returned: when state is present |
| 287 |
type: str |
| 288 |
sample: arn:aws:eks:us-east-1:1231231123:role/asdf |
| 289 |
labels: |
| 290 |
description: The Kubernetes labels applied to the nodes in the node group. |
| 291 |
returned: when state is present |
| 292 |
type: dict |
| 293 |
sample: need_validate |
| 294 |
taints: |
| 295 |
description: The Kubernetes taints to be applied to the nodes in the node group when they are created. |
| 296 |
returned: when state is present |
| 297 |
type: list |
| 298 |
sample: need_validate |
| 299 |
resources: |
| 300 |
description: The resources associated with the node group. |
| 301 |
returned: when state is present |
| 302 |
type: complex |
| 303 |
contains: |
| 304 |
autoScalingGroups: |
| 305 |
description: The Auto Scaling groups associated with the node group. |
| 306 |
returned: when state is present |
| 307 |
type: list |
| 308 |
elements: dict |
| 309 |
remoteAccessSecurityGroup: |
| 310 |
description: The remote access security group associated with the node group. |
| 311 |
returned: when state is present |
| 312 |
type: str |
| 313 |
diskSize: |
| 314 |
description: This is the disk size in the node group configuration. |
| 315 |
returned: when state is present |
| 316 |
type: int |
| 317 |
sample: 20 |
| 318 |
health: |
| 319 |
description: The health status of the node group. |
| 320 |
returned: when state is present |
| 321 |
type: dict |
| 322 |
sample: need_validate |
| 323 |
update_config: |
| 324 |
description: The node group update configuration. |
| 325 |
returned: when state is present |
| 326 |
type: dict |
| 327 |
contains: |
| 328 |
maxUnavailable: |
| 329 |
description: The maximum number of nodes unavailable at once during a version update. |
| 330 |
type: int |
| 331 |
maxUnavailablePercentage: |
| 332 |
description: The maximum percentage of nodes unavailable during a version update. |
| 333 |
type: int |
| 334 |
launch_template: |
| 335 |
description: If a launch template was used to create the node group, then this is the launch template that was used. |
| 336 |
returned: when state is present |
| 337 |
type: dict |
| 338 |
sample: need_validate |
| 339 |
tags: |
| 340 |
description: Nodegroup tags. |
| 341 |
returned: when state is present |
| 342 |
type: dict |
| 343 |
sample: |
| 344 |
foo: bar |
| 345 |
""" |
| 346 |
|
| 347 |
try: |
| 348 |
import botocore |
| 349 |
except ImportError: |
| 350 |
pass |
| 351 |
|
| 352 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 353 |
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict |
| 354 |
|
| 355 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 356 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags |
| 357 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter |
| 358 |
|
| 359 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 360 |
|
| 361 |
|
| 362 |
def validate_tags(client, module, nodegroup): |
| 363 |
changed = False |
| 364 |
|
| 365 |
desired_tags = module.params.get("tags") |
| 366 |
if desired_tags is None: |
| 367 |
return False |
| 368 |
|
| 369 |
try: |
| 370 |
existing_tags = client.list_tags_for_resource(resourceArn=nodegroup["nodegroupArn"])["tags"] |
| 371 |
tags_to_add, tags_to_remove = compare_aws_tags(existing_tags, desired_tags, module.params.get("purge_tags")) |
| 372 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 373 |
module.fail_json_aws(e, msg=f"Unable to list or compare tags for Nodegroup {module.params.get('name')}.") |
| 374 |
if tags_to_remove: |
| 375 |
if not module.check_mode: |
| 376 |
changed = True |
| 377 |
try: |
| 378 |
client.untag_resource(aws_retry=True, ResourceArn=nodegroup["nodegroupArn"], tagKeys=tags_to_remove) |
| 379 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 380 |
module.fail_json_aws(e, msg=f"Unable to set tags for Nodegroup {module.params.get('name')}.") |
| 381 |
if tags_to_add: |
| 382 |
if not module.check_mode: |
| 383 |
changed = True |
| 384 |
try: |
| 385 |
client.tag_resource(aws_retry=True, ResourceArn=nodegroup["nodegroupArn"], tags=tags_to_add) |
| 386 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 387 |
module.fail_json_aws(e, msg=f"Unable to set tags for Nodegroup {module.params.get('name')}.") |
| 388 |
|
| 389 |
return changed |
| 390 |
|
| 391 |
|
| 392 |
def compare_taints(nodegroup_taints, param_taints): |
| 393 |
taints_to_unset = [] |
| 394 |
taints_to_add_or_update = [] |
| 395 |
for taint in nodegroup_taints: |
| 396 |
if taint not in param_taints: |
| 397 |
taints_to_unset.append(taint) |
| 398 |
for taint in param_taints: |
| 399 |
if taint not in nodegroup_taints: |
| 400 |
taints_to_add_or_update.append(taint) |
| 401 |
|
| 402 |
return taints_to_add_or_update, taints_to_unset |
| 403 |
|
| 404 |
|
| 405 |
def validate_taints(client, module, nodegroup, param_taints): |
| 406 |
changed = False |
| 407 |
params = dict() |
| 408 |
params["clusterName"] = nodegroup["clusterName"] |
| 409 |
params["nodegroupName"] = nodegroup["nodegroupName"] |
| 410 |
params["taints"] = [] |
| 411 |
if "taints" not in nodegroup: |
| 412 |
nodegroup["taints"] = [] |
| 413 |
taints_to_add_or_update, taints_to_unset = compare_taints(nodegroup["taints"], param_taints) |
| 414 |
|
| 415 |
if taints_to_add_or_update: |
| 416 |
params["taints"]["addOrUpdateTaints"] = taints_to_add_or_update |
| 417 |
if taints_to_unset: |
| 418 |
params["taints"]["removeTaints"] = taints_to_unset |
| 419 |
if params["taints"]: |
| 420 |
if not module.check_mode: |
| 421 |
changed = True |
| 422 |
try: |
| 423 |
client.update_nodegroup_config(**params) |
| 424 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 425 |
module.fail_json_aws(e, msg=f"Unable to set taints for Nodegroup {params['nodegroupName']}.") |
| 426 |
|
| 427 |
return changed |
| 428 |
|
| 429 |
|
| 430 |
def compare_labels(nodegroup_labels, param_labels): |
| 431 |
labels_to_unset = [] |
| 432 |
labels_to_add_or_update = {} |
| 433 |
for label in nodegroup_labels.keys(): |
| 434 |
if label not in param_labels: |
| 435 |
labels_to_unset.append(label) |
| 436 |
for key, value in param_labels.items(): |
| 437 |
if key not in nodegroup_labels.keys(): |
| 438 |
labels_to_add_or_update[key] = value |
| 439 |
|
| 440 |
return labels_to_add_or_update, labels_to_unset |
| 441 |
|
| 442 |
|
| 443 |
def validate_labels(client, module, nodegroup, param_labels): |
| 444 |
changed = False |
| 445 |
params = dict() |
| 446 |
params["clusterName"] = nodegroup["clusterName"] |
| 447 |
params["nodegroupName"] = nodegroup["nodegroupName"] |
| 448 |
params["labels"] = {} |
| 449 |
labels_to_add_or_update, labels_to_unset = compare_labels(nodegroup["labels"], param_labels) |
| 450 |
|
| 451 |
if labels_to_add_or_update: |
| 452 |
params["labels"]["addOrUpdateLabels"] = labels_to_add_or_update |
| 453 |
if labels_to_unset: |
| 454 |
params["labels"]["removeLabels"] = labels_to_unset |
| 455 |
if params["labels"]: |
| 456 |
if not module.check_mode: |
| 457 |
changed = True |
| 458 |
try: |
| 459 |
client.update_nodegroup_config(**params) |
| 460 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 461 |
module.fail_json_aws(e, msg=f"Unable to set labels for Nodegroup {params['nodegroupName']}.") |
| 462 |
|
| 463 |
return changed |
| 464 |
|
| 465 |
|
| 466 |
def compare_params(module, params, nodegroup): |
| 467 |
for param in ["nodeRole", "subnets", "diskSize", "instanceTypes", "amiTypes", "remoteAccess", "capacityType"]: |
| 468 |
if (param in nodegroup) and (param in params): |
| 469 |
if nodegroup[param] != params[param]: |
| 470 |
module.fail_json(msg=f"Cannot modify parameter {param}.") |
| 471 |
if ("launchTemplate" not in nodegroup) and ("launchTemplate" in params): |
| 472 |
module.fail_json(msg="Cannot add Launch Template in this Nodegroup.") |
| 473 |
if nodegroup["updateConfig"] != params["updateConfig"]: |
| 474 |
return True |
| 475 |
if nodegroup["scalingConfig"] != params["scalingConfig"]: |
| 476 |
return True |
| 477 |
return False |
| 478 |
|
| 479 |
|
| 480 |
def compare_params_launch_template(module, params, nodegroup): |
| 481 |
if "launchTemplate" not in params: |
| 482 |
module.fail_json(msg="Cannot exclude Launch Template in this Nodegroup.") |
| 483 |
else: |
| 484 |
for key in ["name", "id"]: |
| 485 |
if (key in params["launchTemplate"]) and ( |
| 486 |
params["launchTemplate"][key] != nodegroup["launchTemplate"][key] |
| 487 |
): |
| 488 |
module.fail_json(msg=f"Cannot modify Launch Template {key}.") |
| 489 |
if ("version" in params["launchTemplate"]) and ( |
| 490 |
params["launchTemplate"]["version"] != nodegroup["launchTemplate"]["version"] |
| 491 |
): |
| 492 |
return True |
| 493 |
return False |
| 494 |
|
| 495 |
|
| 496 |
def create_or_update_nodegroups(client, module): |
| 497 |
changed = False |
| 498 |
params = dict() |
| 499 |
params["nodegroupName"] = module.params["name"] |
| 500 |
params["clusterName"] = module.params["cluster_name"] |
| 501 |
params["nodeRole"] = module.params["node_role"] |
| 502 |
params["subnets"] = module.params["subnets"] |
| 503 |
params["tags"] = module.params["tags"] or {} |
| 504 |
if module.params["ami_type"] is not None: |
| 505 |
params["amiType"] = module.params["ami_type"] |
| 506 |
if module.params["disk_size"] is not None: |
| 507 |
params["diskSize"] = module.params["disk_size"] |
| 508 |
if module.params["instance_types"] is not None: |
| 509 |
params["instanceTypes"] = module.params["instance_types"] |
| 510 |
if module.params["launch_template"] is not None: |
| 511 |
params["launchTemplate"] = dict() |
| 512 |
if module.params["launch_template"]["id"] is not None: |
| 513 |
params["launchTemplate"]["id"] = module.params["launch_template"]["id"] |
| 514 |
if module.params["launch_template"]["version"] is not None: |
| 515 |
params["launchTemplate"]["version"] = module.params["launch_template"]["version"] |
| 516 |
if module.params["launch_template"]["name"] is not None: |
| 517 |
params["launchTemplate"]["name"] = module.params["launch_template"]["name"] |
| 518 |
if module.params["release_version"] is not None: |
| 519 |
params["releaseVersion"] = module.params["release_version"] |
| 520 |
if module.params["remote_access"] is not None: |
| 521 |
params["remoteAccess"] = dict() |
| 522 |
if module.params["remote_access"]["ec2_ssh_key"] is not None: |
| 523 |
params["remoteAccess"]["ec2SshKey"] = module.params["remote_access"]["ec2_ssh_key"] |
| 524 |
if module.params["remote_access"]["source_sg"] is not None: |
| 525 |
params["remoteAccess"]["sourceSecurityGroups"] = module.params["remote_access"]["source_sg"] |
| 526 |
if module.params["capacity_type"] is not None: |
| 527 |
params["capacityType"] = module.params["capacity_type"].upper() |
| 528 |
if module.params["labels"] is not None: |
| 529 |
params["labels"] = module.params["labels"] |
| 530 |
if module.params["taints"] is not None: |
| 531 |
params["taints"] = module.params["taints"] |
| 532 |
if module.params["update_config"] is not None: |
| 533 |
params["updateConfig"] = dict() |
| 534 |
if module.params["update_config"]["max_unavailable"] is not None: |
| 535 |
params["updateConfig"]["maxUnavailable"] = module.params["update_config"]["max_unavailable"] |
| 536 |
if module.params["update_config"]["max_unavailable_percentage"] is not None: |
| 537 |
params["updateConfig"]["maxUnavailablePercentage"] = module.params["update_config"][ |
| 538 |
"max_unavailable_percentage" |
| 539 |
] |
| 540 |
if module.params["scaling_config"] is not None: |
| 541 |
params["scalingConfig"] = snake_dict_to_camel_dict(module.params["scaling_config"]) |
| 542 |
|
| 543 |
wait = module.params.get("wait") |
| 544 |
nodegroup = get_nodegroup(client, module, params["nodegroupName"], params["clusterName"]) |
| 545 |
|
| 546 |
if nodegroup: |
| 547 |
update_params = dict() |
| 548 |
update_params["clusterName"] = params["clusterName"] |
| 549 |
update_params["nodegroupName"] = params["nodegroupName"] |
| 550 |
|
| 551 |
if "launchTemplate" in nodegroup: |
| 552 |
if compare_params_launch_template(module, params, nodegroup): |
| 553 |
update_params["launchTemplate"] = params["launchTemplate"] |
| 554 |
if not module.check_mode: |
| 555 |
try: |
| 556 |
client.update_nodegroup_version(**update_params) |
| 557 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 558 |
module.fail_json_aws(e, msg="Couldn't update nodegroup.") |
| 559 |
changed |= True |
| 560 |
|
| 561 |
if compare_params(module, params, nodegroup): |
| 562 |
try: |
| 563 |
if "launchTemplate" in update_params: |
| 564 |
update_params.pop("launchTemplate") |
| 565 |
update_params["scalingConfig"] = params["scalingConfig"] |
| 566 |
update_params["updateConfig"] = params["updateConfig"] |
| 567 |
|
| 568 |
if not module.check_mode: |
| 569 |
client.update_nodegroup_config(**update_params) |
| 570 |
|
| 571 |
changed |= True |
| 572 |
|
| 573 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 574 |
module.fail_json_aws(e, msg="Couldn't update nodegroup.") |
| 575 |
|
| 576 |
changed |= validate_tags(client, module, nodegroup) |
| 577 |
|
| 578 |
changed |= validate_labels(client, module, nodegroup, params["labels"]) |
| 579 |
|
| 580 |
if "taints" in nodegroup: |
| 581 |
changed |= validate_taints(client, module, nodegroup, params["taints"]) |
| 582 |
|
| 583 |
if wait: |
| 584 |
wait_until(client, module, "nodegroup_active", params["nodegroupName"], params["clusterName"]) |
| 585 |
|
| 586 |
nodegroup = get_nodegroup(client, module, params["nodegroupName"], params["clusterName"]) |
| 587 |
|
| 588 |
module.exit_json(changed=changed, **camel_dict_to_snake_dict(nodegroup)) |
| 589 |
|
| 590 |
if module.check_mode: |
| 591 |
module.exit_json(changed=True) |
| 592 |
|
| 593 |
try: |
| 594 |
nodegroup = client.create_nodegroup(**params) |
| 595 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 596 |
module.fail_json_aws(e, msg=f"Couldn't create Nodegroup {params['nodegroupName']}.") |
| 597 |
|
| 598 |
if wait: |
| 599 |
wait_until(client, module, "nodegroup_active", params["nodegroupName"], params["clusterName"]) |
| 600 |
nodegroup = get_nodegroup(client, module, params["nodegroupName"], params["clusterName"]) |
| 601 |
|
| 602 |
module.exit_json(changed=True, **camel_dict_to_snake_dict(nodegroup)) |
| 603 |
|
| 604 |
|
| 605 |
def delete_nodegroups(client, module): |
| 606 |
name = module.params.get("name") |
| 607 |
clusterName = module.params["cluster_name"] |
| 608 |
existing = get_nodegroup(client, module, name, clusterName) |
| 609 |
wait = module.params.get("wait") |
| 610 |
|
| 611 |
if not existing: |
| 612 |
module.exit_json(changed=False, msg="Nodegroup '{name}' does not exist") |
| 613 |
|
| 614 |
if existing["status"] == "DELETING": |
| 615 |
if wait: |
| 616 |
wait_until(client, module, "nodegroup_deleted", name, clusterName) |
| 617 |
module.exit_json(changed=False, msg="Nodegroup '{name}' deletion complete") |
| 618 |
module.exit_json(changed=False, msg="Nodegroup '{name}' already in DELETING state") |
| 619 |
|
| 620 |
if module.check_mode: |
| 621 |
module.exit_json(changed=True, msg="Nodegroup '{name}' deletion would be started (check mode)") |
| 622 |
|
| 623 |
try: |
| 624 |
client.delete_nodegroup(clusterName=clusterName, nodegroupName=name) |
| 625 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 626 |
module.fail_json_aws(e, msg=f"Couldn't delete Nodegroup '{name}'.") |
| 627 |
|
| 628 |
if wait: |
| 629 |
wait_until(client, module, "nodegroup_deleted", name, clusterName) |
| 630 |
module.exit_json(changed=True, msg="Nodegroup '{name}' deletion complete") |
| 631 |
|
| 632 |
module.exit_json(changed=True, msg="Nodegroup '{name}' deletion started") |
| 633 |
|
| 634 |
|
| 635 |
def get_nodegroup(client, module, nodegroup_name, cluster_name): |
| 636 |
try: |
| 637 |
return client.describe_nodegroup(clusterName=cluster_name, nodegroupName=nodegroup_name)["nodegroup"] |
| 638 |
except is_boto3_error_code("ResourceNotFoundException"): |
| 639 |
return None |
| 640 |
except ( |
| 641 |
botocore.exceptions.BotoCoreError, |
| 642 |
botocore.exceptions.ClientError, |
| 643 |
) as e: # pylint: disable=duplicate-except |
| 644 |
module.fail_json_aws(e, msg=f"Couldn't get Nodegroup {nodegroup_name}.") |
| 645 |
|
| 646 |
|
| 647 |
def wait_until(client, module, waiter_name, nodegroup_name, cluster_name): |
| 648 |
wait_timeout = module.params.get("wait_timeout") |
| 649 |
waiter = get_waiter(client, waiter_name) |
| 650 |
attempts = 1 + int(wait_timeout / waiter.config.delay) |
| 651 |
try: |
| 652 |
waiter.wait(clusterName=cluster_name, nodegroupName=nodegroup_name, WaiterConfig={"MaxAttempts": attempts}) |
| 653 |
except botocore.exceptions.WaiterError as e: |
| 654 |
module.fail_json_aws(e, msg="An error occurred waiting") |
| 655 |
|
| 656 |
|
| 657 |
def main(): |
| 658 |
argument_spec = dict( |
| 659 |
name=dict(type="str", required=True), |
| 660 |
cluster_name=dict(type="str", required=True), |
| 661 |
node_role=dict(), |
| 662 |
subnets=dict(type="list", elements="str"), |
| 663 |
scaling_config=dict( |
| 664 |
type="dict", |
| 665 |
default={"min_size": 1, "max_size": 2, "desired_size": 1}, |
| 666 |
options=dict( |
| 667 |
min_size=dict(type="int"), |
| 668 |
max_size=dict(type="int"), |
| 669 |
desired_size=dict(type="int"), |
| 670 |
), |
| 671 |
), |
| 672 |
disk_size=dict(type="int"), |
| 673 |
instance_types=dict(type="list", elements="str"), |
| 674 |
ami_type=dict( |
| 675 |
choices=[ |
| 676 |
"AL2_x86_64", |
| 677 |
"AL2_x86_64_GPU", |
| 678 |
"AL2_ARM_64", |
| 679 |
"CUSTOM", |
| 680 |
"BOTTLEROCKET_ARM_64", |
| 681 |
"BOTTLEROCKET_x86_64", |
| 682 |
] |
| 683 |
), |
| 684 |
remote_access=dict( |
| 685 |
type="dict", |
| 686 |
options=dict( |
| 687 |
ec2_ssh_key=dict(no_log=True), |
| 688 |
source_sg=dict(type="list", elements="str"), |
| 689 |
), |
| 690 |
), |
| 691 |
update_config=dict( |
| 692 |
type="dict", |
| 693 |
default={"max_unavailable": 1}, |
| 694 |
options=dict( |
| 695 |
max_unavailable=dict(type="int"), |
| 696 |
max_unavailable_percentage=dict(type="int"), |
| 697 |
), |
| 698 |
), |
| 699 |
labels=dict(type="dict", default={}), |
| 700 |
taints=dict( |
| 701 |
type="list", |
| 702 |
elements="dict", |
| 703 |
default=[], |
| 704 |
options=dict( |
| 705 |
key=dict( |
| 706 |
type="str", |
| 707 |
no_log=False, |
| 708 |
), |
| 709 |
value=dict(type="str"), |
| 710 |
effect=dict(type="str", choices=["NO_SCHEDULE", "NO_EXECUTE", "PREFER_NO_SCHEDULE"]), |
| 711 |
), |
| 712 |
), |
| 713 |
launch_template=dict( |
| 714 |
type="dict", |
| 715 |
options=dict( |
| 716 |
name=dict(type="str"), |
| 717 |
version=dict(type="str"), |
| 718 |
id=dict(type="str"), |
| 719 |
), |
| 720 |
), |
| 721 |
capacity_type=dict(choices=["ON_DEMAND", "SPOT"], default="ON_DEMAND"), |
| 722 |
release_version=dict(), |
| 723 |
tags=dict(type="dict", aliases=["resource_tags"]), |
| 724 |
purge_tags=dict(type="bool", default=True), |
| 725 |
state=dict(choices=["absent", "present"], default="present"), |
| 726 |
wait=dict(default=False, type="bool"), |
| 727 |
wait_timeout=dict(default=1200, type="int"), |
| 728 |
) |
| 729 |
|
| 730 |
module = AnsibleAWSModule( |
| 731 |
argument_spec=argument_spec, |
| 732 |
required_if=[["state", "present", ["node_role", "subnets"]]], |
| 733 |
mutually_exclusive=[ |
| 734 |
("launch_template", "instance_types"), |
| 735 |
("launch_template", "disk_size"), |
| 736 |
("launch_template", "remote_access"), |
| 737 |
("launch_template", "ami_type"), |
| 738 |
], |
| 739 |
supports_check_mode=True, |
| 740 |
) |
| 741 |
|
| 742 |
if module.params["launch_template"] is None: |
| 743 |
if module.params["disk_size"] is None: |
| 744 |
module.params["disk_size"] = 20 |
| 745 |
if module.params["ami_type"] is None: |
| 746 |
module.params["ami_type"] = "AL2_x86_64" |
| 747 |
if module.params["instance_types"] is None: |
| 748 |
module.params["instance_types"] = ["t3.medium"] |
| 749 |
else: |
| 750 |
if (module.params["launch_template"]["id"] is None) and (module.params["launch_template"]["name"] is None): |
| 751 |
module.exit_json(changed=False, msg="To use launch_template, it is necessary to inform the id or name.") |
| 752 |
try: |
| 753 |
client = module.client("eks") |
| 754 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 755 |
module.fail_json_aws(e, msg="Couldn't connect to AWS.") |
| 756 |
|
| 757 |
if module.params.get("state") == "present": |
| 758 |
create_or_update_nodegroups(client, module) |
| 759 |
else: |
| 760 |
delete_nodegroups(client, module) |
| 761 |
|
| 762 |
|
| 763 |
if __name__ == "__main__": |
| 764 |
main() |