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
|
from typing import Any, Optional
from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.utils import utcnow
from moto.moto_api._internal import mock_random as random
from moto.utilities.utils import get_partition
from .exceptions import (
InvalidParameterException,
InvalidRequestException,
ResourceInUseException,
ResourceNotFoundException,
)
from .utils import validate_role_arn
# String Templates
CLUSTER_ARN_TEMPLATE = "arn:{partition}:eks:{region}:{account_id}:cluster/{name}"
FARGATE_PROFILE_ARN_TEMPLATE = "arn:{partition}:eks:{region}:{account_id}:fargateprofile/{cluster_name}/{fargate_profile_name}/{uuid}"
NODEGROUP_ARN_TEMPLATE = "arn:{partition}:eks:{region}:{account_id}:nodegroup/{cluster_name}/{nodegroup_name}/{uuid}"
ISSUER_TEMPLATE = (
"https://oidc.eks.{region}.amazonaws.com/id/" + random.get_random_string(length=10)
)
ENDPOINT_TEMPLATE = (
"https://"
+ random.get_random_string()
+ "."
+ random.get_random_string(3)
+ ".{region}.eks.amazonaws.com/"
)
# Defaults used for creating a Cluster
DEFAULT_KUBERNETES_NETWORK_CONFIG = {"serviceIpv4Cidr": "172.20.0.0/16"}
DEFAULT_KUBERNETES_VERSION = "1.19"
DEFAULT_LOGGING = {
"clusterLogging": [
{
"types": [
"api",
"audit",
"authenticator",
"controllerManager",
"scheduler",
],
"enabled": False,
}
]
}
DEFAULT_PLATFORM_VERSION = "eks.4"
ACTIVE_STATUS = "ACTIVE"
# Defaults used for creating a Managed Nodegroup
DEFAULT_AMI_TYPE = "AL2_x86_64"
DEFAULT_CAPACITY_TYPE = "ON_DEMAND"
DEFAULT_DISK_SIZE = "20"
DEFAULT_INSTANCE_TYPES = ["t3.medium"]
DEFAULT_NODEGROUP_HEALTH: dict[str, Any] = {"issues": []}
DEFAULT_RELEASE_VERSION = "1.19.8-20210414"
DEFAULT_REMOTE_ACCESS = {"ec2SshKey": "eksKeypair"}
DEFAULT_SCALING_CONFIG = {"minSize": 2, "maxSize": 2, "desiredSize": 2}
# Exception messages, also imported into testing.
# Obtained through cURL responses from the actual APIs.
CLUSTER_IN_USE_MSG = "Cluster has nodegroups attached"
CLUSTER_EXISTS_MSG = "Cluster already exists with name: {clusterName}"
CLUSTER_NOT_FOUND_MSG = "No cluster found for name: {clusterName}."
CLUSTER_NOT_READY_MSG = "Cluster '{clusterName}' is not in ACTIVE status"
FARGATE_PROFILE_EXISTS_MSG = (
"A Fargate Profile already exists with this name in this cluster."
)
FARGATE_PROFILE_NEEDS_SELECTOR_MSG = "Fargate Profile requires at least one selector."
FARGATE_PROFILE_NOT_FOUND_MSG = (
"No Fargate Profile found with name: {fargateProfileName}."
)
FARGATE_PROFILE_SELECTOR_NEEDS_NAMESPACE = (
"Fargate Profile must have at least one selector with at least one namespace value."
)
FARGATE_PROFILE_TOO_MANY_LABELS = (
"Request contains Selector with more than 5 Label pairs"
)
LAUNCH_TEMPLATE_WITH_DISK_SIZE_MSG = (
"Disk size must be specified within the launch template."
)
LAUNCH_TEMPLATE_WITH_REMOTE_ACCESS_MSG = (
"Remote access configuration cannot be specified with a launch template."
)
NODEGROUP_EXISTS_MSG = (
"NodeGroup already exists with name {nodegroupName} and cluster name {clusterName}"
)
NODEGROUP_NOT_FOUND_MSG = "No node group found for name: {nodegroupName}."
class Cluster:
def __init__(
self,
name: str,
role_arn: str,
resources_vpc_config: dict[str, Any],
account_id: str,
region_name: str,
aws_partition: str,
version: Optional[str] = None,
kubernetes_network_config: Optional[dict[str, str]] = None,
logging: Optional[dict[str, Any]] = None,
client_request_token: Optional[str] = None,
tags: Optional[dict[str, str]] = None,
encryption_config: Optional[list[dict[str, Any]]] = None,
remote_network_config: Optional[dict[str, list[dict[str, list[str]]]]] = None,
):
if encryption_config is None:
encryption_config = []
if tags is None:
tags = {}
self.nodegroups: dict[str, Nodegroup] = {}
self.nodegroup_count = 0
self.fargate_profiles: dict[str, FargateProfile] = {}
self.fargate_profile_count = 0
self.arn = CLUSTER_ARN_TEMPLATE.format(
partition=aws_partition,
account_id=account_id,
region=region_name,
name=name,
)
self.certificate_authority = {"data": random.get_random_string(1400)}
self.created_at = utcnow()
self.identity = {"oidc": {"issuer": ISSUER_TEMPLATE.format(region=region_name)}}
self.endpoint = ENDPOINT_TEMPLATE.format(region=region_name)
self.kubernetes_network_config = (
kubernetes_network_config or DEFAULT_KUBERNETES_NETWORK_CONFIG
)
self.logging = logging or DEFAULT_LOGGING
self.platformVersion = DEFAULT_PLATFORM_VERSION
self.status = ACTIVE_STATUS
self.version = version or DEFAULT_KUBERNETES_VERSION
self.client_request_token = client_request_token
self.encryption_config = encryption_config
self.name = name
self.resources_vpc_config = resources_vpc_config
self.role_arn = role_arn
self.tags = tags
self.remote_network_config = remote_network_config
@property
def is_active(self) -> bool:
return self.status == "ACTIVE"
class FargateProfile:
def __init__(
self,
cluster_name: str,
fargate_profile_name: str,
pod_execution_role_arn: str,
selectors: list[dict[str, Any]],
account_id: str,
region_name: str,
aws_partition: str,
client_request_token: Optional[str] = None,
subnets: Optional[list[str]] = None,
tags: Optional[dict[str, str]] = None,
):
if subnets is None:
subnets = []
if tags is None:
tags = {}
self.created_at = utcnow()
self.uuid = str(random.uuid4())
self.fargate_profile_arn = FARGATE_PROFILE_ARN_TEMPLATE.format(
partition=aws_partition,
account_id=account_id,
region=region_name,
cluster_name=cluster_name,
fargate_profile_name=fargate_profile_name,
uuid=self.uuid,
)
self.status = ACTIVE_STATUS
self.cluster_name = cluster_name
self.fargate_profile_name = fargate_profile_name
self.pod_execution_role_arn = pod_execution_role_arn
self.client_request_token = client_request_token
self.selectors = selectors
self.subnets = subnets
self.tags = tags
class Nodegroup:
def __init__(
self,
cluster_name: str,
node_role: str,
nodegroup_name: str,
subnets: list[str],
account_id: str,
region_name: str,
aws_partition: str,
scaling_config: Optional[dict[str, int]] = None,
disk_size: Optional[int] = None,
instance_types: Optional[list[str]] = None,
ami_type: Optional[str] = None,
remote_access: Optional[dict[str, Any]] = None,
labels: Optional[dict[str, str]] = None,
taints: Optional[list[dict[str, str]]] = None,
tags: Optional[dict[str, str]] = None,
client_request_token: Optional[str] = None,
launch_template: Optional[dict[str, str]] = None,
capacity_type: Optional[str] = None,
version: Optional[str] = None,
release_version: Optional[str] = None,
):
if tags is None:
tags = {}
if labels is None:
labels = {}
if taints is None:
taints = []
self.uuid = str(random.uuid4())
self.arn = NODEGROUP_ARN_TEMPLATE.format(
partition=aws_partition,
account_id=account_id,
region=region_name,
cluster_name=cluster_name,
nodegroup_name=nodegroup_name,
uuid=self.uuid,
)
self.created_at = utcnow()
self.modified_at = utcnow()
self.health = DEFAULT_NODEGROUP_HEALTH
self.resources = {
"autoScalingGroups": [{"name": "eks-" + self.uuid}],
"remoteAccessSecurityGroup": "sg-" + random.get_random_string(17).lower(),
}
self.ami_type = ami_type or DEFAULT_AMI_TYPE
self.capacity_type = capacity_type or DEFAULT_CAPACITY_TYPE
self.disk_size = disk_size or DEFAULT_DISK_SIZE
self.instance_types = instance_types or DEFAULT_INSTANCE_TYPES
self.release_version = release_version or DEFAULT_RELEASE_VERSION
self.remote_access = remote_access or DEFAULT_REMOTE_ACCESS
self.scaling_config = scaling_config or DEFAULT_SCALING_CONFIG
self.status = ACTIVE_STATUS
self.version = version or DEFAULT_KUBERNETES_VERSION
self.client_request_token = client_request_token
self.cluster_name = cluster_name
self.labels = labels
self.launch_template = launch_template
self.node_role = node_role
self.nodegroup_name = nodegroup_name
self.partition = aws_partition
self.region = region_name
self.subnets = subnets
self.tags = tags
self.taints = taints
# Determine LaunchTemplateId from Name (and vice versa)
try:
from moto.ec2.models import ec2_backends
ec2 = ec2_backends[account_id][region_name]
template = None
if "name" in self.launch_template: # type: ignore
name = self.launch_template["name"] # type: ignore
template = ec2.describe_launch_templates(template_names=[name])[0]
elif "id" in self.launch_template: # type: ignore
_id = self.launch_template["id"] # type: ignore
template = ec2.describe_launch_templates(template_ids=[_id])[0]
self.launch_template["id"] = template.id # type: ignore
self.launch_template["name"] = template.name # type: ignore
except: # noqa: E722 Do not use bare except
pass
class EKSBackend(BaseBackend):
def __init__(self, region_name: str, account_id: str):
super().__init__(region_name, account_id)
self.clusters: dict[str, Cluster] = {}
self.cluster_count = 0
self.partition = get_partition(region_name)
def create_cluster(
self,
name: str,
role_arn: str,
resources_vpc_config: dict[str, Any],
version: Optional[str] = None,
kubernetes_network_config: Optional[dict[str, str]] = None,
logging: Optional[dict[str, Any]] = None,
client_request_token: Optional[str] = None,
tags: Optional[dict[str, str]] = None,
encryption_config: Optional[list[dict[str, Any]]] = None,
remote_network_config: Optional[dict[str, list[dict[str, list[str]]]]] = None,
) -> Cluster:
if name in self.clusters:
# Cluster exists.
raise ResourceInUseException(
clusterName=name,
nodegroupName=None,
addonName=None,
message=CLUSTER_EXISTS_MSG.format(clusterName=name),
)
validate_role_arn(role_arn)
cluster = Cluster(
name=name,
role_arn=role_arn,
resources_vpc_config=resources_vpc_config,
version=version,
kubernetes_network_config=kubernetes_network_config,
logging=logging,
client_request_token=client_request_token,
tags=tags,
encryption_config=encryption_config,
remote_network_config=remote_network_config,
account_id=self.account_id,
region_name=self.region_name,
aws_partition=self.partition,
)
self.clusters[name] = cluster
self.cluster_count += 1
return cluster
def create_fargate_profile(
self,
fargate_profile_name: str,
cluster_name: str,
selectors: list[dict[str, Any]],
pod_execution_role_arn: str,
subnets: Optional[list[str]] = None,
client_request_token: Optional[str] = None,
tags: Optional[dict[str, str]] = None,
) -> FargateProfile:
try:
# Cluster exists.
cluster = self.clusters[cluster_name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=cluster_name),
)
if fargate_profile_name in cluster.fargate_profiles:
# Fargate Profile already exists.
raise ResourceInUseException(
clusterName=None,
nodegroupName=None,
addonName=None,
message=FARGATE_PROFILE_EXISTS_MSG,
)
if not cluster.is_active:
raise InvalidRequestException(
message=CLUSTER_NOT_READY_MSG.format(clusterName=cluster_name)
)
_validate_fargate_profile_selectors(selectors)
fargate_profile = FargateProfile(
cluster_name=cluster_name,
fargate_profile_name=fargate_profile_name,
pod_execution_role_arn=pod_execution_role_arn,
client_request_token=client_request_token,
selectors=selectors,
subnets=subnets,
tags=tags,
account_id=self.account_id,
region_name=self.region_name,
aws_partition=self.partition,
)
cluster.fargate_profiles[fargate_profile_name] = fargate_profile
cluster.fargate_profile_count += 1
return fargate_profile
def create_nodegroup(
self,
cluster_name: str,
node_role: str,
nodegroup_name: str,
subnets: list[str],
scaling_config: Optional[dict[str, int]] = None,
disk_size: Optional[int] = None,
instance_types: Optional[list[str]] = None,
ami_type: Optional[str] = None,
remote_access: Optional[dict[str, Any]] = None,
labels: Optional[dict[str, str]] = None,
taints: Optional[list[dict[str, str]]] = None,
tags: Optional[dict[str, str]] = None,
client_request_token: Optional[str] = None,
launch_template: Optional[dict[str, str]] = None,
capacity_type: Optional[str] = None,
version: Optional[str] = None,
release_version: Optional[str] = None,
) -> Nodegroup:
try:
# Cluster exists.
cluster = self.clusters[cluster_name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=cluster_name),
)
if nodegroup_name in cluster.nodegroups:
# Nodegroup already exists.
raise ResourceInUseException(
clusterName=cluster_name,
nodegroupName=nodegroup_name,
addonName=None,
message=NODEGROUP_EXISTS_MSG.format(
nodegroupName=nodegroup_name, clusterName=cluster_name
),
)
if not cluster.is_active:
raise InvalidRequestException(
message=CLUSTER_NOT_READY_MSG.format(clusterName=cluster_name)
)
if launch_template:
validate_launch_template_combination(disk_size, remote_access)
validate_role_arn(node_role)
nodegroup = Nodegroup(
cluster_name=cluster_name,
node_role=node_role,
nodegroup_name=nodegroup_name,
subnets=subnets,
scaling_config=scaling_config,
disk_size=disk_size,
instance_types=instance_types,
ami_type=ami_type,
remote_access=remote_access,
labels=labels,
taints=taints,
tags=tags,
client_request_token=client_request_token,
launch_template=launch_template,
capacity_type=capacity_type,
version=version,
release_version=release_version,
account_id=self.account_id,
region_name=self.region_name,
aws_partition=self.partition,
)
cluster.nodegroups[nodegroup_name] = nodegroup
cluster.nodegroup_count += 1
return nodegroup
def describe_cluster(self, name: str) -> Cluster:
try:
# Cluster exists.
return self.clusters[name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=name),
)
def describe_fargate_profile(
self, cluster_name: str, fargate_profile_name: str
) -> FargateProfile:
try:
# Cluster exists.
cluster = self.clusters[cluster_name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=cluster_name,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=cluster_name),
)
try:
# Fargate Profile exists.
return cluster.fargate_profiles[fargate_profile_name]
except KeyError:
# Fargate Profile does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=FARGATE_PROFILE_NOT_FOUND_MSG.format(
fargateProfileName=fargate_profile_name
),
)
def describe_nodegroup(self, cluster_name: str, nodegroup_name: str) -> Nodegroup:
try:
# Cluster exists.
cluster = self.clusters[cluster_name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=cluster_name,
nodegroupName=nodegroup_name,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=cluster_name),
)
try:
# Nodegroup exists.
return cluster.nodegroups[nodegroup_name]
except KeyError:
# Nodegroup does not exist.
raise ResourceNotFoundException(
clusterName=cluster_name,
nodegroupName=nodegroup_name,
fargateProfileName=None,
addonName=None,
message=NODEGROUP_NOT_FOUND_MSG.format(nodegroupName=nodegroup_name),
)
def delete_cluster(self, name: str) -> Cluster:
try:
# Cluster exists.
validate_safe_to_delete(self.clusters[name])
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=name),
)
result = self.clusters.pop(name)
self.cluster_count -= 1
return result
def delete_fargate_profile(
self, cluster_name: str, fargate_profile_name: str
) -> FargateProfile:
try:
# Cluster exists.
cluster = self.clusters[cluster_name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=cluster_name,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=cluster_name),
)
try:
# Fargate Profile exists.
deleted_fargate_profile = cluster.fargate_profiles.pop(fargate_profile_name)
except KeyError:
# Fargate Profile does not exist.
raise ResourceNotFoundException(
clusterName=cluster_name,
nodegroupName=None,
fargateProfileName=fargate_profile_name,
addonName=None,
message=FARGATE_PROFILE_NOT_FOUND_MSG.format(
fargateProfileName=fargate_profile_name
),
)
cluster.fargate_profile_count -= 1
return deleted_fargate_profile
def delete_nodegroup(self, cluster_name: str, nodegroup_name: str) -> Nodegroup:
try:
# Cluster exists.
cluster = self.clusters[cluster_name]
except KeyError:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=cluster_name),
)
try:
# Nodegroup exists.
result = cluster.nodegroups.pop(nodegroup_name)
except KeyError:
# Nodegroup does not exist.
raise ResourceNotFoundException(
clusterName=cluster_name,
nodegroupName=nodegroup_name,
fargateProfileName=None,
addonName=None,
message=NODEGROUP_NOT_FOUND_MSG.format(nodegroupName=nodegroup_name),
)
cluster.nodegroup_count -= 1
return result
def tag_resource(self, resource_arn: str, tags: dict[str, str]) -> None:
"""
This function currently will tag an EKS cluster only. It does not tag a managed node group
"""
try:
cluster = next(
self.clusters[x]
for x in self.clusters
if self.clusters[x].arn == resource_arn
)
except StopIteration:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message="An error occurred (NotFoundException) when calling the TagResource operation: Resource was not found",
)
cluster.tags.update(tags)
def untag_resource(self, resource_arn: str, tag_keys: list[str]) -> None:
"""
This function currently will remove tags on an EKS cluster only. It does not remove tags from a managed node group
"""
if not isinstance(tag_keys, list):
tag_keys = [tag_keys]
try:
cluster = next(
self.clusters[x]
for x in self.clusters
if self.clusters[x].arn == resource_arn
)
except StopIteration:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message="An error occurred (NotFoundException) when calling the UntagResource operation: Resource was not found",
)
for name in tag_keys:
if name in cluster.tags:
del cluster.tags[name]
def list_tags_for_resource(self, resource_arn: str) -> dict[str, str]:
"""
This function currently will list tags on an EKS cluster only. It does not list tags from a managed node group
"""
try:
cluster = next(
self.clusters[x]
for x in self.clusters
if self.clusters[x].arn == resource_arn
)
except StopIteration:
# Cluster does not exist.
raise ResourceNotFoundException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message="An error occurred (NotFoundException) when calling the ListTagsForResource operation: Resource was not found",
)
return cluster.tags
def list_clusters(
self, max_results: int, next_token: Optional[str]
) -> tuple[list[Cluster], Optional[Cluster]]:
return paginated_list(list(self.clusters.keys()), max_results, next_token)
def list_fargate_profiles(
self, cluster_name: str, max_results: int, next_token: Optional[str]
) -> tuple[list[FargateProfile], Optional[FargateProfile]]:
cluster = self.clusters[cluster_name]
return paginated_list(
list(cluster.fargate_profiles.keys()), max_results, next_token
)
def list_nodegroups(
self, cluster_name: str, max_results: int, next_token: Optional[str]
) -> tuple[list[Nodegroup], Optional[Nodegroup]]:
cluster = self.clusters[cluster_name]
return paginated_list(list(cluster.nodegroups.keys()), max_results, next_token)
def update_cluster_config(
self,
name: str,
resources_vpc_config: dict[str, Any],
logging: Optional[dict[str, Any]] = None,
client_request_token: Optional[str] = None,
kubernetes_network_config: Optional[dict[str, str]] = None,
remote_network_config: Optional[dict[str, list[dict[str, list[str]]]]] = None,
) -> Cluster:
cluster = self.clusters.get(name)
if cluster:
if resources_vpc_config:
self.clusters[name].resources_vpc_config = resources_vpc_config
if logging:
self.clusters[name].logging = logging
if client_request_token:
self.clusters[name].client_request_token = client_request_token
if kubernetes_network_config:
self.clusters[
name
].kubernetes_network_config = kubernetes_network_config
if remote_network_config:
self.clusters[name].remote_network_config = remote_network_config
return cluster
raise ResourceNotFoundException(
clusterName=name,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=CLUSTER_NOT_FOUND_MSG.format(clusterName=name),
)
def paginated_list(
full_list: list[Any], max_results: int, next_token: Optional[str]
) -> tuple[list[Any], Optional[Any]]:
"""
Returns a tuple containing a slice of the full list
starting at next_token and ending with at most the
max_results number of elements, and the new
next_token which can be passed back in for the next
segment of the full list.
"""
sorted_list = sorted(full_list)
list_len = len(sorted_list)
start = sorted_list.index(next_token) if next_token else 0
end = min(start + max_results, list_len)
new_next = None if end == list_len else sorted_list[end]
return sorted_list[start:end], new_next
def validate_safe_to_delete(cluster: Cluster) -> None:
# A cluster which has nodegroups attached can not be deleted.
if cluster.nodegroup_count:
nodegroup_names = ",".join(list(cluster.nodegroups.keys()))
raise ResourceInUseException(
clusterName=cluster.name,
nodegroupName=nodegroup_names,
addonName=None,
message=CLUSTER_IN_USE_MSG,
)
def validate_launch_template_combination(
disk_size: Optional[int], remote_access: Optional[dict[str, Any]]
) -> None:
if not (disk_size or remote_access):
return
raise InvalidParameterException(
message=LAUNCH_TEMPLATE_WITH_DISK_SIZE_MSG
if disk_size
else LAUNCH_TEMPLATE_WITH_REMOTE_ACCESS_MSG
)
def _validate_fargate_profile_selectors(selectors: list[dict[str, Any]]) -> None:
def raise_exception(message: str) -> None:
raise InvalidParameterException(
clusterName=None,
nodegroupName=None,
fargateProfileName=None,
addonName=None,
message=message,
)
# At least one Selector must exist
if not selectors:
raise_exception(message=FARGATE_PROFILE_NEEDS_SELECTOR_MSG)
for selector in selectors:
# Every existing Selector must have a namespace
if "namespace" not in selector:
raise_exception(message=FARGATE_PROFILE_SELECTOR_NEEDS_NAMESPACE)
# If a selector has labels, it can not have more than 5
if len(selector.get("labels", {})) > 5:
raise_exception(message=FARGATE_PROFILE_TOO_MANY_LABELS)
eks_backends = BackendDict(EKSBackend, "eks")
|