Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle existing zone in dynamically addition of DNS records #236

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ test-companion:

.PHONY: test
test: gvproxy test-companion
go test -v ./test
go test -v ./...
18 changes: 15 additions & 3 deletions pkg/services/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,22 @@ func (s *Server) Mux() http.Handler {
return
}

s.handler.zonesLock.Lock()
s.handler.zones = append([]types.Zone{req}, s.handler.zones...)
s.handler.zonesLock.Unlock()
s.addZone(req)
w.WriteHeader(http.StatusOK)
})
return mux
}

func (s *Server) addZone(req types.Zone) {
s.handler.zonesLock.Lock()
defer s.handler.zonesLock.Unlock()
for i, zone := range s.handler.zones {
if zone.Name == req.Name {
req.Records = append(req.Records, zone.Records...)
s.handler.zones[i] = req
return
}
}
cfergeau marked this conversation as resolved.
Show resolved Hide resolved
// No existing zone for req.Name, add new one
s.handler.zones = append(s.handler.zones, req)
}
194 changes: 194 additions & 0 deletions pkg/services/dns/dns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package dns

import (
"net"
"testing"

"github.com/containers/gvisor-tap-vsock/pkg/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestSuite(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "gvisor-tap-vsock dns suit")
}

var _ = Describe("dns add test", func() {
var server *Server

BeforeEach(func() {
server, _ = New(nil, nil, []types.Zone{})
})

It("should add dns zone with ip", func() {
req := types.Zone{
Name: "internal.",
DefaultIP: net.ParseIP("192.168.0.1"),
}
server.addZone(req)

Expect(server.handler.zones).To(Equal([]types.Zone{req}))
})

It("should add dns zone with record", func() {
req := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testiing",
IP: net.ParseIP("192.168.0.2"),
}},
}
server.addZone(req)

Expect(server.handler.zones).To(Equal([]types.Zone{req}))
})

It("should add dns zone with record and ip", func() {
ipReq := types.Zone{
Name: "dynamic.internal.",
DefaultIP: net.ParseIP("192.168.0.1"),
}
recordReq := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testiing",
IP: net.ParseIP("192.168.0.2"),
}},
}
server.addZone(ipReq)
server.addZone(recordReq)

Expect(server.handler.zones).To(Equal([]types.Zone{ipReq, recordReq}))
})

It("should add new zone to existing zone with default ip", func() {
ipReq := types.Zone{
Name: "internal.",
DefaultIP: net.ParseIP("192.168.0.1"),
}
server.addZone(ipReq)
recordReq := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.2"),
}},
}
server.addZone(recordReq)

Expect(server.handler.zones).To(Equal([]types.Zone{{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.2"),
}},
}}))
})

It("should add new zone to existing zone with records", func() {
ipReq := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.2"),
}},
}
server.addZone(ipReq)
recordReq := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.3"),
}},
}
server.addZone(recordReq)

Expect(server.handler.zones).To(Equal([]types.Zone{{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.3"),
}, {
Name: "crc.testing",
IP: net.ParseIP("192.168.0.2"),
}},
}}))
})

It("should add new zone to existing zone with records", func() {
ipReq := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.2"),
}},
}
server.addZone(ipReq)
recordReq := types.Zone{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.3"),
}},
}
server.addZone(recordReq)

Expect(server.handler.zones).To(Equal([]types.Zone{{
Name: "internal.",
Records: []types.Record{{
Name: "crc.testing",
IP: net.ParseIP("192.168.0.3"),
}, {
Name: "crc.testing",
IP: net.ParseIP("192.168.0.2"),
}},
}}))
})

It("should retain the order of zones", func() {
server, _ = New(nil, nil, []types.Zone{
{
Name: "crc.testing.",
DefaultIP: net.ParseIP("192.168.127.2"),
},
{
Name: "testing.",
Records: []types.Record{
{
Name: "host",
IP: net.ParseIP("192.168.127.3"),
},
},
},
})
server.addZone(types.Zone{
Name: "testing.",
Records: []types.Record{
{
Name: "gateway",
IP: net.ParseIP("192.168.127.1"),
},
},
})
Expect(server.handler.zones).To(Equal([]types.Zone{
{
Name: "crc.testing.",
DefaultIP: net.ParseIP("192.168.127.2"),
},
{
Name: "testing.",
Records: []types.Record{
{
Name: "gateway",
IP: net.ParseIP("192.168.127.1"),
},
{
Name: "host",
IP: net.ParseIP("192.168.127.3"),
},
},
},
}))
})
})
52 changes: 50 additions & 2 deletions test/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ var _ = Describe("dns", func() {
Expect(err).ShouldNot(HaveOccurred())

err = client.AddDNS(&types.Zone{
Name: "dynamic.internal.",
DefaultIP: net.ParseIP("192.168.127.253"),
Name: "dynamic.internal.",
Records: []types.Record{
{
Name: "test",
IP: net.ParseIP("192.168.127.253"),
},
},
})
Expect(err).ShouldNot(HaveOccurred())

Expand All @@ -120,4 +125,47 @@ var _ = Describe("dns", func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(string(out)).To(ContainSubstring("Address: 192.168.127.253"))
})

It("should retain order of existing zone", func() {
client := gvproxyclient.New(&http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", sock)
},
},
}, "http://base")
_ = client.AddDNS(&types.Zone{
Name: "dynamic.testing.",
DefaultIP: net.ParseIP("192.168.127.2"),
})
_ = client.AddDNS(&types.Zone{
Name: "testing.",
Records: []types.Record{
{
Name: "host",
IP: net.ParseIP("192.168.127.3"),
},
},
})
out, err := sshExec("nslookup test.dynamic.internal")
Expect(err).ShouldNot(HaveOccurred())
Expect(string(out)).To(ContainSubstring("Address: 192.168.127.2"))

_ = client.AddDNS(&types.Zone{
Name: "testing.",
Records: []types.Record{
{
Name: "gateway",
IP: net.ParseIP("192.168.127.1"),
},
},
})
out, err = sshExec("nslookup *.dynamic.testing")
Expect(err).ShouldNot(HaveOccurred())
Expect(string(out)).To(ContainSubstring("Address: 192.168.127.2"))

out, err = sshExec("nslookup gateway.testing")
Expect(err).ShouldNot(HaveOccurred())
Expect(string(out)).To(ContainSubstring("Address: 192.168.127.1"))
})
})