From 7c4353a0acb43c8f5ccea3d80ee48b06b09b682f Mon Sep 17 00:00:00 2001 From: Tait Clarridge Date: Wed, 25 Oct 2017 11:18:03 -0400 Subject: [PATCH] Add missing functions for ECS template --- provider/ecs/ecs.go | 24 ++++++++++---- provider/ecs/ecs_test.go | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/provider/ecs/ecs.go b/provider/ecs/ecs.go index 8b09821a4..a1df800e1 100644 --- a/provider/ecs/ecs.go +++ b/provider/ecs/ecs.go @@ -178,7 +178,8 @@ func wrapAws(ctx context.Context, req *request.Request) error { return req.Send() } -func (p *Provider) loadECSConfig(ctx context.Context, client *awsClient) (*types.Configuration, error) { +// generateECSConfig fills the config template with the given instances +func (p *Provider) generateECSConfig(services map[string][]ecsInstance) (*types.Configuration, error) { var ecsFuncMap = template.FuncMap{ "filterFrontends": p.filterFrontends, "getFrontendRule": p.getFrontendRule, @@ -187,8 +188,22 @@ func (p *Provider) loadECSConfig(ctx context.Context, client *awsClient) (*types "getLoadBalancerSticky": p.getLoadBalancerSticky, "hasStickinessLabel": p.hasStickinessLabel, "getStickinessCookieName": p.getStickinessCookieName, + "getProtocol": p.getProtocol, + "getHost": p.getHost, + "getPort": p.getPort, + "getWeight": p.getWeight, + "getPassHostHeader": p.getPassHostHeader, + "getPriority": p.getPriority, + "getEntryPoints": p.getEntryPoints, } + return p.GetConfiguration("templates/ecs.tmpl", ecsFuncMap, struct { + Services map[string][]ecsInstance + }{ + services, + }) +} +func (p *Provider) loadECSConfig(ctx context.Context, client *awsClient) (*types.Configuration, error) { instances, err := p.listInstances(ctx, client) if err != nil { return nil, err @@ -205,12 +220,7 @@ func (p *Provider) loadECSConfig(ctx context.Context, client *awsClient) (*types services[instance.Name] = []ecsInstance{instance} } } - - return p.GetConfiguration("templates/ecs.tmpl", ecsFuncMap, struct { - Services map[string][]ecsInstance - }{ - services, - }) + return p.generateECSConfig(services) } // Find all running Provider tasks in a cluster, also collect the task definitions (for docker labels) diff --git a/provider/ecs/ecs_test.go b/provider/ecs/ecs_test.go index 5af540616..c3dd5a1fc 100644 --- a/provider/ecs/ecs_test.go +++ b/provider/ecs/ecs_test.go @@ -552,3 +552,74 @@ func TestEcsGetBasicAuth(t *testing.T) { }) } } + +func TestGenerateECSConfig(t *testing.T) { + provider := &Provider{} + tests := []struct { + desc string + services map[string][]ecsInstance + exp *types.Configuration + err error + }{ + { + desc: "config parsed successfully", + services: map[string][]ecsInstance{ + "testing": { + { + Name: "instance-1", + containerDefinition: &ecs.ContainerDefinition{ + DockerLabels: map[string]*string{}, + }, + machine: &ec2.Instance{ + PrivateIpAddress: func(s string) *string { return &s }("10.0.0.1"), + }, + container: &ecs.Container{ + NetworkBindings: []*ecs.NetworkBinding{ + { + HostPort: func(i int64) *int64 { return &i }(1337), + }, + }, + }, + }, + }, + }, + exp: &types.Configuration{ + Backends: map[string]*types.Backend{ + "backend-instance-1": { + Servers: map[string]types.Server{ + "server-instance-1": { + URL: "http://10.0.0.1:1337", + }, + }, + }, + "backend-testing": { + LoadBalancer: &types.LoadBalancer{ + Method: "wrr", + }, + }, + }, + Frontends: map[string]*types.Frontend{ + "frontend-testing": { + EntryPoints: []string{}, + Backend: "backend-testing", + Routes: map[string]types.Route{ + "route-frontend-testing": { + Rule: "Host:instance-1.", + }, + }, + PassHostHeader: true, + BasicAuth: []string{}, + }, + }, + }, + }, + } + + for _, test := range tests { + t.Run(test.desc, func(t *testing.T) { + got, err := provider.generateECSConfig(test.services) + assert.Equal(t, test.err, err) + assert.Equal(t, test.exp, got) + }) + } +}