/
netd.go
893 lines (778 loc) · 24.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/***
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 (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log/syslog"
"net/url"
"os"
"os/exec"
"strings"
"time"
"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/netutils"
"github.com/contiv/netplugin/plugin"
"github.com/contiv/netplugin/utils"
"github.com/samalba/dockerclient"
log "github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/hooks/syslog"
)
// a daemon based on etcd client's Watch interface to trigger plugin's
// network provisioning interfaces
type cliOpts struct {
hostLabel string
nativeInteg bool
cfgFile string
debug bool
syslog string
jsonLog bool
vtepIP string
vlanIntf string
forceDeleteEp 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.OvsCfgNetworkState{}
readNet.StateDriver = netPlugin.StateDriver
netCfgs, err := readNet.ReadAll()
if err == nil {
for idx, netCfg := range netCfgs {
net := netCfg.(*drivers.OvsCfgNetworkState)
log.Debugf("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 {
for idx, epCfg := range epCfgs {
ep := epCfg.(*drivers.OvsCfgEndpointState)
log.Debugf("read ep key[%d] %s, populating state \n", idx, ep.ID)
processEpEvent(netPlugin, crt, opts, ep.ID, false)
}
}
peer := &drivers.PeerHostState{}
peer.StateDriver = netPlugin.StateDriver
peerList, err := peer.ReadAll()
if err == nil {
for idx, peerState := range peerList {
peerInfo := peerState.(*drivers.PeerHostState)
log.Debugf("read peer key[%d] %s, populating state \n", idx, peerInfo.ID)
processPeerEvent(netPlugin, opts, peerInfo.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.Errorf("Network operation %s failed. Error: %s", operStr, err)
} else {
log.Infof("Network operation %s succeeded", operStr)
}
return
}
func processPeerEvent(netPlugin *plugin.NetPlugin, opts cliOpts, peerID string, isDelete bool) (err error) {
// if this is our own peer info coming back to us, ignore it
if peerID == opts.hostLabel {
return nil
}
// take a lock to ensure we are programming one event at a time.
netPlugin.Lock()
defer func() { netPlugin.Unlock() }()
operStr := ""
if isDelete {
err = netPlugin.DeletePeerHost(peerID)
operStr = "delete"
} else {
err = netPlugin.CreatePeerHost(peerID)
operStr = "create"
}
if err != nil {
log.Errorf("PeerHost operation %s failed. Error: %s", operStr, err)
} else {
log.Infof("PeerHost operation %s succeeded", operStr)
}
return
}
func getEndpointContainerContext(stateDriver core.StateDriver, epID string) (
*crtclient.ContainerEPContext, error) {
var epCtx crtclient.ContainerEPContext
var err error
epCfg := &drivers.OvsCfgEndpointState{}
epCfg.StateDriver = stateDriver
err = epCfg.Read(epID)
if err != nil {
return &epCtx, nil
}
epCtx.NewContName = epCfg.ContName
epCtx.NewAttachUUID = epCfg.AttachUUID
cfgNet := &drivers.OvsCfgNetworkState{}
cfgNet.StateDriver = stateDriver
err = cfgNet.Read(epCfg.NetID)
if err != nil {
return &epCtx, err
}
epCtx.DefaultGw = cfgNet.DefaultGw
epCtx.SubnetLen = cfgNet.SubnetLen
operEp := &drivers.OvsOperEndpointState{}
operEp.StateDriver = stateDriver
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(stateDriver core.StateDriver, contName string) (
epCtxs []crtclient.ContainerEPContext, err error) {
var epCtx *crtclient.ContainerEPContext
readEp := &drivers.OvsCfgEndpointState{}
readEp.StateDriver = stateDriver
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(stateDriver, cfg.ID)
if err != nil {
log.Errorf("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.Errorf("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.Errorf("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.Infof("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.Errorf("Failed to obtain the container context for ep '%s' \n",
epID)
return
}
log.Debugf("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.Errorf("Endpoint operation %s failed. Error: %s",
operStr, err)
return
}
log.Infof("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.Errorf("Endpoint detach container '%s' from ep '%s' failed . "+
"Error: %s", contEpContext.CurrContName, epID, err)
} else {
log.Infof("Endpoint detach container '%s' from ep '%s' succeeded",
contEpContext.CurrContName, epID)
}
}
if !isDelete && !opts.nativeInteg && contAttachPointAdded(contEpContext) {
// re-read post ep updated state
newContEpContext, err1 := getEndpointContainerContext(
netPlugin.StateDriver, epID)
if err1 != nil {
log.Errorf("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.Errorf("Endpoint attach container '%s' to ep '%s' failed . "+
"Error: %s", contEpContext.NewContName, epID, err)
} else {
log.Infof("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.Errorf("Error '%s' getting Ep context for container %s \n",
err, contName)
return err
}
for _, epCtx := range epContexts {
if epCtx.NewAttachUUID != "" || epCtx.InterfaceID == "" {
log.Errorf("## skipping attach on epctx %v \n", epCtx)
continue
} else {
log.Debugf("## trying attach on epctx %v \n", epCtx)
}
err = crt.AttachEndpoint(&epCtx)
if err != nil {
log.Errorf("Error '%s' attaching container to the network \n", err)
return err
}
}
return nil
}
// search cfg eps to find epid of a matching container name
func getEpIDByContainerName(netPlugin *plugin.NetPlugin, contName string) ([]string, error) {
epIDs := []string{}
readEp := &drivers.OvsCfgEndpointState{}
readEp.StateDriver = netPlugin.StateDriver
epCfgs, err := readEp.ReadAll()
if err != nil {
log.Errorf("got err %v when reading all cfg eps \n", err)
return epIDs, err
}
for _, epCfg := range epCfgs {
ep := epCfg.(*drivers.OvsCfgEndpointState)
if ep.ContName == contName {
epIDs = append(epIDs, ep.ID)
}
}
if len(epIDs) == 0 {
err = errors.New("failed to find epCfg for contName")
log.Errorf("error getting endpoint id from container name '%s': %v \n", contName, err)
}
return epIDs, err
}
// search oper eps to find epid of a matching container uuid
func getEpIDByContainerUUID(netPlugin *plugin.NetPlugin, contUUID string) ([]string, error) {
epIDs := []string{}
readOperEp := &drivers.OvsOperEndpointState{}
readOperEp.StateDriver = netPlugin.StateDriver
epOpers, err := readOperEp.ReadAll()
if err != nil {
log.Errorf("error reading all oper eps: %v \n", err)
return epIDs, err
}
for _, epOper := range epOpers {
ep := epOper.(*drivers.OvsOperEndpointState)
if ep.ContUUID == contUUID {
log.Infof("VJ - deleting ep with following info: %v \n", ep)
epIDs = append(epIDs, ep.ID)
}
}
if len(epIDs) == 0 {
err = errors.New("UUID not found")
log.Errorf("getting endpoint id from uuid %v \n", err)
}
return epIDs, err
}
func createContainerEpOper(netPlugin *plugin.NetPlugin, contUUID, contName string) error {
epIDs, err := getEpIDByContainerName(netPlugin, contName)
if err != nil {
log.Debugf("unable to find ep for container %s, error %v\n", contName, err)
return err
}
for _, epID := range epIDs {
operEp := &drivers.OvsOperEndpointState{}
operEp.StateDriver = netPlugin.StateDriver
err = operEp.Read(epID)
if core.ErrIfKeyExists(err) != nil {
return err
}
if err == nil {
operEp.ContUUID = contUUID
err = operEp.Write()
if err != nil {
log.Errorf("error updating oper state for ep %s \n", contName)
return err
}
log.Infof("updating container '%s' with id '%s' \n", contName, contUUID)
} else {
err = netPlugin.CreateEndpoint(epID)
if err != nil {
log.Errorf("Endpoint creation failed. Error: %s", err)
return err
}
log.Infof("Endpoint operation create succeeded")
}
}
return err
}
func handleContainerStart(netPlugin *plugin.NetPlugin, crt *crt.CRT, opts *cliOpts,
contID string) error {
// var epContexts []crtclient.ContainerEPContext
contName, err := crt.GetContainerName(contID)
if err != nil {
log.Errorf("Could not find container name from container id %s \n", contID)
return err
}
contName = strings.TrimPrefix(contName, "/")
if opts.forceDeleteEp || opts.nativeInteg {
err = createContainerEpOper(netPlugin, contID, contName)
if err != nil {
log.Errorf("error updating container's uuid: %v \n", err)
return err
}
}
if !opts.nativeInteg {
err = attachContainer(netPlugin.StateDriver, crt, contName)
if err != nil {
log.Errorf("error attaching container: %v\n", err)
}
}
return err
}
func handleContainerStop(netPlugin *plugin.NetPlugin, crt *crt.CRT, opts *cliOpts,
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.Errorf("setlocalns failed. Error: %s Output: \n%s\n",
err, output)
return err
}
}
if opts.forceDeleteEp {
log.Infof("deleting operEp for container with uuid %s \n", contID)
epIDs, err := getEpIDByContainerUUID(netPlugin, contID)
if err != nil {
log.Errorf("error obtaining container's epid for uuid %s: %v \n", contID, err)
return err
}
for _, epID := range epIDs {
err = netPlugin.DeleteEndpoint(epID)
if err != nil {
log.Errorf("error deleting an endpoint upon container stop: %v \n", 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.Errorf("error decoding netplugin in handleDocker \n")
}
crt, ok := args[1].(*crt.CRT)
if !ok {
log.Errorf("error decoding netplugin in handleDocker \n")
}
opts, ok := args[2].(*cliOpts)
if !ok {
log.Errorf("error decoding global opts in Docker handler \n")
}
log.Infof("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, opts, event.Id)
if err != nil {
log.Errorf("error '%s' handling container %s \n", err, event.Id)
}
case "die":
log.Debugf("received die event for container \n")
err = handleContainerStop(netPlugin, crt, opts, event.Id)
if err != nil {
log.Errorf("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) {
for {
// block on change notifications
rsp := <-rsps
// For now we deal with only create and delete events
currentState := rsp.Curr
isDelete := false
eventStr := "create"
if rsp.Curr == nil {
currentState = 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.Debugf("Received a modify event, treating it as a 'create'")
}
if nwCfg, ok := currentState.(*drivers.OvsCfgNetworkState); ok {
log.Infof("Received %q for network: %q", eventStr, nwCfg.ID)
processNetEvent(netPlugin, nwCfg.ID, isDelete)
}
if epCfg, ok := currentState.(*drivers.OvsCfgEndpointState); ok {
log.Infof("Received %q for endpoint: %q", eventStr, epCfg.ID)
processEpEvent(netPlugin, crt, opts, epCfg.ID, isDelete)
}
if peerInfo, ok := currentState.(*drivers.PeerHostState); ok {
log.Infof("Received %q for peer host: %q", eventStr, peerInfo.ID)
processPeerEvent(netPlugin, opts, peerInfo.ID, isDelete)
}
}
}
func handleNetworkEvents(netPlugin *plugin.NetPlugin, crt *crt.CRT,
opts cliOpts, retErr chan error) {
rsps := make(chan core.WatchState)
go processStateEvent(netPlugin, crt, opts, rsps)
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)
cfg := drivers.OvsCfgEndpointState{}
cfg.StateDriver = netPlugin.StateDriver
retErr <- cfg.WatchAll(rsps)
return
}
func handlePeerEvents(netPlugin *plugin.NetPlugin, crt *crt.CRT,
opts cliOpts, retErr chan error) {
rsps := make(chan core.WatchState)
go processStateEvent(netPlugin, crt, opts, rsps)
peer := drivers.PeerHostState{}
peer.StateDriver = netPlugin.StateDriver
retErr <- peer.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)
// monitor peer host events
go handlePeerEvents(netPlugin, crt, opts, retErr)
}
func handleEvents(netPlugin *plugin.NetPlugin, crt *crt.CRT, opts cliOpts) error {
recvErr := make(chan error, 1)
recvEventErr := make(chan error)
go handleStateEvents(netPlugin, crt, opts, recvErr)
//monitor and process state change events
if !opts.nativeInteg {
startDockerEventPoll(netPlugin, crt, recvEventErr, opts)
go func() {
for {
err := <-recvEventErr
if err != nil {
log.Warnf("Failure occured talking to docker events. Sleeping for a second and retrying. Error: %s", err)
}
time.Sleep(1 * time.Second)
// FIXME note that we are assuming this goroutine crashed, which if not
// might generate double events. Synchronize this with a channel.
startDockerEventPoll(netPlugin, crt, recvEventErr, opts)
}
}()
}
err := <-recvErr
if err != nil {
log.Errorf("Failure occured. Error: %s", err)
return err
}
return nil
}
func startDockerEventPoll(netPlugin *plugin.NetPlugin, crt *crt.CRT, recvErr chan error, opts cliOpts) {
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, &opts)
}
}
func configureSyslog(syslogParam string) {
var err error
var hook log.Hook
// disable colors if we're writing to syslog *and* we're the default text
// formatter, because the tty detection is useless here.
if tf, ok := log.StandardLogger().Formatter.(*log.TextFormatter); ok {
tf.DisableColors = true
}
if syslogParam == "kernel" {
hook, err = logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "netplugin")
if err != nil {
log.Fatalf("Could not connect to kernel syslog")
}
} else {
u, err := url.Parse(syslogParam)
if err != nil {
log.Fatalf("Could not parse syslog spec: %v", err)
}
hook, err = logrus_syslog.NewSyslogHook(u.Scheme, u.Host, syslog.LOG_INFO, "netplugin")
if err != nil {
log.Fatalf("Could not connect to syslog: %v", err)
}
}
log.AddHook(hook)
}
func main() {
var opts cliOpts
var flagSet *flag.FlagSet
defHostLabel, err := os.Hostname()
if err != nil {
log.Fatalf("Failed to fetch hostname. Error: %s", err)
}
// default to using eth1's IP addr
defVtepIP, _ := netutils.GetInterfaceIP("eth1")
defVlanIntf := "eth2"
flagSet = flag.NewFlagSet("netd", flag.ExitOnError)
flagSet.StringVar(&opts.syslog,
"syslog",
"",
"Log to syslog at proto://ip:port -- use 'kernel' to log via kernel syslog")
flagSet.BoolVar(&opts.debug,
"debug",
false,
"Show debugging information generated by netplugin")
flagSet.BoolVar(&opts.forceDeleteEp,
"force-delete-ep",
false,
"force ep deletion upon container deletion")
flagSet.BoolVar(&opts.jsonLog,
"json-log",
false,
"Format logs as JSON")
flagSet.StringVar(&opts.hostLabel,
"host-label",
defHostLabel,
"label used to identify endpoints homed for this host, default is host name. If -config flag is used then host-label must be specified in the the configuration passed.")
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")
flagSet.StringVar(&opts.cfgFile,
"config",
"",
"plugin configuration. Use '-' to read configuration from stdin")
flagSet.StringVar(&opts.vtepIP,
"vtep-ip",
defVtepIP,
"My VTEP ip address")
flagSet.StringVar(&opts.vlanIntf,
"vlan-if",
defVlanIntf,
"My VTEP ip address")
err = flagSet.Parse(os.Args[1:])
if err != nil {
log.Fatalf("Failed to parse command. Error: %s", err)
}
if opts.debug {
log.SetLevel(log.DebugLevel)
os.Setenv("CONTIV_TRACE", "1")
}
if opts.jsonLog {
log.SetFormatter(&log.JSONFormatter{})
}
if opts.syslog != "" {
configureSyslog(opts.syslog)
}
if flagSet.NFlag() < 1 {
log.Infof("host-label not specified, using default (%s)", opts.hostLabel)
}
if utils.FetchSysAttrs() != nil {
log.Fatalf("Error reading system attributes \n")
} else {
if utils.SysAttrs.OsType == "centos" {
opts.forceDeleteEp = true
}
}
defConfigStr := fmt.Sprintf(`{
"drivers" : {
"network": %q,
"state": "etcd"
},
"plugin-instance": {
"host-label": %q,
"vtep-ip": %q,
"vlan-if": %q
},
%q : {
"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"
}
}`, utils.OvsNameStr, opts.hostLabel, opts.vtepIP,
opts.vlanIntf, utils.OvsNameStr)
netPlugin := &plugin.NetPlugin{}
config := []byte{}
if opts.cfgFile == "" {
log.Infof("config not specified, using default config")
config = []byte(defConfigStr)
} else if opts.cfgFile == "-" {
reader := bufio.NewReader(os.Stdin)
config, err = ioutil.ReadAll(reader)
if err != nil {
log.Fatalf("reading config from stdin failed. Error: %s", err)
}
} else {
config, err = ioutil.ReadFile(opts.cfgFile)
if err != nil {
log.Fatalf("reading config from file failed. Error: %s", err)
}
}
// Parse the config
pluginConfig := plugin.Config{}
err = json.Unmarshal([]byte(config), &pluginConfig)
if err != nil {
log.Fatalf("Error parsing config. Err: %v", err)
}
// extract host-label from the configuration
if pluginConfig.Instance.HostLabel == "" {
log.Fatalf("Empty host-label passed in configuration")
}
opts.hostLabel = pluginConfig.Instance.HostLabel
// Use default values when config options are not specified
if pluginConfig.Instance.VtepIP == "" {
pluginConfig.Instance.VtepIP = opts.vtepIP
}
if pluginConfig.Instance.VlanIntf == "" {
pluginConfig.Instance.VlanIntf = opts.vlanIntf
}
err = netPlugin.Init(pluginConfig, string(config))
if err != nil {
log.Fatalf("Failed to initialize the plugin. Error: %s", err)
}
crt := &crt.CRT{}
err = crt.Init(string(config))
if err != nil {
log.Fatalf("Failed to initialize container run time, err %s \n", err)
}
// Process all current state
processCurrentState(netPlugin, crt, opts)
//logger := log.New(os.Stdout, "go-etcd: ", log.LstdFlags)
//etcd.SetLogger(logger)
if err := handleEvents(netPlugin, crt, opts); err != nil {
os.Exit(1)
}
}