Skip to content

Commit

Permalink
use the standard cluster tag for AWS resources
Browse files Browse the repository at this point in the history
  • Loading branch information
cakab committed Jul 10, 2018
1 parent c454c69 commit 17f5499
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ekscli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

__author__ = """Charles Zhang"""
__email__ = 'charles.cakab@gmail.com'
__version__ = '0.1.0rc2'
__version__ = '0.1.0rc3'
__app_name__ = 'ekscli'


Expand Down
31 changes: 14 additions & 17 deletions ekscli/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ def __init__(self, name, description, status, resource_id=None):
self.status = status
self.resource_id = resource_id

def __copy__(self):
return type(self)(self.name, self.description, self.status, self.resource_id)


class ClusterInfo:
def __init__(self, name, endpoint=None, cert=None, vpc=None, subnets=[], sg=None):
Expand Down Expand Up @@ -107,7 +104,7 @@ class ControlPlane:
OUTPUT_VPC = RESOURCE_EKS_VPC.name
OUTPUT_SUBNETS = 'EKSSubnets'

TAG_CLUSTER = 'eks.k8s.io/cluster'
CLUSTER_TAG = 'kubernetes.io/cluster/{}'

def __init__(self, name, role=None, subnets=None, region=None, kube_ver=None, tags=[],
vpc_cidr=None, zones=None):
Expand All @@ -116,7 +113,7 @@ def __init__(self, name, role=None, subnets=None, region=None, kube_ver=None, ta
self.tag_name = 'eks.{}.cp'.format(name)
self.subnets = subnets
self.tags = dict(t.split('=') for t in tags)
self.tags.update({'Name': self.tag_name, self.TAG_CLUSTER: self.name})
self.tags.update({'Name': self.tag_name, self.CLUSTER_TAG.format(self.name): 'owned'})
self.vpc = None
self.role = role
self.region = region.lower() if region else boto3.session.Session().region_name
Expand Down Expand Up @@ -153,17 +150,17 @@ def delete(self):

stacks = self.get_all_stacks()
for s in stacks:
for t in s.tags:
if t.get('Key') == NodeGroup.TAG_NODEGROUP:
NodeGroup(name=t.get('Value'), cluster_info=ci).delete(stack=s)
break
tags = {t.get('Key'): t.get('Value') for t in s.tags}
if ControlPlane.CLUSTER_TAG.format(self.name) in tags and NodeGroup.NODEGROUP_TAG in tags:
NodeGroup(name=tags.get(NodeGroup.NODEGROUP_TAG), cluster_info=ci).delete(stack=s)

resources = [self.RESOURCE_EKS_CLUSTER, self.RESOURCE_CP_SG, self.RESOURCE_CP_ROLE]
resources = deepcopy([self.RESOURCE_EKS_CLUSTER, self.RESOURCE_CP_SG, self.RESOURCE_CP_ROLE])
for i, subnet in enumerate(ci.subnets):
sname = self.RESOURCE_FORMAT_SUBNET.format(i + 1)
resources.append(Resource(sname, 'EKS VPC {}'.format(sname), Status.created, subnet))
self.RESOURCE_EKS_VPC.resource_id = ci.vpc
resources.append(self.RESOURCE_EKS_VPC)
vpc_resource = copy(self.RESOURCE_EKS_VPC)
vpc_resource.resource_id = ci.vpc
resources.append(vpc_resource)
for r in resources:
r.status = Status.created

Expand Down Expand Up @@ -192,11 +189,11 @@ def query(self):
def get_all_stacks(self):
cf = boto3.session.Session().resource('cloudformation')
return [s for s in cf.stacks.all() for t in s.tags
if t.get('Key') == self.TAG_CLUSTER and t.get('Value') == self.name]
if t.get('Key') == self.CLUSTER_TAG.format(self.name) and t.get('Value') == 'owned']

def get_all_nodegroup_stacks(self):
return {t.get('Value'): s for s in self.get_all_stacks() for t in s.tags
if t.get('Key') == NodeGroup.TAG_NODEGROUP}
if t.get('Key') == NodeGroup.NODEGROUP_TAG}

@staticmethod
def _stack_to_cluster_info(name, outputs):
Expand Down Expand Up @@ -470,7 +467,7 @@ class NodeGroup:

OUTPUT_KEYNAME = 'KeyName'

TAG_NODEGROUP = 'k8s.io/cluster-autoscaler/node-template/label/ng'
NODEGROUP_TAG = 'k8s.io/cluster-autoscaler/node-template/label/ng'

DEFAULT_AMI = {'us-east-1': 'ami-dea4d5a1', 'us-west-2': 'ami-73a6e20b'}
USER_DATA = textwrap.dedent('''\
Expand Down Expand Up @@ -534,8 +531,8 @@ def __init__(self, name, cluster_info=None, region=None, subnets=[], tags={}, mi
self.stack_name = 'eks-{}-ng-{}'.format(self.cluster.name, self.name)
self.tags = tags
self.tags.update({'Name': self.tag_name,
ControlPlane.TAG_CLUSTER: self.cluster.name,
self.TAG_NODEGROUP: self.name})
ControlPlane.CLUSTER_TAG.format(self.cluster.name): 'owned',
self.NODEGROUP_TAG: self.name})
self.region = region
self.role = role
self.sg_igresses = sg_ingresses
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ commands = flake8 ekscli
setenv =
PYTHONPATH = {toxinidir}
deps =
-r{toxinidir}/requirements_dev.txt
-r{toxinidir}/requirements-dev.txt
; If you want to make tox run the tests with the same versions, create a
; requirements.txt with the pinned versions and uncomment the following line:
; -r{toxinidir}/requirements.txt
Expand Down

0 comments on commit 17f5499

Please sign in to comment.