-
Notifications
You must be signed in to change notification settings - Fork 153
/
k8s.go
123 lines (104 loc) · 3.5 KB
/
k8s.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
package daemon
import (
"fmt"
"github.com/AliyunContainerService/terway/deviceplugin"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net"
"os"
"strings"
)
const K8S_POD_NAME_ARGS = "K8S_POD_NAME"
const K8S_POD_NAMESPACE_ARGS = "K8S_POD_NAMESPACE"
const K8S_SYSTEM_NAMESPACE = "kube-system"
const K8S_KUBEADM_CONFIGMAP = "kubeadm-config"
const K8S_KUBEADM_CONFIGMAP_NETWORKING = "MasterConfiguration"
const POD_NEED_ENI = "k8s.aliyun.com/eni"
const POD_INGRESS_BANDWIDTH = "k8s.aliyun.com/ingress-bandwidth"
const POD_EGRESS_BANDWIDTH = "k8s.aliyun.com/egress-bandwidth"
func (eni *ENIService) podCIDR() (*net.IPNet, error) {
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
podName := os.Getenv("POD_NAME")
podNamespace := os.Getenv("POD_NAMESPACE")
if podName == "" || podNamespace == "" {
return nil, fmt.Errorf("env variables POD_NAME and POD_NAMESPACE must be set")
}
pod, err := eni.client.CoreV1().Pods(podNamespace).Get(podName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error retrieving pod spec for '%s/%s': %v", podNamespace, podName, err)
}
nodeName = pod.Spec.NodeName
if nodeName == "" {
return nil, fmt.Errorf("node name not present in pod spec '%s/%s'", podNamespace, podName)
}
}
node, err := eni.client.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error retrieving node spec for '%s': %v", nodeName, err)
}
if node.Spec.PodCIDR == "" {
return nil, fmt.Errorf("node %q pod cidr not assigned", nodeName)
}
_, cidr, err := net.ParseCIDR(node.Spec.PodCIDR)
if err != nil {
return nil, err
}
return cidr, nil
}
func getPodArgsFromArgs(cniArgs string) map[string]string {
argsMap := make(map[string]string)
for _, args := range strings.Split(cniArgs, ";") {
argsKV := strings.SplitN(args, "=", 2)
if len(argsKV) == 2 {
argsMap[argsKV[0]] = argsKV[1]
}
}
return argsMap
}
func (eni *ENIService) getPodInfo(namespace, id string) (*podInfo, error) {
pod, err := eni.client.CoreV1().Pods(namespace).Get(id, metav1.GetOptions{})
if err != nil {
return nil, err
}
pInfo := &podInfo{}
podAnnotation := pod.GetAnnotations()
if needEni, ok := podAnnotation[POD_NEED_ENI]; ok && (needEni != "" && needEni != "false" && needEni != "0") {
pInfo.useENI = true
}
for _, c := range pod.Spec.Containers {
if _, ok := c.Resources.Requests[deviceplugin.DefaultResourceName]; ok {
pInfo.useENI = true
break
}
}
if ingressBandWidth, ok := podAnnotation[POD_INGRESS_BANDWIDTH]; ok {
pInfo.tcIngress = ingressBandWidth
}
if egressBandWidth, ok := podAnnotation[POD_EGRESS_BANDWIDTH]; ok {
pInfo.tcEgress = egressBandWidth
}
return pInfo, nil
}
func (eni *ENIService) getSvcCidr() (string, error) {
kubeadmConfigMap, err := eni.client.CoreV1().ConfigMaps(K8S_SYSTEM_NAMESPACE).Get(K8S_KUBEADM_CONFIGMAP, metav1.GetOptions{})
if err != nil {
return "", err
}
kubeNetworkingConfig, ok := kubeadmConfigMap.Data[K8S_KUBEADM_CONFIGMAP_NETWORKING]
if !ok {
return "", fmt.Errorf("cannot found kubeproxy config for svc cidr")
}
configMap := make(map[interface{}]interface{})
err = yaml.Unmarshal([]byte(kubeNetworkingConfig), &configMap)
if networkingObj, ok := configMap["networking"]; ok {
if networkingMap, ok := networkingObj.(map[interface{}]interface{}); ok {
if svcObj, ok := networkingMap["serviceSubnet"]; ok {
if svcStr, ok := svcObj.(string); ok {
return svcStr, nil
}
}
}
}
return "", fmt.Errorf("cannot found kubeproxy config for svc cidr")
}