From 087b7fa38e352da666bfe1626400787f73896e31 Mon Sep 17 00:00:00 2001 From: Mateus Caruccio Date: Wed, 14 Nov 2018 06:21:56 -0200 Subject: [PATCH] Set node labels from AWS instance tag (#3544) Set kubernetes node labels from a given comma-separated list of `key=value` from AWS instance tag `kubespray-node-labels`. --- contrib/aws_inventory/kubespray-aws-inventory.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/contrib/aws_inventory/kubespray-aws-inventory.py b/contrib/aws_inventory/kubespray-aws-inventory.py index bb31dcf1d..dadec9e3c 100755 --- a/contrib/aws_inventory/kubespray-aws-inventory.py +++ b/contrib/aws_inventory/kubespray-aws-inventory.py @@ -45,15 +45,22 @@ class SearchEC2Tags(object): instances = ec2.instances.filter(Filters=[{'Name': 'tag:'+tag_key, 'Values': tag_value}, {'Name': 'instance-state-name', 'Values': ['running']}]) for instance in instances: + node_labels_tag = list(filter(lambda t: t['Key'] == 'kubespray-node-labels', instance.tags)) + node_labels = '' + if node_labels_tag: + node_labels = dict([ label.strip().split('=') for label in node_labels_tag[0]['Value'].split(',') ]) + if self.vpc_visibility == "public": hosts[group].append(instance.public_dns_name) hosts['_meta']['hostvars'][instance.public_dns_name] = { - 'ansible_ssh_host': instance.public_ip_address + 'ansible_ssh_host': instance.public_ip_address, + 'node_labels': node_labels } else: hosts[group].append(instance.private_dns_name) hosts['_meta']['hostvars'][instance.private_dns_name] = { - 'ansible_ssh_host': instance.private_ip_address + 'ansible_ssh_host': instance.private_ip_address, + 'node_labels': node_labels } hosts['k8s-cluster'] = {'children':['kube-master', 'kube-node']}