github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/environs/networking.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environs
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/core/instance"
    11  	"github.com/juju/juju/environs/context"
    12  	"github.com/juju/juju/network"
    13  )
    14  
    15  // SupportsNetworking is a convenience helper to check if an environment
    16  // supports networking. It returns an interface containing Environ and
    17  // Networking in this case.
    18  var SupportsNetworking = supportsNetworking
    19  
    20  // DefaultSpaceInfo should be passed into Networking.ProviderSpaceInfo
    21  // to get information about the default space.
    22  var DefaultSpaceInfo *network.SpaceInfo
    23  
    24  // DefaultSpaceName is the name of the default space (to which
    25  // application endpoints are bound if no explicit binding is given).
    26  const DefaultSpaceName = ""
    27  
    28  // Networking interface defines methods that environments
    29  // with networking capabilities must implement.
    30  type Networking interface {
    31  	// Subnets returns basic information about subnets known
    32  	// by the provider for the environment.
    33  	Subnets(ctx context.ProviderCallContext, inst instance.Id, subnetIds []network.Id) ([]network.SubnetInfo, error)
    34  
    35  	// SuperSubnets returns information about aggregated subnets - eg. global CIDR
    36  	// for EC2 VPC.
    37  	SuperSubnets(ctx context.ProviderCallContext) ([]string, error)
    38  
    39  	// NetworkInterfaces requests information about the network
    40  	// interfaces on the given instance.
    41  	NetworkInterfaces(ctx context.ProviderCallContext, instId instance.Id) ([]network.InterfaceInfo, error)
    42  
    43  	// SupportsSpaces returns whether the current environment supports
    44  	// spaces. The returned error satisfies errors.IsNotSupported(),
    45  	// unless a general API failure occurs.
    46  	SupportsSpaces(ctx context.ProviderCallContext) (bool, error)
    47  
    48  	// SupportsSpaceDiscovery returns whether the current environment
    49  	// supports discovering spaces from the provider. The returned error
    50  	// satisfies errors.IsNotSupported(), unless a general API failure occurs.
    51  	SupportsSpaceDiscovery(ctx context.ProviderCallContext) (bool, error)
    52  
    53  	// Spaces returns a slice of network.SpaceInfo with info, including
    54  	// details of all associated subnets, about all spaces known to the
    55  	// provider that have subnets available.
    56  	Spaces(ctx context.ProviderCallContext) ([]network.SpaceInfo, error)
    57  
    58  	// ProviderSpaceInfo returns the details of the space requested as
    59  	// a ProviderSpaceInfo. This will contain everything needed to
    60  	// decide whether an Environ of the same type in another
    61  	// controller could route to the space. Details for the default
    62  	// space can be retrieved by passing DefaultSpaceInfo (which is nil).
    63  	//
    64  	// This method accepts a SpaceInfo with details of the space that
    65  	// we need provider details for - this is the Juju model's view of
    66  	// what subnets are in the space. If the provider supports spaces
    67  	// and space discovery then it is the authority on what subnets
    68  	// are actually in the space, and it's free to collect the full
    69  	// space and subnet info using the space's ProviderId (discarding
    70  	// the subnet details passed in which might be out-of date).
    71  	//
    72  	// If the provider doesn't support space discovery then the Juju
    73  	// model's opinion of what subnets are in the space is
    74  	// authoritative. In that case the provider should collect up any
    75  	// other information needed to determine routability and include
    76  	// the passed-in space info in the ProviderSpaceInfo returned.
    77  	ProviderSpaceInfo(ctx context.ProviderCallContext, space *network.SpaceInfo) (*ProviderSpaceInfo, error)
    78  
    79  	// AreSpacesRoutable returns whether the communication between the
    80  	// two spaces can use cloud-local addresses.
    81  	AreSpacesRoutable(ctx context.ProviderCallContext, space1, space2 *ProviderSpaceInfo) (bool, error)
    82  
    83  	// SupportsContainerAddresses returns true if the current environment is
    84  	// able to allocate addresses for containers. If returning false, we also
    85  	// return an IsNotSupported error.
    86  	SupportsContainerAddresses(ctx context.ProviderCallContext) (bool, error)
    87  
    88  	// AllocateContainerAddresses allocates a static address for each of the
    89  	// container NICs in preparedInfo, hosted by the hostInstanceID. Returns the
    90  	// network config including all allocated addresses on success.
    91  	AllocateContainerAddresses(ctx context.ProviderCallContext, hostInstanceID instance.Id, containerTag names.MachineTag, preparedInfo []network.InterfaceInfo) ([]network.InterfaceInfo, error)
    92  
    93  	// ReleaseContainerAddresses releases the previously allocated
    94  	// addresses matching the interface details passed in.
    95  	ReleaseContainerAddresses(ctx context.ProviderCallContext, interfaces []network.ProviderInterfaceInfo) error
    96  
    97  	// SSHAddresses filters provided addresses for addresses usable for SSH
    98  	SSHAddresses(ctx context.ProviderCallContext, addresses []network.Address) ([]network.Address, error)
    99  }
   100  
   101  // NetworkingEnviron combines the standard Environ interface with the
   102  // functionality for networking.
   103  type NetworkingEnviron interface {
   104  	// Environ represents a juju environment.
   105  	Environ
   106  
   107  	// Networking defines the methods of networking capable environments.
   108  	Networking
   109  }
   110  
   111  func supportsNetworking(environ BootstrapEnviron) (NetworkingEnviron, bool) {
   112  	ne, ok := environ.(NetworkingEnviron)
   113  	return ne, ok
   114  }
   115  
   116  // SupportsSpaces checks if the environment implements NetworkingEnviron
   117  // and also if it supports spaces.
   118  func SupportsSpaces(ctx context.ProviderCallContext, env Environ) bool {
   119  	netEnv, ok := supportsNetworking(env)
   120  	if !ok {
   121  		return false
   122  	}
   123  	ok, err := netEnv.SupportsSpaces(ctx)
   124  	if err != nil {
   125  		if !errors.IsNotSupported(err) {
   126  			logger.Errorf("checking model spaces support failed with: %v", err)
   127  		}
   128  		return false
   129  	}
   130  	return ok
   131  }
   132  
   133  // SupportsContainerAddresses checks if the environment will let us allocate
   134  // addresses for containers from the host ranges.
   135  func SupportsContainerAddresses(ctx context.ProviderCallContext, env Environ) bool {
   136  	netEnv, ok := supportsNetworking(env)
   137  	if !ok {
   138  		return false
   139  	}
   140  	ok, err := netEnv.SupportsContainerAddresses(ctx)
   141  	if err != nil {
   142  		if !errors.IsNotSupported(err) {
   143  			logger.Errorf("checking model container address support failed with: %v", err)
   144  		}
   145  		return false
   146  	}
   147  	return ok
   148  }
   149  
   150  // ProviderSpaceInfo contains all the information about a space needed
   151  // by another environ to decide whether it can be routed to.
   152  type ProviderSpaceInfo struct {
   153  	network.SpaceInfo
   154  
   155  	// Cloud type governs what attributes will exist in the
   156  	// provider-specific map.
   157  	CloudType string
   158  
   159  	// Any provider-specific information to needed to identify the
   160  	// network within the cloud, e.g. VPC ID for EC2.
   161  	ProviderAttributes map[string]interface{}
   162  }