-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/ocicni: a cross-platform namespace manager thing
Signed-off-by: Vincent Batts <[email protected]>
- Loading branch information
Showing
4 changed files
with
75 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,8 @@ | ||
package ocicni | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
func getContainerIP(nsenterPath, netnsPath, interfaceName, addrType string) (net.IP, error) { | ||
// Try to retrieve ip inside container network namespace | ||
output, err := exec.Command(nsenterPath, fmt.Sprintf("--net=%s", netnsPath), "-F", "--", | ||
"ip", "-o", addrType, "addr", "show", "dev", interfaceName, "scope", "global").CombinedOutput() | ||
if err != nil { | ||
return nil, fmt.Errorf("Unexpected command output %s with error: %v", output, err) | ||
} | ||
|
||
lines := strings.Split(string(output), "\n") | ||
if len(lines) < 1 { | ||
return nil, fmt.Errorf("Unexpected command output %s", output) | ||
} | ||
fields := strings.Fields(lines[0]) | ||
if len(fields) < 4 { | ||
return nil, fmt.Errorf("Unexpected address output %s ", lines[0]) | ||
} | ||
ip, _, err := net.ParseCIDR(fields[3]) | ||
if err != nil { | ||
return nil, fmt.Errorf("CNI failed to parse ip from output %s due to %v", output, err) | ||
} | ||
|
||
return ip, nil | ||
// newNSManager initializes a new namespace manager, which is a platform dependent struct. | ||
func newNSManager() (*nsManager, error) { | ||
nsm := &nsManager{} | ||
err := nsm.init() | ||
return nsm, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// +build linux | ||
|
||
package ocicni | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
var defaultNamespaceEnterCommandName = "nsenter" | ||
|
||
type nsManager struct { | ||
nsenterPath string | ||
} | ||
|
||
func (nsm *nsManager) init() error { | ||
var err error | ||
nsm.nsenterPath, err = exec.LookPath(defaultNamespaceEnterCommandName) | ||
return err | ||
} | ||
|
||
func getContainerIP(nsm *nsManager, netnsPath, interfaceName, addrType string) (net.IP, error) { | ||
// Try to retrieve ip inside container network namespace | ||
output, err := exec.Command(nsm.nsenterPath, fmt.Sprintf("--net=%s", netnsPath), "-F", "--", | ||
"ip", "-o", addrType, "addr", "show", "dev", interfaceName, "scope", "global").CombinedOutput() | ||
if err != nil { | ||
return nil, fmt.Errorf("Unexpected command output %s with error: %v", output, err) | ||
} | ||
|
||
lines := strings.Split(string(output), "\n") | ||
if len(lines) < 1 { | ||
return nil, fmt.Errorf("Unexpected command output %s", output) | ||
} | ||
fields := strings.Fields(lines[0]) | ||
if len(fields) < 4 { | ||
return nil, fmt.Errorf("Unexpected address output %s ", lines[0]) | ||
} | ||
ip, _, err := net.ParseCIDR(fields[3]) | ||
if err != nil { | ||
return nil, fmt.Errorf("CNI failed to parse ip from output %s due to %v", output, err) | ||
} | ||
|
||
return ip, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// +build !linux | ||
|
||
package ocicni | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
type nsManager struct { | ||
} | ||
|
||
func (nsm *nsManager) init() error { | ||
return nil | ||
} | ||
|
||
func getContainerIP(nsm *nsManager, netnsPath, interfaceName, addrType string) (net.IP, error) { | ||
return nil, fmt.Errorf("not supported yet") | ||
} |