2019-05-08 14:30:50 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-08 14:32:07 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-05-08 14:30:50 +00:00
|
|
|
"strings"
|
2021-01-08 14:32:07 +00:00
|
|
|
"syscall"
|
2019-05-08 14:30:50 +00:00
|
|
|
|
2019-08-28 14:51:34 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2019-06-14 03:05:57 +00:00
|
|
|
clientconfig "github.com/hashicorp/nomad/client/config"
|
2019-05-21 04:11:48 +00:00
|
|
|
"github.com/hashicorp/nomad/client/lib/nsutil"
|
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/drivermanager"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-05-08 14:30:50 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
)
|
|
|
|
|
2019-05-21 04:11:48 +00:00
|
|
|
func newNetworkManager(alloc *structs.Allocation, driverManager drivermanager.Manager) (nm drivers.DriverNetworkManager, err error) {
|
2019-05-08 14:30:50 +00:00
|
|
|
// The defaultNetworkManager is used if a driver doesn't need to create the network
|
|
|
|
nm = &defaultNetworkManager{}
|
2019-05-21 04:11:48 +00:00
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
2019-05-08 14:30:50 +00:00
|
|
|
|
2019-05-08 15:09:35 +00:00
|
|
|
// default netmode to host, this can be overridden by the task or task group
|
2019-05-08 14:30:50 +00:00
|
|
|
tgNetMode := "host"
|
|
|
|
if len(tg.Networks) > 0 && tg.Networks[0].Mode != "" {
|
|
|
|
tgNetMode = tg.Networks[0].Mode
|
|
|
|
}
|
|
|
|
|
2021-09-16 06:13:09 +00:00
|
|
|
groupIsolationMode := netModeToIsolationMode(tgNetMode)
|
|
|
|
|
|
|
|
// Setting the hostname is only possible where the task groups networking
|
|
|
|
// mode is group; meaning bridge or none.
|
|
|
|
if len(tg.Networks) > 0 &&
|
|
|
|
(groupIsolationMode != drivers.NetIsolationModeGroup && tg.Networks[0].Hostname != "") {
|
|
|
|
return nil, fmt.Errorf("hostname cannot be set on task group using %q networking mode",
|
|
|
|
groupIsolationMode)
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:30:50 +00:00
|
|
|
// networkInitiator tracks the task driver which needs to create the network
|
2021-09-16 06:13:09 +00:00
|
|
|
// to check for multiple drivers needing to create the network.
|
2019-05-08 14:30:50 +00:00
|
|
|
var networkInitiator string
|
|
|
|
|
|
|
|
// driverCaps tracks which drivers we've checked capabilities for so as not
|
|
|
|
// to do extra work
|
|
|
|
driverCaps := make(map[string]struct{})
|
|
|
|
for _, task := range tg.Tasks {
|
2019-05-08 15:09:35 +00:00
|
|
|
// the task's netmode defaults to the the task group but can be overridden
|
2019-05-08 14:30:50 +00:00
|
|
|
taskNetMode := tgNetMode
|
|
|
|
if len(task.Resources.Networks) > 0 && task.Resources.Networks[0].Mode != "" {
|
|
|
|
taskNetMode = task.Resources.Networks[0].Mode
|
|
|
|
}
|
|
|
|
|
|
|
|
// netmode host should always work to support backwards compat
|
|
|
|
if taskNetMode == "host" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if capabilities of this task's driver have already been checked
|
|
|
|
if _, ok := driverCaps[task.Driver]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-21 04:11:48 +00:00
|
|
|
driver, err := driverManager.Dispense(task.Driver)
|
2019-05-08 14:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to dispense driver %s: %v", task.Driver, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caps, err := driver.Capabilities()
|
|
|
|
if err != nil {
|
2019-08-16 13:03:42 +00:00
|
|
|
return nil, fmt.Errorf("failed to retrieve capabilities for driver %s: %v",
|
2019-05-08 14:30:50 +00:00
|
|
|
task.Driver, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that the driver supports the requested network isolation mode
|
|
|
|
netIsolationMode := netModeToIsolationMode(taskNetMode)
|
|
|
|
if !caps.HasNetIsolationMode(netIsolationMode) {
|
|
|
|
return nil, fmt.Errorf("task %s does not support %q networking mode", task.Name, taskNetMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the driver needs to create the network and if a different
|
|
|
|
// driver has already claimed it needs to initiate the network
|
|
|
|
if caps.MustInitiateNetwork {
|
|
|
|
if networkInitiator != "" {
|
2019-05-14 02:42:46 +00:00
|
|
|
return nil, fmt.Errorf("tasks %s and %s want to initiate networking but only one driver can do so", networkInitiator, task.Name)
|
2019-05-14 01:26:39 +00:00
|
|
|
}
|
|
|
|
netManager, ok := driver.(drivers.DriverNetworkManager)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("driver %s does not implement network management RPCs", task.Driver)
|
2019-05-08 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 01:26:39 +00:00
|
|
|
nm = netManager
|
2019-05-08 14:30:50 +00:00
|
|
|
networkInitiator = task.Name
|
2021-09-16 06:13:09 +00:00
|
|
|
} else if tg.Networks[0].Hostname != "" {
|
|
|
|
// TODO jrasell: remove once the default linux network manager
|
|
|
|
// supports setting the hostname in bridged mode. This currently
|
|
|
|
// indicates only Docker supports this, which is true unless a
|
|
|
|
// custom driver can which means this check still holds as true as
|
|
|
|
// we can tell.
|
|
|
|
// Please see: https://github.com/hashicorp/nomad/issues/11180
|
|
|
|
return nil, fmt.Errorf("hostname is not currently supported on driver %s", task.Driver)
|
2019-05-08 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark this driver's capabilities as checked
|
|
|
|
driverCaps[task.Driver] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nm, nil
|
|
|
|
}
|
|
|
|
|
2019-05-21 04:11:48 +00:00
|
|
|
// defaultNetworkManager creates a network namespace for the alloc
|
|
|
|
type defaultNetworkManager struct{}
|
|
|
|
|
2021-09-16 06:13:09 +00:00
|
|
|
// CreateNetwork is the CreateNetwork implementation of the
|
|
|
|
// drivers.DriverNetworkManager interface function. It does not currently
|
|
|
|
// support setting the hostname of the network namespace.
|
|
|
|
func (*defaultNetworkManager) CreateNetwork(allocID string, _ *drivers.NetworkCreateRequest) (*drivers.NetworkIsolationSpec, bool, error) {
|
2019-05-21 04:11:48 +00:00
|
|
|
netns, err := nsutil.NewNS(allocID)
|
|
|
|
if err != nil {
|
2021-01-08 14:32:07 +00:00
|
|
|
// 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 {
|
2022-11-11 18:27:36 +00:00
|
|
|
// Let's return a spec that points to the tested nspath, but indicate
|
|
|
|
// that we didn't make the namespace. That will stop the network_hook
|
|
|
|
// from calling its networkConfigurator.Setup function in the reconnect
|
|
|
|
// case, but provide the spec value necessary for the network_hook's
|
|
|
|
// Postrun function to not fast exit.
|
|
|
|
spec := &drivers.NetworkIsolationSpec{
|
|
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
|
|
Path: nsPath,
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec, false, nil
|
2021-01-08 14:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 20:34:57 +00:00
|
|
|
return nil, false, err
|
2019-05-21 04:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spec := &drivers.NetworkIsolationSpec{
|
|
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
|
|
Path: netns.Path(),
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2019-09-18 20:34:57 +00:00
|
|
|
return spec, true, nil
|
2019-05-21 04:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*defaultNetworkManager) DestroyNetwork(allocID string, spec *drivers.NetworkIsolationSpec) error {
|
|
|
|
return nsutil.UnmountNS(spec.Path)
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:30:50 +00:00
|
|
|
func netModeToIsolationMode(netMode string) drivers.NetIsolationMode {
|
|
|
|
switch strings.ToLower(netMode) {
|
|
|
|
case "host":
|
|
|
|
return drivers.NetIsolationModeHost
|
|
|
|
case "bridge", "none":
|
|
|
|
return drivers.NetIsolationModeGroup
|
|
|
|
case "driver":
|
|
|
|
return drivers.NetIsolationModeTask
|
|
|
|
default:
|
2020-05-15 15:09:01 +00:00
|
|
|
if strings.HasPrefix(strings.ToLower(netMode), "cni/") {
|
|
|
|
return drivers.NetIsolationModeGroup
|
|
|
|
}
|
2019-05-08 14:30:50 +00:00
|
|
|
return drivers.NetIsolationModeHost
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 04:29:12 +00:00
|
|
|
|
2019-09-04 20:33:25 +00:00
|
|
|
func newNetworkConfigurator(log hclog.Logger, alloc *structs.Allocation, config *clientconfig.Config) (NetworkConfigurator, error) {
|
2019-06-14 03:05:57 +00:00
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
2019-06-18 04:55:43 +00:00
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
// Check if network block is given
|
2019-06-18 04:55:43 +00:00
|
|
|
if len(tg.Networks) == 0 {
|
2019-09-04 20:33:25 +00:00
|
|
|
return &hostNetworkConfigurator{}, nil
|
2019-06-18 04:55:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
netMode := strings.ToLower(tg.Networks[0].Mode)
|
2020-07-06 22:51:46 +00:00
|
|
|
ignorePortMappingHostIP := config.BindWildcardDefaultHostNetwork
|
|
|
|
if len(config.HostNetworks) > 0 {
|
|
|
|
ignorePortMappingHostIP = false
|
|
|
|
}
|
2020-05-15 15:09:01 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case netMode == "bridge":
|
2023-02-02 15:12:15 +00:00
|
|
|
c, err := newBridgeNetworkConfigurator(log, config.BridgeNetworkName, config.BridgeNetworkAllocSubnet, config.BridgeNetworkHairpinMode, config.CNIPath, ignorePortMappingHostIP)
|
2021-02-03 04:24:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &synchronizedNetworkConfigurator{c}, nil
|
2020-05-15 15:09:01 +00:00
|
|
|
case strings.HasPrefix(netMode, "cni/"):
|
2021-02-03 04:24:57 +00:00
|
|
|
c, err := newCNINetworkConfigurator(log, config.CNIPath, config.CNIInterfacePrefix, config.CNIConfigDir, netMode[4:], ignorePortMappingHostIP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &synchronizedNetworkConfigurator{c}, nil
|
2019-06-14 03:05:57 +00:00
|
|
|
default:
|
2019-09-04 20:33:25 +00:00
|
|
|
return &hostNetworkConfigurator{}, nil
|
2019-06-11 04:29:12 +00:00
|
|
|
}
|
|
|
|
}
|