Skip to content

Commit 1aa88a0

Browse files
author
Vipin Jain
committed
create master state, publish driver state with master's operational params
1 parent 35377a7 commit 1aa88a0

9 files changed

+338
-236
lines changed

drivers/ovsdriver.go

+24-25
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ func (d *OvsDriver) createDeletePort(portName, intfName, intfType, id string,
236236
}
237237

238238
if intfOptions != nil {
239-
// log.Printf("using options %v \n", intfOptions)
240239
intf["options"], err = libovsdb.NewOvsMap(intfOptions)
241240
if err != nil {
242241
log.Printf("error '%s' creating options from %v \n", err, intfOptions)
@@ -329,22 +328,22 @@ func vxlanIfName(netId, vtepIp string) string {
329328
}
330329

331330
func (d *OvsDriver) createVtep(epCfg *OvsCfgEndpointState) error {
332-
operNwState := OvsOperNetworkState{StateDriver: d.stateDriver}
333-
err := operNwState.Read(epCfg.NetId)
331+
cfgNw := OvsCfgNetworkState{StateDriver: d.stateDriver}
332+
err := cfgNw.Read(epCfg.NetId)
334333
if err != nil {
335334
return err
336335
}
337336

338337
intfOptions := make(map[string]interface{})
339338
intfOptions["remote_ip"] = epCfg.VtepIp
340-
intfOptions["key"] = strconv.Itoa(operNwState.ExtPktTag)
339+
intfOptions["key"] = strconv.Itoa(cfgNw.ExtPktTag)
341340

342341
intfName := vxlanIfName(epCfg.NetId, epCfg.VtepIp)
343-
err = d.createDeletePort(intfName, intfName, "vxlan", operNwState.Id,
344-
intfOptions, operNwState.PktTag, CREATE_PORT)
342+
err = d.createDeletePort(intfName, intfName, "vxlan", cfgNw.Id,
343+
intfOptions, cfgNw.PktTag, CREATE_PORT)
345344
if err != nil {
346345
log.Printf("error '%s' creating vxlan peer intfName %s, options %s, tag %d \n",
347-
err, intfName, intfOptions, operNwState.PktTag)
346+
err, intfName, intfOptions, cfgNw.PktTag)
348347
return err
349348
}
350349

@@ -353,18 +352,18 @@ func (d *OvsDriver) createVtep(epCfg *OvsCfgEndpointState) error {
353352

354353
func (d *OvsDriver) deleteVtep(epCfg *OvsCfgEndpointState) error {
355354

356-
operNwState := OvsOperNetworkState{StateDriver: d.stateDriver}
357-
err := operNwState.Read(epCfg.NetId)
355+
cfgNw := OvsCfgNetworkState{StateDriver: d.stateDriver}
356+
err := cfgNw.Read(epCfg.NetId)
358357
if err != nil {
359358
return err
360359
}
361360

362361
intfName := vxlanIfName(epCfg.NetId, epCfg.VtepIp)
363-
err = d.createDeletePort(intfName, intfName, "vxlan", operNwState.Id,
364-
nil, operNwState.PktTag, DELETE_PORT)
362+
err = d.createDeletePort(intfName, intfName, "vxlan", cfgNw.Id,
363+
nil, cfgNw.PktTag, DELETE_PORT)
365364
if err != nil {
366365
log.Printf("error '%s' deleting vxlan peer intfName %s, tag %d \n",
367-
err, intfName, operNwState.PktTag)
366+
err, intfName, cfgNw.PktTag)
368367
return err
369368
}
370369

@@ -424,13 +423,13 @@ func (d *OvsDriver) Deinit() {
424423
}
425424

426425
func (d *OvsDriver) CreateNetwork(id string) error {
427-
cfgNetState := OvsCfgNetworkState{StateDriver: d.stateDriver}
428-
err := cfgNetState.Read(id)
426+
cfgNw := OvsCfgNetworkState{StateDriver: d.stateDriver}
427+
err := cfgNw.Read(id)
429428
if err != nil {
430-
log.Printf("Failed to read net %s \n", cfgNetState.Id)
429+
log.Printf("Failed to read net %s \n", cfgNw.Id)
431430
return err
432431
}
433-
log.Printf("create net %s \n", cfgNetState.Id)
432+
log.Printf("create net %s \n", cfgNw.Id)
434433

435434
return nil
436435
}
@@ -440,13 +439,13 @@ func (d *OvsDriver) DeleteNetwork(value string) error {
440439
// no driver operation for network delete
441440
var err error
442441

443-
cfgNetState := OvsCfgNetworkState{}
444-
err = cfgNetState.Unmarshal(value)
442+
cfgNw := OvsCfgNetworkState{}
443+
err = cfgNw.Unmarshal(value)
445444
if err != nil {
446445
log.Printf("Failed to unmarshal network config, err '%s' \n", err)
447446
return err
448447
}
449-
log.Printf("delete net %s \n", cfgNetState.Id)
448+
log.Printf("delete net %s \n", cfgNw.Id)
450449

451450
return nil
452451
}
@@ -485,15 +484,15 @@ func (d *OvsDriver) CreateEndpoint(id string) error {
485484
intfType = ""
486485
}
487486

488-
operNwState := OvsOperNetworkState{StateDriver: d.stateDriver}
489-
err = operNwState.Read(epCfg.NetId)
487+
cfgNw := OvsCfgNetworkState{StateDriver: d.stateDriver}
488+
err = cfgNw.Read(epCfg.NetId)
490489
if err != nil {
491490
return err
492491
}
493492

494493
// TODO: some updates may mean implicit delete of the previous state
495494
err = d.createDeletePort(portName, intfName, intfType, epCfg.Id,
496-
nil, operNwState.PktTag, CREATE_PORT)
495+
nil, cfgNw.PktTag, CREATE_PORT)
497496
if err != nil {
498497
return err
499498
}
@@ -503,7 +502,7 @@ func (d *OvsDriver) CreateEndpoint(id string) error {
503502
}
504503
}()
505504

506-
operEpState := OvsOperEndpointState{
505+
operEp := OvsOperEndpointState{
507506
StateDriver: d.stateDriver,
508507
Id: id,
509508
PortName: portName,
@@ -513,13 +512,13 @@ func (d *OvsDriver) CreateEndpoint(id string) error {
513512
IntfName: intfName,
514513
HomingHost: epCfg.HomingHost,
515514
VtepIp: epCfg.VtepIp}
516-
err = operEpState.Write()
515+
err = operEp.Write()
517516
if err != nil {
518517
return err
519518
}
520519
defer func() {
521520
if err != nil {
522-
operEpState.Clear()
521+
operEp.Clear()
523522
}
524523
}()
525524

drivers/ovsdriver_test.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,10 @@ func (d *testOvsStateDriver) readStateHelper(isCreateEp bool, oper int,
7878
value core.State) error {
7979
if cfgNw, ok := value.(*OvsCfgNetworkState); ok {
8080
cfgNw.Id = testOvsNwId
81-
return nil
82-
}
83-
84-
if operNw, ok := value.(*OvsOperNetworkState); ok {
85-
operNw.Id = testOvsNwId
86-
operNw.PktTag = testPktTag
87-
operNw.ExtPktTag = testExtPktTag
88-
operNw.SubnetIp = testSubnetIp
89-
operNw.SubnetLen = testSubnetLen
81+
cfgNw.PktTag = testPktTag
82+
cfgNw.ExtPktTag = testExtPktTag
83+
cfgNw.SubnetIp = testSubnetIp
84+
cfgNw.SubnetLen = testSubnetLen
9085
return nil
9186
}
9287

drivers/ovsnetworkstate.go

+5-40
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ const (
4141

4242
type OvsCfgNetworkState struct {
4343
StateDriver core.StateDriver `json:"-"`
44+
Id string `json:"id"`
4445
Tenant string `json:"tenant"`
4546
PktTagType string `json:"pktTagType"`
46-
PktTag string `json:"pktTag"`
47+
PktTag int `json:"pktTag"`
48+
ExtPktTag int `json:"extPktTag"`
4749
SubnetIp string `json:"subnetIp"`
4850
SubnetLen uint `json:"subnetLen"`
4951
DefaultGw string `json:"defaultGw"`
50-
Id string `json:"id"`
52+
EpCount int `json:"epCount"`
53+
IpAllocMap bitset.BitSet `json:"ipAllocMap"`
5154
}
5255

5356
func (s *OvsCfgNetworkState) Write() error {
@@ -73,41 +76,3 @@ func (s *OvsCfgNetworkState) Marshal() (string, error) {
7376
bytes, err := json.Marshal(s)
7477
return string(bytes[:]), err
7578
}
76-
77-
type OvsOperNetworkState struct {
78-
StateDriver core.StateDriver `json:"-"`
79-
Id string `json:"id"`
80-
Tenant string `json:"tenant"`
81-
PktTagType string `json:"pktTagType"`
82-
PktTag int `json:"pktTag"`
83-
ExtPktTag int `json:"extPktTag"`
84-
SubnetIp string `json:"subnetIp"`
85-
SubnetLen uint `json:"subnetLen"`
86-
DefaultGw string `json:"defaultGw"`
87-
EpCount int `json:"epCount"`
88-
IpAllocMap bitset.BitSet `json:"ipAllocMap"`
89-
}
90-
91-
func (s *OvsOperNetworkState) Write() error {
92-
key := fmt.Sprintf(NW_OPER_PATH, s.Id)
93-
return s.StateDriver.WriteState(key, s, json.Marshal)
94-
}
95-
96-
func (s *OvsOperNetworkState) Read(id string) error {
97-
key := fmt.Sprintf(NW_OPER_PATH, id)
98-
return s.StateDriver.ReadState(key, s, json.Unmarshal)
99-
}
100-
101-
func (s *OvsOperNetworkState) Clear() error {
102-
key := fmt.Sprintf(NW_OPER_PATH, s.Id)
103-
return s.StateDriver.ClearState(key)
104-
}
105-
106-
func (s *OvsOperNetworkState) Unmarshal(value string) error {
107-
return json.Unmarshal([]byte(value), s)
108-
}
109-
110-
func (s *OvsOperNetworkState) Marshal() (string, error) {
111-
bytes, err := json.Marshal(s)
112-
return string(bytes[:]), err
113-
}

drivers/ovsnetworkstate_test.go

+5-33
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ import (
2323
)
2424

2525
const (
26-
testNwId = "testNw"
27-
nwCfgKey = NW_CFG_PATH_PREFIX + testNwId
28-
nwOperKey = NW_OPER_PATH_PREFIX + testNwId
26+
testNwId = "testNw"
27+
nwCfgKey = NW_CFG_PATH_PREFIX + testNwId
2928
)
3029

3130
var nwStateDriver *testNwStateDriver = &testNwStateDriver{}
@@ -53,9 +52,9 @@ func (d *testNwStateDriver) ReadRecursive(baseKey string) ([]string, error) {
5352
}
5453

5554
func (d *testNwStateDriver) validateKey(key string) error {
56-
if key != nwCfgKey && key != nwOperKey {
57-
return &core.Error{Desc: fmt.Sprintf("Unexpected key. recvd: %s expected: %s or %s ",
58-
key, nwCfgKey, nwOperKey)}
55+
if key != nwCfgKey {
56+
return &core.Error{Desc: fmt.Sprintf("Unexpected key. "+
57+
"recvd: %s expected: %s ", key, nwCfgKey)}
5958
} else {
6059
return nil
6160
}
@@ -101,30 +100,3 @@ func TestOvsCfgNetworkStateClear(t *testing.T) {
101100
t.Fatalf("clear config state failed. Error: %s", err)
102101
}
103102
}
104-
105-
func TestOvsOperNetworkStateRead(t *testing.T) {
106-
epOper := &OvsOperNetworkState{StateDriver: nwStateDriver}
107-
108-
err := epOper.Read(testNwId)
109-
if err != nil {
110-
t.Fatalf("read oper state failed. Error: %s", err)
111-
}
112-
}
113-
114-
func TestOvsOperNetworkStateWrite(t *testing.T) {
115-
epOper := &OvsOperNetworkState{StateDriver: nwStateDriver, Id: testNwId}
116-
117-
err := epOper.Write()
118-
if err != nil {
119-
t.Fatalf("write oper state failed. Error: %s", err)
120-
}
121-
}
122-
123-
func TestOvsOperNetworkStateClear(t *testing.T) {
124-
epOper := &OvsOperNetworkState{StateDriver: nwStateDriver, Id: testNwId}
125-
126-
err := epOper.Clear()
127-
if err != nil {
128-
t.Fatalf("clear oper state failed. Error: %s", err)
129-
}
130-
}

netd.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,25 @@ func createDeleteVtep(netPlugin *plugin.NetPlugin, netId, preValue string,
6969
}
7070

7171
epCfg := &drivers.OvsCfgEndpointState{StateDriver: netPlugin.StateDriver}
72-
7372
epCfg.Id = getVtepName(netId, opts.hostLabel)
7473
if preValue != "" {
7574
err = epCfg.Clear()
7675
if err != nil {
7776
log.Printf("error '%s' deleting ep %s \n", err, epCfg.Id)
7877
}
7978
} else {
80-
epCfg.HomingHost = opts.hostLabel
81-
epCfg.VtepIp, err = netutils.GetLocalIp()
79+
err = epCfg.Read(epCfg.Id)
8280
if err != nil {
83-
log.Printf("error '%s' getting local IP \n", err)
84-
} else {
85-
epCfg.NetId = netId
86-
err = epCfg.Write()
81+
epCfg.HomingHost = opts.hostLabel
82+
epCfg.VtepIp, err = netutils.GetLocalIp()
8783
if err != nil {
88-
log.Printf("error '%s' adding epCfg %v \n", epCfg)
84+
log.Printf("error '%s' getting local IP \n", err)
85+
} else {
86+
epCfg.NetId = netId
87+
err = epCfg.Write()
88+
if err != nil {
89+
log.Printf("error '%s' adding epCfg %v \n", epCfg)
90+
}
8991
}
9092
}
9193
}
@@ -213,22 +215,22 @@ func getEndpointContainerContext(state *core.StateDriver, epId string) (
213215
}
214216
epCtx.NewContName = epCfg.ContName
215217

216-
operNetState := &drivers.OvsOperNetworkState{StateDriver: *state}
217-
err = operNetState.Read(epCfg.NetId)
218+
cfgNet := &drivers.OvsCfgNetworkState{StateDriver: *state}
219+
err = cfgNet.Read(epCfg.NetId)
218220
if err != nil {
219221
return &epCtx, err
220222
}
221-
epCtx.DefaultGw = operNetState.DefaultGw
222-
epCtx.SubnetLen = operNetState.SubnetLen
223+
epCtx.DefaultGw = cfgNet.DefaultGw
224+
epCtx.SubnetLen = cfgNet.SubnetLen
223225

224-
operEpState := &drivers.OvsOperEndpointState{StateDriver: *state}
225-
err = operEpState.Read(epId)
226+
operEp := &drivers.OvsOperEndpointState{StateDriver: *state}
227+
err = operEp.Read(epId)
226228
if err != nil {
227229
return &epCtx, nil
228230
}
229-
epCtx.CurrContName = operEpState.ContName
230-
epCtx.InterfaceId = operEpState.PortName
231-
epCtx.IpAddress = operEpState.IpAddress
231+
epCtx.CurrContName = operEp.ContName
232+
epCtx.InterfaceId = operEp.PortName
233+
epCtx.IpAddress = operEp.IpAddress
232234

233235
return &epCtx, err
234236
}

netdcli/netdcli.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,11 @@ func executeOpts(opts *cliOpts) error {
386386
}
387387
case CLI_CONSTRUCT_NW:
388388
if opts.oper.Get() == CLI_OPER_GET {
389-
nwOper := &drivers.OvsOperNetworkState{StateDriver: etcdDriver}
390-
state = nwOper
389+
nwCfg := &drivers.OvsCfgNetworkState{StateDriver: etcdDriver}
390+
state = nwCfg
391391
} else {
392392
nwCfg := &drivers.OvsCfgNetworkState{StateDriver: etcdDriver}
393-
nwCfg.PktTag = opts.pktTag
393+
nwCfg.PktTag, _ = strconv.Atoi(opts.pktTag)
394394
nwCfg.Tenant = opts.tenant
395395
nwCfg.PktTagType = opts.pktTagType
396396
nwCfg.SubnetIp = opts.subnetIp

0 commit comments

Comments
 (0)