-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dns): Rewrite dns persistence to allow virtual-outbound to be added
Currently the persisted state on the vips was very restraining. With the addition of virtual-outbounds we'll need something more flexible This migrates the state to a new format that will be extensible. Signed-off-by: Charly Molter <charly.molter@konghq.com>
- Loading branch information
Showing
11 changed files
with
489 additions
and
335 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
package vips | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/Nordix/simple-ipam/pkg/ipam" | ||
) | ||
|
||
// GlobalView keeps a list of all hostname/ips and add the possibility to allocate new ips | ||
type GlobalView struct { | ||
ipam *ipam.IPAM | ||
ipToHostname map[string]Entry | ||
hostnameToIp map[Entry]string | ||
} | ||
|
||
// Reserve add an ip/host to the list of reserved ips (useful when loading an existing store). | ||
func (g *GlobalView) Reserve(hostname Entry, ip string) error { | ||
err := g.ipam.Reserve(net.ParseIP(ip)) | ||
if err != nil { | ||
return err | ||
} | ||
g.ipToHostname[ip] = hostname | ||
g.hostnameToIp[hostname] = ip | ||
return nil | ||
} | ||
|
||
// Allocate assign an ip to a host | ||
func (g *GlobalView) Allocate(hostname Entry) (string, error) { | ||
ip := g.hostnameToIp[hostname] | ||
if ip != "" { | ||
return ip, nil | ||
} | ||
netIp, err := g.ipam.Allocate() | ||
if err != nil { | ||
return "", err | ||
} | ||
ip = netIp.String() | ||
g.ipToHostname[ip] = hostname | ||
g.hostnameToIp[hostname] = ip | ||
return ip, nil | ||
} | ||
|
||
func (g *GlobalView) VipList() map[Entry]string { | ||
return g.hostnameToIp | ||
} | ||
|
||
func NewGlobalView(cidr string) (*GlobalView, error) { | ||
newIPAM, err := ipam.New(cidr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &GlobalView{ | ||
hostnameToIp: map[Entry]string{}, | ||
ipToHostname: map[string]Entry{}, | ||
ipam: newIPAM, | ||
}, 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,108 @@ | ||
package vips_test | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/kumahq/kuma/pkg/dns/vips" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("global view", func() { | ||
|
||
It("should fail when no more ips", func() { | ||
// given | ||
gv, err := vips.NewGlobalView("192.168.0.1/32") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(gv).ToNot(BeNil()) | ||
|
||
// when | ||
ip1, err := gv.Allocate(vips.NewServiceEntry("foo.bar")) | ||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ip1).ToNot(Equal("")) | ||
|
||
// when | ||
ip2, err := gv.Allocate(vips.NewServiceEntry("bar.bar")) | ||
// then | ||
Expect(err).To(HaveOccurred()) | ||
Expect(ip2).To(Equal("")) | ||
}) | ||
|
||
It("should allocate IPs", func() { | ||
// given | ||
gv, err := vips.NewGlobalView("192.168.0.1/24") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(gv).ToNot(BeNil()) | ||
|
||
// when | ||
ip1, err := gv.Allocate(vips.NewServiceEntry("foo.bar")) | ||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// when | ||
ip2, err := gv.Allocate(vips.NewServiceEntry("bar.bar")) | ||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// then | ||
vips := map[vips.Entry]string{ | ||
vips.NewServiceEntry("foo.bar"): ip1, | ||
vips.NewServiceEntry("bar.bar"): ip2, | ||
} | ||
|
||
Expect(gv.VipList()).To(Equal(vips)) | ||
}) | ||
|
||
It("should allocate 2^16 IP addresses", func() { | ||
// given | ||
gv, err := vips.NewGlobalView("240.0.0.0/4") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(gv).ToNot(BeNil()) | ||
|
||
for i := 0; i < math.MaxInt16; i++ { | ||
// when | ||
_, err := gv.Allocate(vips.NewHostEntry(fmt.Sprintf("foo-%d.mesh", i))) | ||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
}) | ||
|
||
It("should give the same ip if we ask for a host already allocated", func() { | ||
// given | ||
host := vips.NewServiceEntry("foo.com") | ||
gv, err := vips.NewGlobalView("240.0.0.0/4") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(gv).ToNot(BeNil()) | ||
|
||
err = gv.Reserve(host, "240.0.0.1") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// when | ||
ip, err := gv.Allocate(host) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// then | ||
Expect(ip).To(Equal("240.0.0.1")) | ||
}) | ||
|
||
It("should give the same ip if when allocating it twice", func() { | ||
// given | ||
host := vips.NewServiceEntry("foo.com") | ||
gv, err := vips.NewGlobalView("240.0.0.0/4") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(gv).ToNot(BeNil()) | ||
|
||
ip, err := gv.Allocate(host) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// when | ||
ip2, err := gv.Allocate(host) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// then | ||
Expect(ip2).To(Equal(ip)) | ||
}) | ||
}) |
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.