Skip to content

Commit

Permalink
safely handle existing net namespace in default network manager
Browse files Browse the repository at this point in the history
When a client restarts, the network_hook's prerun will call
`CreateNetwork`. Drivers that don't implement their own network manager will
fall back to the default network manager, which doesn't handle the case where
the network namespace is being recreated safely. This results in an error and
the task being restarted for `exec` tasks with `network` blocks (this also
impacts the community `containerd` and probably other community task drivers).

If we get an error when attempting to create the namespace and that error is
because the file already exists and is locked by its process, then we'll
return a `nil` error with the `created` flag set to false, just as we do with
the `docker` driver.
  • Loading branch information
tgross committed Jan 8, 2021
1 parent 463fed9 commit 870d955
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client/allocrunner/network_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package allocrunner

import (
"fmt"
"os"
"path"
"strings"
"syscall"

hclog "github.com/hashicorp/go-hclog"
clientconfig "github.com/hashicorp/nomad/client/config"
Expand Down Expand Up @@ -92,6 +95,15 @@ type defaultNetworkManager struct{}
func (*defaultNetworkManager) CreateNetwork(allocID string) (*drivers.NetworkIsolationSpec, bool, error) {
netns, err := nsutil.NewNS(allocID)
if err != nil {
// when a client restarts, the namespace will already exist and
// there will be a namespace file in use by the task process
if e, ok := err.(*os.PathError); ok && e.Err == syscall.EPERM {
nsPath := path.Join(nsutil.NetNSRunDir, allocID)
_, err := os.Stat(nsPath)
if err == nil {
return nil, false, nil
}
}
return nil, false, err
}

Expand Down

0 comments on commit 870d955

Please sign in to comment.