Skip to content

Commit 7ee2e51

Browse files
committed
pkg/ocicni: Use 'ifconfig -j' to access jail network state
The use of 'jexec' for this requires a compatible ifconfig binary inside the jail which owns the network state and using 'ifconfig -j' lets us merge the jail which owns the pod network with the infra container. This also fixes some parsing bugs in getContainerDetails which were not noticed before since most of the time we get the information from cni's CheckNetworkList. Signed-off-by: Doug Rabson <[email protected]>
1 parent eb13a3b commit 7ee2e51

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

pkg/ocicni/util_freebsd.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import (
1010
"strings"
1111
)
1212

13-
var defaultJexecCommandName = "jexec"
14-
15-
type nsManager struct {
16-
jexecPath string
17-
}
13+
type nsManager struct{}
1814

1915
func (nsm *nsManager) init() error {
2016
var err error
21-
nsm.jexecPath, err = exec.LookPath(defaultJexecCommandName)
2217
return err
2318
}
2419

2520
func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType string) (*net.IPNet, *net.HardwareAddr, error) {
2621
// Try to retrieve ip inside container network namespace
22+
if addrType == "-4" {
23+
addrType = "inet"
24+
} else {
25+
addrType = "inet6"
26+
}
2727
output, err := exec.Command(
28-
nsm.jexecPath, netnsJailName,
29-
"ifconfig", "-f", "inet:cidr,inet6:cidr",
28+
"ifconfig", "-j", netnsJailName,
29+
"-f", "inet:cidr,inet6:cidr",
3030
interfaceName,
3131
addrType).CombinedOutput()
3232
if err != nil {
@@ -38,7 +38,7 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
3838
return nil, nil, fmt.Errorf("Unexpected command output %s", output)
3939
}
4040
fields := strings.Fields(strings.TrimSpace(lines[2]))
41-
if len(fields) < 4 {
41+
if len(fields) < 2 {
4242
return nil, nil, fmt.Errorf("Unexpected address output %s ", lines[0])
4343
}
4444
ip, ipNet, err := net.ParseCIDR(fields[1])
@@ -53,8 +53,7 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
5353

5454
// Try to retrieve MAC inside container network namespace
5555
output, err = exec.Command(
56-
nsm.jexecPath, netnsJailName,
57-
"ifconfig", "-f", "inet:cidr,inet6:cidr",
56+
"ifconfig", "-j", netnsJailName, "-f", "inet:cidr,inet6:cidr",
5857
interfaceName,
5958
"ether").CombinedOutput()
6059
if err != nil {
@@ -65,7 +64,7 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
6564
if len(lines) < 3 {
6665
return nil, nil, fmt.Errorf("unexpected ifconfig command output %s", output)
6766
}
68-
fields = strings.Fields(strings.TrimSpace(lines[1]))
67+
fields = strings.Fields(strings.TrimSpace(lines[2]))
6968
if len(fields) < 2 {
7069
return nil, nil, fmt.Errorf("unexpected ether output %s ", lines[0])
7170
}
@@ -78,7 +77,7 @@ func getContainerDetails(nsm *nsManager, netnsJailName, interfaceName, addrType
7877
}
7978

8079
func bringUpLoopback(netns string) error {
81-
if err := exec.Command("jexec", netns, "ifconfig", "lo0", "inet", "127.0.0.1").Run(); err != nil {
80+
if err := exec.Command("ifconfig", "-j", netns, "lo0", "inet", "127.0.0.1").Run(); err != nil {
8281
return fmt.Errorf("failed to initialize loopback: %w", err)
8382
}
8483
return nil

0 commit comments

Comments
 (0)