Skip to content

Commit

Permalink
Set a default TTL for nomad-sourced records.
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrowadat committed Nov 6, 2023
1 parent 2758297 commit dcf247c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
12 changes: 8 additions & 4 deletions clouddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func buildNomadDnsChange(dnsSpec *CloudDNSSpec, tasks []TaskInfo, pruneMissing b
})
}

nomad_rrs, err := buildTaskInfoToRrsets(fq_taskinfo)
nomad_rrs, err := buildTaskInfoToRrsets(fq_taskinfo, dnsSpec.default_ttl)
if err != nil {
log.Fatal("Converting Nomad RRs for zone:", dnsSpec.zone)
}
Expand Down Expand Up @@ -197,16 +197,19 @@ func buildNomadDnsChange(dnsSpec *CloudDNSSpec, tasks []TaskInfo, pruneMissing b
return ret, nil
}

func buildTaskInfoToRrsets(tasks []TaskInfo) ([]*dns.ResourceRecordSet, error) {
func buildTaskInfoToRrsets(tasks []TaskInfo, default_ttl *int) ([]*dns.ResourceRecordSet, error) {
// Take a set of TaskInfo (essentially name to IP) and return a slice of ResourceRecordSet
// use default_ttl as the ttl of all records (nomad has no opinion on ttl).
ret := []*dns.ResourceRecordSet{}

for _, t := range tasks {
ret = mergeAnswerToRrsets(ret, t.jobid, t.ip)
ret = mergeAnswerToRrsets(ret, t.jobid, t.ip, *default_ttl)
}
return ret, nil
}

func mergeAnswerToRrsets(rrsets []*dns.ResourceRecordSet, name string, ip string) []*dns.ResourceRecordSet {
func mergeAnswerToRrsets(rrsets []*dns.ResourceRecordSet, name string, ip string, default_ttl int) []*dns.ResourceRecordSet {
// merges an answer that point name to ip into these rrsets.
// Only handles simple A records.
for _, rr := range rrsets {
if rr.Name == name {
Expand All @@ -227,6 +230,7 @@ func mergeAnswerToRrsets(rrsets []*dns.ResourceRecordSet, name string, ip string
new_rr := &dns.ResourceRecordSet{
Name: name,
Type: "A",
Ttl: int64(default_ttl),
}
new_rr.Rrdatas = []string{ip}
rrsets = append(rrsets, new_rr)
Expand Down
11 changes: 6 additions & 5 deletions clouddns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ func TestZoneFileFragment(t *testing.T) {

func Test_mergeAnswerToRrsets(t *testing.T) {
type args struct {
rrsets []*dns.ResourceRecordSet
name string
ip string
rrsets []*dns.ResourceRecordSet
name string
ip string
default_ttl int
}
tests := []struct {
name string
Expand Down Expand Up @@ -285,9 +286,9 @@ func Test_mergeAnswerToRrsets(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mergeAnswerToRrsets(tt.args.rrsets, tt.args.name, tt.args.ip); !reflect.DeepEqual(got, tt.want) {
if got := mergeAnswerToRrsets(tt.args.rrsets, tt.args.name, tt.args.ip, tt.args.default_ttl); !reflect.DeepEqual(got, tt.want) {
for _, rr := range got {
t.Logf("Got: %s (%s), %d answers.", rr.Name, rr.Type, len(rr.Rrdatas))
t.Logf("Got: %s (%s) TTL %d, %d answers.", rr.Name, rr.Type, rr.Ttl, len(rr.Rrdatas))
for _, rrd := range rr.Rrdatas {
t.Logf("Got: - %s", rrd)
}
Expand Down
18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import (
)

type CloudDNSSpec struct {
svc *dns.Service
project *string
zone *string
domain *string
svc *dns.Service
project *string
zone *string
domain *string
default_ttl *int
}

func main() {
var jsonKeyfile = flag.String("json-keyfile", "key.json", "json credentials file for Cloud DNS")
var cloudProject = flag.String("cloud-project", "", "Google Cloud Project")
var cloudZone = flag.String("cloud-dns-zone", "", "Cloud DNS zone to operate on")
var defaultCloudTtl = flag.Int("cloud-dns-default-ttl", 300, "Default TTL for Cloud DNS records")

var zoneFilename = flag.String("zonefilename", "", "Local zone file to operate on")
var dryRun = flag.Bool("dry-run", false, "Do not update Cloud DNS, print what would be done")
var pruneMissing = flag.Bool("prune-missing", false, "on putzonefile, prune cloud dns entries not in zone file")
Expand Down Expand Up @@ -71,9 +74,10 @@ func main() {
}

dns_spec := &CloudDNSSpec{
svc: dnsservice,
project: cloudProject,
zone: cloudZone,
svc: dnsservice,
project: cloudProject,
zone: cloudZone,
default_ttl: defaultCloudTtl,
}

err = populateDnsSpec(dns_spec)
Expand Down

0 comments on commit dcf247c

Please sign in to comment.