Skip to content

Commit 332bf55

Browse files
committed
Merge pull request contiv#187 from shaleman/service
trim default tenant; fix policy bug
2 parents a995a92 + 866fffa commit 332bf55

10 files changed

Lines changed: 92 additions & 29 deletions

File tree

Godeps/Godeps.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/_workspace/src/github.com/contiv/ofnet/vxlanBridge.go

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mgmtfn/dockplugin/netDriver.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package dockplugin
1818
import (
1919
"encoding/json"
2020
"errors"
21+
"fmt"
2122
"io/ioutil"
2223
"net/http"
2324
"strings"
@@ -33,6 +34,8 @@ import (
3334
"github.com/samalba/dockerclient"
3435
)
3536

37+
const defaultTenantName = "default"
38+
3639
func getCapability() func(http.ResponseWriter, *http.Request) {
3740
return func(w http.ResponseWriter, r *http.Request) {
3841
logEvent("getCapability")
@@ -400,7 +403,6 @@ func netdGetNetwork(networkID string) (*mastercfg.CfgNetworkState, error) {
400403
nwCfg.StateDriver = stateDriver
401404
err = nwCfg.Read(networkID)
402405
if err != nil {
403-
log.Errorf("network %s is not operational", networkID)
404406
return nil, err
405407
}
406408

@@ -433,14 +435,27 @@ func GetDockerNetworkName(nwID string) (string, string, string, error) {
433435
var tenantName, netName, serviceName string
434436
names := strings.Split(nw.Name, ".")
435437
if len(names) == 2 {
436-
// has only network.tenant format
437-
tenantName = names[1]
438-
netName = names[0]
438+
// determine if this is service.network on default tenant or network.tenant
439+
_, err = netdGetNetwork(fmt.Sprintf("%s.%s", names[1], defaultTenantName))
440+
if err == nil {
441+
// This is service.network on default tenant
442+
tenantName = defaultTenantName
443+
netName = names[1]
444+
serviceName = names[0]
445+
} else {
446+
// this is in network.tenant format
447+
tenantName = names[1]
448+
netName = names[0]
449+
}
439450
} else if len(names) == 3 {
440451
// has service.network.tenant format
441452
tenantName = names[2]
442453
netName = names[1]
443454
serviceName = names[0]
455+
} else if len(names) == 1 {
456+
// If only network is specified, use default tenant
457+
tenantName = defaultTenantName
458+
netName = names[0]
444459
} else {
445460
log.Errorf("Invalid network name format for network %s", nw.Name)
446461
return "", "", "", errors.New("Invalid format")

netmaster/master/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"net/http"
2323
"strings"
24+
"sync"
2425

2526
"github.com/contiv/netplugin/netmaster/intent"
2627
"github.com/contiv/netplugin/netmaster/mastercfg"
@@ -75,6 +76,9 @@ type DeleteEndpointResponse struct {
7576
EndpointConfig mastercfg.CfgEndpointState // Endpoint config
7677
}
7778

79+
// Global mutex for address allocation
80+
var addrMutex sync.Mutex
81+
7882
// AllocAddressHandler allocates addresses
7983
func AllocAddressHandler(w http.ResponseWriter, r *http.Request, vars map[string]string) (interface{}, error) {
8084
var allocReq AddressAllocRequest
@@ -88,6 +92,10 @@ func AllocAddressHandler(w http.ResponseWriter, r *http.Request, vars map[string
8892

8993
log.Infof("Received AddressAllocRequest: %+v", allocReq)
9094

95+
// Take a global lock for address allocation
96+
addrMutex.Lock()
97+
defer addrMutex.Unlock()
98+
9199
// Get hold of the state driver
92100
stateDriver, err := utils.GetStateDriver()
93101
if err != nil {

netmaster/master/endpoint.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package master
1818
import (
1919
"fmt"
2020
"net"
21+
"sync"
2122

2223
"github.com/contiv/netplugin/core"
2324
"github.com/contiv/netplugin/netmaster/intent"
@@ -27,6 +28,9 @@ import (
2728
log "github.com/Sirupsen/logrus"
2829
)
2930

31+
// Global mutex for endpoint creation
32+
var epMutex sync.Mutex
33+
3034
func validateEndpointConfig(stateDriver core.StateDriver, tenant *intent.ConfigTenant) error {
3135
var err error
3236

@@ -89,6 +93,10 @@ func CreateEndpoint(stateDriver core.StateDriver, nwCfg *mastercfg.CfgNetworkSta
8993
return epCfg, nil
9094
}
9195

96+
// take the global mutex so that we can safely allocate resources
97+
epMutex.Lock()
98+
defer epMutex.Unlock()
99+
92100
epCfg.NetID = nwCfg.ID
93101
epCfg.ContName = ep.Container
94102
epCfg.AttachUUID = ep.AttachUUID

netmaster/master/network.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ import (
3232
log "github.com/Sirupsen/logrus"
3333
)
3434

35-
const driverName = "netplugin"
35+
const (
36+
driverName = "netplugin"
37+
defaultTenantName = "default"
38+
)
3639

3740
var testMode = false
3841

42+
// Trim default tenant from network name
43+
func trimDefaultTenant(networkName string) string {
44+
return strings.TrimRight(strings.TrimRight(networkName, defaultTenantName), ".")
45+
}
46+
3947
func validateNetworkConfig(tenant *intent.ConfigTenant) error {
4048
var err error
4149

@@ -77,6 +85,9 @@ func createDockNet(networkName, subnetCIDR, gateway string) error {
7785
return nil
7886
}
7987

88+
// Trim default tenant name
89+
networkName = trimDefaultTenant(networkName)
90+
8091
// connect to docker
8192
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
8293
if err != nil {
@@ -129,6 +140,9 @@ func deleteDockNet(networkName string) error {
129140
return nil
130141
}
131142

143+
// Trim default tenant name
144+
networkName = trimDefaultTenant(networkName)
145+
132146
// connect to docker
133147
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
134148
if err != nil {
@@ -277,7 +291,10 @@ func attachServiceContainer(tenantName string, networkName string, stateDriver c
277291
return err
278292
}
279293

280-
err = docker.ConnectNetwork(networkName, contName)
294+
// Trim default tenant
295+
dnetName := trimDefaultTenant(networkName)
296+
297+
err = docker.ConnectNetwork(dnetName, contName)
281298
if err != nil {
282299
log.Errorf("Could not attach container(%s) to network %s. Error: %s",
283300
contName, networkName, err)
@@ -293,9 +310,9 @@ func attachServiceContainer(tenantName string, networkName string, stateDriver c
293310

294311
log.Debugf("Container info: %+v\n Hostconfig: %+v", cinfo, cinfo.HostConfig)
295312

296-
ninfo, err := docker.InspectNetwork(networkName)
313+
ninfo, err := docker.InspectNetwork(dnetName)
297314
if err != nil {
298-
log.Errorf("Error getting network info for %s. Err: %v", networkName, err)
315+
log.Errorf("Error getting network info for %s. Err: %v", dnetName, err)
299316
return err
300317
}
301318

@@ -477,6 +494,8 @@ func networkAllocAddress(nwCfg *mastercfg.CfgNetworkState, reqAddr string) (stri
477494
var ipAddrValue uint
478495
var found bool
479496
var err error
497+
498+
// alloc address
480499
if reqAddr == "" {
481500
ipAddrValue, found = nwCfg.IPAllocMap.NextClear(0)
482501
if !found {

netplugin/netd.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ func contAttachPointDeleted(epCtx *crtclient.ContainerEPContext) bool {
214214
func processEpEvent(netPlugin *plugin.NetPlugin, crt *crt.CRT, opts cliOpts,
215215
epID string, isDelete bool) (err error) {
216216
// Dont process endpoint events in dockplugin mode
217-
if opts.dockPlugin {
217+
// HACK alert: Ugly hack to create proxy port in docker-plugin mode
218+
if opts.dockPlugin && !strings.Contains(epID, "proxyPort") {
219+
log.Infof("Ignoring endpoint create in docker-plugin mode for: %s", epID)
218220
return nil
219221
}
220222
// take a lock to ensure we are programming one event at a time.
@@ -771,6 +773,11 @@ func main() {
771773
configureSyslog(opts.syslog)
772774
}
773775

776+
// Enable nativ-integ flag in docker plugin mode
777+
if opts.dockPlugin == true {
778+
opts.nativeInteg = true
779+
}
780+
774781
if flagSet.NFlag() < 1 {
775782
log.Infof("host-label not specified, using default (%s)", opts.hostLabel)
776783
}

scripts/python/setupProxy.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def setupProxy():
3636
{
3737
"Name" : "private",
3838
"Endpoints" : [ {
39-
"Container" : "myContainer1",
40-
"Host" : "netplugin-node1",
41-
"ServiceName" : "proxy"
39+
"Container" : "proxyPort",
40+
"Host" : "netplugin-node1"
4241
}]
4342
} ]
4443
} ]
@@ -68,7 +67,7 @@ def setupProxy():
6867

6968
# Look for pro
7069
for ep in epList:
71-
if ep['id'] == "private-myContainer1":
70+
if ep['id'] == "private.default-proxyPort":
7271
# Config ip and bringup interface
7372
print "Found the proxy endpoint, bringing up the ovs interface"
7473
print "sudo ifconfig " + ep['portName'] + " " + ep['ipAddress'] + " up"

scripts/python/startPlugin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020

2121
# Cleanup all state and start netplugin/netmaster
2222
testbed = testbedApi.testbed(addrList)
23-
time.sleep(2)
23+
24+
print "Waiting for netmaster to come up"
25+
time.sleep(15)
2426

2527
# Setup proxy
26-
# setupProxy.setupProxy()
28+
setupProxy.setupProxy()
2729

30+
print "################### Started Netplugin #####################"
2831
os._exit(0)

scripts/python/testbedApi.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading
44
import sys
55
import os
6+
import time
67

78
# Utility function to run ssh
89
def ssh_exec_thread(ssh_object, command):
@@ -151,21 +152,30 @@ def __init__(self, addrList):
151152
print "Starting netplugin on " + node.addr
152153
node.startNetplugin()
153154

155+
# Wait few seconds before starting netmaster
156+
time.sleep(3)
157+
154158
# Start netmaster in the end
155159
print "Starting netmaster"
156160
self.nodes[0].startNetmaster()
157161

158162
# Cleanup a testbed once test is done
159163
def cleanup(self):
160164
# Cleanup each node
165+
for node in self.nodes:
166+
print "Stopping containers on " + node.addr
167+
node.cleanupContainers()
168+
169+
# Stop netmaster and remove networks
170+
self.nodes[0].stopNetmaster()
171+
161172
for node in self.nodes:
162173
print "Cleaning up node " + node.addr
163174
node.stopNetplugin()
164175
node.cleanupSlave()
165176

166177
# cleanup master
167178
print "Cleaning up master"
168-
self.nodes[0].stopNetmaster()
169179
self.nodes[0].cleanupMaster()
170180

171181
# Number of nodes in the testbed
@@ -180,9 +190,9 @@ def runContainers(self, numContainer, withService=False):
180190
nodeIdx = cntIdx % self.numNodes()
181191
if withService:
182192
srvName = "srv" + str(cntIdx)
183-
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private.default", serviceName=srvName)
193+
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private", serviceName=srvName)
184194
else:
185-
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private.default")
195+
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private")
186196

187197
containers.append(cnt)
188198

@@ -194,7 +204,7 @@ def runContainersInService(self, numContainer, serviceName):
194204
# Start the containers
195205
for cntIdx in range(numContainer):
196206
nodeIdx = cntIdx % self.numNodes()
197-
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private.default", serviceName=serviceName)
207+
cnt = self.nodes[nodeIdx].runContainer("ubuntu", networkName="private", serviceName=serviceName)
198208
containers.append(cnt)
199209

200210
return containers
@@ -327,10 +337,6 @@ def remove(self):
327337
if exitCode != 0:
328338
self.errorExit("Error removing container", out, err)
329339

330-
# Unpublish the service
331-
# if self.serviceName != None:
332-
# self.node.runCmd("docker service unpublish " + self.serviceName)
333-
334340
# Get IP address of the container
335341
def getIpAddr(self, intfName="eth0"):
336342
if intfName == "eth0":
@@ -377,7 +383,7 @@ def stopListener(self):
377383
# Check if this container can connect to destination port
378384
def checkConnection(self, ipAddr, port, protocol="tcp"):
379385
protoStr = "-u " if protocol == "udp" else " "
380-
out, err, exitCode = self.execCmd("netcat -z -n -v -w 5 " + protoStr + ipAddr + " " + str(port))
386+
out, err, exitCode = self.execCmd("netcat -z -n -v -w 1 " + protoStr + ipAddr + " " + str(port))
381387

382388
print "checkConnection Output(" + str(exitCode) + "): " + ''.join(out)
383389
print "checkConnection Err: " + ''.join(err)

0 commit comments

Comments
 (0)