forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
353 lines (291 loc) · 8.35 KB
/
utils.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 main
import (
"errors"
"fmt"
"net"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api"
apierrs "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/labels"
k8sexec "k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/node"
"k8s.io/kubernetes/pkg/util/sysctl"
"k8s.io/kubernetes/pkg/util/wait"
)
var (
invalidIfaces = []string{"lo", "docker0", "flannel.1", "cbr0"}
nsSvcLbRegex = regexp.MustCompile(`(.*)/(.*):(.*)|(.*)/(.*)`)
vethRegex = regexp.MustCompile(`^veth.*`)
lvsRegex = regexp.MustCompile(`NAT`)
)
type nodeInfo struct {
iface string
ip string
netmask int
}
// getNetworkInfo returns information of the node where the pod is running
func getNetworkInfo(ip string) (*nodeInfo, error) {
iface, _, mask := interfaceByIP(ip)
return &nodeInfo{
iface: iface,
ip: ip,
netmask: mask,
}, nil
}
type podInfo struct {
PodName string
PodNamespace string
NodeIP string
}
// getPodDetails returns runtime information about the pod: name, namespace and IP of the node
func getPodDetails(kubeClient *unversioned.Client) (*podInfo, error) {
podName := os.Getenv("POD_NAME")
podNs := os.Getenv("POD_NAMESPACE")
if podName == "" || podNs == "" {
return nil, fmt.Errorf("Please check the manifest (for missing POD_NAME or POD_NAMESPACE env variables)")
}
err := waitForPodRunning(kubeClient, podNs, podName, time.Millisecond*200, time.Second*30)
if err != nil {
return nil, err
}
pod, _ := kubeClient.Pods(podNs).Get(podName)
if pod == nil {
return nil, fmt.Errorf("Unable to get POD information")
}
node, err := kubeClient.Nodes().Get(pod.Spec.NodeName)
if err != nil {
return nil, err
}
var externalIP string
for _, address := range node.Status.Addresses {
if address.Type == api.NodeExternalIP {
if address.Address != "" {
externalIP = address.Address
break
}
}
if externalIP == "" && address.Type == api.NodeLegacyHostIP {
externalIP = address.Address
}
}
return &podInfo{
PodName: podName,
PodNamespace: podNs,
NodeIP: externalIP,
}, nil
}
// netInterfaces returns a slice containing the local network interfaces
// excluding lo, docker0, flannel.1 and veth interfaces.
func netInterfaces() []net.Interface {
validIfaces := []net.Interface{}
ifaces, err := net.Interfaces()
if err != nil {
return validIfaces
}
for _, iface := range ifaces {
if !vethRegex.MatchString(iface.Name) && stringSlice(invalidIfaces).pos(iface.Name) == -1 {
validIfaces = append(validIfaces, iface)
}
}
return validIfaces
}
// interfaceByIP returns the local network interface name that is using the
// specified IP address. If no interface is found returns an empty string.
func interfaceByIP(ip string) (string, string, int) {
for _, iface := range netInterfaces() {
ifaceIP, mask, err := ipByInterface(iface.Name)
if err == nil && ip == ifaceIP {
return iface.Name, ip, mask
}
}
return "", "", 0
}
func ipByInterface(name string) (string, int, error) {
iface, err := net.InterfaceByName(name)
if err != nil {
return "", 32, err
}
addrs, err := iface.Addrs()
if err != nil {
return "", 32, err
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip := ipnet.IP.String()
ones, _ := ipnet.Mask.Size()
mask := ones
return ip, mask, nil
}
}
}
return "", 32, errors.New("Found no IPv4 addresses.")
}
type stringSlice []string
// pos returns the position of a string in a slice.
// If it does not exists in the slice returns -1.
func (slice stringSlice) pos(value string) int {
for p, v := range slice {
if v == value {
return p
}
}
return -1
}
// getClusterNodesIP returns the IP address of each node in the kubernetes cluster
func getClusterNodesIP(kubeClient *unversioned.Client, nodeSelector string) (clusterNodes []string) {
listOpts := api.ListOptions{}
if nodeSelector != "" {
label, err := labels.Parse(nodeSelector)
if err != nil {
glog.Fatalf("'%v' is not a valid selector: %v", nodeSelector, err)
}
listOpts.LabelSelector = label
}
nodes, err := kubeClient.Nodes().List(listOpts)
if err != nil {
glog.Fatalf("Error getting running nodes: %v", err)
}
for _, nodo := range nodes.Items {
nodeIP, err := node.GetNodeHostIP(&nodo)
if err == nil {
clusterNodes = append(clusterNodes, nodeIP.String())
}
}
sort.Strings(clusterNodes)
return
}
// getNodeNeighbors returns a list of IP address of the nodes
func getNodeNeighbors(nodeInfo *nodeInfo, clusterNodes []string) (neighbors []string) {
for _, neighbor := range clusterNodes {
if nodeInfo.ip != neighbor {
neighbors = append(neighbors, neighbor)
}
}
sort.Strings(neighbors)
return
}
// getPriority returns the priority of one node using the
// IP address as key. It starts in 100
func getNodePriority(ip string, nodes []string) int {
return 100 + stringSlice(nodes).pos(ip)
}
// loadIPVModule load module require to use keepalived
func loadIPVModule() error {
out, err := k8sexec.New().Command("modprobe", "ip_vs").CombinedOutput()
if err != nil {
glog.V(2).Infof("Error loading ip_vip: %s, %v", string(out), err)
return err
}
_, err = os.Stat("/proc/net/ip_vs")
return err
}
// changeSysctl changes the required network setting in /proc to get
// keepalived working in the local system.
func changeSysctl() error {
for k, v := range sysctlAdjustments {
if err := sysctl.SetSysctl(k, v); err != nil {
return err
}
}
return nil
}
func appendIfMissing(slice []string, item string) []string {
for _, elem := range slice {
if elem == item {
return slice
}
}
return append(slice, item)
}
func parseNsName(input string) (string, string, error) {
nsName := strings.Split(input, "/")
if len(nsName) != 2 {
return "", "", fmt.Errorf("invalid format (namespace/name) found in '%v'", input)
}
return nsName[0], nsName[1], nil
}
func parseNsSvcLVS(input string) (string, string, string, error) {
nsSvcLb := nsSvcLbRegex.FindStringSubmatch(input)
if len(nsSvcLb) != 6 {
return "", "", "", fmt.Errorf("invalid format (namespace/service name[:NAT|DR]) found in '%v'", input)
}
ns := nsSvcLb[1]
svc := nsSvcLb[2]
kind := nsSvcLb[3]
if ns == "" {
ns = nsSvcLb[4]
}
if svc == "" {
svc = nsSvcLb[5]
}
if kind == "" {
kind = "NAT"
}
if !lvsRegex.MatchString(kind) {
return "", "", "", fmt.Errorf("invalid LVS method. Only NAT and DR are supported: %v", kind)
}
return ns, svc, kind, nil
}
type nodeSelector map[string]string
func (ns nodeSelector) String() string {
kv := []string{}
for key, val := range ns {
kv = append(kv, fmt.Sprintf("%v=%v", key, val))
}
return strings.Join(kv, ",")
}
func parseNodeSelector(data map[string]string) string {
return nodeSelector(data).String()
}
func waitForPodRunning(kubeClient *unversioned.Client, ns, podName string, interval, timeout time.Duration) error {
condition := func(pod *api.Pod) (bool, error) {
if pod.Status.Phase == api.PodRunning {
return true, nil
}
return false, nil
}
return waitForPodCondition(kubeClient, ns, podName, condition, interval, timeout)
}
// waitForPodCondition waits for a pod in state defined by a condition (func)
func waitForPodCondition(kubeClient *unversioned.Client, ns, podName string, condition func(pod *api.Pod) (bool, error),
interval, timeout time.Duration) error {
err := wait.PollImmediate(interval, timeout, func() (bool, error) {
pod, err := kubeClient.Pods(ns).Get(podName)
if err != nil {
if apierrs.IsNotFound(err) {
return false, nil
}
}
done, err := condition(pod)
if err != nil {
return false, err
}
if done {
return true, nil
}
return false, nil
})
if err != nil {
return fmt.Errorf("timed out waiting to observe own status as Running")
}
return nil
}