Skip to content

Commit dadedd4

Browse files
committed
fix incorrect zone matches
1 parent 18216cd commit dadedd4

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

0 commit comments

Comments
 (0)