mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-10-18 05:11:00 +02:00
* Endpoint.Target is now Endpoint.Targets. This is its own type representing mutliple targets for a single DNS name while adding some convenience for sorting and comparing * Made everything compile and tests run through with the new Endpoint.Targets * The ingress source can now properly handle multiple target ips per host * Added custom conflict resolver, to better understand how conflict resolution has to work for me * My custom conflict resolver behaves a bit different than the PerResource resolver, therefore I needed to modify the expected test result Removed unnecessary FIXME * The ingress source now creates CNAME endpoints with multiple targets to let the DNS provider decide how to handle multiple CNAME targets. This could be interesting for weighted targets etc. * Adopted the expected results to the new way we create endpoints for CNAMEs * Removed Add method from Targets since manipulating the slice through here is unnecessary complicated and doesn't deliver enough convenience * Reverted ConflictResolver to the original one. There is some discussing to do what the best way is to handle conflicts * Added missing documenting comment to IsLess of Targets * Added documenting comments to Targets,Targets.Same and NewTargets to clarify their intention and usage * Service source now also generates endpoints with multiple targets * Service and Ingress source now sort all Targets for every Endpoint to make order of Targets predictable * Endpoints generated by the Google Cloud DNS provider now also have sorted Targets to make order of Targets predictable * Modified provider dyn to be able to compile with multi target changes * Fixed small nitpicks, so my code is acceptable * Fixed merge method after updating to new Targets. Replacing '!=' with .Same of course needs a boolean negation * Tests for dyn provider now also use the new Targets instead of Target * Simplified extractServiceIps as implied by linki to make it more readable * ref: change service ClusterIP retrieval again * Added entry to CHANGELOG.md describing the new features contained in this PR
302 lines
8.0 KiB
Go
302 lines
8.0 KiB
Go
/*
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nesv/go-dynect/dynect"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/kubernetes-incubator/external-dns/endpoint"
|
|
)
|
|
|
|
func TestDynMerge_NoUpdateOnTTL0Changes(t *testing.T) {
|
|
updateOld := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(1),
|
|
RecordType: endpoint.RecordTypeA,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(1),
|
|
RecordType: endpoint.RecordTypeA,
|
|
},
|
|
}
|
|
|
|
updateNew := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(0),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(0),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
assert.Equal(t, 0, len(merge(updateOld, updateNew)))
|
|
}
|
|
|
|
func TestDynMerge_UpdateOnTTLChanges(t *testing.T) {
|
|
updateOld := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(1),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(1),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
updateNew := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(77),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(10),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
merged := merge(updateOld, updateNew)
|
|
assert.Equal(t, 2, len(merged))
|
|
assert.Equal(t, "name1", merged[0].DNSName)
|
|
}
|
|
|
|
func TestDynMerge_AlwaysUpdateTarget(t *testing.T) {
|
|
updateOld := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(1),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(1),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
updateNew := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1-changed"},
|
|
RecordTTL: endpoint.TTL(0),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(0),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
merged := merge(updateOld, updateNew)
|
|
assert.Equal(t, 1, len(merged))
|
|
assert.Equal(t, "target1-changed", merged[0].Targets[0])
|
|
}
|
|
|
|
func TestDynMerge_NoUpdateIfTTLUnchanged(t *testing.T) {
|
|
updateOld := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(55),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(55),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
updateNew := []*endpoint.Endpoint{
|
|
{
|
|
DNSName: "name1",
|
|
Targets: endpoint.Targets{"target1"},
|
|
RecordTTL: endpoint.TTL(55),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
{
|
|
DNSName: "name2",
|
|
Targets: endpoint.Targets{"target2"},
|
|
RecordTTL: endpoint.TTL(55),
|
|
RecordType: endpoint.RecordTypeCNAME,
|
|
},
|
|
}
|
|
|
|
merged := merge(updateOld, updateNew)
|
|
assert.Equal(t, 0, len(merged))
|
|
}
|
|
|
|
func TestDyn_extractTarget(t *testing.T) {
|
|
tests := []struct {
|
|
recordType string
|
|
block *dynect.DataBlock
|
|
target string
|
|
}{
|
|
{"A", &dynect.DataBlock{Address: "address"}, "address"},
|
|
{"CNAME", &dynect.DataBlock{CName: "name."}, "name"}, // note trailing dot is trimmed for CNAMEs
|
|
{"TXT", &dynect.DataBlock{TxtData: "text."}, "text."},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
assert.Equal(t, tc.target, extractTarget(tc.recordType, tc.block))
|
|
}
|
|
}
|
|
|
|
func TestDyn_endpointToRecord(t *testing.T) {
|
|
tests := []struct {
|
|
ep *endpoint.Endpoint
|
|
extractor func(*dynect.DataBlock) string
|
|
}{
|
|
{endpoint.NewEndpoint("address", "the-target", "A"), func(b *dynect.DataBlock) string { return b.Address }},
|
|
{endpoint.NewEndpoint("cname", "the-target", "CNAME"), func(b *dynect.DataBlock) string { return b.CName }},
|
|
{endpoint.NewEndpoint("text", "the-target", "TXT"), func(b *dynect.DataBlock) string { return b.TxtData }},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
block := endpointToRecord(tc.ep)
|
|
assert.Equal(t, "the-target", tc.extractor(block))
|
|
}
|
|
}
|
|
|
|
func TestDyn_buildLinkToRecord(t *testing.T) {
|
|
provider := &dynProviderState{
|
|
DynConfig: DynConfig{
|
|
ZoneIDFilter: NewZoneIDFilter([]string{"example.com"}),
|
|
DomainFilter: NewDomainFilter([]string{"the-target.example.com"}),
|
|
},
|
|
}
|
|
|
|
tests := []struct {
|
|
ep *endpoint.Endpoint
|
|
link string
|
|
}{
|
|
{endpoint.NewEndpoint("sub.the-target.example.com", "address", "A"), "ARecord/example.com/sub.the-target.example.com/"},
|
|
{endpoint.NewEndpoint("the-target.example.com", "cname", "CNAME"), "CNAMERecord/example.com/the-target.example.com/"},
|
|
{endpoint.NewEndpoint("the-target.example.com", "text", "TXT"), "TXTRecord/example.com/the-target.example.com/"},
|
|
{endpoint.NewEndpoint("the-target.google.com", "text", "TXT"), ""},
|
|
{endpoint.NewEndpoint("mail.example.com", "text", "TXT"), ""},
|
|
{nil, ""},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
assert.Equal(t, tc.link, provider.buildLinkToRecord(tc.ep))
|
|
}
|
|
}
|
|
|
|
func TestDyn_errorOrValue(t *testing.T) {
|
|
e := errors.New("an error")
|
|
val := "value"
|
|
assert.Equal(t, e, errorOrValue(e, val))
|
|
assert.Equal(t, val, errorOrValue(nil, val))
|
|
}
|
|
|
|
func TestDyn_filterAndFixLinks(t *testing.T) {
|
|
links := []string{
|
|
"/REST/ARecord/example.com/the-target.example.com/",
|
|
"/REST/ARecord/example.com/the-target.google.com/",
|
|
"/REST/TXTRecord/example.com/the-target.example.com/",
|
|
"/REST/TXTRecord/example.com/the-target.google.com/",
|
|
"/REST/CNAMERecord/example.com/the-target.google.com/",
|
|
"/REST/CNAMERecord/example.com/the-target.example.com/",
|
|
"/REST/NSRecord/example.com/the-target.google.com/",
|
|
"/REST/NSRecord/example.com/the-target.example.com/",
|
|
}
|
|
filter := NewDomainFilter([]string{"example.com"})
|
|
result := filterAndFixLinks(links, filter)
|
|
|
|
// should skip non-example.com records and NS records too
|
|
assert.Equal(t, 3, len(result))
|
|
assert.Equal(t, "ARecord/example.com/the-target.example.com/", result[0])
|
|
assert.Equal(t, "TXTRecord/example.com/the-target.example.com/", result[1])
|
|
assert.Equal(t, "CNAMERecord/example.com/the-target.example.com/", result[2])
|
|
}
|
|
|
|
func TestDyn_fixMissingTTL(t *testing.T) {
|
|
assert.Equal(t, fmt.Sprintf("%v", dynDefaultTTL), fixMissingTTL(endpoint.TTL(0), 0))
|
|
|
|
// nothing to fix
|
|
assert.Equal(t, "111", fixMissingTTL(endpoint.TTL(111), 25))
|
|
|
|
// apply min TTL
|
|
assert.Equal(t, "1992", fixMissingTTL(endpoint.TTL(111), 1992))
|
|
}
|
|
|
|
func TestDyn_cachePut(t *testing.T) {
|
|
c := cache{
|
|
contents: make(map[string]*entry),
|
|
}
|
|
|
|
c.Put("link", &endpoint.Endpoint{
|
|
DNSName: "name",
|
|
Targets: endpoint.Targets{"target"},
|
|
RecordTTL: endpoint.TTL(10000),
|
|
RecordType: "A",
|
|
})
|
|
|
|
found := c.Get("link")
|
|
assert.NotNil(t, found)
|
|
}
|
|
|
|
func TestDyn_cachePutExpired(t *testing.T) {
|
|
c := cache{
|
|
contents: make(map[string]*entry),
|
|
}
|
|
|
|
c.Put("link", &endpoint.Endpoint{
|
|
DNSName: "name",
|
|
Targets: endpoint.Targets{"target"},
|
|
RecordTTL: endpoint.TTL(0),
|
|
RecordType: "A",
|
|
})
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
found := c.Get("link")
|
|
assert.Nil(t, found)
|
|
|
|
assert.Nil(t, c.Get("no-such-records"))
|
|
}
|