2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2017-01-10 17:40:53 -05:00
|
|
|
"io/ioutil"
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2017-01-10 17:40:53 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-12-20 16:26:49 -05:00
|
|
|
composetypes "github.com/docker/docker/cli/compose/types"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// LabelNamespace is the label used to track stack resources
|
|
|
|
LabelNamespace = "com.docker.stack.namespace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
labels[LabelNamespace] = namespace.name
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
type networkMap map[string]composetypes.NetworkConfig
|
|
|
|
|
|
|
|
// Networks from the compose-file type to the engine API type
|
2017-01-10 03:57:36 -05:00
|
|
|
func Networks(namespace Namespace, networks networkMap, servicesNetworks map[string]struct{}) (map[string]types.NetworkCreate, []string) {
|
2016-12-05 16:14:08 -05:00
|
|
|
if networks == nil {
|
|
|
|
networks = make(map[string]composetypes.NetworkConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
externalNetworks := []string{}
|
|
|
|
result := make(map[string]types.NetworkCreate)
|
2017-01-10 03:57:36 -05:00
|
|
|
for internalName := range servicesNetworks {
|
|
|
|
network := networks[internalName]
|
2016-12-05 16:14:08 -05:00
|
|
|
if network.External.External {
|
|
|
|
externalNetworks = append(externalNetworks, network.External.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
createOpts := types.NetworkCreate{
|
2017-02-04 16:55:28 -05:00
|
|
|
Labels: AddStackLabel(namespace, network.Labels),
|
|
|
|
Driver: network.Driver,
|
|
|
|
Options: network.DriverOpts,
|
|
|
|
Internal: network.Internal,
|
|
|
|
Attachable: network.Attachable,
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-01-10 17:40:53 -05:00
|
|
|
|
|
|
|
// Secrets converts secrets from the Compose type to the engine API type
|
|
|
|
func Secrets(namespace Namespace, secrets map[string]composetypes.SecretConfig) ([]swarm.SecretSpec, error) {
|
|
|
|
result := []swarm.SecretSpec{}
|
|
|
|
for name, secret := range secrets {
|
|
|
|
if secret.External.External {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(secret.File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: namespace.Scope(name),
|
|
|
|
Labels: AddStackLabel(namespace, secret.Labels),
|
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|