@@ -52,6 +52,13 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
5252 return nil , err
5353 }
5454
55+ // On failure, delete the veth pair.
56+ defer func () {
57+ if err != nil {
58+ netlink .DeleteLink (contIfName )
59+ }
60+ }()
61+
5562 //
5663 // Host network interface setup.
5764 //
@@ -60,14 +67,14 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
6067 log .Printf ("[net] Setting link %v state up." , hostIfName )
6168 err = netlink .SetLinkState (hostIfName , true )
6269 if err != nil {
63- goto cleanup
70+ return nil , err
6471 }
6572
6673 // Connect host interface to the bridge.
6774 log .Printf ("[net] Setting link %v master %v." , hostIfName , nw .extIf .BridgeName )
6875 err = netlink .SetLinkMaster (hostIfName , nw .extIf .BridgeName )
6976 if err != nil {
70- goto cleanup
77+ return nil , err
7178 }
7279
7380 //
@@ -77,7 +84,7 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
7784 // Query container network interface info.
7885 containerIf , err = net .InterfaceByName (contIfName )
7986 if err != nil {
80- goto cleanup
87+ return nil , err
8188 }
8289
8390 // Setup rules for IP addresses on the container interface.
@@ -86,14 +93,14 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
8693 log .Printf ("[net] Adding ARP reply rule for IP address %v on %v." , ipAddr .String (), contIfName )
8794 err = ebtables .SetArpReply (ipAddr .IP , nw .getArpReplyAddress (containerIf .HardwareAddr ), ebtables .Append )
8895 if err != nil {
89- goto cleanup
96+ return nil , err
9097 }
9198
9299 // Add MAC address translation rule.
93100 log .Printf ("[net] Adding MAC DNAT rule for IP address %v on %v." , ipAddr .String (), contIfName )
94101 err = ebtables .SetDnatForIPAddress (nw .extIf .Name , ipAddr .IP , containerIf .HardwareAddr , ebtables .Append )
95102 if err != nil {
96- goto cleanup
103+ return nil , err
97104 }
98105 }
99106
@@ -103,23 +110,32 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
103110 log .Printf ("[net] Opening netns %v." , epInfo .NetNsPath )
104111 ns , err = OpenNamespace (epInfo .NetNsPath )
105112 if err != nil {
106- goto cleanup
113+ return nil , err
107114 }
108115 defer ns .Close ()
109116
110117 // Move the container interface to container's network namespace.
111118 log .Printf ("[net] Setting link %v netns %v." , contIfName , epInfo .NetNsPath )
112119 err = netlink .SetLinkNetNs (contIfName , ns .GetFd ())
113120 if err != nil {
114- goto cleanup
121+ return nil , err
115122 }
116123
117124 // Enter the container network namespace.
118125 log .Printf ("[net] Entering netns %v." , epInfo .NetNsPath )
119126 err = ns .Enter ()
120127 if err != nil {
121- goto cleanup
128+ return nil , err
122129 }
130+
131+ // Return to host network namespace.
132+ defer func () {
133+ log .Printf ("[net] Exiting netns %v." , epInfo .NetNsPath )
134+ err = ns .Exit ()
135+ if err != nil {
136+ log .Printf ("[net] Failed to exit netns, err:%v." , err )
137+ }
138+ }()
123139 }
124140
125141 // If a name for the container interface is specified...
@@ -128,22 +144,22 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
128144 log .Printf ("[net] Setting link %v state down." , contIfName )
129145 err = netlink .SetLinkState (contIfName , false )
130146 if err != nil {
131- goto cleanup
147+ return nil , err
132148 }
133149
134150 // Rename the container interface.
135151 log .Printf ("[net] Setting link %v name %v." , contIfName , epInfo .IfName )
136152 err = netlink .SetLinkName (contIfName , epInfo .IfName )
137153 if err != nil {
138- goto cleanup
154+ return nil , err
139155 }
140156 contIfName = epInfo .IfName
141157
142158 // Bring the interface back up.
143159 log .Printf ("[net] Setting link %v state up." , contIfName )
144160 err = netlink .SetLinkState (contIfName , true )
145161 if err != nil {
146- goto cleanup
162+ return nil , err
147163 }
148164 }
149165
@@ -152,7 +168,7 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
152168 log .Printf ("[net] Adding IP address %v to link %v." , ipAddr .String (), contIfName )
153169 err = netlink .AddIpAddress (contIfName , ipAddr .IP , & ipAddr )
154170 if err != nil {
155- goto cleanup
171+ return nil , err
156172 }
157173 }
158174
@@ -169,17 +185,7 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
169185
170186 err = netlink .AddIpRoute (nlRoute )
171187 if err != nil {
172- goto cleanup
173- }
174- }
175-
176- // If inside the container network namespace...
177- if ns != nil {
178- // Return to host network namespace.
179- log .Printf ("[net] Exiting netns %v." , epInfo .NetNsPath )
180- err = ns .Exit ()
181- if err != nil {
182- goto cleanup
188+ return nil , err
183189 }
184190 }
185191
@@ -194,12 +200,6 @@ func (nw *network) newEndpointImpl(epInfo *EndpointInfo) (*endpoint, error) {
194200 }
195201
196202 return ep , nil
197-
198- cleanup:
199- // Roll back the changes for the endpoint.
200- netlink .DeleteLink (contIfName )
201-
202- return nil , err
203203}
204204
205205// deleteEndpointImpl deletes an existing endpoint from the network.
0 commit comments