Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
classify vpc subnets
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Euhus <[email protected]>
  • Loading branch information
PacoVK committed Feb 14, 2021
1 parent a19e9b2 commit f2493af
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ecs/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type API interface {
getURLWithPortMapping(ctx context.Context, targetGroupArns []string) ([]compose.PortPublisher, error)
ListTasks(ctx context.Context, cluster string, family string) ([]string, error)
GetPublicIPs(ctx context.Context, interfaces ...string) (map[string]string, error)
ResolveLoadBalancer(ctx context.Context, nameOrArn string) (awsResource, string, string, []awsResource, error)
ResolveLoadBalancer(ctx context.Context, nameOrArn string) (awsResource, string, string, vpcSubNets, error)
GetLoadBalancerURL(ctx context.Context, arn string) (string, error)
GetParameter(ctx context.Context, name string) (string, error)
SecurityGroupExists(ctx context.Context, sg string) (bool, error)
Expand Down
26 changes: 15 additions & 11 deletions ecs/awsResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ import (
"github.com/sirupsen/logrus"
)

// vpcSubNets classification
type vpcSubNets struct {
public []awsResource
private []awsResource
}

// awsResources hold the AWS component being used or created to support services definition
type awsResources struct {
vpc string // shouldn't this also be an awsResource ?
subnets []awsResource
subnets vpcSubNets
cluster awsResource
loadBalancer awsResource
loadBalancerType string
Expand All @@ -66,7 +72,7 @@ func (r *awsResources) allSecurityGroups() []string {

func (r *awsResources) subnetsIDs() []string {
var ids []string
for _, r := range r.subnets {
for _, r := range append(r.subnets.private, r.subnets.public...) {
ids = append(ids, r.ID())
}
return ids
Expand Down Expand Up @@ -207,13 +213,16 @@ func (b *ecsAPIService) parseVPCExtension(ctx context.Context, project *types.Pr
}

var publicSubNets []awsResource
var privateSubNets []awsResource
for _, subNet := range subNets {
isPublic, err := b.aws.IsPublicSubnet(ctx, subNet.ID())
if err != nil {
return err
}
if isPublic {
publicSubNets = append(publicSubNets, subNet)
} else {
privateSubNets = append(privateSubNets, subNet)
}
}

Expand All @@ -222,7 +231,8 @@ func (b *ecsAPIService) parseVPCExtension(ctx context.Context, project *types.Pr
}

r.vpc = vpc
r.subnets = subNets
r.subnets.public = publicSubNets
r.subnets.private = privateSubNets
return nil
}

Expand Down Expand Up @@ -451,14 +461,8 @@ func (b *ecsAPIService) ensureLoadBalancer(r *awsResources, project *types.Proje
}

var publicSubNetIDs []string
for _, subNetID := range r.subnetsIDs() {
isPublic, err := b.aws.IsPublicSubnet(context.Background(), subNetID)
if err != nil {
return err
}
if isPublic {
publicSubNetIDs = append(publicSubNetIDs, subNetID)
}
for _, subNetID := range r.subnetsIDs() {
publicSubNetIDs = append(publicSubNetIDs, subNetID)
}

template.Resources["LoadBalancer"] = &elasticloadbalancingv2.LoadBalancer{
Expand Down
4 changes: 2 additions & 2 deletions ecs/aws_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions ecs/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) (map[string
}
}

func (s sdk) ResolveLoadBalancer(ctx context.Context, nameOrArn string) (awsResource, string, string, []awsResource, error) {
func (s sdk) ResolveLoadBalancer(ctx context.Context, nameOrArn string) (awsResource, string, string, vpcSubNets, error) {
logrus.Debug("Check if LoadBalancer exists: ", nameOrArn)
var arns []*string
var names []*string
Expand All @@ -1060,17 +1060,27 @@ func (s sdk) ResolveLoadBalancer(ctx context.Context, nameOrArn string) (awsReso
Names: names,
})
if err != nil {
return nil, "", "", nil, err
return nil, "", "", vpcSubNets{}, err
}
if len(lbs.LoadBalancers) == 0 {
return nil, "", "", nil, errors.Wrapf(errdefs.ErrNotFound, "load balancer %q does not exist", nameOrArn)
return nil, "", "", vpcSubNets{}, errors.Wrapf(errdefs.ErrNotFound, "load balancer %q does not exist", nameOrArn)
}
it := lbs.LoadBalancers[0]
var subNets []awsResource
var subNets vpcSubNets
for _, az := range it.AvailabilityZones {
subNets = append(subNets, existingAWSResource{
id: aws.StringValue(az.SubnetId),
})
isPublic, err := s.IsPublicSubnet(ctx,aws.StringValue(az.SubnetId));
if err != nil {
return nil, "", "", subNets, err
}
if isPublic {
subNets.public = append(subNets.public, existingAWSResource{
id: aws.StringValue(az.SubnetId),
})
} else {
subNets.private = append(subNets.private, existingAWSResource{
id: aws.StringValue(az.SubnetId),
})
}
}
return existingAWSResource{
arn: aws.StringValue(it.LoadBalancerArn),
Expand Down
4 changes: 2 additions & 2 deletions ecs/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

func (b *ecsAPIService) createNFSMountTarget(project *types.Project, resources awsResources, template *cloudformation.Template) {
for volume := range project.Volumes {
for _, subnet := range resources.subnets {
for _, subnet := range append(resources.subnets.public, resources.subnets.private...) {
name := fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(subnet.ID()))
template.Resources[name] = &efs.MountTarget{
FileSystemId: resources.filesystems[volume].ID(),
Expand All @@ -45,7 +45,7 @@ func (b *ecsAPIService) createNFSMountTarget(project *types.Project, resources a

func (b *ecsAPIService) mountTargets(volume string, resources awsResources) []string {
var refs []string
for _, subnet := range resources.subnets {
for _, subnet := range append(resources.subnets.public, resources.subnets.private...) {
refs = append(refs, fmt.Sprintf("%sNFSMountTargetOn%s", normalizeResourceName(volume), normalizeResourceName(subnet.ID())))
}
return refs
Expand Down

0 comments on commit f2493af

Please sign in to comment.