Skip to content

Commit e7d1e75

Browse files
authored
Merge pull request #41 from Azure/development
Development
2 parents baec8e8 + 4e4427c commit e7d1e75

File tree

4 files changed

+58
-37
lines changed

4 files changed

+58
-37
lines changed

cni/network/network.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
158158
return nil
159159
}
160160

161+
ip, ipv4Address, err := net.ParseCIDR(address)
162+
ipv4Address.IP = ip
163+
if err != nil {
164+
log.Printf("[cni] Failed to parse address %v, err:%v.", address, err)
165+
return nil
166+
}
167+
161168
log.Printf("[cni] Allocated address: %v", address)
162169

163170
// Create the endpoint.
164171
epInfo := network.EndpointInfo{
165172
Id: endpointId,
166173
IfName: args.IfName,
167-
IPv4Address: address,
174+
IPv4Address: *ipv4Address,
168175
NetNsPath: args.Netns,
169176
}
170177

@@ -175,15 +182,8 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
175182
}
176183

177184
// Output the result.
178-
ip, cidr, err := net.ParseCIDR(address)
179-
cidr.IP = ip
180-
if err != nil {
181-
log.Printf("[cni] Failed to parse address, err:%v.", err)
182-
return nil
183-
}
184-
185185
result := &cniTypes.Result{
186-
IP4: &cniTypes.IPConfig{IP: *cidr},
186+
IP4: &cniTypes.IPConfig{IP: *ipv4Address},
187187
}
188188

189189
result.Print()
@@ -238,7 +238,7 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
238238
}
239239

