Skip to content

Commit 725d605

Browse files
authored
feat(dns): embedded DNS server instead of coreDNS (#13124)
## Motivation Add an embedded DNS server that resolves mesh local hostnames. This feature is disabled by default and needs to be enabled with `kuma_dns_proxy_port ` on DPs in universal to a non 0 value or simply with `kuma_runtime_kubernetes_injector_builtin_dns_experimental_proxy =true` on CP in Kubernetes (this leverages sidecar injection). ## Implementation information We leveraged the same system as MeshMetric. The DNS map is exposed in the `_kuma:dynamicconfig` listener, we poll this from the DP at a set frequency and reload the data. We improved the whole configfetcher to make it reusable across different components. We also setup the endpoint so that it works with [etag caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag) while avoids unnecessary reloading and processing, we can therefore refresh the configs more quickly as well. For the moment I added a new matrix for e2e test. If this is stable I'll switch the default. --------- Signed-off-by: Charly Molter <[email protected]>
1 parent 9258e83 commit 725d605

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1302
-341
lines changed

.github/workflows/_e2e.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ env:
1515
E2E_PARAM_CNI_NETWORK_PLUGIN: ${{ fromJSON(inputs.matrix).cniNetworkPlugin }}
1616
E2E_PARAM_ARCH: ${{ fromJSON(inputs.matrix).arch }}
1717
E2E_PARAM_SIDECAR_CONTAINERS: ${{ fromJSON(inputs.matrix).sidecarContainers }}
18+
E2E_PARAM_DP_DNS: ${{ fromJSON(inputs.matrix).dpDNS }}
1819
E2E_PARAM_TARGET: ${{ fromJSON(inputs.matrix).target }}
1920
E2E_PARAM_PARALLELISM: ${{ fromJSON(inputs.matrix).parallelism }}
2021
jobs:
@@ -101,6 +102,9 @@ jobs:
101102
if [[ "${{ env.E2E_PARAM_SIDECAR_CONTAINERS }}" != "" ]]; then
102103
export KUMA_EXPERIMENTAL_SIDECAR_CONTAINERS=true
103104
fi
105+
if [[ "${{ env.E2E_PARAM_DP_DNS }}" != "" ]]; then
106+
export KUMA_EXPERIMENTAL_DP_DNS=true
107+
fi
104108
105109
if [[ "${{ env.E2E_PARAM_TARGET }}" == "" ]]; then
106110
export GINKGO_E2E_LABEL_FILTERS="job-${{ matrix.parallelRunnerId }}"

.github/workflows/_test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ jobs:
6969
{"target":"universal", "k8sVersion":"${{ env.K8S_MAX_VERSION }}"}
7070
],
7171
"include":[
72+
{"dpDNS": "true", "k8sVersion": "${{ env.K8S_MAX_VERSION }}", "target": "kubernetes", "arch": "amd64"},
73+
{"dpDNS": "true", "k8sVersion": "kind", "target": "universal", "arch": "amd64"},
7274
{"sidecarContainers": "sidecarContainers", "k8sVersion": "${{ env.K8S_MAX_VERSION }}", "target": "kubernetes", "arch": "amd64"},
7375
{"k8sVersion": "${{ env.K8S_MIN_VERSION }}", "target": "multizone", "arch": "amd64"},
7476
{"k8sVersion": "${{ env.K8S_MIN_VERSION }}", "target": "kubernetes", "arch": "amd64"},

app/kuma-dp/cmd/context.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/envoy"
1111
kumadp "github.com/kumahq/kuma/pkg/config/app/kuma-dp"
1212
"github.com/kumahq/kuma/pkg/core/runtime/component"
13-
core_xds "github.com/kumahq/kuma/pkg/core/xds"
1413
"github.com/kumahq/kuma/pkg/log"
1514
leader_memory "github.com/kumahq/kuma/pkg/plugins/leader/memory"
1615
)
@@ -25,8 +24,6 @@ type RootContext struct {
2524
LogLevel log.LogLevel
2625
}
2726

