-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
nodes_info.go
107 lines (94 loc) · 3.08 KB
/
nodes_info.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package helpers
import (
"context"
"fmt"
"strings"
)
type NodesInfo struct {
K8s1NodeName string
K8s2NodeName string
OutsideNodeName string
K8s1IP string
K8s2IP string
OutsideIP string
PrivateIface string
PrimaryK8s1IPv6 string
PrimaryK8s2IPv6 string
OutsideIPv6 string
SecondaryK8s1IPv4 string
SecondaryK8s2IPv4 string
SecondaryK8s1IPv6 string
SecondaryK8s2IPv6 string
}
func GetNodesInfo(kubectl *Kubectl) (*NodesInfo, error) {
var (
ni NodesInfo
err error
)
ni.K8s1NodeName, ni.K8s1IP = kubectl.GetNodeInfo(K8s1)
ni.K8s2NodeName, ni.K8s2IP = kubectl.GetNodeInfo(K8s2)
if ExistNodeWithoutCilium() {
ni.OutsideNodeName, ni.OutsideIP = kubectl.GetNodeInfo(kubectl.GetFirstNodeWithoutCiliumLabel())
}
ni.PrivateIface, err = kubectl.GetPrivateIface(K8s1)
if err != nil {
return nil, fmt.Errorf("Cannot determine private iface: %w", err)
}
if GetCurrentIntegration() == "" || IsIntegration(CIIntegrationKind) {
ni.PrimaryK8s1IPv6, err = GetIPv6AddrForIface(kubectl, ni.K8s1NodeName, ni.PrivateIface)
if err != nil {
return nil, err
}
ni.PrimaryK8s2IPv6, err = GetIPv6AddrForIface(kubectl, ni.K8s2NodeName, ni.PrivateIface)
if err != nil {
return nil, err
}
// If there is no integration we assume that these are running in vagrant environment
// so have a Secondary interface with both IPv6 and IPv4 addresses.
ni.SecondaryK8s1IPv4, err = GetIPv4AddrForIface(kubectl, ni.K8s1NodeName, SecondaryIface)
if err != nil {
return nil, err
}
ni.SecondaryK8s2IPv4, err = GetIPv4AddrForIface(kubectl, ni.K8s2NodeName, SecondaryIface)
if err != nil {
return nil, err
}
ni.SecondaryK8s1IPv6, err = GetIPv6AddrForIface(kubectl, ni.K8s1NodeName, SecondaryIface)
if err != nil {
return nil, err
}
ni.SecondaryK8s2IPv6, err = GetIPv6AddrForIface(kubectl, ni.K8s2NodeName, SecondaryIface)
if err != nil {
return nil, err
}
if ExistNodeWithoutCilium() {
ni.OutsideIPv6, err = GetIPv6AddrForIface(kubectl, ni.OutsideNodeName, ni.PrivateIface)
if err != nil {
return nil, err
}
}
}
return &ni, nil
}
func GetIPv4AddrForIface(kubectl *Kubectl, nodeName, iface string) (string, error) {
cmd := fmt.Sprintf("ip -family inet -oneline address show dev %s scope global | awk '{print $4}' | cut -d/ -f1", iface)
res := kubectl.ExecInHostNetNS(context.TODO(), nodeName, cmd)
if err := res.GetError(); err != nil {
return "", fmt.Errorf("Cannot get IPv4 address for interface(%q): %w",
iface, err)
}
ipv4 := strings.Trim(res.Stdout(), "\n")
return ipv4, nil
}
func GetIPv6AddrForIface(kubectl *Kubectl, nodeName, iface string) (string, error) {
cmd := fmt.Sprintf("ip -family inet6 -oneline address show dev %s scope global | awk '{print $4}' | cut -d/ -f1", iface)
res := kubectl.ExecInHostNetNS(context.TODO(), nodeName, cmd)
if err := res.GetError(); err != nil {
return "", fmt.Errorf("Cannot get IPv6 address for interface(%q): %w",
iface, err)
}
ipv6 := strings.Trim(res.Stdout(), "\n")
return ipv6, nil
}