Skip to content
This repository was archived by the owner on Oct 9, 2020. It is now read-only.

Commit ad7f5ce

Browse files
committed
ps shows LoadBalancer URL
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 98c4f8c commit ad7f5ce

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

pkg/amazon/backend/list.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,31 @@ func (b *Backend) Ps(ctx context.Context, project *types.Project) ([]compose.Ser
1414
cluster = project.Name
1515
}
1616

17-
status, err := b.api.DescribeServices(ctx, cluster, project.Name)
17+
resources, err := b.api.ListStackResources(ctx, project.Name)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
var loadBalancer string
23+
if lb, ok := project.Extensions[compose.ExtensionLB]; ok {
24+
loadBalancer = lb.(string)
25+
}
26+
servicesARN := []string{}
27+
for _, r := range resources {
28+
switch r.Type {
29+
case "AWS::ECS::Service":
30+
servicesARN = append(servicesARN, r.ARN)
31+
case "AWS::ElasticLoadBalancingV2::LoadBalancer":
32+
loadBalancer = r.ARN
33+
}
34+
}
35+
36+
status, err := b.api.DescribeServices(ctx, cluster, servicesARN)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
url, err := b.api.GetLoadBalancerURL(ctx, loadBalancer)
1842
if err != nil {
1943
return nil, err
2044
}
@@ -26,7 +50,7 @@ func (b *Backend) Ps(ctx context.Context, project *types.Project) ([]compose.Ser
2650
}
2751
ports := []string{}
2852
for _, p := range s.Ports {
29-
ports = append(ports, fmt.Sprintf("*:%d->%d/%s", p.Published, p.Target, p.Protocol))
53+
ports = append(ports, fmt.Sprintf("%s:%d->%d/%s", url, p.Published, p.Target, p.Protocol))
3054
}
3155
state.Ports = ports
3256
status[i] = state

pkg/amazon/backend/up.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func (b Backend) GetLoadBalancer(ctx context.Context, project *types.Project) (s
105105
if !ok {
106106
return "", fmt.Errorf("Load Balancer does not exist: %s", lb)
107107
}
108-
return b.api.GetLoadBalancerARN(ctx, lbName)
109108
}
110109
return "", nil
111110
}

pkg/amazon/sdk/api.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ type API interface {
1616
StackExists(ctx context.Context, name string) (bool, error)
1717
CreateStack(ctx context.Context, name string, template *cloudformation.Template, parameters map[string]string) error
1818
DeleteStack(ctx context.Context, name string) error
19-
DescribeServices(ctx context.Context, cluster string, project string) ([]compose.ServiceStatus, error)
19+
ListStackResources(ctx context.Context, name string) ([]compose.StackResource, error)
2020
GetStackID(ctx context.Context, name string) (string, error)
2121
WaitStackComplete(ctx context.Context, name string, operation int) error
2222
DescribeStackEvents(ctx context.Context, stackID string) ([]*cf.StackEvent, error)
2323

24-
LoadBalancerExists(ctx context.Context, name string) (bool, error)
25-
GetLoadBalancerARN(ctx context.Context, name string) (string, error)
24+
DescribeServices(ctx context.Context, cluster string, arns []string) ([]compose.ServiceStatus, error)
25+
26+
LoadBalancerExists(ctx context.Context, arn string) (bool, error)
27+
GetLoadBalancerURL(ctx context.Context, arn string) (string, error)
2628

2729
ClusterExists(ctx context.Context, name string) (bool, error)
2830

pkg/amazon/sdk/sdk.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,27 @@ func (s sdk) DescribeStackEvents(ctx context.Context, stackID string) ([]*cloudf
222222
}
223223
}
224224

