Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Kata Containers networking support #817

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/ocihook/ocihook.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ import (
"github.com/sirupsen/logrus"
)

const (
// NetworkNamespace is the network namespace path to be passed to the CNI plugins.
// When this annotation is set from the runtime spec.State payload, it takes
// precedence over the PID based resolution (/proc/<pid>/ns/net) where pid is
// spec.State.Pid.
// This is mostly used for VM based runtime, where the spec.State PID does not
// necessarily lives in the created container networking namespace.
NetworkNamespace = labels.Prefix + "network-namespace"
)

func Run(stdin io.Reader, stderr io.Writer, event, dataStore, cniPath, cniNetconfPath string) error {
if stdin == nil || event == "" || dataStore == "" || cniPath == "" || cniNetconfPath == "" {
return errors.New("got insufficient args")
Expand Down Expand Up @@ -252,9 +262,21 @@ func loadSpec(bundle string) (*hookSpec, error) {
}

func getNetNSPath(state *specs.State) (string, error) {
if state.Pid == 0 {
return "", errors.New("state.Pid is unset")
// If we have a network-namespace annotation we use it over the passed Pid.
netNsPath, netNsFound := state.Annotations[NetworkNamespace]
if netNsFound {
if _, err := os.Stat(netNsPath); err != nil {
return "", err
}

return netNsPath, nil
}

if state.Pid == 0 && !netNsFound {
return "", errors.New("Both state.Pid and the netNs annotation are unset")
}

// We dont't have a networking namespace annotation, but we have a PID.
s := fmt.Sprintf("/proc/%d/ns/net", state.Pid)
if _, err := os.Stat(s); err != nil {
return "", err
Expand Down