@@ -24,8 +24,8 @@ const (
2424type 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 {
3737type 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.
205200func (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.
0 commit comments