forked from contiv/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netd.go
570 lines (492 loc) · 15.6 KB
/
netd.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/***
Copyright 2014 Cisco Systems Inc. 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 (
"flag"
"log"
"os"
"os/exec"
"strings"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/crt"
"github.com/contiv/netplugin/crtclient"
"github.com/contiv/netplugin/crtclient/docker"
"github.com/contiv/netplugin/drivers"
"github.com/contiv/netplugin/plugin"
"github.com/samalba/dockerclient"
)
// a daemon based on etcd client's Watch interface to trigger plugin's
// network provisioning interfaces
const (
RECURSIVE = true
)
type cliOpts struct {
hostLabel string
nativeInteg bool
}
func skipHost(vtepIp, homingHost, myHostLabel string) bool {
return (vtepIp == "" && homingHost != myHostLabel ||
vtepIp != "" && homingHost == myHostLabel)
}
func processCurrentState(netPlugin *plugin.NetPlugin, crt *crt.Crt,
opts cliOpts) error {
readNet := &drivers.OvsCfgEndpointState{}
readNet.StateDriver = netPlugin.StateDriver
netCfgs, err := readNet.ReadAll()
if err != nil {
return err
}
for idx, netCfg := range netCfgs {
net := netCfg.(*drivers.OvsCfgNetworkState)
log.Printf("read net key[%d] %s, populating state \n", idx, net.Id)
processNetEvent(netPlugin, net.Id, false)
}
readEp := &drivers.OvsCfgEndpointState{}
readEp.StateDriver = netPlugin.StateDriver
epCfgs, err := readEp.ReadAll()
if err != nil {
return err
}
for idx, epCfg := range epCfgs {
ep := epCfg.(*drivers.OvsCfgEndpointState)
log.Printf("read ep key[%d] %s, populating state \n", idx, ep.Id)
processEpEvent(netPlugin, crt, opts, ep.Id, false)
}
return nil
}
func processNetEvent(netPlugin *plugin.NetPlugin, netId string,
isDelete bool) (err error) {
// take a lock to ensure we are programming one event at a time.
// Also network create events need to be processed before endpoint creates
// and reverse shall happen for deletes. That order is ensured by netmaster,
// so we don't need to worry about that here
netPlugin.Lock()
defer func() { netPlugin.Unlock() }()
operStr := ""
if isDelete {
err = netPlugin.DeleteNetwork(netId)
operStr = "delete"
} else {
err = netPlugin.CreateNetwork(netId)
operStr = "create"
}
if err != nil {
log.Printf("Network operation %s failed. Error: %s", operStr, err)
} else {
log.Printf("Network operation %s succeeded", operStr)
}
return
}
func getEndpointContainerContext(state core.StateDriver, epId string) (
*crtclient.ContainerEpContext, error) {
var epCtx crtclient.ContainerEpContext
var err error
epCfg := &drivers.OvsCfgEndpointState{}
epCfg.StateDriver = state
err = epCfg.Read(epId)
if err != nil {
return &epCtx, nil
}
epCtx.NewContName = epCfg.ContName
epCtx.NewAttachUUID = epCfg.AttachUUID
cfgNet := &drivers.OvsCfgNetworkState{}
cfgNet.StateDriver = state
err = cfgNet.Read(epCfg.NetId)
if err != nil {
return &epCtx, err
}
epCtx.DefaultGw = cfgNet.DefaultGw
epCtx.SubnetLen = cfgNet.SubnetLen
operEp := &drivers.OvsOperEndpointState{}
operEp.StateDriver = state
err = operEp.Read(epId)
if err != nil {
return &epCtx, nil
}
epCtx.CurrContName = operEp.ContName
epCtx.InterfaceId = operEp.PortName
epCtx.IpAddress = operEp.IpAddress
epCtx.CurrAttachUUID = operEp.AttachUUID
return &epCtx, err
}
func getContainerEpContextByContName(state core.StateDriver, contName string) (
epCtxs []crtclient.ContainerEpContext, err error) {
var epCtx *crtclient.ContainerEpContext
contName = strings.TrimPrefix(contName, "/")
readEp := &drivers.OvsCfgEndpointState{}
readEp.StateDriver = state
epCfgs, err := readEp.ReadAll()
if err != nil {
return
}
epCtxs = make([]crtclient.ContainerEpContext, len(epCfgs))
idx := 0
for _, epCfg := range epCfgs {
cfg := epCfg.(*drivers.OvsCfgEndpointState)
if cfg.ContName != contName {
continue
}
epCtx, err = getEndpointContainerContext(state, cfg.Id)
if err != nil {
log.Printf("error '%s' getting epCfgState for ep %s \n",
err, cfg.Id)
return epCtxs[:idx], nil
}
epCtxs[idx] = *epCtx
idx = idx + 1
}
return epCtxs[:idx], nil
}
func contAttachPointAdded(epCtx *crtclient.ContainerEpContext) bool {
if epCtx.CurrAttachUUID == "" && epCtx.NewAttachUUID != "" {
return true
}
if epCtx.CurrContName == "" && epCtx.NewContName != "" {
return true
}
return false
}
func contAttachPointDeleted(epCtx *crtclient.ContainerEpContext) bool {
if epCtx.CurrAttachUUID != "" && epCtx.NewAttachUUID == "" {
return true
}
if epCtx.CurrContName != "" && epCtx.NewContName == "" {
return true
}
return false
}
func processEpEvent(netPlugin *plugin.NetPlugin, crt *crt.Crt, opts cliOpts,
epId string, isDelete bool) (err error) {
// take a lock to ensure we are programming one event at a time.
// Also network create events need to be processed before endpoint creates
// and reverse shall happen for deletes. That order is ensured by netmaster,
// so we don't need to worry about that here
netPlugin.Lock()
defer func() { netPlugin.Unlock() }()
homingHost := ""
vtepIp := ""
if !isDelete {
epCfg := &drivers.OvsCfgEndpointState{}
epCfg.StateDriver = netPlugin.StateDriver
err = epCfg.Read(epId)
if err != nil {
log.Printf("Failed to read config for ep '%s' \n", epId)
return
}
homingHost = epCfg.HomingHost
vtepIp = epCfg.VtepIp
} else {
epOper := &drivers.OvsOperEndpointState{}
epOper.StateDriver = netPlugin.StateDriver
err = epOper.Read(epId)
if err != nil {
log.Printf("Failed to read oper for ep %s, err '%s' \n", epId, err)
return
}
homingHost = epOper.HomingHost
vtepIp = epOper.VtepIp
}
if skipHost(vtepIp, homingHost, opts.hostLabel) {
log.Printf("skipping mismatching host for ep %s. EP's host %s (my host: %s)",
epId, homingHost, opts.hostLabel)
return
}
// read the context before to be compared with what changed after
contEpContext, err := getEndpointContainerContext(
netPlugin.StateDriver, epId)
if err != nil {
log.Printf("Failed to obtain the container context for ep '%s' \n",
epId)
return
}
// log.Printf("read endpoint context: %s \n", contEpContext)
operStr := ""
if isDelete {
err = netPlugin.DeleteEndpoint(epId)
operStr = "delete"
} else {
err = netPlugin.CreateEndpoint(epId)
operStr = "create"
}
if err != nil {
log.Printf("Endpoint operation %s failed. Error: %s",
operStr, err)
return
}
log.Printf("Endpoint operation %s succeeded", operStr)
// attach or detach an endpoint to a container
if isDelete || contAttachPointDeleted(contEpContext) {
err = crt.ContainerIf.DetachEndpoint(contEpContext)
if err != nil {
log.Printf("Endpoint detach container '%s' from ep '%s' failed . "+
"Error: %s", contEpContext.CurrContName, epId, err)
} else {
log.Printf("Endpoint detach container '%s' from ep '%s' succeeded",
contEpContext.CurrContName, epId)
}
}
if !isDelete && contAttachPointAdded(contEpContext) {
// re-read post ep updated state
newContEpContext, err1 := getEndpointContainerContext(
netPlugin.StateDriver, epId)
if err1 != nil {
log.Printf("Failed to obtain the container context for ep '%s' \n", epId)
return
}
contEpContext.InterfaceId = newContEpContext.InterfaceId
contEpContext.IpAddress = newContEpContext.IpAddress
contEpContext.SubnetLen = newContEpContext.SubnetLen
err = crt.ContainerIf.AttachEndpoint(contEpContext)
if err != nil {
log.Printf("Endpoint attach container '%s' to ep '%s' failed . "+
"Error: %s", contEpContext.NewContName, epId, err)
} else {
log.Printf("Endpoint attach container '%s' to ep '%s' succeeded",
contEpContext.NewContName, epId)
}
contId := crt.ContainerIf.GetContainerId(contEpContext.NewContName)
if contId != "" {
}
}
return
}
func attachContainer(stateDriver core.StateDriver, crt *crt.Crt, contName string) error {
epContexts, err := getContainerEpContextByContName(stateDriver, contName)
if err != nil {
log.Printf("Error '%s' getting Ep context for container %s \n",
err, contName)
return err
}
for _, epCtx := range epContexts {
if epCtx.NewAttachUUID != "" || epCtx.InterfaceId == "" {
log.Printf("## skipping attach on epctx %v \n", epCtx)
continue
} else {
log.Printf("## trying attach on epctx %v \n", epCtx)
}
err = crt.AttachEndpoint(&epCtx)
if err != nil {
log.Printf("Error '%s' attaching container to the network \n", err)
return err
}
}
return nil
}
func handleContainerStart(netPlugin *plugin.NetPlugin, crt *crt.Crt,
contId string) error {
// var epContexts []crtclient.ContainerEpContext
contName, err := crt.GetContainerName(contId)
if err != nil {
log.Printf("Could not find container name from container id %s \n", contId)
return err
}
err = attachContainer(netPlugin.StateDriver, crt, contName)
if err != nil {
log.Printf("error attaching container err \n", err)
}
return err
}
func handleContainerStop(netPlugin *plugin.NetPlugin, crt *crt.Crt,
contId string) error {
// If CONTIV_DIND_HOST_GOPATH env variable is set we can assume we are in docker in docker testbed
// Here we need to set the network namespace of the ports created by netplugin back to NS of the docker host
hostGoPath := os.Getenv("CONTIV_DIND_HOST_GOPATH")
if hostGoPath != "" {
osCmd := exec.Command("github.com/contiv/netplugin/scripts/dockerhost/setlocalns.sh")
osCmd.Dir = os.Getenv("GOSRC")
output, err := osCmd.Output()
if err != nil {
log.Printf("setlocalns failed. Error: %s Output: \n%s\n",
err, output)
return err
}
return err
}
return nil
}
func handleDockerEvents(event *dockerclient.Event, retErr chan error,
args ...interface{}) {
var err error
netPlugin, ok := args[0].(*plugin.NetPlugin)
if !ok {
log.Printf("error decoding netplugin in handleDocker \n")
}
crt, ok := args[1].(*crt.Crt)
if !ok {
log.Printf("error decoding netplugin in handleDocker \n")
}
log.Printf("Received event: %#v, for netPlugin %v \n", *event, netPlugin)
// XXX: with plugin (in a lib) this code will handle these events
// this cod will need to go away then
switch event.Status {
case "start":
err = handleContainerStart(netPlugin, crt, event.Id)
if err != nil {
log.Printf("error '%s' handling container %s \n", err, event.Id)
}
case "die":
log.Printf("received die event for container \n")
err = handleContainerStop(netPlugin, crt, event.Id)
if err != nil {
log.Printf("error '%s' handling container %s \n", err, event.Id)
}
// decide if we should remove the container network policy or leave
// it until ep configuration is removed
// ep configuration as instantiated can be applied to another container
// or reincarnation of the same container
}
if err != nil {
retErr <- err
}
}
func processStateEvent(netPlugin *plugin.NetPlugin, crt *crt.Crt, opts cliOpts,
rsps chan core.WatchState, retErr chan error) {
for {
// block on change notifications
rsp := <-rsps
// For now we deal with only create and delete events
state := rsp.Curr
isDelete := false
eventStr := "create"
if rsp.Curr == nil {
state = rsp.Prev
isDelete = true
eventStr = "delete"
} else if rsp.Prev != nil {
// XXX: late host binding modifies the ep-cfg state to update the host-label.
// Need to treat it as Create, revisit to see if we can prevent this
// by just triggering create once instead.
log.Printf("Received a modify event, treating it as a 'create'")
}
if nwCfg, ok := state.(*drivers.OvsCfgNetworkState); ok {
log.Printf("Received %q for network: %q", eventStr, nwCfg.Id)
processNetEvent(netPlugin, nwCfg.Id, isDelete)
}
if epCfg, ok := state.(*drivers.OvsCfgEndpointState); ok {
log.Printf("Received %q for endpoint: %q", eventStr, epCfg.Id)
processEpEvent(netPlugin, crt, opts, epCfg.Id, isDelete)
}
}
// shall never come here
retErr <- nil
}
func handleNetworkEvents(netPlugin *plugin.NetPlugin, crt *crt.Crt,
opts cliOpts, retErr chan error) {
rsps := make(chan core.WatchState)
go processStateEvent(netPlugin, crt, opts, rsps, retErr)
cfg := drivers.OvsCfgNetworkState{}
cfg.StateDriver = netPlugin.StateDriver
retErr <- cfg.WatchAll(rsps)
return
}
func handleEndpointEvents(netPlugin *plugin.NetPlugin, crt *crt.Crt,
opts cliOpts, retErr chan error) {
rsps := make(chan core.WatchState)
go processStateEvent(netPlugin, crt, opts, rsps, retErr)
cfg := drivers.OvsCfgEndpointState{}
cfg.StateDriver = netPlugin.StateDriver
retErr <- cfg.WatchAll(rsps)
return
}
func handleStateEvents(netPlugin *plugin.NetPlugin, crt *crt.Crt, opts cliOpts,
retErr chan error) {
// monitor network events
go handleNetworkEvents(netPlugin, crt, opts, retErr)
// monitor endpoint events
go handleEndpointEvents(netPlugin, crt, opts, retErr)
}
func handleEvents(netPlugin *plugin.NetPlugin, crt *crt.Crt, opts cliOpts) error {
recvErr := make(chan error, 1)
//monitor and process state change events
go handleStateEvents(netPlugin, crt, opts, recvErr)
if !opts.nativeInteg {
// start docker client and handle docker events
// wait on error chan for problems handling the docker events
dockerCrt := crt.ContainerIf.(*docker.Docker)
dockerCrt.Client.StartMonitorEvents(handleDockerEvents, recvErr,
netPlugin, crt)
}
err := <-recvErr
if err != nil {
log.Printf("Failure occured. Error: %s", err)
return err
}
return nil
}
func main() {
var opts cliOpts
var flagSet *flag.FlagSet
log.SetFlags(log.LstdFlags | log.Lshortfile)
defHostLabel, err := os.Hostname()
if err != nil {
log.Printf("Failed to fetch hostname. Error: %s", err)
os.Exit(1)
}
flagSet = flag.NewFlagSet("netd", flag.ExitOnError)
flagSet.StringVar(&opts.hostLabel,
"host-label",
defHostLabel,
"label used to identify endpoints homed for this host, default is host name")
flagSet.BoolVar(&opts.nativeInteg,
"native-integration",
false,
"do not listen to container runtime events, because the events are natively integrated into their call sequence and external integration is not required")
err = flagSet.Parse(os.Args[1:])
if err != nil {
log.Fatalf("Failed to parse command. Error: %s", err)
}
if flagSet.NFlag() < 1 {
log.Printf("host-label not specified, using default (%s)", opts.hostLabel)
}
configStr := `{
"drivers" : {
"network": "ovs",
"endpoint": "ovs",
"state": "etcd"
},
"ovs" : {
"dbip": "127.0.0.1",
"dbport": 6640
},
"etcd" : {
"machines": ["http://127.0.0.1:4001"]
},
"crt" : {
"type": "docker"
},
"docker" : {
"socket" : "unix:///var/run/docker.sock"
}
}`
netPlugin := &plugin.NetPlugin{}
err = netPlugin.Init(configStr)
if err != nil {
log.Printf("Failed to initialize the plugin. Error: %s", err)
os.Exit(1)
}
crt := &crt.Crt{}
err = crt.Init(configStr)
if err != nil {
log.Printf("Failed to initialize container run time, err %s \n", err)
os.Exit(1)
}
processCurrentState(netPlugin, crt, opts)
//logger := log.New(os.Stdout, "go-etcd: ", log.LstdFlags)
//etcd.SetLogger(logger)
err = handleEvents(netPlugin, crt, opts)
if err != nil {
os.Exit(1)
} else {
os.Exit(0)
}
}