github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/client/allocrunner/networking.go (about)

     1  package allocrunner
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/hashicorp/nomad/plugins/drivers"
     9  )
    10  
    11  // NetworkConfigurator sets up and tears down the interfaces, routes, firewall
    12  // rules, etc for the configured networking mode of the allocation.
    13  type NetworkConfigurator interface {
    14  	Setup(context.Context, *structs.Allocation, *drivers.NetworkIsolationSpec) (*structs.AllocNetworkStatus, error)
    15  	Teardown(context.Context, *structs.Allocation, *drivers.NetworkIsolationSpec) error
    16  }
    17  
    18  // hostNetworkConfigurator is a noop implementation of a NetworkConfigurator for
    19  // when the alloc join's a client host's network namespace and thus does not
    20  // require further configuration
    21  type hostNetworkConfigurator struct{}
    22  
    23  func (h *hostNetworkConfigurator) Setup(context.Context, *structs.Allocation, *drivers.NetworkIsolationSpec) (*structs.AllocNetworkStatus, error) {
    24  	return nil, nil
    25  }
    26  func (h *hostNetworkConfigurator) Teardown(context.Context, *structs.Allocation, *drivers.NetworkIsolationSpec) error {
    27  	return nil
    28  }
    29  
    30  // networkingGlobalMutex is used by a synchronizedNetworkConfigurator to serialize
    31  // network operations done by the client to prevent race conditions when manipulating
    32  // iptables rules
    33  var networkingGlobalMutex sync.Mutex
    34  
    35  // synchronizedNetworkConfigurator wraps a NetworkConfigurator to provide serialized access to network
    36  // operations performed by the client
    37  type synchronizedNetworkConfigurator struct {
    38  	nc NetworkConfigurator
    39  }
    40  
    41  func (s *synchronizedNetworkConfigurator) Setup(ctx context.Context, allocation *structs.Allocation, spec *drivers.NetworkIsolationSpec) (*structs.AllocNetworkStatus, error) {
    42  	networkingGlobalMutex.Lock()
    43  	defer networkingGlobalMutex.Unlock()
    44  	return s.nc.Setup(ctx, allocation, spec)
    45  }
    46  
    47  func (s *synchronizedNetworkConfigurator) Teardown(ctx context.Context, allocation *structs.Allocation, spec *drivers.NetworkIsolationSpec) error {
    48  	networkingGlobalMutex.Lock()
    49  	defer networkingGlobalMutex.Unlock()
    50  	return s.nc.Teardown(ctx, allocation, spec)
    51  }