Skip to content

Commit b2f417e

Browse files
committed
dockplugin: handle leave
1 parent b5b0793 commit b2f417e

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

mgmtfn/dockplugin/dockplugin.go

+51-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/contiv/netplugin/mgmtfn/dockplugin/libnetClient"
3434
"github.com/contiv/netplugin/netmaster/client"
3535
"github.com/contiv/netplugin/netmaster/intent"
36+
"github.com/contiv/netplugin/plugin"
3637
"github.com/docker/docker/pkg/plugins"
3738
"github.com/docker/libnetwork/drivers/remote/api"
3839
"github.com/gorilla/mux"
@@ -41,8 +42,11 @@ import (
4142
const pluginPath = "/run/docker/plugins"
4243
const driverName = "netplugin"
4344

45+
var netPlugin *plugin.NetPlugin
46+
4447
// InitDockPlugin initializes the docker plugin
45-
func InitDockPlugin() error {
48+
func InitDockPlugin(netplugin *plugin.NetPlugin) error {
49+
netPlugin = netplugin
4650
hostname, err := os.Hostname()
4751
if err != nil {
4852
log.Fatalf("Could not retrieve hostname: %v", err)
@@ -436,8 +440,20 @@ func join() func(http.ResponseWriter, *http.Request) {
436440

437441
ep, err := netdcliGetEndpoint(networkName + "-" + jr.EndpointID)
438442
if err != nil {
439-
httpError(w, "Could not derive created interface", err)
440-
return
443+
// Add the endpoint oper state
444+
err = netPlugin.CreateEndpoint(networkName + "-" + jr.EndpointID)
445+
if err != nil {
446+
log.Errorf("Endpoint creation failed. Error: %s", err)
447+
httpError(w, "Could not create endpoint", err)
448+
return
449+
}
450+
451+
// Try to get it again
452+
ep, err = netdcliGetEndpoint(networkName + "-" + jr.EndpointID)
453+
if err != nil {
454+
httpError(w, "Could not find created endpoint", err)
455+
return
456+
}
441457
}
442458

443459
nw, err := netdcliGetNetwork(networkName)
@@ -454,7 +470,7 @@ func join() func(http.ResponseWriter, *http.Request) {
454470
Gateway: nw[0].DefaultGw,
455471
}
456472

457-
log.Infof("Sending JoinResponse: {%+v}", joinResp)
473+
log.Infof("Sending JoinResponse: {%+v}, InterfaceName: %s", joinResp, ep[0].PortName)
458474

459475
content, err = json.Marshal(joinResp)
460476
if err != nil {
@@ -469,6 +485,37 @@ func join() func(http.ResponseWriter, *http.Request) {
469485
func leave() func(http.ResponseWriter, *http.Request) {
470486
return func(w http.ResponseWriter, r *http.Request) {
471487
logEvent("leave")
488+
489+
lr := api.LeaveRequest{}
490+
content, err := ioutil.ReadAll(r.Body)
491+
if err != nil {
492+
httpError(w, "Could not read join request", err)
493+
return
494+
}
495+
496+
if err := json.Unmarshal(content, &lr); err != nil {
497+
httpError(w, "Could not parse join request", err)
498+
return
499+
}
500+
501+
log.Infof("LeaveRequest: %+v", lr)
502+
503+
networkName, err := GetNetworkName(lr.NetworkID)
504+
if err != nil {
505+
log.Errorf("Error getting network name for UUID: %s. Err: %v", lr.NetworkID, err)
506+
httpError(w, "Could not get network name", err)
507+
return
508+
}
509+
510+
// Delete the Endpoint
511+
err = netPlugin.DeleteEndpoint(networkName + "-" + lr.EndpointID)
512+
if err != nil {
513+
log.Errorf("error deleting an endpoint upon container stop: %v \n", err)
514+
httpError(w, "Could not delete endpoint", err)
515+
return
516+
}
517+
518+
// Send response
472519
w.WriteHeader(200)
473520
}
474521
}

netplugin/netd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ func main() {
897897
}
898898

899899
// Initialize docker plugin
900-
dockplugin.InitDockPlugin()
900+
dockplugin.InitDockPlugin(netPlugin)
901901

902902
// Init the driver plugins..
903903
err = netPlugin.Init(pluginConfig, string(config))

scripts/python/sanity.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,14 @@ def testPolicyAddDeleteRule(testbed, numContainer, numIter):
199199
time.sleep(15)
200200

201201
numCntr = testbed.numNodes() * 2
202-
numIteration = 2
202+
numIteration = 3
203203

204-
# Run the test
204+
# Run the tests
205+
startRemoveContainer(testbed, numCntr, numIteration)
206+
startStopContainer(testbed, numCntr, numIteration)
205207
testBasicPolicy(testbed, numCntr, numIteration)
206208
testPolicyAddDeleteRule(testbed, numCntr, numIteration)
207-
startRemoveContainer(testbed, numCntr, numIteration)
208209

209-
# FIXME: This test is not passing yet as native-integ mode cleanup is not working
210-
# startStopContainer(testbed, numCntr, 4)
211210

212211
# Cleanup testbed
213212
testbed.cleanup()

0 commit comments

Comments
 (0)