-
Notifications
You must be signed in to change notification settings - Fork 432
/
cni.go
227 lines (198 loc) · 6.38 KB
/
cni.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net"
"runtime"
"strings"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/request"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func init() {
// this ensures that main runs only on main thread (thread group leader).
// since namespace ops (unshare, setns) are done for a single thread, we
// must ensure that the goroutine does not jump from OS thread to thread
runtime.LockOSThread()
}
func main() {
skel.PluginMain(cmdAdd, nil, cmdDel, version.All, "")
}
func cmdAdd(args *skel.CmdArgs) error {
netConf, cniVersion, err := loadNetConf(args.StdinData)
if err != nil {
return err
}
podName, err := parseValueFromArgs("K8S_POD_NAME", args.Args)
if err != nil {
return err
}
podNamespace, err := parseValueFromArgs("K8S_POD_NAMESPACE", args.Args)
if err != nil {
return err
}
if netConf.Provider == "" && netConf.Type == util.CniTypeName && args.IfName == "eth0" {
netConf.Provider = util.OvnProvider
}
client := request.NewCniServerClient(netConf.ServerSocket)
response, err := client.Add(request.CniRequest{
CniType: netConf.Type,
PodName: podName,
PodNamespace: podNamespace,
ContainerID: args.ContainerID,
NetNs: args.Netns,
IfName: args.IfName,
Provider: netConf.Provider,
Routes: netConf.Routes,
DeviceID: netConf.DeviceID,
VfDriver: netConf.VfDriver,
})
if err != nil {
return err
}
result := generateCNIResult(cniVersion, response)
return types.PrintResult(&result, cniVersion)
}
func generateCNIResult(cniVersion string, cniResponse *request.CniResponse) current.Result {
result := current.Result{CNIVersion: cniVersion}
_, mask, _ := net.ParseCIDR(cniResponse.CIDR)
podIface := current.Interface{
Name: cniResponse.PodNicName,
Mac: cniResponse.MacAddress,
}
switch cniResponse.Protocol {
case kubeovnv1.ProtocolIPv4:
ip, route := assignV4Address(cniResponse.IpAddress, cniResponse.Gateway, mask)
result.IPs = []*current.IPConfig{&ip}
result.Routes = []*types.Route{&route}
result.Interfaces = []*current.Interface{&podIface}
case kubeovnv1.ProtocolIPv6:
ip, route := assignV6Address(cniResponse.IpAddress, cniResponse.Gateway, mask)
result.IPs = []*current.IPConfig{&ip}
result.Routes = []*types.Route{&route}
result.Interfaces = []*current.Interface{&podIface}
case kubeovnv1.ProtocolDual:
var netMask *net.IPNet
for _, cidrBlock := range strings.Split(cniResponse.CIDR, ",") {
_, netMask, _ = net.ParseCIDR(cidrBlock)
if util.CheckProtocol(cidrBlock) == kubeovnv1.ProtocolIPv4 {
ipStr := strings.Split(cniResponse.IpAddress, ",")[0]
gwStr := strings.Split(cniResponse.Gateway, ",")[0]
ip, route := assignV4Address(ipStr, gwStr, netMask)
result.IPs = append(result.IPs, &ip)
result.Routes = append(result.Routes, &route)
} else if util.CheckProtocol(cidrBlock) == kubeovnv1.ProtocolIPv6 {
ipStr := strings.Split(cniResponse.IpAddress, ",")[1]
gwStr := strings.Split(cniResponse.Gateway, ",")[1]
ip, route := assignV6Address(ipStr, gwStr, netMask)
result.IPs = append(result.IPs, &ip)
result.Routes = append(result.Routes, &route)
}
}
result.Interfaces = []*current.Interface{&podIface}
}
return result
}
func cmdDel(args *skel.CmdArgs) error {
netConf, _, err := loadNetConf(args.StdinData)
if err != nil {
return err
}
client := request.NewCniServerClient(netConf.ServerSocket)
podName, err := parseValueFromArgs("K8S_POD_NAME", args.Args)
if err != nil {
return err
}
podNamespace, err := parseValueFromArgs("K8S_POD_NAMESPACE", args.Args)
if err != nil {
return err
}
if netConf.Type == util.CniTypeName && args.IfName == "eth0" {
netConf.Provider = util.OvnProvider
}
return client.Del(request.CniRequest{
CniType: netConf.Type,
PodName: podName,
PodNamespace: podNamespace,
ContainerID: args.ContainerID,
NetNs: args.Netns,
IfName: args.IfName,
Provider: netConf.Provider,
DeviceID: netConf.DeviceID,
})
}
type ipamConf struct {
ServerSocket string `json:"server_socket"`
Provider string `json:"provider"`
}
type netConf struct {
types.NetConf
ServerSocket string `json:"server_socket"`
Provider string `json:"provider"`
Routes []request.Route `json:"routes"`
IPAM *ipamConf `json:"ipam"`
// PciAddrs in case of using sriov
DeviceID string `json:"deviceID"`
VfDriver string `json:"vf_driver"`
}
func loadNetConf(bytes []byte) (*netConf, string, error) {
n := &netConf{}
if err := json.Unmarshal(bytes, n); err != nil {
return nil, "", fmt.Errorf("failed to load netconf: %v", err)
}
if n.Type != util.CniTypeName && n.IPAM != nil {
n.Provider = n.IPAM.Provider
n.ServerSocket = n.IPAM.ServerSocket
}
if n.ServerSocket == "" {
return nil, "", fmt.Errorf("server_socket is required in cni.conf, %+v", n)
}
if n.Provider == "" {
n.Provider = util.OvnProvider
}
return n, n.CNIVersion, nil
}
func parseValueFromArgs(key, argString string) (string, error) {
if argString == "" {
return "", errors.New("CNI_ARGS is required")
}
args := strings.Split(argString, ";")
for _, arg := range args {
if strings.HasPrefix(arg, fmt.Sprintf("%s=", key)) {
value := strings.TrimPrefix(arg, fmt.Sprintf("%s=", key))
if len(value) > 0 {
return value, nil
}
}
}
return "", fmt.Errorf("%s is required in CNI_ARGS", key)
}
func assignV4Address(ipAddress, gateway string, mask *net.IPNet) (current.IPConfig, types.Route) {
ip := current.IPConfig{
Version: "4",
Address: net.IPNet{IP: net.ParseIP(ipAddress).To4(), Mask: mask.Mask},
Gateway: net.ParseIP(gateway).To4(),
}
route := types.Route{
Dst: net.IPNet{IP: net.ParseIP("0.0.0.0").To4(), Mask: net.CIDRMask(0, 32)},
GW: net.ParseIP(gateway).To4(),
}
return ip, route
}
func assignV6Address(ipAddress, gateway string, mask *net.IPNet) (current.IPConfig, types.Route) {
ip := current.IPConfig{
Version: "6",
Address: net.IPNet{IP: net.ParseIP(ipAddress).To16(), Mask: mask.Mask},
Gateway: net.ParseIP(gateway).To16(),
}
route := types.Route{
Dst: net.IPNet{IP: net.ParseIP("::").To16(), Mask: net.CIDRMask(0, 128)},
GW: net.ParseIP(gateway).To16(),
}
return ip, route
}