Skip to content

Commit 521f234

Browse files
authored
fix incorrect zone matches (#37)
* fix incorrect zone matches * fix non-canonical dns names in test
1 parent 3db5a79 commit 521f234

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

internal/designate/provider/provider.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ func (p designateProvider) getZones(ctx context.Context) (map[string]string, err
111111
}
112112

113113
// finds best suitable DNS zone for the hostname
114-
func (p designateProvider) getHostZoneID(hostname string, managedZones map[string]string) (string, error) {
114+
func getHostZoneID(hostname string, managedZones map[string]string) string {
115115
longestZoneLength := 0
116116
resultID := ""
117117

118118
for zoneID, zoneName := range managedZones {
119-
if !strings.HasSuffix(hostname, zoneName) {
119+
if !strings.HasSuffix(hostname, "." + zoneName) && hostname != zoneName {
120120
continue
121121
}
122122
ln := len(zoneName)
@@ -126,7 +126,7 @@ func (p designateProvider) getHostZoneID(hostname string, managedZones map[strin
126126
}
127127
}
128128

129-
return resultID, nil
129+
return resultID
130130
}
131131

132132
// Records returns the list of records.
@@ -266,11 +266,7 @@ func (p designateProvider) ApplyChanges(ctx context.Context, changes *plan.Chang
266266
// apply recordset changes by inserting/updating/deleting recordsets
267267
func (p designateProvider) upsertRecordSet(ctx context.Context, rs *recordSet, managedZones map[string]string) error {
268268
if rs.zoneID == "" {
269-
var err error
270-
rs.zoneID, err = p.getHostZoneID(rs.dnsName, managedZones)
271-
if err != nil {
272-
return err
273-
}
269+
rs.zoneID = getHostZoneID(rs.dnsName, managedZones)
274270
if rs.zoneID == "" {
275271
log.Debugf("Skipping record %s because no hosted zone matching record DNS Name was detected", rs.dnsName)
276272
return nil

internal/designate/provider/provider_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,62 @@ func testDesignateDeleteRecords(t *testing.T, client *fakeDesignateClient) {
609609
t.Errorf("not all expected record-sets were deleted. Remained: %v", expected)
610610
}
611611
}
612+
613+
func TestGetHostZoneID(t *testing.T) {
614+
tests := []struct {
615+
name string
616+
zones []string
617+
hostname string
618+
want string
619+
}{
620+
{
621+
name: "no zone",
622+
zones: []string{},
623+
hostname: "example.com.",
624+
want: "",
625+
},
626+
{
627+
name: "one mismatched zone",
628+
zones: []string{"foo.com."},
629+
hostname: "example.com.",
630+
want: "",
631+
},
632+
{
633+
name: "one matching zone",
634+
zones: []string{"example.com."},
635+
hostname: "example.com.",
636+
want: "example.com.",
637+
},
638+
{
639+
name: "one matching zone, multiple mismatched ones",
640+
zones: []string{"example.com.", "foo.com.", "bar.com."},
641+
hostname: "example.com.",
642+
want: "example.com.",
643+
},
644+
{
645+
name: "should use longer of two matching zones",
646+
zones: []string{"example.com.", "test.example.com."},
647+
hostname: "foo.test.example.com.",
648+
want: "test.example.com.",
649+
},
650+
{
651+
name: "should not match on suffix",
652+
zones: []string{"example.com.", "test.example.com."},
653+
hostname: "first-test.example.com.",
654+
want: "example.com.",
655+
},
656+
}
657+
658+
for _, tt := range tests {
659+
t.Run(tt.name, func(t *testing.T) {
660+
zoneMap := map[string]string{}
661+
for _, zone := range tt.zones {
662+
zoneMap[zone] = zone
663+
}
664+
got := getHostZoneID(tt.hostname, zoneMap)
665+
if got != tt.want {
666+
t.Errorf("got=%s, want=%s for hostname=%s and zones=%s", got, tt.want, tt.hostname, tt.zones)
667+
}
668+
})
669+
}
670+
}

0 commit comments

Comments
 (0)