Skip to content

Commit

Permalink
aws - eks - delete nodegroups & fargate profiles (cloud-custodian#5585)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHillegass authored Apr 27, 2020
1 parent b67175d commit c877c83
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 1 deletion.
55 changes: 54 additions & 1 deletion c7n/resources/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from c7n import tags
from c7n.query import QueryResourceManager, TypeInfo
from c7n.utils import local_session, type_schema

from botocore.waiter import WaiterModel, create_waiter_with_client
from .aws import shape_validate


Expand Down Expand Up @@ -147,6 +147,59 @@ def process(self, resources):
client = local_session(self.manager.session_factory).client('eks')
for r in resources:
try:
self.delete_associated(r, client)
client.delete_cluster(name=r['name'])
except client.exceptions.ResourceNotFoundException:
continue

def delete_associated(self, r, client):
nodegroups = client.list_nodegroups(clusterName=r['name'])['nodegroups']
fargate_profiles = client.list_fargate_profiles(
clusterName=r['name'])['fargateProfileNames']
waiters = []
if nodegroups:
for nodegroup in nodegroups:
self.manager.retry(
client.delete_nodegroup, clusterName=r['name'], nodegroupName=nodegroup)
# Nodegroup supports parallel delete so process in parallel, check these later on
waiters.append({"clusterName": r['name'], "nodegroupName": nodegroup})
if fargate_profiles:
waiter = self.fargate_delete_waiter(client)
for profile in fargate_profiles:
self.manager.retry(
client.delete_fargate_profile,
clusterName=r['name'], fargateProfileName=profile)
# Fargate profiles don't support parallel deletes so process serially
waiter.wait(
clusterName=r['name'], fargateProfileName=profile)
if waiters:
waiter = client.get_waiter('nodegroup_deleted')
for w in waiters:
waiter.wait(**w)

def fargate_delete_waiter(self, client):
# Fargate profiles seem to delete faster @ roughly 2 minutes each so keeping defaults
config = {
'version': 2,
'waiters': {
"FargateProfileDeleted": {
'operation': 'DescribeFargateProfile',
'delay': 30,
'maxAttempts': 40,
'acceptors': [
{
"expected": "DELETE_FAILED",
"matcher": "path",
"state": "failure",
"argument": "fargateprofile.status"
},
{
"expected": "ResourceNotFoundException",
"matcher": "error",
"state": "success"
}
]
}
}
}
return create_waiter_with_client("FargateProfileDeleted", WaiterModel(config), client)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"fargateProfileNames": []
}
}
7 changes: 7 additions & 0 deletions tests/data/placebo/test_eks_delete/eks.ListNodegroups_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"nodegroups": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"cluster": {
"name": "test_f1",
"arn": "arn:aws:eks:us-east-1:644160558196:cluster/test_f1",
"createdAt": {
"__class__": "datetime",
"year": 2020,
"month": 4,
"day": 26,
"hour": 16,
"minute": 46,
"second": 44,
"microsecond": 741000
},
"version": "1.15",
"endpoint": "https://08C627A3689E986CE49CF1F99E544CCF.sk1.us-east-1.eks.amazonaws.com",
"roleArn": "arn:aws:iam::644160558196:role/EKS-signaldancer-ServiceR-AWSServiceRoleForAmazonE-69A4RAE40IF6",
"resourcesVpcConfig": {
"subnetIds": [
"subnet-068dfbf3f275a6ae8",
"subnet-023db907202d61c00",
"subnet-03ec1a55eadb55221"
],
"securityGroupIds": [
"sg-02cd34a089ca06602",
"sg-0d30141b566cfa039"
],
"clusterSecurityGroupId": "sg-0abb351c8f37ded19",
"vpcId": "vpc-072f438c953672ace",
"endpointPublicAccess": true,
"endpointPrivateAccess": false,
"publicAccessCidrs": [
"0.0.0.0/0"
]
},
"logging": {
"clusterLogging": [
{
"types": [
"api",
"audit",
"authenticator",
"controllerManager",
"scheduler"
],
"enabled": false
}
]
},
"identity": {
"oidc": {
"issuer": "https://oidc.eks.us-east-1.amazonaws.com/id/08C627A3689E986CE49CF1F99E544CCF"
}
},
"status": "DELETING",
"certificateAuthority": {
"data": "blank"
},
"platformVersion": "eks.2",
"tags": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"fargateProfile": {
"fargateProfileName": "test",
"fargateProfileArn": "arn:aws:eks:us-east-1:644160558196:fargateprofile/test_f1/test/5cb8dc2a-50f2-6f2c-8573-93d8927beeb7",
"clusterName": "test_f1",
"createdAt": {
"__class__": "datetime",
"year": 2020,
"month": 4,
"day": 26,
"hour": 17,
"minute": 11,
"second": 29,
"microsecond": 178000
},
"podExecutionRoleArn": "arn:aws:iam::644160558196:role/AmazonEKSFargatePodExecutionRole",
"subnets": [
"subnet-068dfbf3f275a6ae8",
"subnet-03ec1a55eadb55221"
],
"selectors": [
{
"namespace": "test",
"labels": {}
}
],
"status": "DELETING",
"tags": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"nodegroup": {
"nodegroupName": "test",
"nodegroupArn": "arn:aws:eks:us-east-1:644160558196:nodegroup/test_f1/test/acb8dc29-f47c-d8b7-71f9-268f44fdbeb0",
"clusterName": "test_f1",
"version": "1.15",
"releaseVersion": "1.15.10-20200228",
"createdAt": {
"__class__": "datetime",
"year": 2020,
"month": 4,
"day": 26,
"hour": 17,
"minute": 10,
"second": 39,
"microsecond": 731000
},
"modifiedAt": {
"__class__": "datetime",
"year": 2020,
"month": 4,
"day": 26,
"hour": 17,
"minute": 32,
"second": 15,
"microsecond": 477000
},
"status": "DELETING",
"scalingConfig": {
"minSize": 1,
"maxSize": 1,
"desiredSize": 1
},
"instanceTypes": [
"t3.medium"
],
"subnets": [
"subnet-023db907202d61c00"
],
"remoteAccess": {
"ec2SshKey": "pikachus"
},
"amiType": "AL2_x86_64",
"nodeRole": "arn:aws:iam::644160558196:role/eksctl-devk8s-nodegroup-ng-2f0ad2-NodeInstanceRole-1P2388MLET9O",
"labels": {},
"resources": {
"autoScalingGroups": [
{
"name": "eks-acb8dc29-f47c-d8b7-71f9-268f44fdbeb0"
}
],
"remoteAccessSecurityGroup": "sg-029bee2954d1815fc"
},
"diskSize": 4,
"health": {
"issues": []
},
"tags": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"cluster": {
"name": "test_f2",
"arn": "arn:aws:eks:us-east-1:644160558196:cluster/test_f2",
"createdAt": {
"__class__": "datetime",
"year": 2020,
"month": 4,
"day": 26,
"hour": 16,
"minute": 47,
"second": 1,
"microsecond": 383000
},
"version": "1.15",
"endpoint": "https://A18EEC7FA5E4E32FED8AAF0D0125F869.gr7.us-east-1.eks.amazonaws.com",
"roleArn": "arn:aws:iam::644160558196:role/EKS-signaldancer-ServiceR-AWSServiceRoleForAmazonE-69A4RAE40IF6",
"resourcesVpcConfig": {
"subnetIds": [
"subnet-068dfbf3f275a6ae8",
"subnet-023db907202d61c00",
"subnet-03ec1a55eadb55221"
],
"securityGroupIds": [
"sg-02cd34a089ca06602",
"sg-0d30141b566cfa039"
],
"clusterSecurityGroupId": "sg-0632d8642bffeb988",
"vpcId": "vpc-072f438c953672ace",
"endpointPublicAccess": true,
"endpointPrivateAccess": false,
"publicAccessCidrs": [
"0.0.0.0/0"
]
},
"logging": {
"clusterLogging": [
{
"types": [
"api",
"audit",
"authenticator",
"controllerManager",
"scheduler"
],
"enabled": false
}
]
},
"identity": {
"oidc": {
"issuer": "https://oidc.eks.us-east-1.amazonaws.com/id/A18EEC7FA5E4E32FED8AAF0D0125F869"
}
},
"status": "DELETING",
"certificateAuthority": {
"data": "blank"
},
"platformVersion": "eks.2",
"tags": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"status_code": 404,
"data": {
"Error": {
"Message": "No Fargate Profile found with name: test.",
"Code": "ResourceNotFoundException"
},
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"status_code": 404,
"data": {
"Error": {
"Message": "No node group found for name: test.",
"Code": "ResourceNotFoundException"
},
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"clusters": [
"test_f1"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"fargateProfileNames": [
"test"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"fargateProfileNames": [
"test"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"fargateProfileNames": []
}
}
Loading

0 comments on commit c877c83

Please sign in to comment.