-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodeip.go
159 lines (133 loc) · 4.2 KB
/
nodeip.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
// 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 k8s
import (
"context"
"fmt"
"net/netip"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/net"
"go.uber.org/zap"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
)
// NodeIPController renders manifests based on templates and config/secrets.
type NodeIPController struct{}
// Name implements controller.Controller interface.
func (ctrl *NodeIPController) Name() string {
return "k8s.NodeIPController"
}
// Inputs implements controller.Controller interface.
func (ctrl *NodeIPController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: k8s.NamespaceName,
Type: k8s.NodeIPConfigType,
ID: optional.Some(k8s.KubeletID),
Kind: controller.InputWeak,
},
{
Namespace: network.NamespaceName,
Type: network.NodeAddressType,
ID: optional.Some(network.FilteredNodeAddressID(network.NodeAddressRoutedID, k8s.NodeAddressFilterNoK8s)),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *NodeIPController) Outputs() []controller.Output {
return []controller.Output{
{
Type: k8s.NodeIPType,
Kind: controller.OutputExclusive,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo
func (ctrl *NodeIPController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
cfg, err := safe.ReaderGet[*k8s.NodeIPConfig](ctx, r, resource.NewMetadata(k8s.NamespaceName, k8s.NodeIPConfigType, k8s.KubeletID, resource.VersionUndefined))
if err != nil {
if state.IsNotFoundError(err) {
continue
}
return fmt.Errorf("error getting config: %w", err)
}
cfgSpec := cfg.TypedSpec()
nodeAddrs, err := safe.ReaderGet[*network.NodeAddress](
ctx,
r,
resource.NewMetadata(
network.NamespaceName,
network.NodeAddressType,
network.FilteredNodeAddressID(network.NodeAddressRoutedID, k8s.NodeAddressFilterNoK8s),
resource.VersionUndefined,
),
)
if err != nil {
if state.IsNotFoundError(err) {
continue
}
return fmt.Errorf("error getting addresses: %w", err)
}
addrs := nodeAddrs.TypedSpec().IPs()
cidrs := make([]string, 0, len(cfgSpec.ValidSubnets)+len(cfgSpec.ExcludeSubnets))
cidrs = append(cidrs, cfgSpec.ValidSubnets...)
cidrs = append(cidrs, xslices.Map(cfgSpec.ExcludeSubnets, func(cidr string) string { return "!" + cidr })...)
ips, err := net.FilterIPs(addrs, cidrs)
if err != nil {
return fmt.Errorf("error filtering IPs: %w", err)
}
if len(ips) == 0 {
logger.Warn("no suitable node IP found, please make sure .machine.kubelet.nodeIP filters and pod/service subnets are set up correctly")
continue
}
// filter down to make sure only one IPv4 and one IPv6 address stays
var hasIPv4, hasIPv6 bool
nodeIPs := make([]netip.Addr, 0, 2)
for _, ip := range ips {
switch {
case ip.Is4():
if !hasIPv4 {
nodeIPs = append(nodeIPs, ip)
hasIPv4 = true
} else {
logger.Warn("node IP skipped, please use .machine.kubelet.nodeIP to provide explicit subnet for the node IP", zap.Stringer("address", ip))
}
case ip.Is6():
if !hasIPv6 {
nodeIPs = append(nodeIPs, ip)
hasIPv6 = true
} else {
logger.Warn("node IP skipped, please use .machine.kubelet.nodeIP to provide explicit subnet for the node IP", zap.Stringer("address", ip))
}
}
}
if err = safe.WriterModify(
ctx,
r,
k8s.NewNodeIP(k8s.NamespaceName, k8s.KubeletID),
func(r *k8s.NodeIP) error {
spec := r.TypedSpec()
spec.Addresses = nodeIPs
return nil
},
); err != nil {
return fmt.Errorf("error modifying NodeIP resource: %w", err)
}
r.ResetRestartBackoff()
}
}