Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-dupe of code to calculate the DNS change required, and --dry-run working. #1

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 37 additions & 59 deletions clouddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,25 @@ func addDomainForZone(name string, domain string) string {
}

func processCloudDnsChange(dnsSpec *CloudDNSSpec, dnsChange *dns.Change) error {
if len(dnsChange.Additions) == 0 && len(dnsChange.Deletions) == 0 {
if dnsChange == nil || (len(dnsChange.Additions) == 0 && len(dnsChange.Deletions) == 0) {
log.Printf("No DNS changes for Cloud")
return nil
}

log.Printf("Adding %d entries to Cloud DNS", len(dnsChange.Additions))
for _, a := range dnsChange.Additions {
log.Printf(" + %s (%s) %s", a.Name, a.Type, strings.Join(a.Rrdatas, " "))
}
log.Printf("Removing %d entries from Cloud DNS", len(dnsChange.Deletions))
for _, a := range dnsChange.Deletions {
log.Printf(" - %s (%s) %s", a.Name, a.Type, strings.Join(a.Rrdatas, " "))
}

if *dnsSpec.dry_run {
log.Print("Running in dry run mode. Not actually updating Cloud DNS.")
return nil
}

call := dnsSpec.svc.Changes.Create(*dnsSpec.project, *dnsSpec.zone, dnsChange)
out, err := call.Do()
if err != nil {
Expand All @@ -139,8 +154,6 @@ func processCloudDnsChange(dnsSpec *CloudDNSSpec, dnsChange *dns.Change) error {
}

func buildNomadDnsChange(dnsSpec *CloudDNSSpec, tasks []TaskInfo, pruneMissing bool) (*dns.Change, error) {
ret := &dns.Change{}

// Build a new TaskInfo with fully qualified dns names.
fq_taskinfo := []TaskInfo{}
for _, t := range tasks {
Expand All @@ -152,48 +165,18 @@ func buildNomadDnsChange(dnsSpec *CloudDNSSpec, tasks []TaskInfo, pruneMissing b

nomad_rrs, err := buildTaskInfoToRrsets(fq_taskinfo, dnsSpec.default_ttl)
if err != nil {
log.Fatal("Converting Nomad RRs for zone:", dnsSpec.zone)
log.Print("Converting Nomad RRs for zone:", dnsSpec.zone)
return nil, err
}

cloud_rrs, err := getResourceRecordSetsForZone(dnsSpec)
if err != nil {
log.Fatal("Getting Cloud DNS RRs for zone:", dnsSpec.zone)
}

for _, nr := range nomad_rrs {
in_cloud := false
for _, cr := range cloud_rrs {
if nr.Name == cr.Name && nr.Type == cr.Type {
in_cloud = true
if !rrsetsEqual(nr, cr) {
// pointer in nomad differs from cloud.
// Delete cloud record and replace.
log.Printf("Updating %s record in cloud: %s", nr.Type, nr.Name)
ret.Deletions = append(ret.Deletions, cr)
ret.Additions = append(ret.Additions, nr)
}
}
}
if !in_cloud {
log.Printf("Adding %s record in cloud: %s", nr.Type, nr.Name)
ret.Additions = append(ret.Additions, nr)
}
log.Print("Getting Cloud DNS RRs for zone:", dnsSpec.zone)
return nil, err
}

if pruneMissing {
for _, cr := range cloud_rrs {
found := false
for _, nr := range nomad_rrs {
if nr.Name == cr.Name && nr.Type == cr.Type {
found = true
}
}
if !found {
// Record missing from nomad, delete from cloud.
ret.Deletions = append(ret.Deletions, cr)
}
}
}
ret := buildDnsChange(cloud_rrs, nomad_rrs, &pruneMissing)

return ret, nil
}

Expand Down Expand Up @@ -334,13 +317,20 @@ func uploadZonefile(dnsSpec *CloudDNSSpec, zoneFilename *string, dryRun *bool, p

log.Printf("Processing %d zonefile entries rendered %d rrsets", len(zf.Entries()), len(zone_rrs))

change := dns.Change{}

cloud_rrs, err := getResourceRecordSetsForZone(dnsSpec)
if err != nil {
log.Fatal("Getting RRs for zone:", dnsSpec.zone)
}

change := buildDnsChange(cloud_rrs, zone_rrs, pruneMissing)

return processCloudDnsChange(dnsSpec, change)
}

func buildDnsChange(cloud_rrs, zone_rrs []*dns.ResourceRecordSet, prune_missing *bool) *dns.Change {

ret := dns.Change{}

for _, z := range zone_rrs {
found := false
for _, c := range cloud_rrs {
Expand All @@ -349,18 +339,18 @@ func uploadZonefile(dnsSpec *CloudDNSSpec, zoneFilename *string, dryRun *bool, p
if !rrsetsEqual(z, c) {
// Modify means a delete of the exact old record plus
// addition of the new one.
change.Additions = append(change.Additions, z)
change.Deletions = append(change.Deletions, c)
ret.Additions = append(ret.Additions, z)
ret.Deletions = append(ret.Deletions, c)
break
}
}
}
if !found {
// Not found in Cloud DNS, set for addition
change.Additions = append(change.Additions, z)
ret.Additions = append(ret.Additions, z)
}
}
if *pruneMissing {
if *prune_missing {
for _, c := range cloud_rrs {
found := false
if c.Type == "SOA" || c.Type == "NS" {
Expand All @@ -373,23 +363,11 @@ func uploadZonefile(dnsSpec *CloudDNSSpec, zoneFilename *string, dryRun *bool, p
}
if !found {
// Missing from zone file, prune from cloud.
change.Deletions = append(change.Deletions, c)
ret.Deletions = append(ret.Deletions, c)
}
}

}
log.Printf("Adding %d entries to Cloud DNS", len(change.Additions))
for _, a := range change.Additions {
log.Printf(" + %s (%s) %s", a.Name, a.Type, strings.Join(a.Rrdatas, " "))
}
log.Printf("Removing %d entries from Cloud DNS", len(change.Deletions))
for _, a := range change.Deletions {
log.Printf(" - %s (%s) %s", a.Name, a.Type, strings.Join(a.Rrdatas, " "))
}
if len(change.Additions) == 0 && len(change.Deletions) == 0 {
log.Printf("No Changes to do")
return nil
}
return &ret

return processCloudDnsChange(dnsSpec, &change)
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CloudDNSSpec struct {
zone *string
domain *string
default_ttl *int
dry_run *bool
}

func main() {
Expand Down Expand Up @@ -78,6 +79,7 @@ func main() {
project: cloudProject,
zone: cloudZone,
default_ttl: defaultCloudTtl,
dry_run: dryRun,
}

err = populateDnsSpec(dns_spec)
Expand Down