-
Notifications
You must be signed in to change notification settings - Fork 458
/
v1alpha1_network_options.go
135 lines (103 loc) · 3.75 KB
/
v1alpha1_network_options.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
// 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 (
"github.com/siderolabs/go-pointer"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
)
// NetworkConfigOption generates NetworkConfig.
type NetworkConfigOption func(machine.Type, *NetworkConfig) error
// WithNetworkConfig sets whole network config structure, overwrites any previous options.
func WithNetworkConfig(c *NetworkConfig) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
*cfg = *c
return nil
}
}
// WithNetworkNameservers sets global nameservers list.
func WithNetworkNameservers(nameservers ...string) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
cfg.NameServers = append(cfg.NameServers, nameservers...)
return nil
}
}
// WithNetworkInterfaceIgnore marks interface as ignored.
func WithNetworkInterfaceIgnore(iface string) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
cfg.getDevice(iface).DeviceIgnore = pointer.To(true)
return nil
}
}
// WithNetworkInterfaceDHCP enables DHCP for the interface.
func WithNetworkInterfaceDHCP(iface string, enable bool) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
cfg.getDevice(iface).DeviceDHCP = pointer.To(true)
return nil
}
}
// WithNetworkInterfaceDHCPv4 enables DHCPv4 for the interface.
func WithNetworkInterfaceDHCPv4(iface string, enable bool) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
dev := cfg.getDevice(iface)
if dev.DeviceDHCPOptions == nil {
dev.DeviceDHCPOptions = &DHCPOptions{}
}
dev.DeviceDHCPOptions.DHCPIPv4 = pointer.To(enable)
return nil
}
}
// WithNetworkInterfaceDHCPv6 enables DHCPv6 for the interface.
func WithNetworkInterfaceDHCPv6(iface string, enable bool) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
dev := cfg.getDevice(iface)
if dev.DeviceDHCPOptions == nil {
dev.DeviceDHCPOptions = &DHCPOptions{}
}
dev.DeviceDHCPOptions.DHCPIPv6 = pointer.To(enable)
return nil
}
}
// WithNetworkInterfaceCIDR configures interface for static addressing.
func WithNetworkInterfaceCIDR(iface, cidr string) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
cfg.getDevice(iface).DeviceAddresses = append(cfg.getDevice(iface).DeviceAddresses, cidr)
return nil
}
}
// WithNetworkInterfaceMTU configures interface MTU.
func WithNetworkInterfaceMTU(iface string, mtu int) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
cfg.getDevice(iface).DeviceMTU = mtu
return nil
}
}
// WithNetworkInterfaceWireguard configures interface for Wireguard.
func WithNetworkInterfaceWireguard(iface string, wireguardConfig *DeviceWireguardConfig) NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
cfg.getDevice(iface).DeviceWireguardConfig = wireguardConfig
return nil
}
}
// WithNetworkInterfaceVirtualIP configures interface for Virtual IP.
func WithNetworkInterfaceVirtualIP(iface, cidr string) NetworkConfigOption {
return func(machineType machine.Type, cfg *NetworkConfig) error {
if machineType == machine.TypeWorker {
return nil
}
cfg.getDevice(iface).DeviceVIPConfig = &DeviceVIPConfig{
SharedIP: cidr,
}
return nil
}
}
// WithKubeSpan configures a KubeSpan interface.
func WithKubeSpan() NetworkConfigOption {
return func(_ machine.Type, cfg *NetworkConfig) error {
if cfg.NetworkKubeSpan == nil {
cfg.NetworkKubeSpan = &NetworkKubeSpan{}
}
cfg.NetworkKubeSpan.KubeSpanEnabled = pointer.To(true)
return nil
}
}