Browse code

support both endpoint modes in stack

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2017/02/17 14:34:49
Showing 3 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"os"
6 6
 	"sort"
7
+	"strings"
7 8
 	"time"
8 9
 
9 10
 	"github.com/docker/docker/api/types"
... ...
@@ -54,7 +55,7 @@ func convertService(
54 54
 ) (swarm.ServiceSpec, error) {
55 55
 	name := namespace.Scope(service.Name)
56 56
 
57
-	endpoint, err := convertEndpointSpec(service.Ports)
57
+	endpoint, err := convertEndpointSpec(service.EndpointMode, service.Ports)
58 58
 	if err != nil {
59 59
 		return swarm.ServiceSpec{}, err
60 60
 	}
... ...
@@ -374,7 +375,7 @@ func (a byPublishedPort) Len() int           { return len(a) }
374 374
 func (a byPublishedPort) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
375 375
 func (a byPublishedPort) Less(i, j int) bool { return a[i].PublishedPort < a[j].PublishedPort }
376 376
 
377
-func convertEndpointSpec(source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {
377
+func convertEndpointSpec(endpointMode string, source []composetypes.ServicePortConfig) (*swarm.EndpointSpec, error) {
378 378
 	portConfigs := []swarm.PortConfig{}
379 379
 	for _, port := range source {
380 380
 		portConfig := swarm.PortConfig{
... ...
@@ -387,7 +388,10 @@ func convertEndpointSpec(source []composetypes.ServicePortConfig) (*swarm.Endpoi
387 387
 	}
388 388
 
389 389
 	sort.Sort(byPublishedPort(portConfigs))
390
-	return &swarm.EndpointSpec{Ports: portConfigs}, nil
390
+	return &swarm.EndpointSpec{
391
+		Mode:  swarm.ResolutionMode(strings.ToLower(endpointMode)),
392
+		Ports: portConfigs,
393
+	}, nil
391 394
 }
392 395
 
393 396
 func convertEnvironment(source map[string]string) []string {
... ...
@@ -156,9 +156,10 @@ func TestConvertEndpointSpec(t *testing.T) {
156 156
 			Published: 80,
157 157
 		},
158 158
 	}
159
-	endpoint, err := convertEndpointSpec(source)
159
+	endpoint, err := convertEndpointSpec("vip", source)
160 160
 
161 161
 	expected := swarm.EndpointSpec{
162
+		Mode: swarm.ResolutionMode(strings.ToLower("vip")),
162 163
 		Ports: []swarm.PortConfig{
163 164
 			{
164 165
 				TargetPort:    8080,
... ...
@@ -89,6 +89,7 @@ type ServiceConfig struct {
89 89
 	DNS             StringList
90 90
 	DNSSearch       StringList `mapstructure:"dns_search"`
91 91
 	DomainName      string     `mapstructure:"domainname"`
92
+	EndpointMode    string
92 93
 	Entrypoint      ShellCommand
93 94
 	Environment     MappingWithEquals
94 95
 	EnvFile         StringList `mapstructure:"env_file"`