-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kuma-cp) domain name support in dataplane.networking.address (#965)
Signed-off-by: Ilya Lobkov <[email protected]>
- Loading branch information
1 parent
adda484
commit 19421f8
Showing
18 changed files
with
358 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package lookup | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/kumahq/kuma/pkg/core" | ||
) | ||
|
||
type cacheRecord struct { | ||
ips []net.IP | ||
creationTime time.Time | ||
} | ||
|
||
func CachedLookupIP(f LookupIPFunc, ttl time.Duration) LookupIPFunc { | ||
cache := map[string]*cacheRecord{} | ||
var rwmux sync.RWMutex | ||
return func(host string) ([]net.IP, error) { | ||
rwmux.RLock() | ||
r, ok := cache[host] | ||
rwmux.RUnlock() | ||
|
||
if ok && r.creationTime.Add(ttl).After(core.Now()) { | ||
return r.ips, nil | ||
} | ||
|
||
ips, err := f(host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rwmux.Lock() | ||
cache[host] = &cacheRecord{ips: ips, creationTime: core.Now()} | ||
rwmux.Unlock() | ||
|
||
return ips, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package lookup_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDNSCaching(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "DNS with cache Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package lookup_test | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/kumahq/kuma/pkg/core" | ||
"github.com/kumahq/kuma/pkg/core/dns/lookup" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("DNS with cache", func() { | ||
var counter int | ||
var table map[string][]net.IP | ||
lookupFunc := func(host string) ([]net.IP, error) { | ||
counter++ | ||
return table[host], nil | ||
} | ||
var cachingLookupFunc lookup.LookupIPFunc | ||
|
||
BeforeEach(func() { | ||
cachingLookupFunc = lookup.CachedLookupIP(lookupFunc, 1*time.Second) | ||
table = map[string][]net.IP{} | ||
counter = 0 | ||
}) | ||
|
||
It("should use cache on the second call", func() { | ||
_, _ = cachingLookupFunc("example.com") | ||
_, _ = cachingLookupFunc("example.com") | ||
Expect(counter).To(Equal(1)) | ||
}) | ||
|
||
It("should avoid cache after TTL", func() { | ||
table["example.com"] = []net.IP{net.ParseIP("192.168.0.1")} | ||
|
||
ip, _ := cachingLookupFunc("example.com") | ||
Expect(ip[0]).To(Equal(net.ParseIP("192.168.0.1"))) | ||
|
||
ip, _ = cachingLookupFunc("example.com") | ||
Expect(ip[0]).To(Equal(net.ParseIP("192.168.0.1"))) | ||
|
||
table["example.com"] = []net.IP{net.ParseIP("10.20.0.1")} | ||
core.Now = func() time.Time { | ||
return time.Now().Add(2 * time.Second) | ||
} | ||
ip, _ = cachingLookupFunc("example.com") | ||
Expect(ip[0]).To(Equal(net.ParseIP("10.20.0.1"))) | ||
Expect(counter).To(Equal(2)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lookup | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
type LookupIPFunc func(string) ([]net.IP, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.