From 65f4239d44ca845dc9bc5a9d3d6c78ed0f23b23c Mon Sep 17 00:00:00 2001 From: leigh schrandt Date: Mon, 13 Nov 2017 10:28:14 -0700 Subject: [PATCH] provider=google: Add Ttl from annotations: Use `int64(ep.RecordTTL)` in `newRecord()` Fallback to hardcoded 300s for backwards-compat Add `TestNewRecords()` Add notes in *ttl.md* --- CHANGELOG.md | 2 ++ docs/ttl.md | 18 +++++++++++++++++- provider/google.go | 12 +++++++++++- provider/google_test.go | 23 +++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e021a73542..11ffd7088a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ + - Google: Support configuring TTL by annotation: `external-dns.alpha.kubernetes.io/ttl`. (#389) @stealthybox + ## v0.4.8 - 2017-11-09 - AWS: Added change batch limiting to a maximum of 4000 Route53 updates in one API call. Changes exceeding the limit will be dropped but all related changes by hostname are preserved within the limit. (#368) @bitvector2 diff --git a/docs/ttl.md b/docs/ttl.md index 302d57c07b..6c300c7d1f 100644 --- a/docs/ttl.md +++ b/docs/ttl.md @@ -24,7 +24,23 @@ Providers - [ ] Azure - [ ] Cloudflare - [ ] DigitalOcean -- [ ] Google +- [x] Google - [ ] InMemory PRs welcome! + +Notes +===== +When the `external-dns.alpha.kubernetes.io/ttl` annotation is not provided, the Ttl will default to 0 seconds and `enpoint.TTL.isConfigured()` will be false. + +### AWS Provider +The AWS Provider overrides the value to 300s when the Ttl is 0. +This value is a constant in the provider code. + +### Google Provider +Previously with the Google Provider, Ttl's were hard-coded to 300s. +For safety, the Google Provider overrides the value to 300s when the Ttl is 0. +This value is a constant in the provider code. + +For the moment, it is impossible to use a Ttl value of 0 with the AWS and Google Providers. +This behavior may change in the future. diff --git a/provider/google.go b/provider/google.go index d8e0e2dbd8..aeb201ed17 100644 --- a/provider/google.go +++ b/provider/google.go @@ -33,6 +33,10 @@ import ( "github.com/kubernetes-incubator/external-dns/plan" ) +const ( + googleRecordTTL = 300 +) + type managedZonesCreateCallInterface interface { Do(opts ...googleapi.CallOption) (*dns.ManagedZone, error) } @@ -319,10 +323,16 @@ func newRecord(ep *endpoint.Endpoint) *dns.ResourceRecordSet { target = ensureTrailingDot(target) } + // no annotation results in a Ttl of 0, default to 300 for backwards-compatability + var ttl int64 = googleRecordTTL + if ep.RecordTTL.IsConfigured() { + ttl = int64(ep.RecordTTL) + } + return &dns.ResourceRecordSet{ Name: ensureTrailingDot(ep.DNSName), Rrdatas: []string{target}, - Ttl: 300, + Ttl: ttl, Type: ep.RecordType, } } diff --git a/provider/google_test.go b/provider/google_test.go index 998ac0e582..28406c7d8e 100644 --- a/provider/google_test.go +++ b/provider/google_test.go @@ -398,6 +398,27 @@ func TestGoogleApplyChangesEmpty(t *testing.T) { assert.NoError(t, provider.ApplyChanges(&plan.Changes{})) } +func TestNewRecords(t *testing.T) { + records := newRecords([]*endpoint.Endpoint{ + endpoint.NewEndpointWithTTL("update-test.zone-2.ext-dns-test-2.gcp.zalan.do", "8.8.4.4", endpoint.RecordTypeA, 1), + endpoint.NewEndpointWithTTL("delete-test.zone-2.ext-dns-test-2.gcp.zalan.do", "8.8.4.4", endpoint.RecordTypeA, 120), + endpoint.NewEndpointWithTTL("update-test-cname.zone-1.ext-dns-test-2.gcp.zalan.do", "bar.elb.amazonaws.com", endpoint.RecordTypeCNAME, 4000), + // test fallback to Ttl:300 when Ttl==0 : + endpoint.NewEndpointWithTTL("update-test.zone-1.ext-dns-test-2.gcp.zalan.do", "8.8.8.8", endpoint.RecordTypeA, 0), + endpoint.NewEndpoint("delete-test.zone-1.ext-dns-test-2.gcp.zalan.do", "8.8.8.8", endpoint.RecordTypeA), + endpoint.NewEndpoint("delete-test-cname.zone-1.ext-dns-test-2.gcp.zalan.do", "qux.elb.amazonaws.com", endpoint.RecordTypeCNAME), + }) + + validateChangeRecords(t, records, []*dns.ResourceRecordSet{ + {Name: "update-test.zone-2.ext-dns-test-2.gcp.zalan.do.", Rrdatas: []string{"8.8.4.4"}, Type: "A", Ttl: 1}, + {Name: "delete-test.zone-2.ext-dns-test-2.gcp.zalan.do.", Rrdatas: []string{"8.8.4.4"}, Type: "A", Ttl: 120}, + {Name: "update-test-cname.zone-1.ext-dns-test-2.gcp.zalan.do.", Rrdatas: []string{"bar.elb.amazonaws.com."}, Type: "CNAME", Ttl: 4000}, + {Name: "update-test.zone-1.ext-dns-test-2.gcp.zalan.do.", Rrdatas: []string{"8.8.8.8"}, Type: "A", Ttl: 300}, + {Name: "delete-test.zone-1.ext-dns-test-2.gcp.zalan.do.", Rrdatas: []string{"8.8.8.8"}, Type: "A", Ttl: 300}, + {Name: "delete-test-cname.zone-1.ext-dns-test-2.gcp.zalan.do.", Rrdatas: []string{"qux.elb.amazonaws.com."}, Type: "CNAME", Ttl: 300}, + }) +} + func TestSeparateChanges(t *testing.T) { change := &dns.Change{ Additions: []*dns.ResourceRecordSet{ @@ -475,7 +496,9 @@ func validateChangeRecords(t *testing.T, records []*dns.ResourceRecordSet, expec func validateChangeRecord(t *testing.T, record *dns.ResourceRecordSet, expected *dns.ResourceRecordSet) { assert.Equal(t, expected.Name, record.Name) + assert.Equal(t, expected.Rrdatas, record.Rrdatas) assert.Equal(t, expected.Ttl, record.Ttl) + assert.Equal(t, expected.Type, record.Type) } func newGoogleProvider(t *testing.T, domainFilter DomainFilter, dryRun bool, records []*endpoint.Endpoint) *GoogleProvider {