Repository navigation
Expand file tree
/
Copy pathresolver.go
More file actions
443 lines (388 loc) · 15.2 KB
/
Copy pathresolver.go
File metadata and controls
443 lines (388 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package network
//docgen:jsonschema
import (
"errors"
"fmt"
"net/netip"
"slices"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/value"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-pointer"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/config/container"
"github.com/siderolabs/talos/pkg/machinery/config/internal/registry"
"github.com/siderolabs/talos/pkg/machinery/config/types/meta"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/config/validation"
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
)
// ResolverKind is a ResolverConfig document kind.
const ResolverKind = "ResolverConfig"
func init() {
registry.Register(ResolverKind, func(version string) config.Document {
switch version {
case "v1alpha1": //nolint:goconst
return &ResolverConfigV1Alpha1{}
default:
return nil
}
})
}
// Check interfaces.
var (
_ config.NetworkResolverConfig = &ResolverConfigV1Alpha1{}
_ config.NetworkHostDNSConfig = &ResolverConfigV1Alpha1{}
_ config.Validator = &ResolverConfigV1Alpha1{}
_ container.V1Alpha1ConflictValidator = &ResolverConfigV1Alpha1{}
)
// ResolverConfigV1Alpha1 is a config document to configure DNS resolving.
//
// examples:
// - value: exampleResolverConfigV1Alpha1()
// - value: exampleResolverConfigV1Alpha2()
// - value: exampleResolverConfigV1Alpha3()
// - value: exampleResolverConfigV1Alpha4()
// - value: exampleResolverConfigV1Alpha5()
// alias: ResolverConfig
// schemaRoot: true
// schemaMeta: v1alpha1/ResolverConfig
type ResolverConfigV1Alpha1 struct {
meta.Meta `yaml:",inline"`
// description: |
// A list of nameservers (DNS servers) to use for resolving domain names.
//
// Nameservers are used to resolve domain names on the host, and they are also
// propagated to Kubernetes DNS (CoreDNS) for use by pods running on the cluster.
//
// This overrides any nameservers obtained via DHCP or platform configuration.
// Default configuration is to use 1.1.1.1, 8.8.8.8 (IPv4), and their IPv6
// counterparts 2606:4700:4700::1111, 2001:4860:4860::8888 as nameservers.
ResolverNameservers []NameserverConfig `yaml:"nameservers,omitempty"`
// description: |
// Configuration for search domains (in /etc/resolv.conf).
//
// The default is to derive search domains from the hostname FQDN.
ResolverSearchDomains SearchDomainsConfig `yaml:"searchDomains,omitempty"`
// description: |
// Configuration for host DNS resolver.
//
// This configures a local DNS caching resolver on the host to improve DNS resolution performance and reliability.
ResolverHostDNS HostDNSConfig `yaml:"hostDNS,omitempty"`
}
// NameserverConfig represents a single nameserver configuration.
type NameserverConfig struct {
// description: |
// The IP address of the nameserver.
// examples:
// - value: >
// meta.Addr{netip.MustParseAddr("10.0.0.1")}
// schema:
// type: string
// pattern: ^[0-9a-f.:]+$
Address meta.Addr `yaml:"address"`
// description: |
// A DNS protocol to use.
//
// The default protocol is plain DNS (`Do53`) (DNS over TCP/UDP). Set this to
// `DoT` to use DNS over TLS (RFC 7858) on TCP port 853, or `DoH` to use DNS
// over HTTPS (RFC 8484) on TCP port 443 with the `/dns-query` URL path. Both
// `DoT` and `DoH` deliver encrypted queries to this nameserver.
//
// Note: encrypted DNS protocols require a correct system clock to validate
// certificates. If NTP is configured with hostnames that need to be resolved
// through DoT/DoH, the boot may stall: NTP needs DNS, and TLS needs valid
// time. Either rely on the hardware clock, configure NTP servers by IP, or
// keep at least one plain-DNS fallback nameserver.
// values:
// - "Do53"
// - "DoT"
// - "DoH"
Protocol nethelpers.DNSProtocol `yaml:"protocol,omitempty"`
// description: |
// TLS server name to validate the nameserver certificate against.
//
// This field should be set if the protocol is set to `DoT` or `DoH`.
// The value is used both as the SNI sent during the TLS handshake and as the
// name verified against the server certificate. For `DoH`, it is also used as
// the host portion of the request URL (`https://<tlsServerName>/dns-query`)
// while the connection itself is established to the configured `address`.
//
// examples:
// - value: >
// "dns1.example.com"
TLSServerName string `yaml:"tlsServerName,omitempty"`
}
// SearchDomainsConfig represents search domains configuration.
type SearchDomainsConfig struct {
// description: |
// A list of search domains to be used for DNS resolution.
//
// Search domains are appended to unqualified domain names during DNS resolution.
// For example, if "example.com" is a search domain and a user tries to resolve
// "host", the system will attempt to resolve "host.example.com".
//
// If set, this overrides any search domains obtained via DHCP or platform configuration.
// An empty list (`domains: []`) clears search domains obtained from DHCP or platform,
// while leaving this field unset inherits them.
// The default configuration derives the search domain from the hostname FQDN.
// schema:
// type: array
// items:
// type: string
SearchDomains SearchDomainList `yaml:"domains,omitempty" talos:"omitonlyifnil" merge:"replace"`
// description: |
// Disable default search domain configuration from hostname FQDN.
//
// When set to true, the system will not derive search domains from the hostname FQDN.
// This allows for a custom configuration of search domains without any defaults.
SearchDisableDefault *bool `yaml:"disableDefault,omitempty"`
}
// SearchDomainList is a list of DNS search domains.
//
// A nil list means that search domains are not configured (and are inherited from
// other configuration layers), while an explicitly empty list clears search domains
// obtained from DHCP or platform.
type SearchDomainList []string
// IsZero implements yaml.IsZeroer.
//
// Only a nil list is considered zero, so that an explicitly empty list survives
// encoding with the `omitempty` tag instead of being dropped.
func (l SearchDomainList) IsZero() bool {
return l == nil
}
// HostDNSConfig represents host DNS configuration.
type HostDNSConfig struct {
// description: |
// Enable host DNS caching resolver.
//
// When enabled, a local DNS caching resolver is deployed on the host to improve DNS resolution performance and reliability.
// Upstream DNS servers for the host resolver are configured using the `nameservers` field in this config document.
HostDNSEnabled *bool `yaml:"enabled,omitempty"`
// description: |
// Use the host DNS resolver as upstream for Kubernetes CoreDNS pods.
//
// When enabled, CoreDNS pods use host DNS server as the upstream DNS (instead of
// using configured upstream DNS resolvers directly).
HostDNSForwardKubeDNSToHost *bool `yaml:"forwardKubeDNSToHost,omitempty"`
// description: |
// Resolve member hostnames using the host DNS resolver.
//
// When enabled, cluster member hostnames and node names are resolved using the host DNS resolver.
// This requires service discovery to be enabled.
HostDNSResolveMemberNames *bool `yaml:"resolveMemberNames,omitempty"`
// description: |
// Log level for the host DNS resolver.
//
// The default log level is `debug`, which logs every DNS request and response.
// Raise the log level to reduce the verbosity of the host DNS resolver logs.
// values:
// - "debug"
// - "info"
// - "warn"
// - "error"
// examples:
// - value: >
// "warn"
HostDNSLogLevel string `yaml:"logLevel,omitempty"`
}
// NewResolverConfigV1Alpha1 creates a new ResolverConfig config document.
func NewResolverConfigV1Alpha1() *ResolverConfigV1Alpha1 {
return &ResolverConfigV1Alpha1{
Meta: meta.Meta{
MetaKind: ResolverKind,
MetaAPIVersion: "v1alpha1",
},
}
}
func exampleResolverConfigV1Alpha1() *ResolverConfigV1Alpha1 {
cfg := NewResolverConfigV1Alpha1()
cfg.ResolverNameservers = []NameserverConfig{
{
Address: meta.Addr{Addr: netip.MustParseAddr("1.1.1.1")},
},
{
Address: meta.Addr{Addr: netip.MustParseAddr("ff08::1")},
},
}
cfg.ResolverSearchDomains = SearchDomainsConfig{
SearchDomains: SearchDomainList{"example.com"},
}
return cfg
}
func exampleResolverConfigV1Alpha2() *ResolverConfigV1Alpha1 {
cfg := NewResolverConfigV1Alpha1()
cfg.ResolverSearchDomains = SearchDomainsConfig{
SearchDisableDefault: new(true),
}
return cfg
}
func exampleResolverConfigV1Alpha3() *ResolverConfigV1Alpha1 {
cfg := NewResolverConfigV1Alpha1()
cfg.ResolverHostDNS = HostDNSConfig{
HostDNSEnabled: new(true),
HostDNSForwardKubeDNSToHost: new(true),
HostDNSResolveMemberNames: new(true),
}
return cfg
}
func exampleResolverConfigV1Alpha4() *ResolverConfigV1Alpha1 {
cfg := NewResolverConfigV1Alpha1()
cfg.ResolverNameservers = []NameserverConfig{
{
Address: meta.Addr{Addr: netip.MustParseAddr("9.9.9.9")},
Protocol: nethelpers.DNSProtocolDNSOverTLS,
TLSServerName: "dns.quad9.net",
},
{
Address: meta.Addr{Addr: netip.MustParseAddr("2620:fe::fe")},
Protocol: nethelpers.DNSProtocolDNSOverTLS,
TLSServerName: "dns.quad9.net",
},
}
return cfg
}
func exampleResolverConfigV1Alpha5() *ResolverConfigV1Alpha1 {
cfg := NewResolverConfigV1Alpha1()
cfg.ResolverNameservers = []NameserverConfig{
{
Address: meta.Addr{Addr: netip.MustParseAddr("1.1.1.1")},
Protocol: nethelpers.DNSProtocolDNSOverHTTP,
TLSServerName: "cloudflare-dns.com",
},
{
Address: meta.Addr{Addr: netip.MustParseAddr("2606:4700:4700::1111")},
Protocol: nethelpers.DNSProtocolDNSOverHTTP,
TLSServerName: "cloudflare-dns.com",
},
}
return cfg
}
// Clone implements config.Document interface.
func (s *ResolverConfigV1Alpha1) Clone() config.Document {
return s.DeepCopy()
}
// V1Alpha1ConflictValidate implements container.V1Alpha1ConflictValidator interface.
func (s *ResolverConfigV1Alpha1) V1Alpha1ConflictValidate(v1alpha1Cfg *v1alpha1.Config) error {
if v1alpha1Cfg.SearchDomains().IsPresent() {
return errors.New(".machine.network.searchDomains is already set in v1alpha1 config")
}
if v1alpha1Cfg.Resolvers() != nil {
return errors.New(".machine.network.nameservers is already set in v1alpha1 config")
}
if v1alpha1Cfg.DisableSearchDomain() {
return errors.New(".machine.network.disableSearchDomain is already set in v1alpha1 config")
}
if !value.IsZero(s.ResolverHostDNS) {
if v1alpha1Cfg.NetworkHostDNSConfig() != nil {
return errors.New(".machine.features.hostDNS is already set in v1alpha1 config")
}
}
return nil
}
// Validate implements config.Validator interface.
//
//nolint:gocyclo,cyclop
func (s *ResolverConfigV1Alpha1) Validate(validation.RuntimeMode, ...validation.Option) ([]string, error) {
var (
warnings []string
errs error
)
if !value.IsZero(s.ResolverHostDNS) {
if !s.HostDNSEnabled() {
if s.ForwardKubeDNSToHost() {
errs = errors.Join(errs, errors.New("hostDNS.forwardKubeDNSToHost cannot be enabled when hostDNS.enabled is false"))
}
if s.ResolveMemberNames() {
errs = errors.Join(errs, errors.New("hostDNS.resolveMemberNames cannot be enabled when hostDNS.enabled is false"))
}
if s.LogLevel() != "" {
errs = errors.Join(errs, errors.New("hostDNS.logLevel cannot be set when hostDNS.enabled is false"))
}
}
switch s.LogLevel() {
case "", "debug", "info", "warn", "error":
default:
errs = errors.Join(errs, fmt.Errorf("hostDNS.logLevel %q is invalid, must be one of: debug, info, warn, error", s.LogLevel()))
}
}
nonRegularDNS := 0
for idx, ns := range s.ResolverNameservers {
switch ns.Protocol {
case nethelpers.DNSProtocolDNSOverTLS:
nonRegularDNS++
if ns.TLSServerName == "" {
errs = errors.Join(errs, fmt.Errorf("tlsServerName must be set when protocol is DoT: entry %d", idx))
}
case nethelpers.DNSProtocolDNSOverHTTP:
nonRegularDNS++
if ns.TLSServerName == "" {
errs = errors.Join(errs, fmt.Errorf("tlsServerName must be set when protocol is DoH: entry %d", idx))
}
case nethelpers.DNSProtocolDefault:
if ns.TLSServerName != "" {
errs = errors.Join(errs, fmt.Errorf("tlsServerName must be empty when protocol is Do53: entry %d", idx))
}
default:
errs = errors.Join(errs, fmt.Errorf("unsupported DNS protocol: entry %d", idx))
}
if !ns.Address.IsValid() {
errs = errors.Join(errs, fmt.Errorf("nameserver address must be a valid IP: entry %d", idx))
}
}
// this is a warning (and not an error) to keep accepting machine configuration which was valid before
for _, domain := range s.ResolverSearchDomains.SearchDomains {
if err := nethelpers.ValidateDNSNameChars(domain); err != nil {
warnings = append(warnings, fmt.Sprintf("searchDomains: %s, it will be ignored", err))
}
}
if nonRegularDNS > 0 && nonRegularDNS == len(s.ResolverNameservers) {
warnings = append(
warnings,
"all configured nameservers use encrypted DNS (DoT or DoH): validating certificates requires a correct system clock, "+
"so boot may stall when NTP servers are configured by hostname; consider keeping at least one plain-DNS fallback "+
"or configuring NTP servers by IP address",
)
}
return warnings, errs
}
// Resolvers implements NetworkResolverConfig interface.
func (s *ResolverConfigV1Alpha1) Resolvers() []config.NetworkResolver {
return xslices.Map(s.ResolverNameservers, func(ns NameserverConfig) config.NetworkResolver {
return config.NetworkResolver{
Addr: ns.Address.Addr,
Protocol: ns.Protocol,
TLSServerName: ns.TLSServerName,
}
})
}
// SearchDomains implements NetworkResolverConfig interface.
func (s *ResolverConfigV1Alpha1) SearchDomains() optional.Optional[[]string] {
if s.ResolverSearchDomains.SearchDomains == nil {
return optional.None[[]string]()
}
return optional.Some([]string(slices.Clone(s.ResolverSearchDomains.SearchDomains)))
}
// DisableSearchDomain implements NetworkResolverConfig interface.
func (s *ResolverConfigV1Alpha1) DisableSearchDomain() bool {
return pointer.SafeDeref(s.ResolverSearchDomains.SearchDisableDefault)
}
// HostDNSEnabled implements NetworkHostDNSConfig interface.
func (s *ResolverConfigV1Alpha1) HostDNSEnabled() bool {
return pointer.SafeDeref(s.ResolverHostDNS.HostDNSEnabled)
}
// ForwardKubeDNSToHost implements NetworkHostDNSConfig interface.
func (s *ResolverConfigV1Alpha1) ForwardKubeDNSToHost() bool {
return pointer.SafeDeref(s.ResolverHostDNS.HostDNSForwardKubeDNSToHost)
}
// ResolveMemberNames implements NetworkHostDNSConfig interface.
func (s *ResolverConfigV1Alpha1) ResolveMemberNames() bool {
return pointer.SafeDeref(s.ResolverHostDNS.HostDNSResolveMemberNames)
}
// LogLevel implements NetworkHostDNSConfig interface.
func (s *ResolverConfigV1Alpha1) LogLevel() string {
return s.ResolverHostDNS.HostDNSLogLevel
}