Skip to content

Commit 281fda6

Browse files
schwajotroll-os
authored andcommitted
fix(rfc2136): use correct index for accessing UpdateOld (kubernetes-sigs#5542)
1 parent 4eef9b4 commit 281fda6

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

provider/rfc2136/rfc2136.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,11 @@ func (r *rfc2136Provider) ApplyChanges(ctx context.Context, changes *plan.Change
408408
zone := findMsgZone(ep, r.zoneNames)
409409
m[zone].SetUpdate(zone)
410410

411-
r.UpdateRecord(m[zone], changes.UpdateOld[i], ep)
411+
// calculate corresponding index in the unsplitted UpdateOld for current endpoint ep in chunk
412+
j := (c * r.batchChangeSize) + i
413+
r.UpdateRecord(m[zone], changes.UpdateOld[j], ep)
412414
if r.createPTR && (ep.RecordType == "A" || ep.RecordType == "AAAA") {
413-
r.RemoveReverseRecord(changes.UpdateOld[i].Targets[0], ep.DNSName)
415+
r.RemoveReverseRecord(changes.UpdateOld[j].Targets[0], ep.DNSName)
414416
r.AddReverseRecord(ep.Targets[0], ep.DNSName)
415417
}
416418
}

provider/rfc2136/rfc2136_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,17 @@ func createRfc2136StubProviderWithStrategy(stub *rfc2136Stub, strategy string) (
256256
return NewRfc2136Provider([]string{"rfc2136-host1", "rfc2136-host2", "rfc2136-host3"}, 0, nil, false, "key", "secret", "hmac-sha512", true, &endpoint.DomainFilter{}, false, 300*time.Second, false, false, "", "", "", 50, tlsConfig, strategy, stub)
257257
}
258258

259+
func createRfc2136StubProviderWithBatchChangeSize(stub *rfc2136Stub, batchChangeSize int) (provider.Provider, error) {
260+
tlsConfig := TLSConfig{
261+
UseTLS: false,
262+
SkipTLSVerify: false,
263+
CAFilePath: "",
264+
ClientCertFilePath: "",
265+
ClientCertKeyFilePath: "",
266+
}
267+
return NewRfc2136Provider([]string{""}, 0, nil, false, "key", "secret", "hmac-sha512", true, &endpoint.DomainFilter{}, false, 300*time.Second, false, false, "", "", "", batchChangeSize, tlsConfig, "", stub)
268+
}
269+
259270
func extractUpdateSectionFromMessage(msg fmt.Stringer) []string {
260271
const searchPattern = "UPDATE SECTION:"
261272
updateSectionOffset := strings.Index(msg.String(), searchPattern)
@@ -959,3 +970,44 @@ func TestRandomLoadBalancing(t *testing.T) {
959970

960971
assert.Greater(t, len(nameserverCounts), 1, "Expected multiple nameservers to be used in random strategy")
961972
}
973+
974+
// TestRfc2136ApplyChangesWithMultipleChunks tests Updates with multiple chunks
975+
func TestRfc2136ApplyChangesWithMultipleChunks(t *testing.T) {
976+
stub := newStub()
977+
978+
provider, err := createRfc2136StubProviderWithBatchChangeSize(stub, 2)
979+
assert.NoError(t, err)
980+
981+
var oldRecords []*endpoint.Endpoint
982+
var newRecords []*endpoint.Endpoint
983+
984+
for i := 1; i <= 4; i++ {
985+
oldRecords = append(oldRecords, &endpoint.Endpoint{
986+
DNSName: fmt.Sprintf("%s%d%s", "v", i, ".foo.com"),
987+
RecordType: "A",
988+
Targets: []string{fmt.Sprintf("10.0.0.%d", i)},
989+
RecordTTL: endpoint.TTL(400),
990+
})
991+
newRecords = append(newRecords, &endpoint.Endpoint{
992+
DNSName: fmt.Sprintf("%s%d%s", "v", i, ".foo.com"),
993+
RecordType: "A",
994+
Targets: []string{fmt.Sprintf("10.0.1.%d", i)},
995+
RecordTTL: endpoint.TTL(400),
996+
})
997+
}
998+
999+
p := &plan.Changes{
1000+
UpdateOld: oldRecords,
1001+
UpdateNew: newRecords,
1002+
}
1003+
1004+
err = provider.ApplyChanges(context.Background(), p)
1005+
assert.NoError(t, err)
1006+
1007+
assert.Len(t, stub.updateMsgs, 4)
1008+
1009+
assert.Contains(t, stub.updateMsgs[0].String(), "\nv1.foo.com.\t0\tNONE\tA\t10.0.0.1\nv1.foo.com.\t400\tIN\tA\t10.0.1.1\n")
1010+
assert.Contains(t, stub.updateMsgs[0].String(), "\nv2.foo.com.\t0\tNONE\tA\t10.0.0.2\nv2.foo.com.\t400\tIN\tA\t10.0.1.2\n")
1011+
assert.Contains(t, stub.updateMsgs[2].String(), "\nv3.foo.com.\t0\tNONE\tA\t10.0.0.3\nv3.foo.com.\t400\tIN\tA\t10.0.1.3\n")
1012+
assert.Contains(t, stub.updateMsgs[2].String(), "\nv4.foo.com.\t0\tNONE\tA\t10.0.0.4\nv4.foo.com.\t400\tIN\tA\t10.0.1.4\n")
1013+
}

0 commit comments

Comments
 (0)