Skip to content

Commit ce51646

Browse files
authored
Update (#2)
* Refactoring and better error checking * Add the possibility to specify the nsPath
1 parent 5422104 commit ce51646

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

main.go

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/vishvananda/netns"
1818
)
1919

20-
var ip, command, gateway, intf, logLevel string
20+
var ip, command, gateway, intf, logLevel, nsPath string
2121
var log = logrus.New()
2222

2323
func init() {
@@ -26,6 +26,12 @@ func init() {
2626
flag.StringVar(&command, "command", "ip route", "command to be executed")
2727
flag.StringVar(&gateway, "gw", "", "gateway of the request")
2828
flag.StringVar(&logLevel, "log-level", "info", "min level of logs to print")
29+
flag.StringVar(
30+
&nsPath,
31+
"ns-path",
32+
fmt.Sprintf("/var/run/netns/w000t%d", os.Getpid()),
33+
"path of the temporary namespace to be created, default will be /var/run/netns/w000t$PID",
34+
)
2935
flag.Parse()
3036
}
3137

@@ -58,18 +64,17 @@ func main() {
5864
// Check the /run/netns mount
5965
err = setupNetnsDir()
6066
if err != nil {
61-
log.Warn("Error setting up netns", err)
67+
log.Warn("Error setting up netns: ", err)
6268
return
6369
}
6470

6571
eth, err := netlink.LinkByName(intf)
6672
if err != nil {
67-
log.Warnf("error while getting %s : %s", intf, err)
73+
log.Warnf("Error while getting %s : %s", intf, err)
6874
return
6975
}
7076
log.Debugf("%s : %+v", intf, eth.Attrs().Flags)
7177

72-
// askAndPrint()
7378
// ============================== Create the macVLAN
7479

7580
log.Debug("Create a new macVlan")
@@ -96,26 +101,27 @@ func main() {
96101

97102
link, err := netlink.LinkByName("peth0")
98103
if err != nil {
99-
log.Warn("error while getting macVlan :", err)
104+
log.Warn("Error while getting macVlan: ", err)
100105
return
101106
}
102107
log.Debugf("MacVlan created : %+v", link)
103108

104-
// askAndPrint()
105109
// ============================== Create the new Namespace
106110

107111
newns, err := newNS()
108112
if err != nil {
109-
log.Warn("error while creating new NS :", err)
113+
log.Warn("error while creating new NS: ", err)
110114
return
111115
}
112-
defer delNS(newns)
116+
defer deleteNS(newns)
113117

114118
log.Debug("Go back to original NS")
115119

116-
netns.Set(origns)
117-
118-
// askAndPrint()
120+
err = netns.Set(origns)
121+
if err != nil {
122+
log.Warn("Failed to change the namespace: ", err)
123+
return
124+
}
119125

120126
// ============================== Add the MacVlan in the new Namespace
121127

@@ -129,46 +135,62 @@ func main() {
129135

130136
log.Debug("Enter the namespace")
131137

132-
netns.Set(*newns)
138+
err = netns.Set(*newns)
139+
if err != nil {
140+
log.Warn("Failed to enter the namespace: ", err)
141+
return
142+
}
133143

134144
// ============================= Configure the new namespace to configure it
135145

136146
addr, err := netlink.ParseAddr(ip)
137147
if err != nil {
138-
log.Warn("Failed to parse ip", err)
148+
log.Warn("Failed to parse the given IP: ", err)
139149
return
140150
}
141151

142152
log.Debugf("Add the addr to the macVlan: %+v", addr)
143-
netlink.AddrAdd(link, addr)
153+
// ============================= Set the address in the namespace
154+
err = netlink.AddrAdd(link, addr)
155+
if err != nil {
156+
log.Warn("Failed to add the IP to the macVlan: ", err)
157+
return
158+
}
144159
gwaddr := net.ParseIP(gateway)
145160

146161
log.Debug("Set the macVlan interface UP")
162+
// ============================= Set the link up in the namespace
147163
err = netlink.LinkSetUp(link)
148164
if err != nil {
149-
log.Warn("Error while setting up the interface peth0", err)
165+
log.Warn("Error while setting up the interface peth0: ", err)
150166
return
151167
}
152168

153169
log.Debugf("Set %s as the route", gwaddr)
170+
// ============================= Set the default route in the namespace
154171
err = netlink.RouteAdd(&netlink.Route{
155172
Scope: netlink.SCOPE_UNIVERSE,
156173
LinkIndex: link.Attrs().Index,
157174
Gw: gwaddr,
158175
})
159176
if err != nil {
160-
log.Warn("Error while setting up route on interface peth0", err)
177+
log.Warn("Error while setting up route on interface peth0 :", err)
161178
return
162179
}
163180

181+
// ============================= Execute the command in the namespace
164182
err = execCmd(command)
165183
if err != nil {
166-
log.Warn("error while checking IP", err)
184+
log.Warn("Error while running command : ", err)
167185
}
168186

169187
log.Debug("Go back to orignal namspace")
170188

171-
netns.Set(origns)
189+
err = netns.Set(origns)
190+
if err != nil {
191+
log.Warn("Error while going back to the original namespace: ", err)
192+
return
193+
}
172194

173195
log.Debug("Cleaning ...")
174196
}
@@ -238,7 +260,7 @@ func newNS() (*netns.NsHandle, error) {
238260
}
239261

240262
src := fmt.Sprintf("/proc/%d/ns/net", pid)
241-
target := getNsName()
263+
target := nsPath
242264

243265
log.Debugf("Create file %s", target)
244266
// Create an empty file
@@ -248,9 +270,14 @@ func newNS() (*netns.NsHandle, error) {
248270
return nil, err
249271
}
250272
// And close it
251-
file.Close()
273+
err = file.Close()
274+
if err != nil {
275+
log.Warn(err)
276+
return nil, err
277+
}
252278

253279
log.Debugf("Mount %s", target)
280+
254281
// Mount the namespace in /var/run/netns so it becomes a named namespace
255282
if err := syscall.Mount(src, target, "proc", syscall.MS_BIND|syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV, ""); err != nil {
256283
return nil, err
@@ -259,33 +286,28 @@ func newNS() (*netns.NsHandle, error) {
259286
return &newns, nil
260287
}
261288

262-
// getNsName gets the default namespace name : w000t$PID$
263-
func getNsName() string {
264-
pid := os.Getpid()
265-
return fmt.Sprintf("/var/run/netns/w000t%d", pid)
266-
}
267-
268-
func delNS(ns *netns.NsHandle) error {
289+
// deleteNS will delete the given namespace
290+
func deleteNS(ns *netns.NsHandle) error {
269291
// Close the nsHandler
270292
err := ns.Close()
271293
if err != nil {
272-
log.Warn("Error while closing", err)
294+
log.Warn("Error while closing the namespace: ", err)
273295
return err
274296
}
275297

276298
// Unmount the named namespace
277-
target := getNsName()
299+
target := nsPath
278300

279301
log.Debugf("Unmounting %s", target)
280302
if err := syscall.Unmount(target, 0); err != nil {
281-
log.Warn("Error while unmounting", err)
303+
log.Warnf("Error while unmounting %s : %s", target, err)
282304
return err
283305
}
284306

285307
// Delete the namespace file
286308
log.Debugf("Deleting %s", target)
287309
if err := os.Remove(target); err != nil {
288-
log.Warn(err)
310+
log.Warn("Error while deleting %s : %s", target, err)
289311
return err
290312
}
291313

0 commit comments

Comments
 (0)