add endpoint mode a default value

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2017-01-16 16:58:23 +08:00
parent 53edcd37a2
commit 67db25f42e
1 changed files with 32 additions and 22 deletions

View File

@ -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) { func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
var service swarm.ServiceSpec var service swarm.ServiceSpec
@ -368,6 +387,16 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
currentEnv = append(currentEnv, env) 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{ service = swarm.ServiceSpec{
Annotations: swarm.Annotations{ Annotations: swarm.Annotations{
Name: opts.name, Name: opts.name,
@ -393,6 +422,7 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
Hosts: convertExtraHostsToSwarmHosts(opts.hosts.GetAll()), Hosts: convertExtraHostsToSwarmHosts(opts.hosts.GetAll()),
StopGracePeriod: opts.stopGrace.Value(), StopGracePeriod: opts.stopGrace.Value(),
Secrets: nil, Secrets: nil,
Healthcheck: healthConfig,
}, },
Networks: convertNetworks(opts.networks.GetAll()), Networks: convertNetworks(opts.networks.GetAll()),
Resources: opts.resources.ToResourceRequirements(), Resources: opts.resources.ToResourceRequirements(),
@ -403,7 +433,7 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
LogDriver: opts.logDriver.toLogDriver(), LogDriver: opts.logDriver.toLogDriver(),
}, },
Networks: convertNetworks(opts.networks.GetAll()), Networks: convertNetworks(opts.networks.GetAll()),
Mode: swarm.ServiceMode{}, Mode: serviceMode,
UpdateConfig: &swarm.UpdateConfig{ UpdateConfig: &swarm.UpdateConfig{
Parallelism: opts.update.parallelism, Parallelism: opts.update.parallelism,
Delay: opts.update.delay, Delay: opts.update.delay,
@ -414,26 +444,6 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
EndpointSpec: opts.endpoint.ToEndpointSpec(), 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 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.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.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") flags.BoolVar(&opts.registryAuth, flagRegistryAuth, false, "Send registry authentication details to swarm agents")