From 500e17990dc7a6e9a257b9520d5a2a7fdc0aa7ec Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Tue, 19 Dec 2017 19:57:08 +0100 Subject: [PATCH] Fix deleteDNSRecord Make sure only the desired record is deleted taking into account that the `Start*` params only assure that `ListResourceRecordSets()` returns the records whose attributes are lexicographically equal or greater than the corresponding `Start*` params in asceding order. Improve the logging in this method. --- ecssd_agent.go | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/ecssd_agent.go b/ecssd_agent.go index 4cfec98..da84f77 100644 --- a/ecssd_agent.go +++ b/ecssd_agent.go @@ -333,35 +333,34 @@ func deleteDNSRecord(serviceName string, dockerId string) error { r53 := route53.New(sess) srvRecordName := serviceName + "." + DNSName srvSetIdentifier := configuration.Hostname + ":" + dockerId + log.Infof("Deleting Record %s (%s)", srvRecordName, srvSetIdentifier) // This API Call looks for the Route53 DNS record for this service and docker ID to get the values to delete paramsList := &route53.ListResourceRecordSetsInput{ - HostedZoneId: aws.String(configuration.HostedZoneId), // Required - MaxItems: aws.String("100"), - StartRecordName: aws.String(srvRecordName), - StartRecordType: aws.String(route53.RRTypeSrv), + HostedZoneId: aws.String(configuration.HostedZoneId), // Required + MaxItems: aws.String("100"), + StartRecordIdentifier: aws.String(srvSetIdentifier), + StartRecordName: aws.String(srvRecordName), + StartRecordType: aws.String(route53.RRTypeSrv), } - more := true var recordSetToDelete *route53.ResourceRecordSet resp, err := r53.ListResourceRecordSets(paramsList) - for more && recordSetToDelete == nil && err == nil { - for _, rrset := range resp.ResourceRecordSets { - if isManagedResourceRecordSet(rrset) && *rrset.SetIdentifier == srvSetIdentifier { - recordSetToDelete = rrset - } - } - - more = resp.IsTruncated != nil && *resp.IsTruncated - if more { - paramsList.StartRecordIdentifier = resp.NextRecordIdentifier - resp, err = r53.ListResourceRecordSets(paramsList) - } - } - logErrorNoFatal(err) if err != nil { + logErrorNoFatal(err) return err } + + if len(resp.ResourceRecordSets) > 0 { + rrset := resp.ResourceRecordSets[0] + if isManagedResourceRecordSet(rrset) && + *rrset.SetIdentifier == srvSetIdentifier && + strings.HasPrefix(*rrset.Name, srvRecordName) && + *rrset.Type == route53.RRTypeSrv { + recordSetToDelete = rrset + } + } + if recordSetToDelete == nil { - log.Error("Route53 record doesn't exist") + log.Errorf("Route53 record %s (%s) doesn't exist", srvRecordName, srvSetIdentifier) return nil } @@ -381,7 +380,7 @@ func deleteDNSRecord(serviceName string, dockerId string) error { _, err = r53.ChangeResourceRecordSets(params) logErrorNoFatal(err) if err == nil { - log.Info("Record " + srvRecordName + " deleted") + log.Infof("Record %s (%s) deleted", srvRecordName, srvSetIdentifier) } return err }