225+
func (s sdk) ListStackResources(ctx context.Context, name string) ([]compose.StackResource, error) {
226+
// FIXME handle pagination
227+
res, err := s.CF.ListStackResourcesWithContext(ctx, &cloudformation.ListStackResourcesInput{
228+
StackName: aws.String(name),
229+
})
230+
if err != nil {
231+
return nil, err
232+
}
233+
234+
resources := []compose.StackResource{}
235+
for _, r := range res.StackResourceSummaries {
236+
resources = append(resources, compose.StackResource{
237+
LogicalID: *r.LogicalResourceId,
238+
Type: *r.ResourceType,
239+
ARN: *r.PhysicalResourceId,
240+
Status: *r.ResourceStatus,
241+
})
242+
}
243+
return resources, nil
244+
}
245+
225246
func (s sdk) DeleteStack(ctx context.Context, name string) error {
226247
logrus.Debug("Delete CloudFormation stack")
227248
_, err := s.CF.DeleteStackWithContext(ctx, &cloudformation.DeleteStackInput{
@@ -270,7 +291,6 @@ func (s sdk) InspectSecret(ctx context.Context, id string) (compose.Secret, erro
270291
}
271292

272293
func (s sdk) ListSecrets(ctx context.Context) ([]compose.Secret, error) {
273-
274294
logrus.Debug("List secrets ...")
275295
response, err := s.SM.ListSecrets(&secretsmanager.ListSecretsInput{})
276296
if err != nil {
@@ -336,18 +356,10 @@ func (s sdk) GetLogs(ctx context.Context, name string, consumer compose.LogConsu
336356
}
337357
}
338358

339-
func (s sdk) DescribeServices(ctx context.Context, cluster string, project string) ([]compose.ServiceStatus, error) {
340-
// TODO handle pagination
341-
list, err := s.ECS.ListServicesWithContext(ctx, &ecs.ListServicesInput{
342-
Cluster: aws.String(cluster),
343-
})
344-
if err != nil {
345-
return nil, err
346-
}
347-
359+
func (s sdk) DescribeServices(ctx context.Context, cluster string, arns []string) ([]compose.ServiceStatus, error) {
348360
services, err := s.ECS.DescribeServicesWithContext(ctx, &ecs.DescribeServicesInput{
349361
Cluster: aws.String(cluster),
350-
Services: list.ServiceArns,
362+
Services: aws.StringSlice(arns),
351363
Include: aws.StringSlice([]string{"TAGS"}),
352364
})
353365
if err != nil {
@@ -356,17 +368,13 @@ func (s sdk) DescribeServices(ctx context.Context, cluster string, project strin
356368
status := []compose.ServiceStatus{}
357369
for _, service := range services.Services {
358370
var name string
359-
var stack string
360371
for _, t := range service.Tags {
361-
switch *t.Key {
362-
case compose.ProjectTag:
363-
stack = *t.Value
364-
case compose.ServiceTag:
372+
if *t.Key == compose.ServiceTag {
365373
name = *t.Value
366374
}
367375
}
368-
if stack != project {
369-
continue
376+
if name == "" {
377+
return nil, fmt.Errorf("service %s doesn't have a %s tag", *service.ServiceArn, compose.ServiceTag)
370378
}
371379
status = append(status, compose.ServiceStatus{
372380
ID: *service.ServiceName,
@@ -410,24 +418,24 @@ func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) (map[string
410418
return publicIPs, nil
411419
}
412420

413-
func (s sdk) LoadBalancerExists(ctx context.Context, name string) (bool, error) {
414-
logrus.Debug("Check if cluster was already created: ", name)
421+
func (s sdk) LoadBalancerExists(ctx context.Context, arn string) (bool, error) {
422+
logrus.Debug("Check if LoadBalancer exists: ", arn)
415423
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
416-
Names: []*string{aws.String(name)},
424+
LoadBalancerArns: []*string{aws.String(arn)},
417425
})
418426
if err != nil {
419427
return false, err
420428
}
421429
return len(lbs.LoadBalancers) > 0, nil
422430
}
423431

424-
func (s sdk) GetLoadBalancerARN(ctx context.Context, name string) (string, error) {
425-
logrus.Debug("Check if cluster was already created: ", name)
432+
func (s sdk) GetLoadBalancerURL(ctx context.Context, arn string) (string, error) {
433+
logrus.Debug("Retrieve load balancer URL: ", arn)
426434
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
427-
Names: []*string{aws.String(name)},
435+
LoadBalancerArns: []*string{aws.String(arn)},
428436
})
429437
if err != nil {
430438
return "", err
431439
}
432-
return *lbs.LoadBalancers[0].LoadBalancerArn, nil
440+
return *lbs.LoadBalancers[0].DNSName, nil
433441
}

pkg/compose/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ package compose
22

33
import "encoding/json"
44

5+
type StackResource struct {
6+
LogicalID string
7+
Type string
8+
ARN string
9+
Status string
10+
}
11+
512
type ServiceStatus struct {
613
ID string
714
Name string

0 commit comments

Comments
 (0)