forked from MFALHI/netplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetd.go
More file actions
402 lines (346 loc) · 11.1 KB
/
netd.go
File metadata and controls
402 lines (346 loc) · 11.1 KB
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
/***
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"
"flag"
"fmt"
"io/ioutil"
"log/syslog"
"net/url"
"os"
"os/user"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/mgmtfn/dockplugin"
"github.com/contiv/netplugin/netmaster/mastercfg"
"github.com/contiv/netplugin/netplugin/cluster"
"github.com/contiv/netplugin/netplugin/plugin"
"github.com/contiv/netplugin/svcplugin"
"github.com/contiv/netplugin/utils"
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
dockPlugin bool // Operate in docker plugin mode
cfgFile string
debug bool
syslog string
jsonLog bool
ctrlIP string // IP address to be used by control protocols
vtepIP string // IP address to be used by the VTEP
vlanIntf string // Uplink interface for VLAN switching
}
func skipHost(vtepIP, homingHost, myHostLabel string) bool {
return (vtepIP == "" && homingHost != myHostLabel ||
vtepIP != "" && homingHost == myHostLabel)
}
func processCurrentState(netPlugin *plugin.NetPlugin, opts cliOpts) error {
readNet := &mastercfg.CfgNetworkState{}
readNet.StateDriver = netPlugin.StateDriver
netCfgs, err := readNet.ReadAll()
if err == nil {
for idx, netCfg := range netCfgs {
net := netCfg.(*mastercfg.CfgNetworkState)
log.Debugf("read net key[%d] %s, populating state \n", idx, net.ID)
processNetEvent(netPlugin, net, false)
}
}
readEp := &mastercfg.CfgEndpointState{}
readEp.StateDriver = netPlugin.StateDriver
epCfgs, err := readEp.ReadAll()
if err == nil {
for idx, epCfg := range epCfgs {
ep := epCfg.(*mastercfg.CfgEndpointState)
log.Debugf("read ep key[%d] %s, populating state \n", idx, ep.ID)
processEpState(netPlugin, opts, ep.ID)
}
}
return nil
}
func processNetEvent(netPlugin *plugin.NetPlugin, nwCfg *mastercfg.CfgNetworkState,
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(nwCfg.ID, nwCfg.PktTagType, nwCfg.PktTag, nwCfg.ExtPktTag)
operStr = "delete"
} else {
err = netPlugin.CreateNetwork(nwCfg.ID)
operStr = "create"
}
if err != nil {
log.Errorf("Network operation %s failed. Error: %s", operStr, err)
} else {
log.Infof("Network operation %s succeeded", operStr)
}
return
}
// processEpState restores endpoint state
func processEpState(netPlugin *plugin.NetPlugin, opts cliOpts, epID string) 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() }()
// read endpoint config
epCfg := &mastercfg.CfgEndpointState{}
epCfg.StateDriver = netPlugin.StateDriver
err := epCfg.Read(epID)
if err != nil {
log.Errorf("Failed to read config for ep '%s' \n", epID)
return err
}
// if the endpoint is not for this host, ignore it
if skipHost(epCfg.VtepIP, epCfg.HomingHost, opts.hostLabel) {
log.Infof("skipping mismatching host for ep %s. EP's host %s (my host: %s)",
epID, epCfg.HomingHost, opts.hostLabel)
return nil
}
// Create the endpoint
err = netPlugin.CreateEndpoint(epID)
if err != nil {
log.Errorf("Endpoint operation create failed. Error: %s", err)
return err
}
log.Infof("Endpoint operation create succeeded")
return err
}
func processStateEvent(netPlugin *plugin.NetPlugin, 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.(*mastercfg.CfgNetworkState); ok {
log.Infof("Received %q for network: %q", eventStr, nwCfg.ID)
processNetEvent(netPlugin, nwCfg, isDelete)
}
}
}
func handleNetworkEvents(netPlugin *plugin.NetPlugin, opts cliOpts, retErr chan error) {
rsps := make(chan core.WatchState)
go processStateEvent(netPlugin, opts, rsps)
cfg := mastercfg.CfgNetworkState{}
cfg.StateDriver = netPlugin.StateDriver
retErr <- cfg.WatchAll(rsps)
return
}
func handleEvents(netPlugin *plugin.NetPlugin, opts cliOpts) error {
recvErr := make(chan error, 1)
go handleNetworkEvents(netPlugin, opts, recvErr)
err := <-recvErr
if err != nil {
log.Errorf("Failure occured. Error: %s", err)
return err
}
return nil
}
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
svcplugin.QuitCh = make(chan struct{})
defer close(svcplugin.QuitCh)
defHostLabel, err := os.Hostname()
if err != nil {
log.Fatalf("Failed to fetch hostname. Error: %s", err)
}
// default to using local IP addr
localIP, err := cluster.GetLocalAddr()
if err != nil {
log.Fatalf("Error getting local address. Err: %v", err)
}
defCtrlIP := localIP
defVtepIP := localIP
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.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.dockPlugin,
"docker-plugin",
true,
"Operate in docker plugin mode")
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.ctrlIP,
"ctrl-ip",
defCtrlIP,
"Local ip address to be used for control communication")
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)
}
// Make sure we are running as root
usr, err := user.Current()
if (err != nil) || (usr.Username != "root") {
log.Fatalf("This process can only be run as root")
}
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)
}
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"]
},
"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
}
// Initialize docker plugin
if opts.dockPlugin {
dockplugin.InitDockPlugin(netPlugin)
}
// Init the driver plugins..
err = netPlugin.Init(pluginConfig, string(config))
if err != nil {
log.Fatalf("Failed to initialize the plugin. Error: %s", err)
}
// Process all current state
processCurrentState(netPlugin, opts)
// Initialize clustering
cluster.Init(netPlugin, opts.ctrlIP)
//logger := log.New(os.Stdout, "go-etcd: ", log.LstdFlags)
//etcd.SetLogger(logger)
if err := handleEvents(netPlugin, opts); err != nil {
os.Exit(1)
}
}