Skip to content
This repository was archived by the owner on Jun 22, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
get ecs services
  • Loading branch information
dannyrandall committed Oct 20, 2023
commit 4a57fcc0acb062831bf69b8b681f6b74dd2bcdeb
33 changes: 33 additions & 0 deletions internal/pkg/aws/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type api interface {
StopTask(input *ecs.StopTaskInput) (*ecs.StopTaskOutput, error)
UpdateService(input *ecs.UpdateServiceInput) (*ecs.UpdateServiceOutput, error)
WaitUntilTasksRunning(input *ecs.DescribeTasksInput) error
ListServicesByNamespace(*ecs.ListServicesByNamespaceInput) (*ecs.ListServicesByNamespaceOutput, error)
}

type ssmSessionStarter interface {
Expand Down Expand Up @@ -100,6 +101,7 @@ func (e *ECS) TaskDefinition(taskDefName string) (*TaskDefinition, error) {

// Service calls ECS API and returns the specified service running in the cluster.
func (e *ECS) Service(clusterName, serviceName string) (*Service, error) {
// TODO use Services() below
resp, err := e.client.DescribeServices(&ecs.DescribeServicesInput{
Cluster: aws.String(clusterName),
Services: aws.StringSlice([]string{serviceName}),
Expand All @@ -116,6 +118,37 @@ func (e *ECS) Service(clusterName, serviceName string) (*Service, error) {
return nil, fmt.Errorf("cannot find service %s", serviceName)
}

func (e *ECS) Services(cluster string, services ...string) ([]*Service, error) {
// TODO split into groups of 10
resp, err := e.client.DescribeServices(&ecs.DescribeServicesInput{
Cluster: aws.String(cluster),
Services: aws.StringSlice(services),
})
if err != nil {
return nil, fmt.Errorf("describe services: %w", err)
}

var out []*Service

for i := range resp.Services {
svc := Service(*resp.Services[i])
out = append(out, &svc)
}

// TODO make sure len(services) == len(out)
return out, nil
}

func (e *ECS) ListServicesByNamespace(namespace string) ([]string, error) {
arns, err := e.client.ListServicesByNamespace(&ecs.ListServicesByNamespaceInput{
Namespace: aws.String(namespace),
})
if err != nil {
return nil, err
}
return aws.StringValueSlice(arns.ServiceArns), nil
}

// UpdateServiceOpts sets the optional parameter for UpdateService.
type UpdateServiceOpts func(*ecs.UpdateServiceInput)

Expand Down
1 change: 1 addition & 0 deletions internal/pkg/cli/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ type repositoryService interface {

type ecsLocalClient interface {
TaskDefinition(app, env, svc string) (*awsecs.TaskDefinition, error)
ServiceConnectServices(app, env, svc string) ([]*awsecs.Service, error)
}

type logEventsWriter interface {
Expand Down
55 changes: 55 additions & 0 deletions internal/pkg/cli/run_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ func (o *runLocalOpts) Execute() error {
return fmt.Errorf("get task: %w", err)
}

// TODO add proxy flag
hd := &hostDiscoverer{
app: o.appName,
env: o.envName,
wkld: o.wkldName,
ecs: ecs.New(o.sess), // TODO update env manager role to support actions
}
_, err = hd.Hosts(ctx)
if err != nil {
return fmt.Errorf("find hosts to connect to: %w", err)
}

mft, _, err := workloadManifest(&workloadManifestInput{
name: o.wkldName,
appName: o.appName,
Expand Down Expand Up @@ -559,6 +571,49 @@ func (o *runLocalOpts) getSecret(ctx context.Context, valueFrom string) (string,
return getter.GetSecretValue(ctx, valueFrom)
}

type host struct {
host string
port string
}

type hostDiscoverer struct {
rg resourceGetter
ecs ecsLocalClient
Comment thread
dannyrandall marked this conversation as resolved.
Resolved
app string
env string
wkld string
// rg := resourcegroups.New(o.sess) // TODO move to newRunLocalOpts()
}

func (h *hostDiscoverer) Hosts(ctx context.Context) ([]host, error) {
// rds instances
// elasticcache instances
// ecs services via
// ecs services via SC
svcs, err := h.ecs.ServiceConnectServices(h.app, h.env, h.wkld)
if err != nil {
return nil, fmt.Errorf("get service: %w", err)
}

var hosts []host
for _, svc := range svcs {
// TODO is there only one deployment?
for _, dep := range svc.Deployments {
for _, sc := range dep.ServiceConnectConfiguration.Services {
for _, alias := range sc.ClientAliases {
hosts = append(hosts, host{
host: aws.StringValue(alias.DnsName),
port: strconv.Itoa(int(aws.Int64Value(alias.Port))),
})
}
}
}
}

fmt.Printf("hosts: %+v\n", hosts)
return nil, nil
}

// BuildRunLocalCmd builds the command for running a workload locally
func BuildRunLocalCmd() *cobra.Command {
vars := runLocalVars{}
Expand Down
31 changes: 31 additions & 0 deletions internal/pkg/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ecs
import (
"encoding/json"
"fmt"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -47,6 +48,8 @@ type ecsClient interface {
DescribeTasks(cluster string, taskARNs []string) ([]*ecs.Task, error)
ActiveClusters(arns ...string) ([]string, error)
ActiveServices(clusterName string, serviceARNs ...string) ([]string, error)
ListServicesByNamespace(namespace string) ([]string, error)
Services(cluster string, services ...string) ([]*ecs.Service, error)
}

type stepFunctionsClient interface {
Expand Down Expand Up @@ -133,6 +136,34 @@ func (c Client) Service(app, env, svc string) (*ecs.Service, error) {
return service, nil
}

// ServiceConnectServices returns a list of services that are in the same
// service connect namespace as the given service.
Comment thread
dannyrandall marked this conversation as resolved.
Resolved
func (c Client) ServiceConnectServices(app, env, svc string) ([]*ecs.Service, error) {
s, err := c.Service(app, env, svc)
if err != nil {
return nil, fmt.Errorf("get service: %w", err)
}
if len(s.Deployments) == 0 {
return nil, nil
}

arns, err := c.ecsClient.ListServicesByNamespace(aws.StringValue(s.Deployments[0].ServiceConnectConfiguration.Namespace))
if err != nil {
return nil, fmt.Errorf("get services in same namespace: %w", err)
Comment thread
dannyrandall marked this conversation as resolved.
Resolved
}

// remove this service's arn
arns = slices.DeleteFunc(arns, func(arn string) bool {
return arn == aws.StringValue(s.ServiceArn)
})

svcs, err := c.ecsClient.Services(aws.StringValue(s.ClusterArn), arns...)
if err != nil {
return nil, fmt.Errorf("get services: %w", err)
}
return svcs, nil
}

// LastUpdatedAt returns the last updated time of the ECS service.
func (c Client) LastUpdatedAt(app, env, svc string) (time.Time, error) {
detail, err := c.Service(app, env, svc)
Expand Down
⌂ 换网址