Skip to content

Commit

Permalink
refactor(kuma-dp): put Corefile into embedded FS (#8117)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Beaumont <mjboamail@gmail.com>
  • Loading branch information
michaelbeaumont authored Oct 24, 2023
1 parent b478866 commit 218a57a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
15 changes: 15 additions & 0 deletions app/kuma-dp/pkg/dataplane/dnsserver/Corefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.:{{ .CoreDNSPort }} {
forward . 127.0.0.1:{{ .EnvoyDNSPort }}
# We want all requests to be sent to the Envoy DNS Filter, unsuccessful responses should be forwarded to the original DNS server.
# For example: requests other than A, AAAA and SRV will return NOTIMP when hitting the envoy filter and should be sent to the original DNS server.
# Codes from: https://github.com/miekg/dns/blob/master/msg.go#L138
alternate NOTIMP,FORMERR,NXDOMAIN,SERVFAIL,REFUSED . /etc/resolv.conf
prometheus localhost:{{ .PrometheusPort }}
errors
}

.:{{ .CoreDNSEmptyPort }} {
template ANY ANY . {
rcode NXDOMAIN
}
}
4 changes: 4 additions & 0 deletions app/kuma-dp/pkg/dataplane/dnsserver/config_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dnsserver

import (
"embed"
"os"
"path/filepath"

Expand All @@ -9,6 +10,9 @@ import (
kuma_dp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
)

//go:embed Corefile
var config embed.FS

func GenerateConfigFile(cfg kuma_dp.DNS, config []byte) (string, error) {
configFile := filepath.Join(cfg.ConfigDir, "Corefile")
if err := writeFile(configFile, config, 0o600); err != nil {
Expand Down
23 changes: 5 additions & 18 deletions app/kuma-dp/pkg/dataplane/dnsserver/dnsserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ type Opts struct {
Quit chan struct{}
}

// DefaultCoreFileTemplate defines the template to use to configure coreDNS to use the envoy dns filter.
const DefaultCoreFileTemplate = `.:{{ .CoreDNSPort }} {
forward . 127.0.0.1:{{ .EnvoyDNSPort }}
# We want all requests to be sent to the Envoy DNS Filter, unsuccessful responses should be forwarded to the original DNS server.
# For example: requests other than A, AAAA and SRV will return NOTIMP when hitting the envoy filter and should be sent to the original DNS server.
# Codes from: https://github.com/miekg/dns/blob/master/msg.go#L138
alternate NOTIMP,FORMERR,NXDOMAIN,SERVFAIL,REFUSED . /etc/resolv.conf
prometheus localhost:{{ .PrometheusPort }}
errors
}
.:{{ .CoreDNSEmptyPort }} {
template ANY ANY . {
rcode NXDOMAIN
}
}`

func lookupDNSServerPath(configuredPath string) (string, error) {
return files.LookupBinaryPath(
files.LookupInPath(configuredPath),
Expand Down Expand Up @@ -108,7 +91,11 @@ func (s *DNSServer) Start(stop <-chan struct{}) error {

tmpl = t
} else {
t, err := template.New("Corefile").Parse(DefaultCoreFileTemplate)
corefile, err := config.ReadFile("Corefile")
if err != nil {
return errors.Wrap(err, "couldn't open embedded Corefile")
}
t, err := template.New("Corefile").Parse(string(corefile))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion app/kuma-dp/pkg/dataplane/dnsserver/dnsserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ var _ = Describe("DNS Server", func() {
template ANY ANY . {
rcode NXDOMAIN
}
}`))
}
`))
}))

It("should return an error if DNS Server crashes", test.Within(10*time.Second, func() {
Expand Down

0 comments on commit 218a57a

Please sign in to comment.