Skip to content

Commit feb81fe

Browse files
authored
refactored local dns proxy. (#348)
* refactored local dns proxy. * refactored local dns proxy. * refactored local dns proxy. * refactored local dns proxy.
1 parent 31d4390 commit feb81fe

File tree

23 files changed

+795
-99
lines changed

23 files changed

+795
-99
lines changed

charts/fsm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ The following table lists the configurable parameters of the fsm chart and their
349349
| fsm.injector.resource | object | `{"limits":{"cpu":"1","memory":"512M"},"requests":{"cpu":"0.5","memory":"128M"}}` | Sidecar injector's container resource parameters |
350350
| fsm.injector.tolerations | list | `[]` | Node tolerations applied to control plane pods. The specified tolerations allow pods to schedule onto nodes with matching taints. |
351351
| fsm.injector.webhookTimeoutSeconds | int | `20` | Mutating webhook timeout |
352-
| fsm.localDNSProxy | object | `{"enable":false,"generateIPv6BasedOnIPv4":false,"searchesWithNamespace":true,"searchesWithTrustDomain":true,"wildcard":{"enable":false,"ips":[{"ipv4":"127.0.0.2"}]}}` | Local DNS Proxy improves the performance of your computer by caching the responses coming from your DNS servers |
352+
| fsm.localDNSProxy | object | `{"enable":false,"generateIPv6BasedOnIPv4":false,"searchesWithNamespace":true,"searchesWithTrustDomain":true,"wildcard":{"enable":false,"ips":[{"ipv4":"127.0.0.2"}],"los":[]}}` | Local DNS Proxy improves the performance of your computer by caching the responses coming from your DNS servers |
353353
| fsm.localProxyMode | string | `"Localhost"` | Proxy mode for the proxy sidecar. Acceptable values are ['Localhost', 'PodIP'] |
354354
| fsm.maxDataPlaneConnections | int | `0` | Sets the max data plane connections allowed for an instance of fsm-controller, set to 0 to not enforce limits |
355355
| fsm.meshName | string | `"fsm"` | Identifier for the instance of a service mesh within a cluster |

charts/fsm/templates/fsm-deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ spec:
104104
containerPort: 9091
105105
- name: webhook
106106
containerPort: 9443
107+
- name: dns-proxy
108+
containerPort: 15053
109+
protocol: UDP
107110
command: ['/fsm-controller']
108111
args: [
109112
"--verbosity", "{{.Values.fsm.controllerLogLevel}}",

charts/fsm/templates/fsm-service.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ spec:
2020
- name: webhook
2121
port: 9443
2222
targetPort: 9443
23+
- name: dns-proxy
24+
port: 53
25+
targetPort: 15053
26+
protocol: UDP
2327
selector:
2428
app: fsm-controller

charts/fsm/values.schema.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@
12511251
"description": "The wildcard schema for local DNS Proxy",
12521252
"required": [
12531253
"enable",
1254+
"los",
12541255
"ips"
12551256
],
12561257
"properties": {
@@ -1260,6 +1261,30 @@
12601261
"title": "The enable schema for wildcard",
12611262
"description": "Indicates whether wildcard is enabled or not"
12621263
},
1264+
"los": {
1265+
"$id": "#/properties/fsm/properties/localDNSProxy/properties/wildcard/properties/los",
1266+
"type": "array",
1267+
"title": "The los schema for wildcard",
1268+
"items": {
1269+
"type": "object",
1270+
"required": [
1271+
"ipv4"
1272+
],
1273+
"properties": {
1274+
"ipv4": {
1275+
"$id": "#/properties/fsm/properties/localDNSProxy/properties/wildcard/properties/los/properties/ipv4",
1276+
"type": "string",
1277+
"title": "ipv4",
1278+
"pattern": "((?:\\d{1,3}\\.){3}\\d{1,3})$"
1279+
},
1280+
"ipv6": {
1281+
"$id": "#/properties/fsm/properties/localDNSProxy/properties/wildcard/properties/los/properties/ipv6",
1282+
"type": "string",
1283+
"title": "ipv6"
1284+
}
1285+
}
1286+
}
1287+
},
12631288
"ips": {
12641289
"$id": "#/properties/fsm/properties/localDNSProxy/properties/wildcard/properties/ips",
12651290
"type": "array",

charts/fsm/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ fsm:
510510
generateIPv6BasedOnIPv4: false
511511
wildcard:
512512
enable: false
513+
los: []
513514
ips:
514515
- ipv4: 127.0.0.2
515516
# -- Sets the max data plane connections allowed for an instance of fsm-controller, set to 0 to not enforce limits

cmd/fsm-bootstrap/crds/config.flomesh.io_meshconfigs.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,9 +2224,29 @@ spec:
22242224
- ipv4
22252225
type: object
22262226
type: array
2227+
los:
2228+
description: LOs defines loopback addresses for resolve
2229+
DN.
2230+
items:
2231+
description: ResolveAddr is the type to represent FSM's
2232+
Resolve Addr configuration.
2233+
properties:
2234+
ipv4:
2235+
description: IPv4 defines a ipv4 address for resolve
2236+
DN.
2237+
type: string
2238+
ipv6:
2239+
description: IPv6 defines a ipv6 address for resolve
2240+
DN.
2241+
type: string
2242+
required:
2243+
- ipv4
2244+
type: object
2245+
type: array
22272246
required:
22282247
- enable
22292248
- ips
2249+
- los
22302250
type: object
22312251
required:
22322252
- enable

cmd/fsm-controller/fsm-controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/go-logr/zerologr"
1717

18+
"github.com/flomesh-io/fsm/pkg/dns"
1819
connectorClientset "github.com/flomesh-io/fsm/pkg/gen/client/connector/clientset/versioned"
1920
machineClientset "github.com/flomesh-io/fsm/pkg/gen/client/machine/clientset/versioned"
2021
policyAttachmentClientset "github.com/flomesh-io/fsm/pkg/gen/client/policyattachment/clientset/versioned"
@@ -371,6 +372,8 @@ func main() {
371372
events.GenericEventRecorder().FatalEvent(err, events.InitializationError, "Error starting the validating webhook server")
372373
}
373374

375+
dns.Init(cfg)
376+
374377
version.SetMetric()
375378

376379
// Initialize FSM's http service server
@@ -399,6 +402,7 @@ func main() {
399402
debugConfig := debugger.NewDebugConfig(certManager, meshCatalog, kubeConfig, kubeClient, cfg, k8sClient, msgBroker)
400403
go debugConfig.StartDebugServerConfigListener(background.DebugHandlers, stop)
401404

405+
go dns.WatchAndUpdateLocalDNSProxy(msgBroker, stop)
402406
// Start the k8s pod watcher that updates corresponding k8s secrets
403407
go k8s.WatchAndUpdateProxyBootstrapSecret(kubeClient, msgBroker, stop)
404408
// Start the global log level watcher that updates the log level dynamically

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ require (
9393
github.com/hashicorp/consul/api v1.21.0
9494
github.com/hashicorp/golang-lru/v2 v2.0.7
9595
github.com/hudl/fargo v1.4.0
96+
github.com/jonboulle/clockwork v0.2.2
9697
github.com/kelseyhightower/envconfig v1.4.0
98+
github.com/miekg/dns v1.1.58
9799
github.com/nacos-group/nacos-sdk-go/v2 v2.2.5
98100
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
99101
github.com/pkg/errors v0.9.1
@@ -338,7 +340,6 @@ require (
338340
github.com/mbilski/exhaustivestruct v1.1.0 // indirect
339341
github.com/mdlayher/netlink v1.6.0 // indirect
340342
github.com/mdlayher/socket v0.1.1 // indirect
341-
github.com/miekg/dns v1.1.58 // indirect
342343
github.com/mitchellh/cli v1.1.5 // indirect
343344
github.com/mitchellh/copystructure v1.2.0 // indirect
344345
github.com/mitchellh/go-homedir v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,8 @@ github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
17501750
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
17511751
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
17521752
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
1753+
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
1754+
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
17531755
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
17541756
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
17551757
github.com/josharian/native v0.0.0-20200817173448-b6b71def0850/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=

pkg/apis/config/v1alpha3/mesh_config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ type WildcardDN struct {
101101
// Enable defines a boolean indicating if wildcard are enabled for local DNS Proxy.
102102
Enable bool `json:"enable"`
103103

104+
// LOs defines loopback addresses for resolve DN.
105+
LOs []*ResolveAddr `json:"los"`
106+
104107
// IPs defines ip addresses for resolve DN.
105-
IPs []ResolveAddr `json:"ips"`
108+
IPs []*ResolveAddr `json:"ips"`
106109
}
107110

108111
// ResolveDN is the type to represent FSM's Resolve DN configuration.
@@ -111,7 +114,7 @@ type ResolveDN struct {
111114
DN string `json:"dn"`
112115

113116
// IPs defines ip addresses for resolve DN.
114-
IPs []ResolveAddr `json:"ips"`
117+
IPs []*ResolveAddr `json:"ips"`
115118
}
116119

117120
// LocalDNSProxy is the type to represent FSM's local DNS proxy configuration.

pkg/apis/config/v1alpha3/zz_generated.deepcopy.go

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/catalog/outbound_traffic_policies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (mc *MeshCatalog) GetOutboundMeshTrafficPolicy(downstreamIdentity identity.
7171
}
7272
}
7373

74-
if mc.configurator.IsLocalDNSProxyEnabled() && !mc.configurator.IsWildcardDNSProxyEnabled() {
74+
if mc.configurator.IsLocalDNSProxyEnabled() {
7575
if !existIntraEndpoints {
7676
resolvableIPSet := mapset.NewSet()
7777
for _, endp := range endpoints {

pkg/constants/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ const (
7272
// SidecarPrometheusInboundListenerPort is Sidecar's inbound listener port number for prometheus
7373
SidecarPrometheusInboundListenerPort = 15010
7474

75+
// FSMDNSProxyPort is the dns proxy listener port.
76+
FSMDNSProxyPort = uint32(15053)
77+
7578
// InjectorWebhookPort is the port on which the sidecar injection webhook listens
7679
InjectorWebhookPort = 9090
7780

pkg/dns/config.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dns
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/jonboulle/clockwork"
7+
8+
configv1alpha3 "github.com/flomesh-io/fsm/pkg/apis/config/v1alpha3"
9+
"github.com/flomesh-io/fsm/pkg/configurator"
10+
)
11+
12+
// WallClock is the wall clock
13+
var WallClock = clockwork.NewRealClock()
14+
15+
// Config holds the configuration parameters
16+
type Config struct {
17+
cfg configurator.Configurator
18+
CustomDNSRecords []string // manual custom dns entries
19+
}
20+
21+
// GetNameservers nameservers to forward queries to
22+
func (c *Config) GetNameservers() []string {
23+
var nameservers []string
24+
if upstream := c.cfg.GetLocalDNSProxyPrimaryUpstream(); len(upstream) > 0 {
25+
nameservers = append(nameservers, fmt.Sprintf("%s:53", upstream))
26+
}
27+
if upstream := c.cfg.GetLocalDNSProxySecondaryUpstream(); len(upstream) > 0 {
28+
nameservers = append(nameservers, fmt.Sprintf("%s:53", upstream))
29+
}
30+
return nameservers
31+
}
32+
33+
func (c *Config) IsWildcard() bool {
34+
return c.cfg.IsWildcardDNSProxyEnabled()
35+
}
36+
37+
func (c *Config) GetWildcardResolveDB() []*configv1alpha3.ResolveAddr {
38+
return c.cfg.GetMeshConfig().Spec.Sidecar.LocalDNSProxy.Wildcard.IPs
39+
}
40+
41+
func (c *Config) GetLoopbackResolveDB() []*configv1alpha3.ResolveAddr {
42+
return c.cfg.GetMeshConfig().Spec.Sidecar.LocalDNSProxy.Wildcard.LOs
43+
}
44+
45+
func (c *Config) GenerateIPv6BasedOnIPv4() bool {
46+
return c.cfg.GenerateIPv6BasedOnIPv4()
47+
}
48+
49+
// GetNXDomain response to blocked queries with a NXDOMAIN
50+
func (c *Config) GetNXDomain() bool {
51+
return false
52+
}
53+
54+
// GetInterval concurrency interval for lookups in miliseconds
55+
func (c *Config) GetInterval() int {
56+
return 200
57+
}
58+
59+
// GetTimeout query timeout for dns lookups in seconds
60+
func (c *Config) GetTimeout() int {
61+
return 5
62+
}

0 commit comments

Comments
 (0)