-
Notifications
You must be signed in to change notification settings - Fork 457
/
v1alpha1_validation.go
796 lines (629 loc) · 26.1 KB
/
v1alpha1_validation.go
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// 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 v1alpha1
import (
"encoding/base64"
"errors"
"fmt"
"net"
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/talos-systems/go-debug"
talosnet "github.com/talos-systems/net"
"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/machinery/kubelet"
"github.com/talos-systems/talos/pkg/machinery/nethelpers"
)
var (
// General.
// ErrRequiredSection denotes a section is required.
ErrRequiredSection = errors.New("required config section")
// ErrRequiredSectionOptions denotes at least one section is required.
ErrRequiredSectionOptions = errors.New("required either config section to be set")
// ErrInvalidVersion denotes that the config file version is invalid.
ErrInvalidVersion = errors.New("invalid config version")
// ErrMutuallyExclusive denotes that config sections are mutually exclusive.
ErrMutuallyExclusive = errors.New("config sections are mutually exclusive")
// ErrEmpty denotes that config section should have at least a single field defined.
ErrEmpty = errors.New("config section should contain at least one field")
// Security.
// ErrInvalidCert denotes that the certificate specified is invalid.
ErrInvalidCert = errors.New("certificate is invalid")
// ErrInvalidCertType denotes that the certificate type is invalid.
ErrInvalidCertType = errors.New("certificate type is invalid")
// Services.
// ErrUnsupportedCNI denotes that the specified CNI is invalid.
ErrUnsupportedCNI = errors.New("unsupported CNI driver")
// ErrInvalidTrustdToken denotes that a trustd token has not been specified.
ErrInvalidTrustdToken = errors.New("trustd token is invalid")
// Networking.
// ErrInvalidAddress denotes that a bad address was provided.
ErrInvalidAddress = errors.New("invalid network address")
)
// NetworkDeviceCheck defines the function type for checks.
type NetworkDeviceCheck func(*Device, map[string]string) ([]string, error)
// Validate implements the config.Provider interface.
//
//nolint:gocyclo,cyclop
func (c *Config) Validate(mode config.RuntimeMode, options ...config.ValidationOption) ([]string, error) {
var (
warnings []string
result *multierror.Error
)
opts := config.NewValidationOptions(options...)
if c.MachineConfig == nil {
result = multierror.Append(result, errors.New("machine instructions are required"))
return nil, result.ErrorOrNil()
}
if err := c.ClusterConfig.Validate(); err != nil {
result = multierror.Append(result, err)
}
if mode.RequiresInstall() {
if c.MachineConfig.MachineInstall == nil {
result = multierror.Append(result, fmt.Errorf("install instructions are required in %q mode", mode))
} else {
if opts.Local {
if c.MachineConfig.MachineInstall.InstallDisk == "" && len(c.MachineConfig.MachineInstall.DiskMatchers()) == 0 {
result = multierror.Append(result, fmt.Errorf("either install disk or diskSelector should be defined"))
}
} else {
disk, err := c.MachineConfig.MachineInstall.Disk()
if err != nil {
result = multierror.Append(result, err)
} else {
if disk == "" {
result = multierror.Append(result, fmt.Errorf("an install disk is required in %q mode", mode))
}
if _, err := os.Stat(disk); os.IsNotExist(err) {
result = multierror.Append(result, fmt.Errorf("specified install disk does not exist: %q", c.MachineConfig.MachineInstall.InstallDisk))
}
}
}
}
}
if t := c.Machine().Type(); t != machine.TypeUnknown && t.String() != c.MachineConfig.MachineType {
warnings = append(warnings, fmt.Sprintf("use %q instead of %q for machine type", t.String(), c.MachineConfig.MachineType))
}
switch c.Machine().Type() {
case machine.TypeInit, machine.TypeControlPlane:
warn, err := ValidateCNI(c.Cluster().Network().CNI())
warnings = append(warnings, warn...)
result = multierror.Append(result, err)
case machine.TypeWorker:
for _, d := range c.Machine().Network().Devices() {
if d.VIPConfig() != nil {
result = multierror.Append(result, errors.New("virtual (shared) IP is not allowed on non-controlplane nodes"))
}
for _, vlan := range d.Vlans() {
if vlan.VIPConfig() != nil {
result = multierror.Append(result, errors.New("virtual (shared) IP is not allowed on non-controlplane nodes"))
}
}
}
case machine.TypeUnknown:
fallthrough
default:
result = multierror.Append(result, fmt.Errorf("unknown machine type %q", c.MachineConfig.MachineType))
}
if c.MachineConfig.MachineNetwork != nil {
bondedInterfaces := map[string]string{}
bridgedInterfaces := map[string]string{}
for _, device := range c.MachineConfig.MachineNetwork.NetworkInterfaces {
if device.Bond() != nil && device.Bridge() != nil {
result = multierror.Append(result, fmt.Errorf("interface has both bridge and bond sections set %q: %w", device.Interface(), ErrMutuallyExclusive))
}
if device.Bond() != nil {
for _, iface := range device.Bond().Interfaces() {
if otherIface, exists := bondedInterfaces[iface]; exists && otherIface != device.Interface() {
result = multierror.Append(result, fmt.Errorf("interface %q is declared as part of two bonds: %q and %q", iface, otherIface, device.Interface()))
}
if bridgeIface, exists := bridgedInterfaces[iface]; exists {
result = multierror.Append(result, fmt.Errorf("interface %q is declared as part of an interface and a bond: %q and %q", iface, bridgeIface, device.Interface()))
}
bondedInterfaces[iface] = device.Interface()
}
}
if device.Bridge() != nil {
for _, iface := range device.Bridge().Interfaces() {
if otherIface, exists := bridgedInterfaces[iface]; exists && otherIface != device.Interface() {
result = multierror.Append(result, fmt.Errorf("interface %q is declared as part of two bridges: %q and %q", iface, otherIface, device.Interface()))
}
if bondIface, exists := bondedInterfaces[iface]; exists {
result = multierror.Append(result, fmt.Errorf("interface %q is declared as part of an interface and a bond: %q and %q", iface, bondIface, device.Interface()))
}
bridgedInterfaces[iface] = device.Interface()
}
}
}
for _, device := range c.MachineConfig.MachineNetwork.NetworkInterfaces {
warn, err := ValidateNetworkDevices(device, bondedInterfaces, CheckDeviceInterface, CheckDeviceAddressing, CheckDeviceRoutes)
warnings = append(warnings, warn...)
result = multierror.Append(result, err)
}
}
if c.MachineConfig.MachineDisks != nil {
for _, disk := range c.MachineConfig.MachineDisks {
for i, pt := range disk.DiskPartitions {
if pt.DiskSize == 0 && i != len(disk.DiskPartitions)-1 {
result = multierror.Append(result, fmt.Errorf("partition for disk %q is set to occupy full disk, but it's not the last partition in the list", disk.Device()))
}
}
}
}
if c.MachineConfig.MachineKubelet != nil {
warn, err := c.MachineConfig.MachineKubelet.Validate()
warnings = append(warnings, warn...)
result = multierror.Append(result, err)
}
for _, label := range []string{constants.EphemeralPartitionLabel, constants.StatePartitionLabel} {
encryptionConfig := c.MachineConfig.SystemDiskEncryption().Get(label)
if encryptionConfig != nil {
if len(encryptionConfig.Keys()) == 0 {
result = multierror.Append(result, fmt.Errorf("no encryption keys provided for the ephemeral partition encryption"))
}
slotsInUse := map[int]bool{}
for _, key := range encryptionConfig.Keys() {
if slotsInUse[key.Slot()] {
result = multierror.Append(result, fmt.Errorf("encryption key slot %d is already in use", key.Slot()))
}
slotsInUse[key.Slot()] = true
if key.NodeID() == nil && key.Static() == nil {
result = multierror.Append(result, fmt.Errorf("encryption key at slot %d doesn't have any settings", key.Slot()))
}
}
}
}
if c.Machine().Network().KubeSpan().Enabled() {
if !c.Cluster().Discovery().Enabled() {
result = multierror.Append(result, fmt.Errorf(".cluster.discovery should be enabled when .machine.network.kubespan is enabled"))
}
if c.Cluster().ID() == "" {
result = multierror.Append(result, fmt.Errorf(".cluster.id should be set when .machine.network.kubespan is enabled"))
}
if c.Cluster().Secret() == "" {
result = multierror.Append(result, fmt.Errorf(".cluster.secret should be set when .machine.network.kubespan is enabled"))
}
}
if c.MachineConfig.MachineLogging != nil {
err := c.MachineConfig.MachineLogging.Validate()
result = multierror.Append(result, err)
}
if c.MachineConfig.MachineInstall != nil {
extensions := map[string]struct{}{}
for _, ext := range c.MachineConfig.MachineInstall.InstallExtensions {
if _, exists := extensions[ext.Image()]; exists {
result = multierror.Append(result, fmt.Errorf("duplicate system extension %q", ext.Image()))
}
extensions[ext.Image()] = struct{}{}
}
}
if c.Machine().Features().KubernetesTalosAPIAccess().Enabled() && !c.Machine().Features().RBACEnabled() {
result = multierror.Append(result, fmt.Errorf("feature API RBAC should be enabled when Kubernetes Talos API Access feature is enabled"))
}
if c.Machine().Features().KubernetesTalosAPIAccess().Enabled() && !c.Machine().Type().IsControlPlane() {
result = multierror.Append(result, fmt.Errorf("feature Kubernetes Talos API Access can only be enabled on control plane machines"))
}
if opts.Strict {
for _, w := range warnings {
result = multierror.Append(result, fmt.Errorf("warning: %s", w))
}
warnings = nil
}
return warnings, result.ErrorOrNil()
}
var rxDNSName = regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`)
func isValidDNSName(name string) bool {
if name == "" || len(name)-strings.Count(name, ".") > 255 {
return false
}
return rxDNSName.MatchString(name)
}
// Validate validates the config.
//
//nolint:gocyclo
func (c *ClusterConfig) Validate() error {
var result *multierror.Error
if c == nil {
return fmt.Errorf("cluster instructions are required")
}
if c.ControlPlane == nil || c.ControlPlane.Endpoint == nil {
return fmt.Errorf("cluster controlplane endpoint is required")
}
if err := talosnet.ValidateEndpointURI(c.ControlPlane.Endpoint.URL.String()); err != nil {
result = multierror.Append(result, fmt.Errorf("invalid controlplane endpoint: %w", err))
}
if c.ClusterNetwork != nil && !isValidDNSName(c.ClusterNetwork.DNSDomain) {
result = multierror.Append(result, fmt.Errorf("%q is not a valid DNS name", c.ClusterNetwork.DNSDomain))
}
if ecp := c.ExternalCloudProviderConfig; ecp != nil {
result = multierror.Append(result, ecp.Validate())
}
if c.EtcdConfig != nil && c.EtcdConfig.EtcdSubnet != "" {
if _, _, err := net.ParseCIDR(c.EtcdConfig.EtcdSubnet); err != nil {
result = multierror.Append(result, fmt.Errorf("%q is not a valid subnet", c.EtcdConfig.EtcdSubnet))
}
}
result = multierror.Append(result, c.ClusterInlineManifests.Validate(), c.ClusterDiscoveryConfig.Validate(c))
return result.ErrorOrNil()
}
// ValidateCNI validates CNI config.
func ValidateCNI(cni config.CNI) ([]string, error) {
var (
warnings []string
result *multierror.Error
)
switch cni.Name() {
case constants.FlannelCNI:
fallthrough
case constants.NoneCNI:
if len(cni.URLs()) != 0 {
err := fmt.Errorf(`"urls" field should be empty for %q CNI`, cni.Name())
result = multierror.Append(result, err)
}
case constants.CustomCNI:
if len(cni.URLs()) == 0 {
warn := fmt.Sprintf(`"urls" field should not be empty for %q CNI`, cni.Name())
warnings = append(warnings, warn)
}
for _, u := range cni.URLs() {
if err := talosnet.ValidateEndpointURI(u); err != nil {
result = multierror.Append(result, err)
}
}
default:
err := fmt.Errorf("cni name should be one of [%q, %q, %q]", constants.FlannelCNI, constants.CustomCNI, constants.NoneCNI)
result = multierror.Append(result, err)
}
return warnings, result.ErrorOrNil()
}
// Validate validates external cloud provider configuration.
func (ecp *ExternalCloudProviderConfig) Validate() error {
if !ecp.Enabled() && (len(ecp.ExternalManifests) != 0) {
return fmt.Errorf("external cloud provider is disabled, but manifests are provided")
}
var result *multierror.Error
for _, url := range ecp.ExternalManifests {
if err := talosnet.ValidateEndpointURI(url); err != nil {
err = fmt.Errorf("invalid external cloud provider manifest url %q: %w", url, err)
result = multierror.Append(result, err)
}
}
return result.ErrorOrNil()
}
// Validate the inline manifests.
func (manifests ClusterInlineManifests) Validate() error {
var result *multierror.Error
manifestNames := map[string]struct{}{}
for _, manifest := range manifests {
if strings.TrimSpace(manifest.InlineManifestName) == "" {
result = multierror.Append(result, fmt.Errorf("inline manifest name can't be empty"))
}
if _, ok := manifestNames[manifest.InlineManifestName]; ok {
result = multierror.Append(result, fmt.Errorf("inline manifest name %q is duplicate", manifest.InlineManifestName))
}
manifestNames[manifest.InlineManifestName] = struct{}{}
}
return result.ErrorOrNil()
}
// Validate the discovery config.
//
//nolint:gocyclo
func (c *ClusterDiscoveryConfig) Validate(clusterCfg *ClusterConfig) error {
var result *multierror.Error
if c == nil || !c.Enabled() {
return nil
}
if c.Registries().Service().Enabled() {
url, err := url.ParseRequestURI(c.Registries().Service().Endpoint())
if err != nil {
result = multierror.Append(result, fmt.Errorf("cluster discovery service registry endpoint is invalid: %w", err))
} else {
// allow insecure discovery service for easier local development
if !debug.Enabled {
if url.Scheme != "https" {
result = multierror.Append(result, fmt.Errorf("cluster discovery service should use TLS"))
}
}
if url.Path != "" && url.Path != "/" {
result = multierror.Append(result, fmt.Errorf("cluster discovery service path should be empty"))
}
}
if clusterCfg.ID() == "" {
result = multierror.Append(result, fmt.Errorf("cluster discovery service requires .cluster.id"))
}
if clusterCfg.Secret() == "" {
result = multierror.Append(result, fmt.Errorf("cluster discovery service requires .cluster.secret"))
}
}
return result.ErrorOrNil()
}
// ValidateNetworkDevices runs the specified validation checks specific to the
// network devices.
func ValidateNetworkDevices(d *Device, pairedInterfaces map[string]string, checks ...NetworkDeviceCheck) ([]string, error) {
// todo utku
var result *multierror.Error
if d == nil {
return nil, fmt.Errorf("empty device")
}
if d.Ignore() {
return nil, result.ErrorOrNil()
}
var warnings []string
for _, check := range checks {
warn, err := check(d, pairedInterfaces)
warnings = append(warnings, warn...)
result = multierror.Append(result, err)
}
return warnings, result.ErrorOrNil()
}
// CheckDeviceInterface ensures that the interface has been specified.
//
//nolint:gocyclo
func CheckDeviceInterface(d *Device, _ map[string]string) ([]string, error) {
var result *multierror.Error
if d == nil {
return nil, fmt.Errorf("empty device")
}
if d.DeviceInterface == "" && d.DeviceSelector == nil {
result = multierror.Append(result, fmt.Errorf("[%s], [%s]: %w", "networking.os.device.interface", "networking.os.device.deviceSelector", ErrRequiredSectionOptions))
} else if d.DeviceInterface != "" && d.DeviceSelector != nil {
result = multierror.Append(result, fmt.Errorf("[%s], [%s]: %w", "networking.os.device.interface", "networking.os.device.deviceSelector", ErrMutuallyExclusive))
}
if d.DeviceSelector != nil && reflect.ValueOf(d.DeviceSelector).Elem().IsZero() {
result = multierror.Append(result, fmt.Errorf("[%s]: %w", "networking.os.device.deviceSelector", ErrEmpty))
}
if d.DeviceBond != nil {
result = multierror.Append(result, checkBond(d.DeviceBond))
}
if d.DeviceWireguardConfig != nil {
result = multierror.Append(result, checkWireguard(d.DeviceWireguardConfig))
}
if d.DeviceVlans != nil {
result = multierror.Append(result, checkVlans(d))
}
return nil, result.ErrorOrNil()
}
//nolint:gocyclo,cyclop
func checkBond(b *Bond) error {
var result *multierror.Error
bondMode, err := nethelpers.BondModeByName(b.BondMode)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.BondXmitHashPolicyByName(b.BondHashPolicy)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.LACPRateByName(b.BondLACPRate)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.ARPValidateByName(b.BondARPValidate)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.ARPAllTargetsByName(b.BondARPAllTargets)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.PrimaryReselectByName(b.BondPrimaryReselect)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.FailOverMACByName(b.BondFailOverMac)
if err != nil {
result = multierror.Append(result, err)
}
_, err = nethelpers.ADSelectByName(b.BondADSelect)
if err != nil {
result = multierror.Append(result, err)
}
if b.BondMIIMon == 0 {
if b.BondUpDelay != 0 {
result = multierror.Append(result, fmt.Errorf("bond.upDelay can't be set if miiMon is zero"))
}
if b.BondDownDelay != 0 {
result = multierror.Append(result, fmt.Errorf("bond.downDelay can't be set if miiMon is zero"))
}
} else {
if b.BondUpDelay%b.BondMIIMon != 0 {
result = multierror.Append(result, fmt.Errorf("bond.upDelay should be a multiple of miiMon"))
}
if b.BondDownDelay%b.BondMIIMon != 0 {
result = multierror.Append(result, fmt.Errorf("bond.downDelay should be a multiple of miiMon"))
}
}
if len(b.BondARPIPTarget) > 0 {
result = multierror.Append(result, fmt.Errorf("bond.arpIPTarget is not supported"))
}
if b.BondLACPRate != "" && bondMode != nethelpers.BondMode8023AD {
result = multierror.Append(result, fmt.Errorf("bond.lacpRate is only available in 802.3ad mode"))
}
if b.BondADActorSystem != "" {
result = multierror.Append(result, fmt.Errorf("bond.adActorSystem is not supported"))
}
if (bondMode == nethelpers.BondMode8023AD || bondMode == nethelpers.BondModeALB || bondMode == nethelpers.BondModeTLB) && b.BondARPValidate != "" {
result = multierror.Append(result, fmt.Errorf("bond.arpValidate is not available in %s mode", bondMode))
}
if !(bondMode == nethelpers.BondModeActiveBackup || bondMode == nethelpers.BondModeALB || bondMode == nethelpers.BondModeTLB) && b.BondPrimary != "" {
result = multierror.Append(result, fmt.Errorf("bond.primary is not available in %s mode", bondMode))
}
if (bondMode == nethelpers.BondMode8023AD || bondMode == nethelpers.BondModeALB || bondMode == nethelpers.BondModeTLB) && b.BondARPInterval > 0 {
result = multierror.Append(result, fmt.Errorf("bond.arpInterval is not available in %s mode", bondMode))
}
if bondMode != nethelpers.BondModeRoundrobin && b.BondPacketsPerSlave > 1 {
result = multierror.Append(result, fmt.Errorf("bond.packetsPerSlave is not available in %s mode", bondMode))
}
if !(bondMode == nethelpers.BondModeALB || bondMode == nethelpers.BondModeTLB) && b.BondTLBDynamicLB > 0 {
result = multierror.Append(result, fmt.Errorf("bond.tlbDynamicTLB is not available in %s mode", bondMode))
}
if bondMode != nethelpers.BondMode8023AD && b.BondADActorSysPrio > 0 {
result = multierror.Append(result, fmt.Errorf("bond.adActorSysPrio is only available in 802.3ad mode"))
}
if bondMode != nethelpers.BondMode8023AD && b.BondADUserPortKey > 0 {
result = multierror.Append(result, fmt.Errorf("bond.adUserPortKey is only available in 802.3ad mode"))
}
return result.ErrorOrNil()
}
func checkWireguard(b *DeviceWireguardConfig) error {
var result *multierror.Error
// avoid pulling in wgctrl code to keep machinery dependencies slim
checkKey := func(key string) error {
raw, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return err
}
if len(raw) != 32 {
return fmt.Errorf("wrong key %q length: %d", key, len(raw))
}
return nil
}
if err := checkKey(b.WireguardPrivateKey); err != nil {
result = multierror.Append(result, fmt.Errorf("private key is invalid: %w", err))
}
for _, peer := range b.WireguardPeers {
if err := checkKey(peer.WireguardPublicKey); err != nil {
result = multierror.Append(result, fmt.Errorf("public key invalid: %w", err))
}
if peer.WireguardEndpoint != "" {
if !talosnet.AddressContainsPort(peer.WireguardEndpoint) {
result = multierror.Append(result, fmt.Errorf("peer endpoint %q is invalid", peer.WireguardEndpoint))
}
}
for _, allowedIP := range peer.WireguardAllowedIPs {
if _, _, err := net.ParseCIDR(allowedIP); err != nil {
result = multierror.Append(result, fmt.Errorf("peer allowed IP %q is invalid: %w", allowedIP, err))
}
}
}
return result.ErrorOrNil()
}
func checkVlans(d *Device) error {
var result *multierror.Error
// check VLAN addressing
for _, vlan := range d.DeviceVlans {
if len(vlan.VlanAddresses) > 0 && vlan.VlanCIDR != "" {
result = multierror.Append(result, fmt.Errorf("[%s] %s.%d: %s", "networking.os.device.vlan", d.DeviceInterface, vlan.VlanID, "vlan can't have both .cidr and .addresses set"))
}
if vlan.VlanCIDR != "" {
if err := validateIPOrCIDR(vlan.VlanCIDR); err != nil {
result = multierror.Append(result, fmt.Errorf("[%s] %s.%d: %w", "networking.os.device.vlan.CIDR", d.DeviceInterface, vlan.VlanID, err))
}
}
for _, address := range vlan.VlanAddresses {
if err := validateIPOrCIDR(address); err != nil {
result = multierror.Append(result, fmt.Errorf("[%s] %s.%d: %w", "networking.os.device.vlan.addresses", d.DeviceInterface, vlan.VlanID, err))
}
}
}
return result.ErrorOrNil()
}
func validateIPOrCIDR(address string) error {
if strings.IndexByte(address, '/') >= 0 {
_, _, err := net.ParseCIDR(address)
return err
}
if ip := net.ParseIP(address); ip == nil {
return fmt.Errorf("failed to parse IP address %q", address)
}
return nil
}
// CheckDeviceAddressing ensures that an appropriate addressing method.
// has been specified.
//
//nolint:gocyclo
func CheckDeviceAddressing(d *Device, bondedInterfaces map[string]string) ([]string, error) {
var result *multierror.Error
if d == nil {
return nil, fmt.Errorf("empty device")
}
var warnings []string
if _, bonded := bondedInterfaces[d.Interface()]; bonded {
if d.DHCP() || d.DeviceCIDR != "" || len(d.DeviceAddresses) > 0 || d.DeviceVIPConfig != nil {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %s", "networking.os.device", d.DeviceInterface, "bonded interface shouldn't have any addressing methods configured"))
}
}
// ensure either legacy CIDR is set or new addresses, but not both
if len(d.DeviceAddresses) > 0 && d.DeviceCIDR != "" {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %s", "networking.os.device", d.DeviceInterface, "interface can't have both .cidr and .addresses set"))
}
// ensure cidr is a valid address
if d.DeviceCIDR != "" {
if err := validateIPOrCIDR(d.DeviceCIDR); err != nil {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %w", "networking.os.device.CIDR", d.DeviceInterface, err))
}
warnings = append(warnings, fmt.Sprintf("%q: machine.network.interface.cidr is deprecated, please use machine.network.interface.addresses", d.DeviceInterface))
}
// ensure addresses are valid addresses
for _, address := range d.DeviceAddresses {
if err := validateIPOrCIDR(address); err != nil {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %w", "networking.os.device.addresses", d.DeviceInterface, err))
}
}
// check VIP IP is valid
if d.DeviceVIPConfig != nil {
if ip := net.ParseIP(d.DeviceVIPConfig.IP()); ip == nil {
result = multierror.Append(result, fmt.Errorf("[%s] failed to parse %q as IP address", "networking.os.device.vip", d.DeviceVIPConfig.IP()))
}
}
return warnings, result.ErrorOrNil()
}
// CheckDeviceRoutes ensures that the specified routes are valid.
//
//nolint:gocyclo
func CheckDeviceRoutes(d *Device, _ map[string]string) ([]string, error) {
var result *multierror.Error
if d == nil {
return nil, fmt.Errorf("empty device")
}
if len(d.DeviceRoutes) == 0 {
return nil, result.ErrorOrNil()
}
for idx, route := range d.DeviceRoutes {
if route.Network() != "" {
if _, _, err := net.ParseCIDR(route.Network()); err != nil {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %w", "networking.os.device.route["+strconv.Itoa(idx)+"].network", route.Network(), ErrInvalidAddress))
}
}
if route.Gateway() != "" {
if ip := net.ParseIP(route.Gateway()); ip == nil {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %w", "networking.os.device.route["+strconv.Itoa(idx)+"].gateway", route.Gateway(), ErrInvalidAddress))
}
}
if route.Gateway() == "" && route.Network() == "" {
result = multierror.Append(result, fmt.Errorf("[%s]: %s", "networking.os.device.route["+strconv.Itoa(idx)+"]", "either network or gateway should be set"))
}
if route.Source() != "" {
if ip := net.ParseIP(route.Source()); ip == nil {
result = multierror.Append(result, fmt.Errorf("[%s] %q: %w", "networking.os.device.route["+strconv.Itoa(idx)+"].source", route.Source(), ErrInvalidAddress))
}
}
}
return nil, result.ErrorOrNil()
}
// Validate kubelet configuration.
func (k *KubeletConfig) Validate() ([]string, error) {
var result *multierror.Error
if k.KubeletNodeIP != nil {
for _, cidr := range k.KubeletNodeIP.KubeletNodeIPValidSubnets {
cidr = strings.TrimPrefix(cidr, "!")
if _, err := talosnet.ParseCIDR(cidr); err != nil {
result = multierror.Append(result, fmt.Errorf("kubelet nodeIP subnet is not valid: %q", cidr))
}
}
}
for _, field := range kubelet.ProtectedConfigurationFields {
if _, exists := k.KubeletExtraConfig.Object[field]; exists {
result = multierror.Append(result, fmt.Errorf("kubelet configuration field %q can't be overridden", field))
}
}
return nil, result.ErrorOrNil()
}