This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
386 lines (329 loc) · 9.65 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
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
// Copyright 2016-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package node
import (
"encoding/json"
"net"
"path"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/defaults"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/kvstore/store"
"github.com/cilium/cilium/pkg/node/addressing"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/source"
)
// Identity represents the node identity of a node.
type Identity struct {
Name string
Cluster string
}
// String returns the string representation on NodeIdentity.
func (nn Identity) String() string {
return path.Join(nn.Cluster, nn.Name)
}
// ParseCiliumNode parses a CiliumNode custom resource and returns a Node
// instance. Invalid IP and CIDRs are silently ignored
func ParseCiliumNode(n *ciliumv2.CiliumNode) (node Node) {
node = Node{
Name: n.Name,
EncryptionKey: uint8(n.Spec.Encryption.Key),
Cluster: option.Config.ClusterName,
ClusterID: option.Config.ClusterID,
Source: source.CustomResource,
}
for _, cidrString := range n.Spec.IPAM.PodCIDRs {
ipnet, err := cidr.ParseCIDR(cidrString)
if err == nil {
if ipnet.IP.To4() != nil {
node.IPv4AllocCIDR = ipnet
} else {
node.IPv6AllocCIDR = ipnet
}
}
}
if healthIP := n.Spec.HealthAddressing.IPv4; healthIP != "" {
node.IPv4HealthIP = net.ParseIP(healthIP)
}
if healthIP := n.Spec.HealthAddressing.IPv6; healthIP != "" {
node.IPv6HealthIP = net.ParseIP(healthIP)
}
for _, address := range n.Spec.Addresses {
if ip := net.ParseIP(address.IP); ip != nil {
node.IPAddresses = append(node.IPAddresses, Address{Type: address.Type, IP: ip})
}
}
return
}
// Node contains the nodes name, the list of addresses to this address
//
// +k8s:deepcopy-gen=true
type Node struct {
// Name is the name of the node. This is typically the hostname of the node.
Name string
// Cluster is the name of the cluster the node is associated with
Cluster string
IPAddresses []Address
// IPv4AllocCIDR if set, is the IPv4 address pool out of which the node
// allocates IPs for local endpoints from
IPv4AllocCIDR *cidr.CIDR
// IPv6AllocCIDR if set, is the IPv6 address pool out of which the node
// allocates IPs for local endpoints from
IPv6AllocCIDR *cidr.CIDR
// IPv4HealthIP if not nil, this is the IPv4 address of the
// cilium-health endpoint located on the node.
IPv4HealthIP net.IP
// IPv6HealthIP if not nil, this is the IPv6 address of the
// cilium-health endpoint located on the node.
IPv6HealthIP net.IP
// ClusterID is the unique identifier of the cluster
ClusterID int
// Source is the source where the node configuration was generated / created.
Source source.Source
// Key index used for transparent encryption or 0 for no encryption
EncryptionKey uint8
}
// Fullname returns the node's full name including the cluster name if a
// cluster name value other than the default value has been specified
func (n *Node) Fullname() string {
if n.Cluster != defaults.ClusterName {
return path.Join(n.Cluster, n.Name)
}
return n.Name
}
// Address is a node address which contains an IP and the address type.
//
// +k8s:deepcopy-gen=true
type Address struct {
Type addressing.AddressType
IP net.IP
}
func (n *Node) getNodeIP(ipv6 bool) (net.IP, addressing.AddressType) {
var (
backupIP net.IP
ipType addressing.AddressType
)
for _, addr := range n.IPAddresses {
if (ipv6 && addr.IP.To4() != nil) ||
(!ipv6 && addr.IP.To4() == nil) {
continue
}
switch addr.Type {
// Ignore CiliumInternalIPs
case addressing.NodeCiliumInternalIP:
continue
// Always prefer a cluster internal IP
case addressing.NodeInternalIP:
return addr.IP, addr.Type
case addressing.NodeExternalIP:
// Fall back to external Node IP
// if no internal IP could be found
backupIP = addr.IP
ipType = addr.Type
default:
// As a last resort, if no internal or external
// IP was found, use any node address available
if backupIP == nil {
backupIP = addr.IP
ipType = addr.Type
}
}
}
return backupIP, ipType
}
// GetNodeIP returns one of the node's IP addresses available with the
// following priority:
// - NodeInternalIP
// - NodeExternalIP
// - other IP address type
func (n *Node) GetNodeIP(ipv6 bool) net.IP {
result, _ := n.getNodeIP(ipv6)
return result
}
// GetCiliumInternalIP returns the CiliumInternalIP e.g. the IP associated
// with cilium_host on the node.
func (n *Node) GetCiliumInternalIP(ipv6 bool) net.IP {
for _, addr := range n.IPAddresses {
if (ipv6 && addr.IP.To4() != nil) ||
(!ipv6 && addr.IP.To4() == nil) {
continue
}
if addr.Type == addressing.NodeCiliumInternalIP {
return addr.IP
}
}
return nil
}
func (n *Node) getPrimaryAddress() *models.NodeAddressing {
v4, v4Type := n.getNodeIP(false)
v6, v6Type := n.getNodeIP(true)
var ipv4AllocStr, ipv6AllocStr string
if n.IPv4AllocCIDR != nil {
ipv4AllocStr = n.IPv4AllocCIDR.String()
}
if n.IPv6AllocCIDR != nil {
ipv6AllocStr = n.IPv6AllocCIDR.String()
}
var v4Str, v6Str string
if v4 != nil {
v4Str = v4.String()
}
if v6 != nil {
v6Str = v6.String()
}
return &models.NodeAddressing{
IPV4: &models.NodeAddressingElement{
Enabled: option.Config.EnableIPv4,
IP: v4Str,
AllocRange: ipv4AllocStr,
AddressType: string(v4Type),
},
IPV6: &models.NodeAddressingElement{
Enabled: option.Config.EnableIPv6,
IP: v6Str,
AllocRange: ipv6AllocStr,
AddressType: string(v6Type),
},
}
}
func (n *Node) isPrimaryAddress(addr Address, ipv4 bool) bool {
return addr.IP.String() == n.GetNodeIP(!ipv4).String()
}
func (n *Node) getSecondaryAddresses() []*models.NodeAddressingElement {
result := []*models.NodeAddressingElement{}
for _, addr := range n.IPAddresses {
ipv4 := false
if addr.IP.To4() != nil {
ipv4 = true
}
if !n.isPrimaryAddress(addr, ipv4) {
result = append(result, &models.NodeAddressingElement{
IP: addr.IP.String(),
AddressType: string(addr.Type),
})
}
}
return result
}
func (n *Node) getHealthAddresses() *models.NodeAddressing {
if n.IPv4HealthIP == nil && n.IPv6HealthIP == nil {
return nil
}
var v4Str, v6Str string
if n.IPv4HealthIP != nil {
v4Str = n.IPv4HealthIP.String()
}
if n.IPv6HealthIP != nil {
v6Str = n.IPv6HealthIP.String()
}
return &models.NodeAddressing{
IPV4: &models.NodeAddressingElement{
Enabled: option.Config.EnableIPv4,
IP: v4Str,
},
IPV6: &models.NodeAddressingElement{
Enabled: option.Config.EnableIPv6,
IP: v6Str,
},
}
}
// GetModel returns the API model representation of a node.
func (n *Node) GetModel() *models.NodeElement {
return &models.NodeElement{
Name: n.Fullname(),
PrimaryAddress: n.getPrimaryAddress(),
SecondaryAddresses: n.getSecondaryAddresses(),
HealthEndpointAddress: n.getHealthAddresses(),
}
}
// Identity returns the identity of the node
func (n *Node) Identity() Identity {
return Identity{
Name: n.Name,
Cluster: n.Cluster,
}
}
func getCluster() string {
return option.Config.ClusterName
}
// IsLocal returns true if this is the node on which the agent itself is
// running on
func (n *Node) IsLocal() bool {
return n != nil && n.Name == GetName() && n.Cluster == getCluster()
}
// PublicAttrEquals returns true only if the public attributes of both nodes
// are the same otherwise returns false.
func (n *Node) PublicAttrEquals(o *Node) bool {
if (n == nil) != (o == nil) {
return false
}
if n.Name == o.Name &&
n.Cluster == o.Cluster &&
n.IPv4HealthIP.Equal(o.IPv4HealthIP) &&
n.IPv6HealthIP.Equal(o.IPv6HealthIP) &&
n.ClusterID == o.ClusterID &&
n.Source == o.Source {
if len(n.IPAddresses) != len(o.IPAddresses) {
return false
}
for i := range n.IPAddresses {
if (n.IPAddresses[i].Type != o.IPAddresses[i].Type) ||
!n.IPAddresses[i].IP.Equal(o.IPAddresses[i].IP) {
return false
}
}
if (n.IPv4AllocCIDR == nil) != (o.IPv4AllocCIDR == nil) {
return false
}
if n.IPv4AllocCIDR.String() != o.IPv4AllocCIDR.String() {
return false
}
if (n.IPv6AllocCIDR == nil) != (o.IPv6AllocCIDR == nil) {
return false
}
if n.IPv6AllocCIDR.String() != o.IPv6AllocCIDR.String() {
return false
}
return true
}
return false
}
// GetKeyNodeName constructs the API name for the given cluster and node name.
func GetKeyNodeName(cluster, node string) string {
// WARNING - STABLE API: Changing the structure of the key may break
// backwards compatibility
return path.Join(cluster, node)
}
// GetKeyName returns the kvstore key to be used for the node
func (n *Node) GetKeyName() string {
return GetKeyNodeName(n.Cluster, n.Name)
}
// DeepKeyCopy creates a deep copy of the LocalKey
func (n *Node) DeepKeyCopy() store.LocalKey {
return n.DeepCopy()
}
// Marshal returns the node object as JSON byte slice
func (n *Node) Marshal() ([]byte, error) {
return json.Marshal(n)
}
// Unmarshal parses the JSON byte slice and updates the node receiver
func (n *Node) Unmarshal(data []byte) error {
newNode := Node{}
if err := json.Unmarshal(data, &newNode); err != nil {
return err
}
*n = newNode
return nil
}