28-
var features = []string{core_xds.FeatureTCPAccessLogViaNamedPipe}
29-
3027
// defaultDataplaneTokenGenerator uses only given tokens or paths from the
3128
// config.
3229
func defaultDataplaneTokenGenerator(cfg *kumadp.Config) (component.Component, error) {
@@ -54,7 +51,7 @@ func DefaultRootContext() *RootContext {
5451
config := kumadp.DefaultConfig()
5552
return &RootContext{
5653
ComponentManager: component.NewManager(leader_memory.NewNeverLeaderElector()),
57-
BootstrapGenerator: envoy.NewRemoteBootstrapGenerator(runtime.GOOS, features),
54+
BootstrapGenerator: envoy.NewRemoteBootstrapGenerator(runtime.GOOS),
5855
Config: &config,
5956
BootstrapDynamicMetadata: map[string]string{},
6057
DataplaneTokenGenerator: defaultDataplaneTokenGenerator,

app/kuma-dp/cmd/run.go

+64-37
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cmd
22

33
import (
44
"context"
5+
"net"
56
"os"
67
"path/filepath"
8+
"strconv"
79
"time"
810

911
envoy_bootstrap_v3 "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v3"
@@ -13,6 +15,8 @@ import (
1315
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
1416
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/accesslogs"
1517
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/certificate"
18+
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/configfetcher"
19+
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/dnsproxy"
1620
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/dnsserver"
1721
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/envoy"
1822
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/meshmetrics"
@@ -27,7 +31,9 @@ import (
2731
"github.com/kumahq/kuma/pkg/core/resources/model/rest"
2832
"github.com/kumahq/kuma/pkg/core/runtime/component"
2933
core_xds "github.com/kumahq/kuma/pkg/core/xds"
30-
"github.com/kumahq/kuma/pkg/util/net"
34+
dns_dpapi "github.com/kumahq/kuma/pkg/dns/dpapi"
35+
meshmetric_dpapi "github.com/kumahq/kuma/pkg/plugins/policies/meshmetric/dpapi"
36+
kuma_net "github.com/kumahq/kuma/pkg/util/net"
3137
"github.com/kumahq/kuma/pkg/util/proto"
3238
kuma_version "github.com/kumahq/kuma/pkg/version"
3339
"github.com/kumahq/kuma/pkg/xds/bootstrap/types"
@@ -84,7 +90,7 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
8490

8591
proxyResource, err = readResource(cmd, &cfg.DataplaneRuntime)
8692
if err != nil {
87-
runLog.Error(err, "failed to read policy", "proxyType", cfg.Dataplane.ProxyType)
93+
runLog.Error(err, "failed to read dataplane", "proxyType", cfg.Dataplane.ProxyType)
8894

8995
return err
9096
}
@@ -205,6 +211,12 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
205211
}
206212
opts.AdminPort = bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue()
207213

214+
confFetcher := configfetcher.NewConfigFetcher(
215+
core_xds.MeshMetricsDynamicConfigurationSocketName(cfg.DataplaneRuntime.SocketDir),
216+
time.NewTicker(cfg.DataplaneRuntime.DynamicConfiguration.RefreshInterval.Duration),
217+
cfg.DataplaneRuntime.DynamicConfiguration.RefreshInterval.Duration,
218+
)
219+
208220
if cfg.DNS.Enabled && !cfg.Dataplane.IsZoneProxy() {
209221
dnsOpts := &dnsserver.Opts{
210222
Config: *cfg,
@@ -216,29 +228,49 @@ func newRunCmd(opts kuma_cmd.RunCmdOpts, rootCtx *RootContext) *cobra.Command {
216228
if len(kumaSidecarConfiguration.Networking.CorefileTemplate) > 0 {
217229
dnsOpts.ProvidedCorefileTemplate = kumaSidecarConfiguration.Networking.CorefileTemplate
218230
}
231+
if dnsOpts.Config.DNS.ProxyPort != 0 {
232+
runLog.Info("Running with embedded DNS proxy port", "port", dnsOpts.Config.DNS.ProxyPort)
233+
// Using embedded DNS
234+
dnsproxyServer, err := dnsproxy.NewServer(net.JoinHostPort("localhost", strconv.Itoa(int(dnsOpts.Config.DNS.ProxyPort))))
235+
if err != nil {
236+
return err
237+
}
238+
if err := confFetcher.AddHandler(dns_dpapi.PATH, dnsproxyServer.ReloadMap); err != nil {
239+
return err
240+
}
241+
components = append(components, dnsproxyServer)
242+
} else {
243+
dnsServer, err := dnsserver.New(dnsOpts)
244+
if err != nil {
245+
return err
246+
}
219247

220-
dnsServer, err := dnsserver.New(dnsOpts)
221-
if err != nil {
222-
return err
223-
}
248+
version, err := dnsServer.GetVersion()
249+
if err != nil {
250+
return err
251+
}
224252

225-
version, err := dnsServer.GetVersion()
226-
if err != nil {
227-
return err
253+
rootCtx.BootstrapDynamicMetadata[core_xds.FieldPrefixDependenciesVersion+".coredns"] = version
254+
components = append(components, dnsServer)
228255
}
229-
230-
rootCtx.BootstrapDynamicMetadata[core_xds.FieldPrefixDependenciesVersion+".coredns"] = version
231-
232-
components = append(components, dnsServer)
233256
}
234257

235258
envoyComponent, err := envoy.New(opts)
236259
if err != nil {
237260
return err
238261
}
239262
components = append(components, envoyComponent)
240-
241-
observabilityComponents := setupObservability(kumaSidecarConfiguration, bootstrap, cfg)
263+
components = append(components, component.NewResilientComponent(
264+
runLog.WithName("configfetcher"),
265+
confFetcher,
266+
cfg.Dataplane.ResilientComponentBaseBackoff.Duration,
267+
cfg.Dataplane.ResilientComponentMaxBackoff.Duration,
268+
))
269+
270+
observabilityComponents, err := setupObservability(gracefulCtx, kumaSidecarConfiguration, bootstrap, cfg, confFetcher)
271+
if err != nil {
272+
return err
273+
}
242274
components = append(components, observabilityComponents...)
243275

244276
var readinessReporter *readiness.Reporter
@@ -355,7 +387,7 @@ func getApplicationsToScrape(kumaSidecarConfiguration *types.KumaSidecarConfigur
355387
Name: item.Name,
356388
Path: item.Path,
357389
Port: item.Port,
358-
IsIPv6: net.IsAddressIPv6(item.Address),
390+
IsIPv6: kuma_net.IsAddressIPv6(item.Address),
359391
QueryModifier: metrics.RemoveQueryParameters,
360392
MeshMetricMutator: metrics.AggregatedOtelMutator(),
361393
})
@@ -382,18 +414,16 @@ func writeFile(filename string, data []byte, perm os.FileMode) error {
382414
return os.WriteFile(filename, data, perm)
383415
}
384416

385-
func setupObservability(kumaSidecarConfiguration *types.KumaSidecarConfiguration, bootstrap *envoy_bootstrap_v3.Bootstrap, cfg *kumadp.Config) []component.Component {
386-
resilientComponentBaseBackoff := 5 * time.Second
387-
resilientComponentMaxBackoff := 1 * time.Minute
417+
func setupObservability(ctx context.Context, kumaSidecarConfiguration *types.KumaSidecarConfiguration, bootstrap *envoy_bootstrap_v3.Bootstrap, cfg *kumadp.Config, fetcher *configfetcher.ConfigFetcher) ([]component.Component, error) {
388418
baseApplicationsToScrape := getApplicationsToScrape(kumaSidecarConfiguration, bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue())
389419

390420
accessLogStreamer := component.NewResilientComponent(
391421
runLog.WithName("access-log-streamer"),
392422
accesslogs.NewAccessLogStreamer(
393423
core_xds.AccessLogSocketName(cfg.DataplaneRuntime.SocketDir, cfg.Dataplane.Name, cfg.Dataplane.Mesh),
394424
),
395-
resilientComponentBaseBackoff,
396-
resilientComponentMaxBackoff,
425+
cfg.Dataplane.ResilientComponentBaseBackoff.Duration,
426+
cfg.Dataplane.ResilientComponentMaxBackoff.Duration,
397427
)
398428

399429
openTelemetryProducer := metrics.NewAggregatedMetricsProducer(
@@ -410,21 +440,18 @@ func setupObservability(kumaSidecarConfiguration *types.KumaSidecarConfiguration
410440
openTelemetryProducer,
411441
)
412442

413-
meshMetricsConfigFetcher := component.NewResilientComponent(
414-
runLog.WithName("mesh-metric-config-fetcher"),
415-
meshmetrics.NewMeshMetricConfigFetcher(
416-
core_xds.MeshMetricsDynamicConfigurationSocketName(cfg.DataplaneRuntime.SocketDir),
417-
time.NewTicker(cfg.DataplaneRuntime.DynamicConfiguration.RefreshInterval.Duration),
418-
metricsServer,
419-
openTelemetryProducer,
420-
kumaSidecarConfiguration.Networking.Address,
421-
bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue(),
422-
bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetAddress(),
423-
cfg.Dataplane.DrainTime.Duration,
424-
),
425-
resilientComponentBaseBackoff,
426-
resilientComponentMaxBackoff,
443+
mm := meshmetrics.NewManager(
444+
ctx,
445+
metricsServer,
446+
openTelemetryProducer,
447+
kumaSidecarConfiguration.Networking.Address,
448+
bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetPortValue(),
449+
bootstrap.GetAdmin().GetAddress().GetSocketAddress().GetAddress(),
450+
cfg.Dataplane.DrainTime.Duration,
427451
)
428-
429-
return []component.Component{accessLogStreamer, meshMetricsConfigFetcher, metricsServer}
452+
err := fetcher.AddHandler(meshmetric_dpapi.PATH, mm.OnChange)
453+
if err != nil {
454+
return nil, err
455+
}
456+
return []component.Component{accessLogStreamer, metricsServer, mm}, nil
430457
}

app/kuma-dp/cmd/run_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
envoy_bootstrap_v3 "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v3"
1717
. "github.com/onsi/ginkgo/v2"
1818
. "github.com/onsi/gomega"
19+
"github.com/prometheus/client_golang/prometheus"
1920

2021
"github.com/kumahq/kuma/app/kuma-dp/pkg/dataplane/envoy"
2122
kuma_cmd "github.com/kumahq/kuma/pkg/cmd"
@@ -36,6 +37,7 @@ var _ = Describe("run", func() {
3637
var tmpDir string
3738

3839
BeforeEach(func() {
40+
prometheus.DefaultRegisterer = prometheus.NewRegistry()
3941
ctx, cancel = context.WithCancel(context.Background())
4042
var err error
4143
tmpDir, err = os.MkdirTemp("", "")

0 commit comments

Comments
 (0)