Skip to content

Commit 655f213

Browse files
authored
Merge pull request #817 from sameo/topic/kata-network
Fix Kata Containers networking support
2 parents 41ceb29 + 17d8c00 commit 655f213

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

pkg/ocihook/ocihook.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ import (
4444
"github.com/sirupsen/logrus"
4545
)
4646

47+
const (
48+
// NetworkNamespace is the network namespace path to be passed to the CNI plugins.
49+
// When this annotation is set from the runtime spec.State payload, it takes
50+
// precedence over the PID based resolution (/proc/<pid>/ns/net) where pid is
51+
// spec.State.Pid.
52+
// This is mostly used for VM based runtime, where the spec.State PID does not
53+
// necessarily lives in the created container networking namespace.
54+
NetworkNamespace = labels.Prefix + "network-namespace"
55+
)
56+
4757
func Run(stdin io.Reader, stderr io.Writer, event, dataStore, cniPath, cniNetconfPath string) error {
4858
if stdin == nil || event == "" || dataStore == "" || cniPath == "" || cniNetconfPath == "" {
4959
return errors.New("got insufficient args")
@@ -252,9 +262,21 @@ func loadSpec(bundle string) (*hookSpec, error) {
252262
}
253263

254264
func getNetNSPath(state *specs.State) (string, error) {
255-
if state.Pid == 0 {
256-
return "", errors.New("state.Pid is unset")
265+
// If we have a network-namespace annotation we use it over the passed Pid.
266+
netNsPath, netNsFound := state.Annotations[NetworkNamespace]
267+
if netNsFound {
268+
if _, err := os.Stat(netNsPath); err != nil {
269+
return "", err
270+
}
271+
272+
return netNsPath, nil
257273
}
274+
275+
if state.Pid == 0 && !netNsFound {
276+
return "", errors.New("Both state.Pid and the netNs annotation are unset")
277+
}
278+
279+
// We dont't have a networking namespace annotation, but we have a PID.
258280
s := fmt.Sprintf("/proc/%d/ns/net", state.Pid)
259281
if _, err := os.Stat(s); err != nil {
260282
return "", err

0 commit comments

Comments
 (0)