Skip to content

Commit

Permalink
GH-551 check traversability of generated endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymvavilov committed May 3, 2024
1 parent db7a73b commit cdfa5a1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
12 changes: 12 additions & 0 deletions controllers/dnspolicy_controller_multi_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}),
}),
))
g.Expect(testDNSRecordsTraversable(recordList, map[string][]string{
TestHostOne: {TestIPAddressOne, TestIPAddressTwo},
TestHostWildcard: {TestIPAddressOne, TestIPAddressTwo},
})).To(BeTrue())
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})

Expand Down Expand Up @@ -272,6 +276,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}),
}),
)
g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue())

g.Expect(*wildcardDnsRecord).To(
MatchFields(IgnoreExtras, Fields{
Expand Down Expand Up @@ -337,6 +342,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}),
}),
)
g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue())
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})
})
Expand Down Expand Up @@ -532,6 +538,10 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}),
}),
))
g.Expect(testDNSRecordsTraversable(recordList, map[string][]string{
TestHostOne: {TestIPAddressOne, TestIPAddressTwo},
TestHostWildcard: {TestIPAddressOne, TestIPAddressTwo},
})).To(BeTrue())
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})

Expand Down Expand Up @@ -636,6 +646,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}),
}),
)
g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue())

g.Expect(*wildcardDnsRecord).To(
MatchFields(IgnoreExtras, Fields{
Expand Down Expand Up @@ -701,6 +712,7 @@ var _ = Describe("DNSPolicy Multi Cluster", func() {
}),
}),
)
g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue())
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})
})
Expand Down
6 changes: 6 additions & 0 deletions controllers/dnspolicy_controller_single_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
}),
}),
))
g.Expect(testDNSRecordsTraversable(recordList, map[string][]string{
TestHostOne: {TestIPAddressOne, TestIPAddressTwo},
TestHostWildcard: {TestIPAddressOne, TestIPAddressTwo},
})).To(BeTrue())
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})

Expand Down Expand Up @@ -241,6 +245,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
}),
}),
)
g.Expect(testEndpointsTraversable(dnsRecord.Spec.Endpoints, TestHostOne, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue())
g.Expect(*wildcardDnsRecord).To(
MatchFields(IgnoreExtras, Fields{
"ObjectMeta": HaveField("Name", wildcardRecordName),
Expand Down Expand Up @@ -290,6 +295,7 @@ var _ = Describe("DNSPolicy Single Cluster", func() {
}),
}),
)
g.Expect(testEndpointsTraversable(wildcardDnsRecord.Spec.Endpoints, TestHostWildcard, []string{TestIPAddressOne, TestIPAddressTwo})).To(BeTrue())
}, TestTimeoutMedium, TestRetryIntervalMedium, ctx).Should(Succeed())
})

Expand Down
49 changes: 49 additions & 0 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"errors"
"io"
"os"
"sigs.k8s.io/external-dns/endpoint"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -538,6 +540,53 @@ func testBuildManagedZone(name, ns, domainName string) *kuadrantdnsv1alpha1.Mana
}
}

// testEndpointsTraversable consumes an array of endpoints and returns a boolean
// indicating presence of that path from host to all IPs
func testEndpointsTraversable(endpoints []*endpoint.Endpoint, host string, ips []string) bool {
for _, ip := range ips {
for _, ep := range endpoints {
// the target on the endpoint exists as an endpoint
if ep.DNSName == host {
// target is our IP
if slices.Contains(ep.Targets, ip) {
return true
} else {
// target is not out IP. Use target as a host and check for existence of Endpoints with such a DNSName
allTargetsFound := true
for _, target := range ep.Targets {
// if at least one returns as false allTargetsFound will be locked in false
allTargetsFound = allTargetsFound && testEndpointsTraversable(endpoints, target, []string{ip})
}
return allTargetsFound
}
}
}
// the target on the endpoint is not existent as an endpoint
return false
}
// there are no IPs to reach / default response
return false
}

// testDNSRecordsTraversable consumes a list of DNSRecords and tests if at least one of them could
// be traversable from any of key/value pairs: hostName (the key) to all the IPs (the []string value)
// returns false if a key/value pair is not traversable on any of the DNSRecords in the list
// returns true if all key/value pairs are traversable on at least one records in the list
func testDNSRecordsTraversable(list *kuadrantdnsv1alpha1.DNSRecordList, hostnames map[string][]string) bool {
for hostname, IP := range hostnames {
for _, record := range list.Items {
// found traversable record
if testEndpointsTraversable(record.Spec.Endpoints, hostname, IP) {
return true
}
}
// no records are traversable for this key/value pair
return false
}
// no hostnames to check
return false
}

//Gateway

func testBuildGatewayClass(name, ns, controllerName string) *gatewayapiv1.GatewayClass {
Expand Down

0 comments on commit cdfa5a1

Please sign in to comment.