-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
node.go
248 lines (223 loc) · 8.34 KB
/
node.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package k8s
import (
"fmt"
"net"
"strconv"
"github.com/cilium/cilium/pkg/annotation"
"github.com/cilium/cilium/pkg/cidr"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/node/addressing"
nodeTypes "github.com/cilium/cilium/pkg/node/types"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/source"
"github.com/sirupsen/logrus"
)
// ParseNodeAddressType converts a Kubernetes NodeAddressType to a Cilium
// NodeAddressType. If the Kubernetes NodeAddressType does not have a
// corresponding Cilium AddressType, returns an error.
func ParseNodeAddressType(k8sAddress slim_corev1.NodeAddressType) (addressing.AddressType, error) {
var err error
convertedAddr := addressing.AddressType(k8sAddress)
switch convertedAddr {
case addressing.NodeExternalDNS, addressing.NodeExternalIP, addressing.NodeHostName, addressing.NodeInternalIP, addressing.NodeInternalDNS:
default:
err = fmt.Errorf("invalid Kubernetes NodeAddressType %s", convertedAddr)
}
return convertedAddr, err
}
type nodeAddressGroup struct {
typ slim_corev1.NodeAddressType
family slim_corev1.IPFamily
}
// ParseNode parses a kubernetes node to a cilium node
func ParseNode(k8sNode *slim_corev1.Node, source source.Source) *nodeTypes.Node {
addrGroups := make(map[nodeAddressGroup]struct{})
scopedLog := log.WithFields(logrus.Fields{
logfields.NodeName: k8sNode.Name,
logfields.K8sNodeID: k8sNode.UID,
})
addrs := []nodeTypes.Address{}
for _, addr := range k8sNode.Status.Addresses {
// We only care about this address types,
// we ignore all other types.
switch addr.Type {
case slim_corev1.NodeInternalIP, slim_corev1.NodeExternalIP:
default:
continue
}
// If the address is not set let's not parse it at all.
// This can be the case for corev1.NodeExternalIPs
if addr.Address == "" {
continue
}
addrGroup := nodeAddressGroup{
typ: addr.Type,
}
ip := net.ParseIP(addr.Address)
switch {
case ip != nil && ip.To4() != nil:
addrGroup.family = slim_corev1.IPv4Protocol
case ip != nil && ip.To16() != nil:
addrGroup.family = slim_corev1.IPv6Protocol
default:
scopedLog.WithFields(logrus.Fields{
logfields.IPAddr: addr.Address,
logfields.Type: addr.Type,
}).Warn("Ignoring invalid node IP")
continue
}
_, groupFound := addrGroups[addrGroup]
if groupFound {
scopedLog.WithFields(logrus.Fields{
logfields.Node: k8sNode.Name,
logfields.Type: addr.Type,
}).Warn("Detected multiple IPs of the same address type and family, Cilium will only consider the first IP in the Node resource")
continue
}
addrGroups[addrGroup] = struct{}{}
addressType, err := ParseNodeAddressType(addr.Type)
if err != nil {
scopedLog.WithError(err).Warn("invalid address type for node")
}
na := nodeTypes.Address{
Type: addressType,
IP: ip,
}
addrs = append(addrs, na)
}
newNode := &nodeTypes.Node{
Name: k8sNode.Name,
Cluster: option.Config.ClusterName,
IPAddresses: addrs,
Source: source,
}
if len(k8sNode.Spec.PodCIDRs) != 0 {
if len(k8sNode.Spec.PodCIDRs) > 2 {
scopedLog.WithField("podCIDR", k8sNode.Spec.PodCIDRs).Errorf("Invalid PodCIDRs expected 1 or 2 PodCIDRs, received %d", len(k8sNode.Spec.PodCIDRs))
} else {
for _, podCIDR := range k8sNode.Spec.PodCIDRs {
if allocCIDR, err := cidr.ParseCIDR(podCIDR); err != nil {
scopedLog.WithError(err).WithField("podCIDR", k8sNode.Spec.PodCIDR).Warn("Invalid PodCIDR value for node")
} else {
if allocCIDR.IP.To4() != nil {
newNode.IPv4AllocCIDR = allocCIDR
} else {
newNode.IPv6AllocCIDR = allocCIDR
}
}
}
}
} else if len(k8sNode.Spec.PodCIDR) != 0 {
if allocCIDR, err := cidr.ParseCIDR(k8sNode.Spec.PodCIDR); err != nil {
scopedLog.WithError(err).WithField(logfields.V4Prefix, k8sNode.Spec.PodCIDR).Warn("Invalid PodCIDR value for node")
} else {
if allocCIDR.IP.To4() != nil {
newNode.IPv4AllocCIDR = allocCIDR
} else {
newNode.IPv6AllocCIDR = allocCIDR
}
}
}
newNode.Labels = k8sNode.GetLabels()
newNode.Annotations = make(map[string]string)
// Propagate only Cilium specific annotations.
for key, value := range k8sNode.GetAnnotations() {
if annotation.CiliumPrefixRegex.MatchString(key) {
newNode.Annotations[key] = value
}
}
if !option.Config.AnnotateK8sNode {
return newNode
}
// Any code bellow this line will depend on k8s node annotations. If we are
// not annotating the node then we should not use any annotations.
k8sNodeAddHostIP := func(key string, alias string) {
if ciliumInternalIP, ok := annotation.Get(k8sNode, key, alias); !ok || ciliumInternalIP == "" {
scopedLog.Debugf("Missing %s (or %s). Annotation required when IPSec Enabled", key, alias)
} else if ip := net.ParseIP(ciliumInternalIP); ip == nil {
scopedLog.Debugf("ParseIP %s error", ciliumInternalIP)
} else {
na := nodeTypes.Address{
Type: addressing.NodeCiliumInternalIP,
IP: ip,
}
addrs = append(addrs, na)
scopedLog.Debugf("Add NodeCiliumInternalIP: %s", ip)
}
}
k8sNodeAddHostIP(annotation.CiliumHostIP, annotation.CiliumHostIPAlias)
k8sNodeAddHostIP(annotation.CiliumHostIPv6, annotation.CiliumHostIPv6Alias)
newNode.IPAddresses = addrs
if key, ok := annotation.Get(k8sNode, annotation.CiliumEncryptionKey, annotation.CiliumEncryptionKeyAlias); ok {
if u, err := strconv.ParseUint(key, 10, 8); err == nil {
newNode.EncryptionKey = uint8(u)
}
}
// Spec.PodCIDR takes precedence since it's
// the CIDR assigned by k8s controller manager
// In case it's invalid or empty then we fall back to our annotations.
if newNode.IPv4AllocCIDR == nil {
if ipv4CIDR, ok := annotation.Get(k8sNode, annotation.V4CIDRName, annotation.V4CIDRNameAlias); !ok || ipv4CIDR == "" {
scopedLog.Debug("Empty IPv4 CIDR annotation in node")
} else {
allocCIDR, err := cidr.ParseCIDR(ipv4CIDR)
if err != nil {
scopedLog.WithError(err).WithField(logfields.V4Prefix, ipv4CIDR).Error("BUG, invalid IPv4 annotation CIDR in node")
} else {
newNode.IPv4AllocCIDR = allocCIDR
}
}
}
if newNode.IPv6AllocCIDR == nil {
if ipv6CIDR, ok := annotation.Get(k8sNode, annotation.V6CIDRName, annotation.V6CIDRNameAlias); !ok || ipv6CIDR == "" {
scopedLog.Debug("Empty IPv6 CIDR annotation in node")
} else {
allocCIDR, err := cidr.ParseCIDR(ipv6CIDR)
if err != nil {
scopedLog.WithError(err).WithField(logfields.V6Prefix, ipv6CIDR).Error("BUG, invalid IPv6 annotation CIDR in node")
} else {
newNode.IPv6AllocCIDR = allocCIDR
}
}
}
if newNode.IPv4HealthIP == nil {
if healthIP, ok := annotation.Get(k8sNode, annotation.V4HealthName, annotation.V4HealthNameAlias); !ok || healthIP == "" {
scopedLog.Debug("Empty IPv4 health endpoint annotation in node")
} else if ip := net.ParseIP(healthIP); ip == nil {
scopedLog.WithField(logfields.V4HealthIP, healthIP).Error("BUG, invalid IPv4 health endpoint annotation in node")
} else {
newNode.IPv4HealthIP = ip
}
}
if newNode.IPv6HealthIP == nil {
if healthIP, ok := annotation.Get(k8sNode, annotation.V6HealthName, annotation.V6HealthNameAlias); !ok || healthIP == "" {
scopedLog.Debug("Empty IPv6 health endpoint annotation in node")
} else if ip := net.ParseIP(healthIP); ip == nil {
scopedLog.WithField(logfields.V6HealthIP, healthIP).Error("BUG, invalid IPv6 health endpoint annotation in node")
} else {
newNode.IPv6HealthIP = ip
}
}
if newNode.IPv4IngressIP == nil {
if ingressIP, ok := annotation.Get(k8sNode, annotation.V4IngressName, annotation.V4IngressNameAlias); !ok || ingressIP == "" {
scopedLog.Debug("Empty IPv4 Ingress annotation in node")
} else if ip := net.ParseIP(ingressIP); ip == nil {
scopedLog.WithField(logfields.V4IngressIP, ingressIP).Error("BUG, invalid IPv4 Ingress annotation in node")
} else {
newNode.IPv4IngressIP = ip
}
}
if newNode.IPv6IngressIP == nil {
if ingressIP, ok := annotation.Get(k8sNode, annotation.V6IngressName, annotation.V6IngressNameAlias); !ok || ingressIP == "" {
scopedLog.Debug("Empty IPv6 Ingress annotation in node")
} else if ip := net.ParseIP(ingressIP); ip == nil {
scopedLog.WithField(logfields.V6IngressIP, ingressIP).Error("BUG, invalid IPv6 Ingress annotation in node")
} else {
newNode.IPv6IngressIP = ip
}
}
return newNode
}