github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/compose/convert/compose.go (about)

     1  package convert
     2  
     3  import (
     4  	"github.com/docker/docker/api/types"
     5  	networktypes "github.com/docker/docker/api/types/network"
     6  	composetypes "github.com/docker/docker/cli/compose/types"
     7  )
     8  
     9  const (
    10  	// LabelNamespace is the label used to track stack resources
    11  	LabelNamespace = "com.docker.stack.namespace"
    12  )
    13  
    14  // Namespace mangles names by prepending the name
    15  type Namespace struct {
    16  	name string
    17  }
    18  
    19  // Scope prepends the namespace to a name
    20  func (n Namespace) Scope(name string) string {
    21  	return n.name + "_" + name
    22  }
    23  
    24  // Name returns the name of the namespace
    25  func (n Namespace) Name() string {
    26  	return n.name
    27  }
    28  
    29  // NewNamespace returns a new Namespace for scoping of names
    30  func NewNamespace(name string) Namespace {
    31  	return Namespace{name: name}
    32  }
    33  
    34  // AddStackLabel returns labels with the namespace label added
    35  func AddStackLabel(namespace Namespace, labels map[string]string) map[string]string {
    36  	if labels == nil {
    37  		labels = make(map[string]string)
    38  	}
    39  	labels[LabelNamespace] = namespace.name
    40  	return labels
    41  }
    42  
    43  type networkMap map[string]composetypes.NetworkConfig
    44  
    45  // Networks from the compose-file type to the engine API type
    46  func Networks(namespace Namespace, networks networkMap, servicesNetworks map[string]struct{}) (map[string]types.NetworkCreate, []string) {
    47  	if networks == nil {
    48  		networks = make(map[string]composetypes.NetworkConfig)
    49  	}
    50  
    51  	externalNetworks := []string{}
    52  	result := make(map[string]types.NetworkCreate)
    53  	for internalName := range servicesNetworks {
    54  		network := networks[internalName]
    55  		if network.External.External {
    56  			externalNetworks = append(externalNetworks, network.External.Name)
    57  			continue
    58  		}
    59  
    60  		createOpts := types.NetworkCreate{
    61  			Labels:  AddStackLabel(namespace, network.Labels),
    62  			Driver:  network.Driver,
    63  			Options: network.DriverOpts,
    64  		}
    65  
    66  		if network.Ipam.Driver != "" || len(network.Ipam.Config) > 0 {
    67  			createOpts.IPAM = &networktypes.IPAM{}
    68  		}
    69  
    70  		if network.Ipam.Driver != "" {
    71  			createOpts.IPAM.Driver = network.Ipam.Driver
    72  		}
    73  		for _, ipamConfig := range network.Ipam.Config {
    74  			config := networktypes.IPAMConfig{
    75  				Subnet: ipamConfig.Subnet,
    76  			}
    77  			createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
    78  		}
    79  		result[internalName] = createOpts
    80  	}
    81  
    82  	return result, externalNetworks
    83  }