-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskynet.go
256 lines (232 loc) · 7.12 KB
/
skynet.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
package main
import (
"runtime"
"os"
"flag"
"fmt"
"github.com/containernetworking/cni/pkg/skel"
"encoding/json"
log "github.com/Sirupsen/logrus"
"skynet/neutron"
"strings"
"errors"
plugin "skynet/pluginv2"
"skynet/util"
"github.com/containernetworking/cni/pkg/version"
"github.com/containernetworking/cni/pkg/types"
"k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/tools/clientcmd"
"k8s.io/client-go/1.5/pkg/api/v1"
)
var hostname string
//consts in pod spec to specify pod network config
const NETWORK_ID_KEY = "skynet/network_id"
const SUBNET_ID_KEY = "skynet/subnet_id"
const SECURITY_GROUP_IDS_KEY = "skynet/security_group_ids"
const IP_KEY = "skynet/ip"
const VERSION = "1.0"
func init() {
runtime.LockOSThread()
hostname, _ = os.Hostname()
}
func getPodSpec(conf *util.NetConf, podName, namespace string) (pod *v1.Pod, err error) {
config, err := clientcmd.BuildConfigFromFlags(conf.Kubernetes.K8sAPIRoot, conf.Kubernetes.Kubeconfig)
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
pod, err = client.Core().Pods(namespace).Get(podName)
return
}
func getPodNetwork(conf *util.NetConf, neutronApi *neutron.NeutronApi, podName, namespace string) (network *neutron.OpenStackNetwork, subnet *neutron.OpenStackSubnet, ipSpecified string, securityGroupIds []string, err error) {
where := func() {
_, file, line, _ := runtime.Caller(1)
log.Printf("%s:%d", file, line)
}
pod, err := getPodSpec(conf, podName, namespace)
if err != nil {
return nil, nil, ipSpecified, securityGroupIds, err
}
annotations := pod.ObjectMeta.Annotations
if value, ok := annotations[SECURITY_GROUP_IDS_KEY]; ok {
securityGroupIds = strings.Split(value, ",")
}else if conf.Neutron.DefaultSecurityGroupIds != "" {
securityGroupIds = strings.Split(conf.Neutron.DefaultSecurityGroupIds, ",")
}else {
securityGroupIds = make([]string, 0)
}
var networkId string
if value, ok := annotations[NETWORK_ID_KEY]; ok {
networkId = value
}else {
networkId = conf.Neutron.DefaultNetworkId
}
var subnetId string
if value, ok := annotations[SUBNET_ID_KEY]; ok {
subnetId = value
}else if conf.Neutron.DefaultSubnetId != "" {
subnetId = conf.Neutron.DefaultSubnetId
}
if value, ok := annotations[IP_KEY]; ok {
ipSpecified = value
}
if subnetId != "" {
subnet, err = neutronApi.GetSubnet(subnetId)
log.Println(subnet, &subnet, err)
if err != nil {
return nil, nil, ipSpecified, securityGroupIds, err
}
network, err = neutronApi.GetNetwork(subnet.NetworkId)
if err != nil {
return nil, nil, ipSpecified, securityGroupIds, err
}
return network, subnet, ipSpecified, securityGroupIds, nil
}else if networkId != "" {
network, err = neutronApi.GetNetwork(networkId)
where()
if err != nil {
return nil, nil, ipSpecified, securityGroupIds, err
}
return network, subnet, ipSpecified, securityGroupIds, nil
}
where()
return nil, nil, ipSpecified, securityGroupIds, errors.New("unable to get pod network config")
}
func cmdAdd(args *skel.CmdArgs) error {
// Unmarshall the network config, and perform validation
conf := util.NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %v", err)
}
util.ConfigureLogging(conf.LogLevel)
workload, podName, namespace, err := util.GetPortIdentifier(args)
if err != nil {
return err
}
logger := util.CreateContextLogger(workload)
// Allow the hostname to be overridden by the network config
if conf.Hostname != "" {
hostname = conf.Hostname
}
logger.WithFields(log.Fields{
"WorkLoad": workload,
"Node": hostname,
}).Info("Extracted identifiers")
logger.WithFields(log.Fields{"NetConfg": conf, "Args":args}).Info("Loaded CNI NetConf")
//create port from neutron
neutronApi := neutron.NewNeutronApi(conf.Neutron.NeutronUrl)
network, subnet, ipSpecified, securityGroupIds, err := getPodNetwork(&conf, neutronApi, podName, namespace)
if err != nil {
return err
}
ports, err := neutronApi.GetPorts(workload)
var podPort *neutron.OpenStackPort
var needUpdatePort bool
if len(ports) >= 1 {
podPort = &ports[0]
for _, port := range ports[1:] {
neutronApi.DeletePort(port.Id)
}
needUpdatePort = true
}
//use Id to check port is valid or not
if podPort == nil {
if ipSpecified != "" {
err = neutronApi.EnsureIpFree(network.Id, ipSpecified)
if err != nil {
return err
}
}
podPort, err = neutronApi.CreatePort(network, workload, ipSpecified, podName, namespace, subnet, securityGroupIds)
if err != nil {
return err
}
}
if needUpdatePort {
podPort, err = neutronApi.UpdatePort(podPort.Id, hostname)
if err != nil {
return nil
}
}
fmt.Fprintf(os.Stderr, "Skynet pod port is %v\n", podPort)
podIP := podPort.FixedIPS[0]
if subnet == nil {
subnet, err = neutronApi.GetSubnet(podIP.SubnetId)
fmt.Fprintln(os.Stderr, subnet, &subnet)
}
fmt.Fprintf(os.Stderr, "Skynet begin to setup container interface\n")
err = plugin.SetupInterface(args, podName, namespace, network, subnet, podPort, podIP, conf)
if err != nil {
fmt.Fprintf(os.Stderr, "Skynet setup container with err %v\n", err)
neutronApi.DeletePort(podPort.Id)
return err
}
//_, ipNet, _ := net.ParseCIDR(podIP.IpAddress + "/24")
result := types.Result{}
//kubelt 1.3.2 requires must output to stdout
data, _ := json.Marshal(result)
fmt.Fprintf(os.Stdout, string(data))
return nil
}
func cmdDel(args *skel.CmdArgs) error {
conf := util.NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("failed to load netconf: %v", err)
}
util.ConfigureLogging(conf.LogLevel)
workload, _, _, err := util.GetPortIdentifier(args)
if err != nil {
return err
}
logger := util.CreateContextLogger(workload)
// Allow the hostname to be overridden by the network config
if conf.Hostname != "" {
hostname = conf.Hostname
}
logger.WithFields(log.Fields{
"Workload": workload,
"Node": hostname,
"Conf":conf,
}).Info("Extracted identifiers")
// Always try to release the address. Don't deal with any errors till the endpoints are cleaned up.
fmt.Fprintf(os.Stderr, "Skynet CNI releasing IP address\n")
// Only try to delete the device if a namespace was passed in.
neutronApi := neutron.NewNeutronApi(conf.Neutron.NeutronUrl)
portId, err := neutronApi.DeletePortByName(workload)
if err != nil {
return err
}
if args.Netns != "" {
fmt.Fprintf(os.Stderr, "Calico CNI deleting device in netns %s\n", args.Netns)
err = plugin.DeleteInterface(args.Netns, args.IfName, portId, conf)
if err != nil {
return err
}
}
result := types.Result{}
//kubelt 1.3.2 requires must output to stdout
data, _ := json.Marshal(result)
fmt.Fprintf(os.Stdout, string(data))
return nil
}
func main() {
flagSet := flag.NewFlagSet("Skynet", flag.ExitOnError)
versionFlag := flagSet.Bool("version", false, "Display version")
err := flagSet.Parse(os.Args[1:])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if *versionFlag {
fmt.Println(VERSION)
os.Exit(0)
}
if err := util.AddIgnoreUnknownArgs(); err != nil {
os.Exit(1)
}
versionInfo := version.PluginSupports("1.0")
skel.PluginMain(cmdAdd, cmdDel, versionInfo)
}