Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Apr 28, 2023
1 parent 955a38d commit 55c1b4e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ No modules.
| <a name="input_enable_sns"></a> [enable\_sns](#input\_enable\_sns) | Enable use of SNS for reporting errors | `bool` | `false` | no |
| <a name="input_enable_sqs"></a> [enable\_sqs](#input\_enable\_sqs) | Enable use of SQS for SNS to send errors. Requires the use of enable\_sns as well | `bool` | `false` | no |
| <a name="input_kms_key_name"></a> [kms\_key\_name](#input\_kms\_key\_name) | Different KMS Key (for SNS and SQS) to override default of var.name | `string` | `null` | no |
| <a name="input_lambda_environment_variables"></a> [lambda\_environment\_variables](#input\_lambda\_environment\_variables) | Map of lambda environment variables and values | `map(string)` | <pre>{<br> "DNS_RR_TimeToLive": 60,<br> "DebugLogLevel": "INFO",<br> "DynamoDBName": null,<br> "HeritageIdentifier": "dynr53",<br> "HeritageTXTRecordPrefix": "_txt",<br> "MaxApiRetry": 10,<br> "SleepTime": 60,<br> "SnsEnable": false,<br> "SnsTopicArn": "",<br> "TagKeyCname": "boc:dns:cname",<br> "TagKeyHostName": "boc:dns:name",<br> "TagKeyZone": "boc:dns:zone"<br>}</pre> | no |
| <a name="input_lambda_environment_variables"></a> [lambda\_environment\_variables](#input\_lambda\_environment\_variables) | Map of lambda environment variables and values | `map(string)` | <pre>{<br> "DNS_RR_TimeToLive": 60,<br> "DebugLogLevel": "INFO",<br> "DynamoDBName": null,<br> "HeritageIdentifier": "dynr53",<br> "HeritageTXTRecordPrefix": "_txt",<br> "MaxApiRetry": 10,<br> "SleepTime": 60,<br> "SnsEnable": false,<br> "SnsTopicArn": "",<br> "TagKeyCname": "boc:dns:cname",<br> "TagKeyFlags": "boc:dns:flags",<br> "TagKeyHostName": "boc:dns:name",<br> "TagKeyPtrname": "boc:dns:ptrname",<br> "TagKeyZone": "boc:dns:zone"<br>}</pre> | no |
| <a name="input_lambda_environment_variables_override"></a> [lambda\_environment\_variables\_override](#input\_lambda\_environment\_variables\_override) | Map of lambda environment variables and values to override from the defaults | `map(string)` | `{}` | no |
| <a name="input_lambda_name"></a> [lambda\_name](#input\_lambda\_name) | Different Lambda name to override default of var.name | `string` | `null` | no |
| <a name="input_name"></a> [name](#input\_name) | Name to use within all the created resources (default: inf-dynamic-route53) | `string` | `"inf-dynamic-route53"` | no |
Expand Down
46 changes: 27 additions & 19 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
LOGGER = logging.getLogger()
account_id = None
region = None
VERSION = '1.2.0b85'
VERSION = '1.2.0b86'

# Read Env variables
DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO')
Expand Down Expand Up @@ -667,6 +667,9 @@ def lambda_handler(
tag_data['name'] = tag_data_tuple(*process_tags_name(tags))
LOGGER.debug("New tag_data structure: %s", str(pformat(tag_data)) + lineno())

emr_status = discover_emr(tags_dict)
LOGGER.info(f"discover_emr instance: {instance_id} result {emr_status}")

default_hostname = '-'.join(['ip'] + private_ip.split('.'))
if tag_data['option_zone'].valid:
if tag_data['option_name'].valid:
Expand Down Expand Up @@ -877,7 +880,7 @@ def lambda_handler(
LOGGER.info(
f"flags=noforward, not adding A and heritage TXT for host {final_private_hostname} zone {zone_data_forward.name} value {private_ip}")

if not flags['noreverse']:
if not flags['noptr']:
# fqdn = create_fqdn(final_private_hostname, final_hosted_zone_name)
try:
if reverse_zone_associated:
Expand Down Expand Up @@ -946,7 +949,7 @@ def lambda_handler(
instance_id, str(err) + lineno())
else:
LOGGER.info(
f"flags=noreverse, not adding PTR and heritage TXT for host {tag_data['ptr_entry'].hostname} zone {tag_data['ptr_entry'].zonename} value {final_private_dns_name}")
f"flags=noptr, not adding PTR and heritage TXT for host {tag_data['ptr_entry'].hostname} zone {tag_data['ptr_entry'].zonename} value {final_private_dns_name}")

# else: # not running so delete the records. Note this may leave orphans around if the flags are set and then the host is shut down. We may want to remove no matter what.
# go through the dns_data records, and delete them. dns_data contains the records that were added. It is possible the tags have changed
Expand Down Expand Up @@ -1025,11 +1028,14 @@ def lambda_handler(
# create CNAME record in private zone
if state == 'running':
try:
if not flags['nocname']:
LOGGER.debug(f"cname_host_name: {cf_hostname} {lineno()}")
LOGGER.debug(f"cname_domain_suffix: {cf_zonename} {lineno()}")
LOGGER.debug(f"cname_domain_suffix_id: {cf_zonename_id} {lineno()}")
LOGGER.debug(f"cname_target: {final_private_dns_name} {lineno()}")
if not flags['nocname'] and (all(emr_status.is_cluster, emr_status.is_master) or not emr_status.is_cluster)
LOGGER.debug(f"cname_host_name: {cf_hostname} {lineno()}")
LOGGER.debug(f"cname_domain_suffix: {cf_zonename} {lineno()}")
LOGGER.debug(f"cname_domain_suffix_id: {cf_zonename_id} {lineno()}")
LOGGER.debug(f"cname_target: {final_private_dns_name} {lineno()}")
if emr_status.is_cluster:
LOGGER.info(
f"instance {instance_id}: is_cluster && is_master cluster_id {cluster_id} setting CNAME {cf_hostname} in zone {cf_zonename} {lineno()}")

create_response = create_resource_record(
route53,
Expand Down Expand Up @@ -3096,7 +3102,12 @@ def tags_to_dict(tags):

def process_tags_flags(tags):
"""
Process the DNS flags tags into for tags[key]=='boc:dns:flags'
Process the DNS flags tags into for tags[key]=='boc:dns:flags'. Available flags:
- noforward: do not define A or AAAA (when available) or the associated heritage TXT record
- noptr: do not define a PTR or associated heritage TXT record with default or boc:dns:ptrname flag
- nocname: do not define a CNAME or associated heritage TXT record, even if specified in the boc:dns:cname flag
- noheritage: do not create a heritage TXT record, used to indicate which service created the entries
:param list(dict(string)) tags: tags from instance, list of dict of string. Keys and values turned to lowercase.
:return dict(string): flag settings in defaultdict for controlling which names are registered and when
Expand Down Expand Up @@ -3269,10 +3280,6 @@ def parse_hostname_to_components(name):
return (False, host, domain)


# noforward
# noreverse
# noheritage

def create_fqdn(host, zone):
"""
This takes a hostname (may or nay not be FQDN) and a zone, and returns the proper concatenation of the two, with a trailing dot.
Expand All @@ -3287,7 +3294,7 @@ def create_fqdn(host, zone):
return fqdn


def discover_emr_master(tags):
def discover_emr_cluster(tags):
"""
This tags a dict of tags and determines if the appropriate EMR tags are set.
Expand All @@ -3298,16 +3305,17 @@ def discover_emr_master(tags):
- emr:elasticmapreduce:instance-group-role == MASTER
:param dict tags: dict of tag
:return (bool,str): Tuple containing is_master (is an EMR node and is the master), and the cluster_id if it's a cluster. cluster_id will be empty if not a cluster.
:return (bool,str): Tuple containing is_master, is_cluster, and cluster_id if it's a cluster. cluster_id will be empty if not a cluster.
"""

cluster_tuple = namedtuple('EMRCluster', ['cluster', 'master', 'cluster_id'])
cluster_id = tags.get('aws:elasticmapreduce:job-flow-id', '')
is_master = tags.get('aws:elasticmapreduce:instance-group-role', '') == 'MASTER'

is_cluster = cluster_id != ''
LOGGER.debug(
f"discover_emr: is_cluster {is_cluster}, is_master {is_master}, cluster_id {cluster_id}")
return (is_master and is_cluster, cluster_id)

result = cluster_tuple(is_cluster, is_master, cluster_id)
LOGGER.debug(f"discover_emr: {str(result)} {lineno()}")
return result


def evaluate_event_action(event):
Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ variable "lambda_environment_variables" {
TagKeyCname = "boc:dns:cname"
TagKeyHostName = "boc:dns:name"
TagKeyZone = "boc:dns:zone"
TagKeyPtrname = "boc:dns:ptrname"
TagKeyFlags = "boc:dns:flags"
}
}

Expand Down

0 comments on commit 55c1b4e

Please sign in to comment.