-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge network namespaces work into master (#6046)
Merge network namespaces work into master
- Loading branch information
Showing
101 changed files
with
8,510 additions
and
907 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
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
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
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
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,88 @@ | ||
package allocrunner | ||
|
||
import ( | ||
"fmt" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/plugins/drivers" | ||
) | ||
|
||
// networkHook is an alloc lifecycle hook that manages the network namespace | ||
// for an alloc | ||
type networkHook struct { | ||
// setter is a callback to set the network isolation spec when after the | ||
// network is created | ||
setter networkIsolationSetter | ||
|
||
// manager is used when creating the network namespace. This defaults to | ||
// bind mounting a network namespace descritor under /var/run/netns but | ||
// can be created by a driver if nessicary | ||
manager drivers.DriverNetworkManager | ||
|
||
// alloc should only be read from | ||
alloc *structs.Allocation | ||
|
||
// spec described the network namespace and is syncronized by specLock | ||
spec *drivers.NetworkIsolationSpec | ||
|
||
// networkConfigurator configures the network interfaces, routes, etc once | ||
// the alloc network has been created | ||
networkConfigurator NetworkConfigurator | ||
|
||
logger hclog.Logger | ||
} | ||
|
||
func newNetworkHook(logger hclog.Logger, ns networkIsolationSetter, | ||
alloc *structs.Allocation, netManager drivers.DriverNetworkManager, | ||
netConfigurator NetworkConfigurator) *networkHook { | ||
return &networkHook{ | ||
setter: ns, | ||
alloc: alloc, | ||
manager: netManager, | ||
networkConfigurator: netConfigurator, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (h *networkHook) Name() string { | ||
return "network" | ||
} | ||
|
||
func (h *networkHook) Prerun() error { | ||
tg := h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup) | ||
if len(tg.Networks) == 0 || tg.Networks[0].Mode == "host" || tg.Networks[0].Mode == "" { | ||
return nil | ||
} | ||
|
||
if h.manager == nil || h.networkConfigurator == nil { | ||
h.logger.Trace("shared network namespaces are not supported on this platform, skipping network hook") | ||
return nil | ||
} | ||
|
||
spec, err := h.manager.CreateNetwork(h.alloc.ID) | ||
if err != nil { | ||
return fmt.Errorf("failed to create network for alloc: %v", err) | ||
} | ||
|
||
if spec != nil { | ||
h.spec = spec | ||
h.setter.SetNetworkIsolation(spec) | ||
} | ||
|
||
if err := h.networkConfigurator.Setup(h.alloc, spec); err != nil { | ||
return fmt.Errorf("failed to configure networking for alloc: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (h *networkHook) Postrun() error { | ||
if h.spec == nil { | ||
return nil | ||
} | ||
|
||
if err := h.networkConfigurator.Teardown(h.alloc, h.spec); err != nil { | ||
h.logger.Error("failed to cleanup network for allocation, resources may have leaked", "alloc", h.alloc.ID, "error", err) | ||
} | ||
return h.manager.DestroyNetwork(h.alloc.ID, h.spec) | ||
} |
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,86 @@ | ||
package allocrunner | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/client/allocrunner/interfaces" | ||
"github.com/hashicorp/nomad/helper/testlog" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/plugins/drivers" | ||
"github.com/hashicorp/nomad/plugins/drivers/testutils" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// statically assert network hook implements the expected interfaces | ||
var _ interfaces.RunnerPrerunHook = (*networkHook)(nil) | ||
var _ interfaces.RunnerPostrunHook = (*networkHook)(nil) | ||
|
||
type mockNetworkIsolationSetter struct { | ||
t *testing.T | ||
expectedSpec *drivers.NetworkIsolationSpec | ||
called bool | ||
} | ||
|
||
func (m *mockNetworkIsolationSetter) SetNetworkIsolation(spec *drivers.NetworkIsolationSpec) { | ||
m.called = true | ||
require.Exactly(m.t, m.expectedSpec, spec) | ||
} | ||
|
||
// Test that the prerun and postrun hooks call the setter with the expected spec when | ||
// the network mode is not host | ||
func TestNetworkHook_Prerun_Postrun(t *testing.T) { | ||
alloc := mock.Alloc() | ||
alloc.Job.TaskGroups[0].Networks = []*structs.NetworkResource{ | ||
{ | ||
Mode: "bridge", | ||
}, | ||
} | ||
spec := &drivers.NetworkIsolationSpec{ | ||
Mode: drivers.NetIsolationModeGroup, | ||
Path: "test", | ||
Labels: map[string]string{"abc": "123"}, | ||
} | ||
|
||
destroyCalled := false | ||
nm := &testutils.MockDriver{ | ||
MockNetworkManager: testutils.MockNetworkManager{ | ||
CreateNetworkF: func(allocID string) (*drivers.NetworkIsolationSpec, error) { | ||
require.Equal(t, alloc.ID, allocID) | ||
return spec, nil | ||
}, | ||
|
||
DestroyNetworkF: func(allocID string, netSpec *drivers.NetworkIsolationSpec) error { | ||
destroyCalled = true | ||
require.Equal(t, alloc.ID, allocID) | ||
require.Exactly(t, spec, netSpec) | ||
return nil | ||
}, | ||
}, | ||
} | ||
setter := &mockNetworkIsolationSetter{ | ||
t: t, | ||
expectedSpec: spec, | ||
} | ||
require := require.New(t) | ||
|
||
logger := testlog.HCLogger(t) | ||
hook := newNetworkHook(logger, setter, alloc, nm, &hostNetworkConfigurator{}) | ||
require.NoError(hook.Prerun()) | ||
require.True(setter.called) | ||
require.False(destroyCalled) | ||
require.NoError(hook.Postrun()) | ||
require.True(destroyCalled) | ||
|
||
// reset and use host network mode | ||
setter.called = false | ||
destroyCalled = false | ||
alloc.Job.TaskGroups[0].Networks[0].Mode = "host" | ||
hook = newNetworkHook(logger, setter, alloc, nm, &hostNetworkConfigurator{}) | ||
require.NoError(hook.Prerun()) | ||
require.False(setter.called) | ||
require.False(destroyCalled) | ||
require.NoError(hook.Postrun()) | ||
require.False(destroyCalled) | ||
|
||
} |
Oops, something went wrong.