2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
2016-11-30 15:33:54 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
composetypes "github.com/aanand/compose-file/types"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-11-30 16:34:29 -05:00
|
|
|
// LabelNamespace is the label used to track stack resources
|
|
|
|
LabelNamespace = "com.docker.stack.namespace"
|
2016-11-30 15:33:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Namespace mangles names by prepending the name
|
|
|
|
type Namespace struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scope prepends the namespace to a name
|
|
|
|
func (n Namespace) Scope(name string) string {
|
|
|
|
return n.name + "_" + name
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:34:29 -05:00
|
|
|
// Name returns the name of the namespace
|
|
|
|
func (n Namespace) Name() string {
|
|
|
|
return n.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNamespace returns a new Namespace for scoping of names
|
|
|
|
func NewNamespace(name string) Namespace {
|
|
|
|
return Namespace{name: name}
|
|
|
|
}
|
|
|
|
|
2016-11-30 15:33:54 -05:00
|
|
|
// AddStackLabel returns labels with the namespace label added
|
|
|
|
func AddStackLabel(namespace Namespace, labels map[string]string) map[string]string {
|
|
|
|
if labels == nil {
|
|
|
|
labels = make(map[string]string)
|
|
|
|
}
|
2016-11-30 16:34:29 -05:00
|
|
|
labels[LabelNamespace] = namespace.name
|
2016-11-30 15:33:54 -05:00
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
2016-12-01 13:15:18 -05:00
|
|
|
type networkMap map[string]composetypes.NetworkConfig
|
2016-11-30 15:33:54 -05:00
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
// Networks from the compose-file type to the engine API type
|
|
|
|
func Networks(namespace Namespace, networks networkMap) (map[string]types.NetworkCreate, []string) {
|
2016-11-30 15:33:54 -05:00
|
|
|
if networks == nil {
|
|
|
|
networks = make(map[string]composetypes.NetworkConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: only add default network if it's used
|
2016-12-01 13:15:18 -05:00
|
|
|
if _, ok := networks["default"]; !ok {
|
|
|
|
networks["default"] = composetypes.NetworkConfig{}
|
|
|
|
}
|
2016-11-30 15:33:54 -05:00
|
|
|
|
|
|
|
externalNetworks := []string{}
|
|
|
|
result := make(map[string]types.NetworkCreate)
|
|
|
|
|
|
|
|
for internalName, network := range networks {
|
|
|
|
if network.External.External {
|
|
|
|
externalNetworks = append(externalNetworks, network.External.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
createOpts := types.NetworkCreate{
|
|
|
|
Labels: AddStackLabel(namespace, network.Labels),
|
|
|
|
Driver: network.Driver,
|
|
|
|
Options: network.DriverOpts,
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.Ipam.Driver != "" || len(network.Ipam.Config) > 0 {
|
|
|
|
createOpts.IPAM = &networktypes.IPAM{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.Ipam.Driver != "" {
|
|
|
|
createOpts.IPAM.Driver = network.Ipam.Driver
|
|
|
|
}
|
|
|
|
for _, ipamConfig := range network.Ipam.Config {
|
|
|
|
config := networktypes.IPAMConfig{
|
|
|
|
Subnet: ipamConfig.Subnet,
|
|
|
|
}
|
|
|
|
createOpts.IPAM.Config = append(createOpts.IPAM.Config, config)
|
|
|
|
}
|
|
|
|
result[internalName] = createOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, externalNetworks
|
|
|
|
}
|