github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/allocrunner/network_hook.go (about)

     1  package allocrunner
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	hclog "github.com/hashicorp/go-hclog"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  	"github.com/hashicorp/nomad/plugins/drivers"
    10  )
    11  
    12  // networkHook is an alloc lifecycle hook that manages the network namespace
    13  // for an alloc
    14  type networkHook struct {
    15  	// setter is a callback to set the network isolation spec when after the
    16  	// network is created
    17  	setter networkIsolationSetter
    18  
    19  	// manager is used when creating the network namespace. This defaults to
    20  	// bind mounting a network namespace descritor under /var/run/netns but
    21  	// can be created by a driver if nessicary
    22  	manager drivers.DriverNetworkManager
    23  
    24  	// alloc should only be read from
    25  	alloc *structs.Allocation
    26  
    27  	// spec described the network namespace and is syncronized by specLock
    28  	spec *drivers.NetworkIsolationSpec
    29  
    30  	// networkConfigurator configures the network interfaces, routes, etc once
    31  	// the alloc network has been created
    32  	networkConfigurator NetworkConfigurator
    33  
    34  	logger hclog.Logger
    35  }
    36  
    37  func newNetworkHook(logger hclog.Logger, ns networkIsolationSetter,
    38  	alloc *structs.Allocation, netManager drivers.DriverNetworkManager,
    39  	netConfigurator NetworkConfigurator) *networkHook {
    40  	return &networkHook{
    41  		setter:              ns,
    42  		alloc:               alloc,
    43  		manager:             netManager,
    44  		networkConfigurator: netConfigurator,
    45  		logger:              logger,
    46  	}
    47  }
    48  
    49  func (h *networkHook) Name() string {
    50  	return "network"
    51  }
    52  
    53  func (h *networkHook) Prerun() error {
    54  	tg := h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup)
    55  	if len(tg.Networks) == 0 || tg.Networks[0].Mode == "host" || tg.Networks[0].Mode == "" {
    56  		return nil
    57  	}
    58  
    59  	if h.manager == nil || h.networkConfigurator == nil {
    60  		h.logger.Trace("shared network namespaces are not supported on this platform, skipping network hook")
    61  		return nil
    62  	}
    63  
    64  	spec, created, err := h.manager.CreateNetwork(h.alloc.ID)
    65  
    66  	if err != nil {
    67  		return fmt.Errorf("failed to create network for alloc: %v", err)
    68  	}
    69  
    70  	if spec != nil {
    71  		h.spec = spec
    72  		h.setter.SetNetworkIsolation(spec)
    73  	}
    74  
    75  	if created {
    76  		if err := h.networkConfigurator.Setup(context.TODO(), h.alloc, spec); err != nil {
    77  			return fmt.Errorf("failed to configure networking for alloc: %v", err)
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func (h *networkHook) Postrun() error {
    84  	if h.spec == nil {
    85  		return nil
    86  	}
    87  
    88  	if err := h.networkConfigurator.Teardown(context.TODO(), h.alloc, h.spec); err != nil {
    89  		h.logger.Error("failed to cleanup network for allocation, resources may have leaked", "alloc", h.alloc.ID, "error", err)
    90  	}
    91  	return h.manager.DestroyNetwork(h.alloc.ID, h.spec)
    92  }