2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-10 17:40:53 -05:00
|
|
|
"os"
|
2017-01-31 15:45:45 -05:00
|
|
|
"sort"
|
2017-02-17 00:34:49 -05:00
|
|
|
"strings"
|
2016-12-05 16:14:08 -05:00
|
|
|
"time"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
servicecli "github.com/docker/cli/cli/command/service"
|
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-03-24 19:58:42 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-24 10:43:28 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
2017-06-02 19:21:41 -04:00
|
|
|
const (
|
|
|
|
defaultNetwork = "default"
|
|
|
|
// LabelImage is the label used to store image name provided in the compose file
|
|
|
|
LabelImage = "com.docker.stack.image"
|
|
|
|
)
|
2017-02-22 13:52:09 -05:00
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
// Services from compose-file types to engine API types
|
|
|
|
func Services(
|
|
|
|
namespace Namespace,
|
|
|
|
config *composetypes.Config,
|
2017-03-24 19:58:42 -04:00
|
|
|
client client.CommonAPIClient,
|
2016-12-05 16:14:08 -05:00
|
|
|
) (map[string]swarm.ServiceSpec, error) {
|
|
|
|
result := make(map[string]swarm.ServiceSpec)
|
|
|
|
|
|
|
|
services := config.Services
|
|
|
|
volumes := config.Volumes
|
|
|
|
networks := config.Networks
|
|
|
|
|
|
|
|
for _, service := range services {
|
2017-01-18 13:06:36 -05:00
|
|
|
secrets, err := convertServiceSecrets(client, namespace, service.Secrets, config.Secrets)
|
2017-01-10 17:40:53 -05:00
|
|
|
if err != nil {
|
2017-03-24 10:43:28 -04:00
|
|
|
return nil, errors.Wrapf(err, "service %s", service.Name)
|
2017-01-10 17:40:53 -05:00
|
|
|
}
|
2017-05-15 11:19:32 -04:00
|
|
|
configs, err := convertServiceConfigObjs(client, namespace, service.Configs, config.Configs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "service %s", service.Name)
|
|
|
|
}
|
|
|
|
|
2017-06-08 04:44:05 -04:00
|
|
|
serviceSpec, err := Service(client.ClientVersion(), namespace, service, networks, volumes, secrets, configs)
|
2016-12-05 16:14:08 -05:00
|
|
|
if err != nil {
|
2017-03-24 10:43:28 -04:00
|
|
|
return nil, errors.Wrapf(err, "service %s", service.Name)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
result[service.Name] = serviceSpec
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-06-08 04:44:05 -04:00
|
|
|
// Service converts a ServiceConfig into a swarm ServiceSpec
|
|
|
|
func Service(
|
2017-03-24 19:58:42 -04:00
|
|
|
apiVersion string,
|
2016-12-05 16:14:08 -05:00
|
|
|
namespace Namespace,
|
|
|
|
service composetypes.ServiceConfig,
|
|
|
|
networkConfigs map[string]composetypes.NetworkConfig,
|
|
|
|
volumes map[string]composetypes.VolumeConfig,
|
2017-01-10 17:40:53 -05:00
|
|
|
secrets []*swarm.SecretReference,
|
2017-05-15 11:19:32 -04:00
|
|
|
configs []*swarm.ConfigReference,
|
2016-12-05 16:14:08 -05:00
|
|
|
) (swarm.ServiceSpec, error) {
|
|
|
|
name := namespace.Scope(service.Name)
|
|
|
|
|
2017-03-13 14:53:13 -04:00
|
|
|
endpoint, err := convertEndpointSpec(service.Deploy.EndpointMode, service.Ports)
|
2016-12-05 16:14:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, err := convertDeployMode(service.Deploy.Mode, service.Deploy.Replicas)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts, err := Volumes(service.Volumes, volumes, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resources, err := convertResources(service.Deploy.Resources)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
restartPolicy, err := convertRestartPolicy(
|
|
|
|
service.Restart, service.Deploy.RestartPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
healthcheck, err := convertHealthcheck(service.HealthCheck)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
networks, err := convertServiceNetworks(service.Networks, networkConfigs, namespace, service.Name)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:38:17 -04:00
|
|
|
dnsConfig, err := convertDNSConfig(service.DNS, service.DNSSearch)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
2017-05-11 08:30:04 -04:00
|
|
|
var privileges swarm.Privileges
|
|
|
|
privileges.CredentialSpec, err = convertCredentialSpec(service.CredentialSpec)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
var logDriver *swarm.Driver
|
|
|
|
if service.Logging != nil {
|
|
|
|
logDriver = &swarm.Driver{
|
|
|
|
Name: service.Logging.Driver,
|
|
|
|
Options: service.Logging.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceSpec := swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: name,
|
|
|
|
Labels: AddStackLabel(namespace, service.Deploy.Labels),
|
|
|
|
},
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
|
|
|
ContainerSpec: swarm.ContainerSpec{
|
|
|
|
Image: service.Image,
|
|
|
|
Command: service.Entrypoint,
|
|
|
|
Args: service.Command,
|
|
|
|
Hostname: service.Hostname,
|
2017-01-27 10:09:02 -05:00
|
|
|
Hosts: sortStrings(convertExtraHosts(service.ExtraHosts)),
|
2017-03-23 19:38:17 -04:00
|
|
|
DNSConfig: dnsConfig,
|
2016-12-05 16:14:08 -05:00
|
|
|
Healthcheck: healthcheck,
|
2017-01-27 10:09:02 -05:00
|
|
|
Env: sortStrings(convertEnvironment(service.Environment)),
|
2016-12-05 16:14:08 -05:00
|
|
|
Labels: AddStackLabel(namespace, service.Labels),
|
|
|
|
Dir: service.WorkingDir,
|
|
|
|
User: service.User,
|
|
|
|
Mounts: mounts,
|
|
|
|
StopGracePeriod: service.StopGracePeriod,
|
2017-07-25 21:59:25 -04:00
|
|
|
StopSignal: service.StopSignal,
|
2016-12-05 16:14:08 -05:00
|
|
|
TTY: service.Tty,
|
|
|
|
OpenStdin: service.StdinOpen,
|
2017-01-10 17:40:53 -05:00
|
|
|
Secrets: secrets,
|
2017-05-27 00:01:22 -04:00
|
|
|
Configs: configs,
|
2017-05-11 09:26:38 -04:00
|
|
|
ReadOnly: service.ReadOnly,
|
2017-05-11 08:30:04 -04:00
|
|
|
Privileges: &privileges,
|
2016-12-05 16:14:08 -05:00
|
|
|
},
|
|
|
|
LogDriver: logDriver,
|
|
|
|
Resources: resources,
|
|
|
|
RestartPolicy: restartPolicy,
|
|
|
|
Placement: &swarm.Placement{
|
|
|
|
Constraints: service.Deploy.Placement.Constraints,
|
2017-05-06 09:49:42 -04:00
|
|
|
Preferences: getPlacementPreference(service.Deploy.Placement.Preferences),
|
2016-12-05 16:14:08 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
EndpointSpec: endpoint,
|
|
|
|
Mode: mode,
|
|
|
|
UpdateConfig: convertUpdateConfig(service.Deploy.UpdateConfig),
|
|
|
|
}
|
|
|
|
|
2017-06-02 19:21:41 -04:00
|
|
|
// add an image label to serviceSpec
|
|
|
|
serviceSpec.Labels[LabelImage] = service.Image
|
|
|
|
|
2017-03-24 19:58:42 -04:00
|
|
|
// ServiceSpec.Networks is deprecated and should not have been used by
|
|
|
|
// this package. It is possible to update TaskTemplate.Networks, but it
|
|
|
|
// is not possible to update ServiceSpec.Networks. Unfortunately, we
|
|
|
|
// can't unconditionally start using TaskTemplate.Networks, because that
|
|
|
|
// will break with older daemons that don't support migrating from
|
|
|
|
// ServiceSpec.Networks to TaskTemplate.Networks. So which field to use
|
|
|
|
// is conditional on daemon version.
|
|
|
|
if versions.LessThan(apiVersion, "1.29") {
|
|
|
|
serviceSpec.Networks = networks
|
|
|
|
} else {
|
|
|
|
serviceSpec.TaskTemplate.Networks = networks
|
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
return serviceSpec, nil
|
|
|
|
}
|
|
|
|
|
2017-05-06 09:49:42 -04:00
|
|
|
func getPlacementPreference(preferences []composetypes.PlacementPreferences) []swarm.PlacementPreference {
|
|
|
|
result := []swarm.PlacementPreference{}
|
|
|
|
for _, preference := range preferences {
|
|
|
|
spreadDescriptor := preference.Spread
|
|
|
|
result = append(result, swarm.PlacementPreference{
|
|
|
|
Spread: &swarm.SpreadOver{
|
|
|
|
SpreadDescriptor: spreadDescriptor,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:09:02 -05:00
|
|
|
func sortStrings(strs []string) []string {
|
|
|
|
sort.Strings(strs)
|
|
|
|
return strs
|
|
|
|
}
|
|
|
|
|
|
|
|
type byNetworkTarget []swarm.NetworkAttachmentConfig
|
|
|
|
|
|
|
|
func (a byNetworkTarget) Len() int { return len(a) }
|
|
|
|
func (a byNetworkTarget) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byNetworkTarget) Less(i, j int) bool { return a[i].Target < a[j].Target }
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func convertServiceNetworks(
|
|
|
|
networks map[string]*composetypes.ServiceNetworkConfig,
|
|
|
|
networkConfigs networkMap,
|
|
|
|
namespace Namespace,
|
|
|
|
name string,
|
|
|
|
) ([]swarm.NetworkAttachmentConfig, error) {
|
|
|
|
if len(networks) == 0 {
|
2017-02-22 13:52:09 -05:00
|
|
|
networks = map[string]*composetypes.ServiceNetworkConfig{
|
|
|
|
defaultNetwork: {},
|
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
nets := []swarm.NetworkAttachmentConfig{}
|
|
|
|
for networkName, network := range networks {
|
|
|
|
networkConfig, ok := networkConfigs[networkName]
|
2017-02-22 13:52:09 -05:00
|
|
|
if !ok && networkName != defaultNetwork {
|
2017-03-24 10:43:28 -04:00
|
|
|
return nil, errors.Errorf("undefined network %q", networkName)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
var aliases []string
|
|
|
|
if network != nil {
|
|
|
|
aliases = network.Aliases
|
|
|
|
}
|
|
|
|
target := namespace.Scope(networkName)
|
|
|
|
if networkConfig.External.External {
|
|
|
|
target = networkConfig.External.Name
|
|
|
|
}
|
2017-05-25 22:50:08 -04:00
|
|
|
netAttachConfig := swarm.NetworkAttachmentConfig{
|
2016-12-05 16:14:08 -05:00
|
|
|
Target: target,
|
2017-05-26 14:25:20 -04:00
|
|
|
Aliases: aliases,
|
2017-05-25 22:50:08 -04:00
|
|
|
}
|
2017-05-26 14:25:20 -04:00
|
|
|
// Only add default aliases to user defined networks. Other networks do
|
|
|
|
// not support aliases.
|
2017-05-25 22:50:08 -04:00
|
|
|
if container.NetworkMode(target).IsUserDefined() {
|
2017-05-26 14:25:20 -04:00
|
|
|
netAttachConfig.Aliases = append(netAttachConfig.Aliases, name)
|
2017-05-25 22:50:08 -04:00
|
|
|
}
|
|
|
|
nets = append(nets, netAttachConfig)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2017-01-27 10:09:02 -05:00
|
|
|
|
|
|
|
sort.Sort(byNetworkTarget(nets))
|
2016-12-05 16:14:08 -05:00
|
|
|
return nets, nil
|
|
|
|
}
|
|
|
|
|
2017-01-10 17:40:53 -05:00
|
|
|
// TODO: fix secrets API so that SecretAPIClient is not required here
|
|
|
|
func convertServiceSecrets(
|
|
|
|
client client.SecretAPIClient,
|
|
|
|
namespace Namespace,
|
|
|
|
secrets []composetypes.ServiceSecretConfig,
|
2017-01-18 13:06:36 -05:00
|
|
|
secretSpecs map[string]composetypes.SecretConfig,
|
2017-01-10 17:40:53 -05:00
|
|
|
) ([]*swarm.SecretReference, error) {
|
2017-03-16 13:54:18 -04:00
|
|
|
refs := []*swarm.SecretReference{}
|
2017-01-10 17:40:53 -05:00
|
|
|
for _, secret := range secrets {
|
|
|
|
target := secret.Target
|
|
|
|
if target == "" {
|
|
|
|
target = secret.Source
|
|
|
|
}
|
2017-01-18 13:06:36 -05:00
|
|
|
|
2017-03-24 10:43:28 -04:00
|
|
|
secretSpec, exists := secretSpecs[secret.Source]
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.Errorf("undefined secret %q", secret.Source)
|
|
|
|
}
|
|
|
|
|
2017-01-18 13:06:36 -05:00
|
|
|
source := namespace.Scope(secret.Source)
|
|
|
|
if secretSpec.External.External {
|
|
|
|
source = secretSpec.External.Name
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:00:46 -05:00
|
|
|
uid := secret.UID
|
|
|
|
gid := secret.GID
|
|
|
|
if uid == "" {
|
|
|
|
uid = "0"
|
|
|
|
}
|
|
|
|
if gid == "" {
|
|
|
|
gid = "0"
|
|
|
|
}
|
2017-02-14 09:12:03 -05:00
|
|
|
mode := secret.Mode
|
|
|
|
if mode == nil {
|
|
|
|
mode = uint32Ptr(0444)
|
|
|
|
}
|
2017-01-26 12:00:46 -05:00
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
refs = append(refs, &swarm.SecretReference{
|
|
|
|
File: &swarm.SecretReferenceFileTarget{
|
|
|
|
Name: target,
|
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
|
|
|
Mode: os.FileMode(*mode),
|
|
|
|
},
|
|
|
|
SecretName: source,
|
2017-01-10 17:40:53 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:54:18 -04:00
|
|
|
return servicecli.ParseSecrets(client, refs)
|
2017-01-10 17:40:53 -05:00
|
|
|
}
|
|
|
|
|
2017-05-15 11:19:32 -04:00
|
|
|
// TODO: fix configs API so that ConfigsAPIClient is not required here
|
|
|
|
func convertServiceConfigObjs(
|
|
|
|
client client.ConfigAPIClient,
|
|
|
|
namespace Namespace,
|
|
|
|
configs []composetypes.ServiceConfigObjConfig,
|
|
|
|
configSpecs map[string]composetypes.ConfigObjConfig,
|
|
|
|
) ([]*swarm.ConfigReference, error) {
|
|
|
|
refs := []*swarm.ConfigReference{}
|
|
|
|
for _, config := range configs {
|
|
|
|
target := config.Target
|
|
|
|
if target == "" {
|
|
|
|
target = config.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
configSpec, exists := configSpecs[config.Source]
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.Errorf("undefined config %q", config.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
source := namespace.Scope(config.Source)
|
|
|
|
if configSpec.External.External {
|
|
|
|
source = configSpec.External.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := config.UID
|
|
|
|
gid := config.GID
|
|
|
|
if uid == "" {
|
|
|
|
uid = "0"
|
|
|
|
}
|
|
|
|
if gid == "" {
|
|
|
|
gid = "0"
|
|
|
|
}
|
|
|
|
mode := config.Mode
|
|
|
|
if mode == nil {
|
|
|
|
mode = uint32Ptr(0444)
|
|
|
|
}
|
|
|
|
|
|
|
|
refs = append(refs, &swarm.ConfigReference{
|
|
|
|
File: &swarm.ConfigReferenceFileTarget{
|
|
|
|
Name: target,
|
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
|
|
|
Mode: os.FileMode(*mode),
|
|
|
|
},
|
|
|
|
ConfigName: source,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return servicecli.ParseConfigs(client, refs)
|
|
|
|
}
|
|
|
|
|
2017-02-14 09:12:03 -05:00
|
|
|
func uint32Ptr(value uint32) *uint32 {
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func convertExtraHosts(extraHosts map[string]string) []string {
|
|
|
|
hosts := []string{}
|
|
|
|
for host, ip := range extraHosts {
|
|
|
|
hosts = append(hosts, fmt.Sprintf("%s %s", ip, host))
|
|
|
|
}
|
|
|
|
return hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertHealthcheck(healthcheck *composetypes.HealthCheckConfig) (*container.HealthConfig, error) {
|
|
|
|
if healthcheck == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var (
|
2016-11-29 04:58:47 -05:00
|
|
|
timeout, interval, startPeriod time.Duration
|
|
|
|
retries int
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
if healthcheck.Disable {
|
|
|
|
if len(healthcheck.Test) != 0 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, errors.Errorf("test and disable can't be set at the same time")
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
return &container.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
2017-08-29 21:09:06 -04:00
|
|
|
if healthcheck.Timeout != nil {
|
|
|
|
timeout = *healthcheck.Timeout
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2017-08-29 21:09:06 -04:00
|
|
|
if healthcheck.Interval != nil {
|
|
|
|
interval = *healthcheck.Interval
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2017-08-29 21:09:06 -04:00
|
|
|
if healthcheck.StartPeriod != nil {
|
|
|
|
startPeriod = *healthcheck.StartPeriod
|
2016-11-29 04:58:47 -05:00
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
if healthcheck.Retries != nil {
|
|
|
|
retries = int(*healthcheck.Retries)
|
|
|
|
}
|
|
|
|
return &container.HealthConfig{
|
2016-11-29 04:58:47 -05:00
|
|
|
Test: healthcheck.Test,
|
|
|
|
Timeout: timeout,
|
|
|
|
Interval: interval,
|
|
|
|
Retries: retries,
|
|
|
|
StartPeriod: startPeriod,
|
2016-12-05 16:14:08 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*swarm.RestartPolicy, error) {
|
|
|
|
// TODO: log if restart is being ignored
|
|
|
|
if source == nil {
|
2017-06-05 18:23:21 -04:00
|
|
|
policy, err := opts.ParseRestartPolicy(restart)
|
2016-12-05 16:14:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case policy.IsNone():
|
|
|
|
return nil, nil
|
|
|
|
case policy.IsAlways(), policy.IsUnlessStopped():
|
|
|
|
return &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyConditionAny,
|
|
|
|
}, nil
|
|
|
|
case policy.IsOnFailure():
|
|
|
|
attempts := uint64(policy.MaximumRetryCount)
|
|
|
|
return &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyConditionOnFailure,
|
|
|
|
MaxAttempts: &attempts,
|
|
|
|
}, nil
|
|
|
|
default:
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, errors.Errorf("unknown restart policy: %s", restart)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyCondition(source.Condition),
|
|
|
|
Delay: source.Delay,
|
|
|
|
MaxAttempts: source.MaxAttempts,
|
|
|
|
Window: source.Window,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertUpdateConfig(source *composetypes.UpdateConfig) *swarm.UpdateConfig {
|
|
|
|
if source == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parallel := uint64(1)
|
|
|
|
if source.Parallelism != nil {
|
|
|
|
parallel = *source.Parallelism
|
|
|
|
}
|
|
|
|
return &swarm.UpdateConfig{
|
|
|
|
Parallelism: parallel,
|
|
|
|
Delay: source.Delay,
|
|
|
|
FailureAction: source.FailureAction,
|
|
|
|
Monitor: source.Monitor,
|
|
|
|
MaxFailureRatio: source.MaxFailureRatio,
|
2017-07-19 04:55:34 -04:00
|
|
|
Order: source.Order,
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertResources(source composetypes.Resources) (*swarm.ResourceRequirements, error) {
|
|
|
|
resources := &swarm.ResourceRequirements{}
|
2017-01-09 14:22:02 -05:00
|
|
|
var err error
|
2016-12-05 16:14:08 -05:00
|
|
|
if source.Limits != nil {
|
2017-01-09 14:22:02 -05:00
|
|
|
var cpus int64
|
|
|
|
if source.Limits.NanoCPUs != "" {
|
|
|
|
cpus, err = opts.ParseCPUs(source.Limits.NanoCPUs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
resources.Limits = &swarm.Resources{
|
|
|
|
NanoCPUs: cpus,
|
|
|
|
MemoryBytes: int64(source.Limits.MemoryBytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if source.Reservations != nil {
|
2017-01-09 14:22:02 -05:00
|
|
|
var cpus int64
|
|
|
|
if source.Reservations.NanoCPUs != "" {
|
|
|
|
cpus, err = opts.ParseCPUs(source.Reservations.NanoCPUs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
resources.Reservations = &swarm.Resources{
|
|
|
|
NanoCPUs: cpus,
|
|
|
|
MemoryBytes: int64(source.Reservations.MemoryBytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2017-01-27 10:09:02 -05:00
|
|
|
type byPublishedPort []swarm.PortConfig
|
|
|
|
|
|
|
|
func (a byPublishedPort) Len() int { return len(a) }
|
|
|
|
func (a byPublishedPort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byPublishedPort) Less(i, j int) bool { return a[i].PublishedPort < a[j].PublishedPort }
|
|
|
|
|
2017-02-17 00:34:49 -05:00
|
|
|
func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {
|
2016-12-05 16:14:08 -05:00
|
|
|
portConfigs := []swarm.PortConfig{}
|
2017-01-31 15:45:45 -05:00
|
|
|
for _, port := range source {
|
|
|
|
portConfig := swarm.PortConfig{
|
|
|
|
Protocol: swarm.PortConfigProtocol(port.Protocol),
|
|
|
|
TargetPort: port.Target,
|
|
|
|
PublishedPort: port.Published,
|
|
|
|
PublishMode: swarm.PortConfigPublishMode(port.Mode),
|
2017-01-12 12:01:29 -05:00
|
|
|
}
|
2017-01-31 15:45:45 -05:00
|
|
|
portConfigs = append(portConfigs, portConfig)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 10:09:02 -05:00
|
|
|
sort.Sort(byPublishedPort(portConfigs))
|
2017-02-17 00:34:49 -05:00
|
|
|
return &swarm.EndpointSpec{
|
|
|
|
Mode: swarm.ResolutionMode(strings.ToLower(endpointMode)),
|
|
|
|
Ports: portConfigs,
|
|
|
|
}, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
func convertEnvironment(source map[string]*string) []string {
|
2016-12-05 16:14:08 -05:00
|
|
|
var output []string
|
|
|
|
|
|
|
|
for name, value := range source {
|
2017-03-14 12:39:26 -04:00
|
|
|
switch value {
|
|
|
|
case nil:
|
|
|
|
output = append(output, name)
|
|
|
|
default:
|
|
|
|
output = append(output, fmt.Sprintf("%s=%s", name, *value))
|
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertDeployMode(mode string, replicas *uint64) (swarm.ServiceMode, error) {
|
|
|
|
serviceMode := swarm.ServiceMode{}
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "global":
|
|
|
|
if replicas != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return serviceMode, errors.Errorf("replicas can only be used with replicated mode")
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
serviceMode.Global = &swarm.GlobalService{}
|
|
|
|
case "replicated", "":
|
|
|
|
serviceMode.Replicated = &swarm.ReplicatedService{Replicas: replicas}
|
|
|
|
default:
|
2017-03-09 13:23:45 -05:00
|
|
|
return serviceMode, errors.Errorf("Unknown mode: %s", mode)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
return serviceMode, nil
|
|
|
|
}
|
2017-03-23 19:38:17 -04:00
|
|
|
|
|
|
|
func convertDNSConfig(DNS []string, DNSSearch []string) (*swarm.DNSConfig, error) {
|
|
|
|
if DNS != nil || DNSSearch != nil {
|
|
|
|
return &swarm.DNSConfig{
|
|
|
|
Nameservers: DNS,
|
|
|
|
Search: DNSSearch,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-05-11 08:30:04 -04:00
|
|
|
|
|
|
|
func convertCredentialSpec(spec composetypes.CredentialSpecConfig) (*swarm.CredentialSpec, error) {
|
|
|
|
if spec.File == "" && spec.Registry == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if spec.File != "" && spec.Registry != "" {
|
|
|
|
return nil, errors.New("Invalid credential spec - must provide one of `File` or `Registry`")
|
|
|
|
}
|
2017-06-09 17:42:16 -04:00
|
|
|
swarmCredSpec := swarm.CredentialSpec(spec)
|
|
|
|
return &swarmCredSpec, nil
|
2017-05-11 08:30:04 -04:00
|
|
|
}
|