diff --git a/command/service/opts.go b/command/service/opts.go index b794b07a30..292aa66d5e 100644 --- a/command/service/opts.go +++ b/command/service/opts.go @@ -346,6 +346,25 @@ func newServiceOptions() *serviceOptions { } } +func (opts *serviceOptions) ToServiceMode() (swarm.ServiceMode, error) { + serviceMode := swarm.ServiceMode{} + switch opts.mode { + case "global": + if opts.replicas.Value() != nil { + return serviceMode, fmt.Errorf("replicas can only be used with replicated mode") + } + + serviceMode.Global = &swarm.GlobalService{} + case "replicated": + serviceMode.Replicated = &swarm.ReplicatedService{ + Replicas: opts.replicas.Value(), + } + default: + return serviceMode, fmt.Errorf("Unknown mode: %s, only replicated and global supported", opts.mode) + } + return serviceMode, nil +} + func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { var service swarm.ServiceSpec @@ -368,6 +387,16 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { currentEnv = append(currentEnv, env) } + healthConfig, err := opts.healthcheck.toHealthConfig() + if err != nil { + return service, err + } + + serviceMode, err := opts.ToServiceMode() + if err != nil { + return service, err + } + service = swarm.ServiceSpec{ Annotations: swarm.Annotations{ Name: opts.name, @@ -393,6 +422,7 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { Hosts: convertExtraHostsToSwarmHosts(opts.hosts.GetAll()), StopGracePeriod: opts.stopGrace.Value(), Secrets: nil, + Healthcheck: healthConfig, }, Networks: convertNetworks(opts.networks.GetAll()), Resources: opts.resources.ToResourceRequirements(), @@ -403,7 +433,7 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { LogDriver: opts.logDriver.toLogDriver(), }, Networks: convertNetworks(opts.networks.GetAll()), - Mode: swarm.ServiceMode{}, + Mode: serviceMode, UpdateConfig: &swarm.UpdateConfig{ Parallelism: opts.update.parallelism, Delay: opts.update.delay, @@ -414,26 +444,6 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { EndpointSpec: opts.endpoint.ToEndpointSpec(), } - healthConfig, err := opts.healthcheck.toHealthConfig() - if err != nil { - return service, err - } - service.TaskTemplate.ContainerSpec.Healthcheck = healthConfig - - switch opts.mode { - case "global": - if opts.replicas.Value() != nil { - return service, fmt.Errorf("replicas can only be used with replicated mode") - } - - service.Mode.Global = &swarm.GlobalService{} - case "replicated": - service.Mode.Replicated = &swarm.ReplicatedService{ - Replicas: opts.replicas.Value(), - } - default: - return service, fmt.Errorf("Unknown mode: %s", opts.mode) - } return service, nil } @@ -465,7 +475,7 @@ func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) { flags.StringVar(&opts.update.onFailure, flagUpdateFailureAction, "pause", "Action on update failure (pause|continue)") flags.Var(&opts.update.maxFailureRatio, flagUpdateMaxFailureRatio, "Failure rate to tolerate during an update") - flags.StringVar(&opts.endpoint.mode, flagEndpointMode, "", "Endpoint mode (vip or dnsrr)") + flags.StringVar(&opts.endpoint.mode, flagEndpointMode, "vip", "Endpoint mode (vip or dnsrr)") flags.BoolVar(&opts.registryAuth, flagRegistryAuth, false, "Send registry authentication details to swarm agents")