240240
// Release the address.
241-
err = plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwInfo.Subnets[0], epInfo.IPv4Address)
241+
err = plugin.am.ReleaseAddress(nwCfg.Ipam.AddrSpace, nwInfo.Subnets[0], epInfo.IPv4Address.IP.String())
242242
if err != nil {
243243
log.Printf("[cni] Failed to release address, err:%v.", err)
244244
return nil

cnm/network/network.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package network
55

66
import (
7+
"net"
78
"net/http"
89

910
"github.com/Azure/azure-container-networking/cnm"
@@ -18,6 +19,9 @@ const (
1819

1920
// Plugin capabilities.
2021
scope = "local"
22+
23+
// Prefix for container network interface names.
24+
containerInterfacePrefix = "eth"
2125
)
2226

2327
// NetPlugin represents a CNM (libnetwork) network plugin.
@@ -186,14 +190,20 @@ func (plugin *netPlugin) createEndpoint(w http.ResponseWriter, r *http.Request)
186190
}
187191

188192
// Process request.
189-
var ipv4Address string
193+
var ipv4Address *net.IPNet
190194
if req.Interface != nil {
191-
ipv4Address = req.Interface.Address
195+
var ip net.IP
196+
ip, ipv4Address, err = net.ParseCIDR(req.Interface.Address)
197+
if err != nil {
198+
plugin.SendErrorResponse(w, err)
199+
return
200+
}
201+
ipv4Address.IP = ip
192202
}
193203

194204
epInfo := network.EndpointInfo{
195205
Id: req.EndpointID,
196-
IPv4Address: ipv4Address,
206+
IPv4Address: *ipv4Address,
197207
}
198208

199209
err = plugin.nm.CreateEndpoint(req.NetworkID, &epInfo)
@@ -257,8 +267,8 @@ func (plugin *netPlugin) join(w http.ResponseWriter, r *http.Request) {
257267

258268
// Encode response.
259269
ifname := interfaceName{
260-
SrcName: ep.SrcName,
261-
DstPrefix: ep.DstPrefix,
270+
SrcName: ep.IfName,
271+
DstPrefix: containerInterfacePrefix,
262272
}
263273

264274
resp := joinResponse{

network/endpoint.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const (
2424
type endpoint struct {
2525
Id string
2626
SandboxKey string
27-
SrcName string
28-
DstPrefix string
27+
IfName string
28+
HostIfName string
2929
MacAddress net.HardwareAddr
3030
IPv4Address net.IPNet
3131
IPv6Address net.IPNet
@@ -37,8 +37,8 @@ type endpoint struct {
3737
type EndpointInfo struct {
3838
Id string
3939
IfName string
40-
IPv4Address string
4140
NetNsPath string
41+
IPv4Address net.IPNet
4242
}
4343

4444
// NewEndpoint creates a new endpoint in the network.
@@ -54,12 +54,7 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
5454
return nil, errEndpointExists
5555
}
5656

57-
// Parse IP address.
58-
ipAddr, ipNet, err := net.ParseCIDR(epInfo.IPv4Address)
59-
ipNet.IP = ipAddr
60-
if err != nil {
61-
return nil, err
62-
}
57+
ipAddr := epInfo.IPv4Address
6358

6459
// Create a veth pair.
6560
hostIfName := fmt.Sprintf("%s%s", hostInterfacePrefix, epInfo.Id[:7])
@@ -102,7 +97,7 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
10297

10398
// Setup MAC address translation rules for container interface.
10499
log.Printf("[net] Setting up MAC address translation rules for endpoint %v.", contIfName)
105-
err = ebtables.SetupDnatBasedOnIPV4Address(ipAddr.String(), containerIf.HardwareAddr.String())
100+
err = ebtables.SetupDnatBasedOnIPV4Address(ipAddr.IP.String(), containerIf.HardwareAddr.String())
106101
if err != nil {
107102
goto cleanup
108103
}
@@ -158,8 +153,8 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
158153
}
159154

160155
// Assign IP address to container network interface.
161-
log.Printf("[net] Adding IP address %v to link %v.", ipAddr, contIfName)
162-
err = netlink.AddIpAddress(contIfName, ipAddr, ipNet)
156+
log.Printf("[net] Adding IP address %v to link %v.", ipAddr.String(), contIfName)
157+
err = netlink.AddIpAddress(contIfName, ipAddr.IP, &ipAddr)
163158
if err != nil {
164159
goto cleanup
165160
}
@@ -177,10 +172,10 @@ func (nw *network) newEndpoint(epInfo *EndpointInfo) (*endpoint, error) {
177172
// Create the endpoint object.
178173
ep = &endpoint{
179174
Id: epInfo.Id,
180-
SrcName: contIfName,
181-
DstPrefix: containerInterfacePrefix,
175+
IfName: contIfName,
176+
HostIfName: hostIfName,
182177
MacAddress: containerIf.HardwareAddr,
183-
IPv4Address: *ipNet,
178+
IPv4Address: ipAddr,
184179
IPv6Address: net.IPNet{},
185180
IPv4Gateway: nw.extIf.IPv4Gateway,
186181
IPv6Gateway: nw.extIf.IPv6Gateway,
@@ -203,25 +198,41 @@ cleanup:
203198

204199
// DeleteEndpoint deletes an existing endpoint from the network.
205200
func (nw *network) deleteEndpoint(endpointId string) error {
201+
log.Printf("[net] Deleting endpoint %v from network %v.", endpointId, nw.Id)
202+
203+
// Look up the endpoint.
206204
ep, err := nw.getEndpoint(endpointId)
207205
if err != nil {
208-
return err
206+
goto cleanup
209207
}
210208

211-
log.Printf("[net] Deleting endpoint %+v.", ep)
212-
213-
// Delete veth pair.
214-
netlink.DeleteLink(ep.SrcName)
209+
// Delete the veth pair by deleting one of the peer interfaces.
210+
// Deleting the host interface is more convenient since it does not require
211+
// entering the container netns and hence works both for CNI and CNM.
212+
log.Printf("[net] Deleting veth pair %v %v.", ep.HostIfName, ep.IfName)
213+
err = netlink.DeleteLink(ep.HostIfName)
214+
if err != nil {
215+
goto cleanup
216+
}
215217

216-
// Cleanup MAC address translation rules.
218+
// Delete MAC address translation rule.
219+
log.Printf("[net] Deleting MAC address translation rule for endpoint %v.", endpointId)
217220
err = ebtables.RemoveDnatBasedOnIPV4Address(ep.IPv4Address.IP.String(), ep.MacAddress.String())
221+
if err != nil {
222+
goto cleanup
223+
}
218224

219225
// Remove the endpoint object.
220226
delete(nw.Endpoints, endpointId)
221227

222228
log.Printf("[net] Deleted endpoint %+v.", ep)
223229

224230
return nil
231+
232+
cleanup:
233+
log.Printf("[net] Deleting endpoint %v failed, err:%v.", endpointId, err)
234+
235+
return err
225236
}
226237

227238
// GetEndpoint returns the endpoint with the given ID.

network/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (nm *networkManager) GetEndpointInfo(networkId string, endpointId string) (
251251

252252
epInfo := &EndpointInfo{
253253
Id: endpointId,
254-
IPv4Address: ep.IPv4Address.String(),
254+
IPv4Address: ep.IPv4Address,
255255
}
256256

257257
return epInfo, nil

0 commit comments

Comments
 (0)