From cd1cde6e77af142e69be02067c2f92bf531e2b86 Mon Sep 17 00:00:00 2001 From: allencloud Date: Fri, 17 Feb 2017 13:34:49 +0800 Subject: [PATCH] support both endpoint modes in stack Signed-off-by: allencloud --- compose/convert/service.go | 10 +++++++--- compose/convert/service_test.go | 3 ++- compose/types/types.go | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/compose/convert/service.go b/compose/convert/service.go index 93b910967e..55368e2410 100644 --- a/compose/convert/service.go +++ b/compose/convert/service.go @@ -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 { diff --git a/compose/convert/service_test.go b/compose/convert/service_test.go index 64ccfd038e..69fa90dbc2 100644 --- a/compose/convert/service_test.go +++ b/compose/convert/service_test.go @@ -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, diff --git a/compose/types/types.go b/compose/types/types.go index dce13c928a..d1d762900d 100644 --- a/compose/types/types.go +++ b/compose/types/types.go @@ -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"`