@@ -221,56 +221,24 @@ func (l *LinuxJail) setupNetworking() error {
221221 vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
222222 vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
223223
224- cmd := exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail )
225- err := cmd .Run ()
226- if err != nil {
227- return fmt .Errorf ("failed to create veth pair: %v" , err )
228- }
229-
230- // Move netjail end to namespace
231- cmd = exec .Command ("ip" , "link" , "set" , vethNetJail , "netns" , l .namespace )
232- err = cmd .Run ()
233- if err != nil {
234- return fmt .Errorf ("failed to move veth to namespace: %v" , err )
235- }
236-
237- // Configure host side of veth pair
238- cmd = exec .Command ("ip" , "addr" , "add" , "192.168.100.1/24" , "dev" , vethHost )
239- err = cmd .Run ()
240- if err != nil {
241- return fmt .Errorf ("failed to configure host veth: %v" , err )
242- }
243-
244- cmd = exec .Command ("ip" , "link" , "set" , vethHost , "up" )
245- err = cmd .Run ()
246- if err != nil {
247- return fmt .Errorf ("failed to bring up host veth: %v" , err )
248- }
249-
250- // Configure namespace side of veth pair
251- cmd = exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "addr" , "add" , "192.168.100.2/24" , "dev" , vethNetJail )
252- err = cmd .Run ()
253- if err != nil {
254- return fmt .Errorf ("failed to configure namespace veth: %v" , err )
255- }
256-
257- cmd = exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "link" , "set" , vethNetJail , "up" )
258- err = cmd .Run ()
259- if err != nil {
260- return fmt .Errorf ("failed to bring up namespace veth: %v" , err )
224+ setupCmds := []struct {
225+ description string
226+ command * exec.Cmd
227+ }{
228+ {"create veth pair" , exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail )},
229+ {"move veth to namespace" , exec .Command ("ip" , "link" , "set" , vethNetJail , "netns" , l .namespace )},
230+ {"configure host veth" , exec .Command ("ip" , "addr" , "add" , "192.168.100.1/24" , "dev" , vethHost )},
231+ {"bring up host veth" , exec .Command ("ip" , "link" , "set" , vethHost , "up" )},
232+ {"configure namespace veth" , exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "addr" , "add" , "192.168.100.2/24" , "dev" , vethNetJail )},
233+ {"bring up namespace veth" , exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "link" , "set" , vethNetJail , "up" )},
234+ {"bring up loopback" , exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "link" , "set" , "lo" , "up" )},
235+ {"set default route in namespace" , exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "route" , "add" , "default" , "via" , "192.168.100.1" )},
261236 }
262237
263- cmd = exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "link" , "set" , "lo" , "up" )
264- err = cmd .Run ()
265- if err != nil {
266- return fmt .Errorf ("failed to bring up loopback: %v" , err )
267- }
268-
269- // Set default route in namespace
270- cmd = exec .Command ("ip" , "netns" , "exec" , l .namespace , "ip" , "route" , "add" , "default" , "via" , "192.168.100.1" )
271- err = cmd .Run ()
272- if err != nil {
273- return fmt .Errorf ("failed to set default route: %v" , err )
238+ for _ , command := range setupCmds {
239+ if err := command .command .Run (); err != nil {
240+ return fmt .Errorf ("failed to %s: %v" , command .description , err )
241+ }
274242 }
275243
276244 return nil
@@ -355,4 +323,4 @@ func (l *LinuxJail) removeNamespace() error {
355323 return fmt .Errorf ("failed to remove namespace: %v" , err )
356324 }
357325 return nil
358- }
326+ }
0 commit comments