support both endpoint modes in stack

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2017-02-17 13:34:49 +08:00 committed by Daniel Nephin
parent 63c3221dd3
commit cd1cde6e77
3 changed files with 10 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/docker/docker/api/types"
@ -54,7 +55,7 @@ func convertService(
) (swarm.ServiceSpec, error) {
name := namespace.Scope(service.Name)
endpoint, err := convertEndpointSpec(service.Ports)
endpoint, err := convertEndpointSpec(service.EndpointMode, service.Ports)
if err != nil {
return swarm.ServiceSpec{}, err
}
@ -374,7 +375,7 @@ 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 }
func convertEndpointSpec(source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {
func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {
portConfigs := []swarm.PortConfig{}
for _, port := range source {
portConfig := swarm.PortConfig{
@ -387,7 +388,10 @@ func convertEndpointSpec(source []composetypes.ServicePortConfig) (*swarm.Endpoi
}
sort.Sort(byPublishedPort(portConfigs))
return &swarm.EndpointSpec{Ports: portConfigs}, nil
return &swarm.EndpointSpec{
Mode: swarm.ResolutionMode(strings.ToLower(endpointMode)),
Ports: portConfigs,
}, nil
}
func convertEnvironment(source map[string]string) []string {

View File

@ -156,9 +156,10 @@ func TestConvertEndpointSpec(t *testing.T) {
Published: 80,
},
}
endpoint, err := convertEndpointSpec(source)
endpoint, err := convertEndpointSpec("vip", source)
expected := swarm.EndpointSpec{
Mode: swarm.ResolutionMode(strings.ToLower("vip")),
Ports: []swarm.PortConfig{
{
TargetPort: 8080,

View File

@ -89,6 +89,7 @@ type ServiceConfig struct {
DNS StringList
DNSSearch StringList `mapstructure:"dns_search"`
DomainName string `mapstructure:"domainname"`
EndpointMode string
Entrypoint ShellCommand
Environment MappingWithEquals
EnvFile StringList `mapstructure:"env_file"`