-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathlinux_validate.go
236 lines (205 loc) · 7.26 KB
/
linux_validate.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
package validate
import (
"context"
"encoding/json"
"github.com/Azure/azure-container-networking/cns"
restserver "github.com/Azure/azure-container-networking/cns/restserver"
acnk8s "github.com/Azure/azure-container-networking/test/internal/kubernetes"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
)
const (
cnsLabelSelector = "k8s-app=azure-cns"
ciliumLabelSelector = "k8s-app=cilium"
overlayClusterLabelName = "overlay"
)
var (
restartNetworkCmd = []string{"bash", "-c", "systemctl restart systemd-networkd"}
cnsManagedStateFileCmd = []string{"bash", "-c", "cat /var/run/azure-cns/azure-endpoints.json"}
azureVnetStateFileCmd = []string{"bash", "-c", "cat /var/run/azure-vnet.json"}
azureVnetIpamStateCmd = []string{"bash", "-c", "cat /var/run/azure-vnet-ipam.json"}
ciliumStateFileCmd = []string{"bash", "-c", "cilium endpoint list -o json"}
cnsCachedAssignedIPStateCmd = []string{"curl", "localhost:10090/debug/ipaddresses", "-d", "{\"IPConfigStateFilter\":[\"Assigned\"]}"}
)
type stateFileIpsFunc func([]byte) (map[string]string, error)
var linuxChecksMap = map[string][]check{
"cilium": {
{"cns", cnsManagedStateFileIps, cnsLabelSelector, privilegedNamespace, cnsManagedStateFileCmd}, // cns configmap "ManageEndpointState": true, | Endpoints managed in CNS State File
{"cilium", ciliumStateFileIps, ciliumLabelSelector, privilegedNamespace, ciliumStateFileCmd},
{"cns cache", cnsCacheStateFileIps, cnsLabelSelector, privilegedNamespace, cnsCachedAssignedIPStateCmd},
},
"cniv1": {
{"azure-vnet", azureVnetStateIps, privilegedLabelSelector, privilegedNamespace, azureVnetStateFileCmd},
{"azure-vnet-ipam", azureVnetIpamStateIps, privilegedLabelSelector, privilegedNamespace, azureVnetIpamStateCmd},
},
"cniv2": {
{"cns cache", cnsCacheStateFileIps, cnsLabelSelector, privilegedNamespace, cnsCachedAssignedIPStateCmd},
{"azure-vnet", azureVnetStateIps, privilegedLabelSelector, privilegedNamespace, azureVnetStateFileCmd}, // cns configmap "ManageEndpointState": false, | Endpoints managed in CNI State File
},
"dualstack": {
{"cns cache", cnsCacheStateFileIps, cnsLabelSelector, privilegedNamespace, cnsCachedAssignedIPStateCmd},
{"azure dualstackoverlay", azureVnetStateIps, privilegedLabelSelector, privilegedNamespace, azureVnetStateFileCmd},
},
}
type CnsManagedState struct {
Endpoints map[string]restserver.EndpointInfo `json:"Endpoints"`
}
type CNSLocalCache struct {
IPConfigurationStatus []cns.IPConfigurationStatus `json:"IPConfigurationStatus"`
}
type CiliumEndpointStatus struct {
Status NetworkingStatus `json:"status"`
}
type NetworkingStatus struct {
Networking NetworkingAddressing `json:"networking"`
}
type NetworkingAddressing struct {
Addresses []Address `json:"addressing"`
InterfaceName string `json:"interface-name"`
}
type Address struct {
Addr string `json:"ipv4"`
}
// parse azure-vnet.json
// azure cni manages endpoint state
type AzureCniState struct {
AzureCniState AzureVnetNetwork `json:"Network"`
}
type AzureVnetNetwork struct {
Version string `json:"Version"`
TimeStamp string `json:"TimeStamp"`
ExternalInterfaces map[string]InterfaceInfo `json:"ExternalInterfaces"`
}
type InterfaceInfo struct {
Name string `json:"Name"`
Networks map[string]AzureVnetNetworkInfo `json:"Networks"` // key: networkName, value: AzureVnetNetworkInfo
}
type AzureVnetInfo struct {
Name string
Networks map[string]AzureVnetNetworkInfo // key: network name, value: NetworkInfo
}
type AzureVnetNetworkInfo struct {
ID string
Mode string
Subnets []Subnet
Endpoints map[string]AzureVnetEndpointInfo // key: azure endpoint name, value: AzureVnetEndpointInfo
PodName string
}
type Subnet struct {
Family int
Prefix Prefix
Gateway string
PrimaryIP string
}
type Prefix struct {
IP string
Mask string
}
type AzureVnetEndpointInfo struct {
IfName string
MacAddress string
IPAddresses []Prefix
PodName string
}
func cnsManagedStateFileIps(result []byte) (map[string]string, error) {
var cnsResult CnsManagedState
err := json.Unmarshal(result, &cnsResult)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal cns endpoint list")
}
cnsPodIps := make(map[string]string)
for _, v := range cnsResult.Endpoints {
for ifName, ip := range v.IfnameToIPMap {
if ifName == "eth0" {
ip := ip.IPv4[0].IP.String()
cnsPodIps[ip] = v.PodName
}
}
}
return cnsPodIps, nil
}
func ciliumStateFileIps(result []byte) (map[string]string, error) {
var ciliumResult []CiliumEndpointStatus
err := json.Unmarshal(result, &ciliumResult)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal cilium endpoint list")
}
ciliumPodIps := make(map[string]string)
for _, v := range ciliumResult {
for _, addr := range v.Status.Networking.Addresses {
if addr.Addr != "" {
ciliumPodIps[addr.Addr] = v.Status.Networking.InterfaceName
}
}
}
return ciliumPodIps, nil
}
func azureVnetStateIps(result []byte) (map[string]string, error) {
var azureVnetResult AzureCniState
err := json.Unmarshal(result, &azureVnetResult)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal azure vnet")
}
azureVnetPodIps := make(map[string]string)
for _, v := range azureVnetResult.AzureCniState.ExternalInterfaces {
for _, v := range v.Networks {
for _, e := range v.Endpoints {
for _, v := range e.IPAddresses {
// collect both ipv4 and ipv6 addresses
azureVnetPodIps[v.IP] = e.IfName
}
}
}
}
return azureVnetPodIps, nil
}
func azureVnetIpamStateIps(result []byte) (map[string]string, error) {
var azureVnetIpamResult AzureVnetIpam
err := json.Unmarshal(result, &azureVnetIpamResult)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal azure vnet ipam")
}
azureVnetIpamPodIps := make(map[string]string)
for _, v := range azureVnetIpamResult.IPAM.AddrSpaces {
for _, v := range v.Pools {
for _, v := range v.Addresses {
if v.InUse {
azureVnetIpamPodIps[v.Addr.String()] = v.Addr.String()
}
}
}
}
return azureVnetIpamPodIps, nil
}
// Linux only function
func (v *Validator) validateRestartNetwork(ctx context.Context) error {
nodes, err := acnk8s.GetNodeList(ctx, v.clientset)
if err != nil {
return errors.Wrapf(err, "failed to get node list")
}
for index := range nodes.Items {
node := nodes.Items[index]
if node.Status.NodeInfo.OperatingSystem != string(corev1.Linux) {
continue
}
// get the privileged pod
pod, err := acnk8s.GetPodsByNode(ctx, v.clientset, privilegedNamespace, privilegedLabelSelector, node.Name)
if err != nil {
return errors.Wrapf(err, "failed to get privileged pod")
}
if len(pod.Items) == 0 {
return errors.Errorf("there are no privileged pods on node - %v", node.Name)
}
privilegedPod := pod.Items[0]
// exec into the pod to get the state file
_, err = acnk8s.ExecCmdOnPod(ctx, v.clientset, privilegedNamespace, privilegedPod.Name, restartNetworkCmd, v.config)
if err != nil {
return errors.Wrapf(err, "failed to exec into privileged pod %s on node %s", privilegedPod.Name, node.Name)
}
err = acnk8s.WaitForPodsRunning(ctx, v.clientset, "", "")
if err != nil {
return errors.Wrapf(err, "failed to wait for pods running")
}
}
return nil
}