Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix services networking in external launch type #781

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions ecs_composex/common/stacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def __init__(
raise TypeError(
"stack_template is", type(stack_template), "expected", Template
)
self.stack_template = stack_template
self.stack_template: Template = stack_template
if stack_parameters is None:
self.stack_parameters = {}
self.stack_parameters: dict = {}
elif not isinstance(stack_parameters, dict):
raise TypeError("parameters is", type(stack_parameters), "expected", dict)
stack_kwargs = {x: kwargs[x] for x in self.props.keys() if x in kwargs}
Expand Down
18 changes: 16 additions & 2 deletions ecs_composex/compose/compose_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from ecs_composex.compose.compose_volumes.services_helpers import map_volumes
from ecs_composex.ecs.ecs_conditions import (
IPC_FROM_HOST_CON_T,
USE_EXTERNAL_LT_T,
USE_FARGATE_CON_T,
USE_WINDOWS_OS_T,
)
Expand Down Expand Up @@ -885,13 +886,15 @@ def handle_expose_ports(self, aws_vpc_mappings):
f" Skipping {self.name}.expose.{expose_port}"
)

def define_port_mappings(self) -> list:
def define_port_mappings(self, family: ComposeFamily) -> list:
"""
Define the list of port mappings to use for either AWS VPC deployments or else (bridge etc).
Not in use atm as AWS VPC is made mandatory
"""
if self.container_definition:
service_port_mappings = getattr(self.container_definition, "PortMappings")
service_port_mappings: list[PortMapping] = getattr(
self.container_definition, "PortMappings"
)
else:
return []
for protocol, mappings in self.ingress_mappings.items():
Expand Down Expand Up @@ -924,6 +927,17 @@ def define_port_mappings(self) -> list:
)
)
self.handle_expose_ports(service_port_mappings)
if family and family.service_compute.launch_type == "EXTERNAL":
new_port_mappings: list[PortMapping] = []
for _port_mapping in service_port_mappings:
new_port_mappings.append(
PortMapping(
ContainerPort=_port_mapping.ContainerPort,
HostPort=_port_mapping.HostPort,
Protocol=_port_mapping.Protocol,
)
)
return new_port_mappings
return service_port_mappings

def import_docker_labels(self, definition: dict):
Expand Down
2 changes: 1 addition & 1 deletion ecs_composex/ecs/ecs_family/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def finalize_services_networking_settings(self, settings: ComposeXSettings) -> N
setattr(
service.container_definition,
"PortMappings",
service.define_port_mappings(),
service.define_port_mappings(self),
)

def init_network_settings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def finalize_network_settings(
Evaluates the ECS Connect settings to be configured by the service.
If there is a configuration to be set, ensures it's set on the ECS Service definition.
"""
family.service_networking.set_ecs_connect(settings)
if family.service_compute.launch_type != "EXTERNAL":
family.service_networking.set_ecs_connect(settings)
if family.service_networking.ecs_connect_config and family.ecs_service:
setattr(
family.ecs_service.ecs_service,
Expand Down
10 changes: 4 additions & 6 deletions ecs_composex/ecs/ecs_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def add_compose_families(
family.iam_manager.task_role.name_param,
family.iam_manager.exec_role.arn_param,
family.iam_manager.exec_role.name_param,
families_sg_stack.services_mappings[family.name].parameter,
families_sg_stack.services_mappings[family].parameter,
],
)
family.stack.Parameters.update(
Expand All @@ -126,11 +126,9 @@ def add_compose_families(
family.iam_manager.exec_role.arn_param.title: family.iam_manager.exec_role.output_arn,
family.iam_manager.exec_role.name_param.title: family.iam_manager.exec_role.output_name,
ecs_params.SERVICE_HOSTNAME.title: family.family_hostname,
families_sg_stack.services_mappings[
family.name
].parameter.title: GetAtt(
families_sg_stack.services_mappings[family.name].stack.title,
f"Outputs.{families_sg_stack.services_mappings[family.name].parameter.title}",
families_sg_stack.services_mappings[family].parameter.title: GetAtt(
families_sg_stack.services_mappings[family].stack.title,
f"Outputs.{families_sg_stack.services_mappings[family].parameter.title}",
),
}
)
Expand Down
2 changes: 1 addition & 1 deletion ecs_composex/ecs/managed_sidecars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def add_to_family(self, family: ComposeFamily, is_dependency: bool = False) -> N
"awslogs-stream-prefix": self.name,
},
)
self.define_port_mappings()
self.define_port_mappings(family)
self.family.service_networking.merge_services_ports()

def set_as_dependency_to_family_services(self, is_dependency: bool = False) -> None:
Expand Down
2 changes: 1 addition & 1 deletion ecs_composex/ecs/service_networking/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, family: ComposeFamily, families_sg_stack: EcsIngressStack):
self.merge_networks()
self.definition = merge_family_services_networking(family)
self.security_group: ServiceSecurityGroup = families_sg_stack.services_mappings[
family.name
family
]
self.extra_security_groups = [self.security_group.parameter]
self._subnets = Ref(APP_SUBNETS)
Expand Down
2 changes: 1 addition & 1 deletion ecs_composex/ecs_composex.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def generate_full_template(settings: ComposeXSettings):
set_ecs_cluster_identifier(settings.root_stack, settings)
add_all_tags(settings.root_stack.stack_template, settings)
set_all_mappings_to_root_stack(settings)
families_sg_stack.update_vpc_settings(vpc_stack)
families_sg_stack.update_vpc_settings(vpc_stack, settings.root_stack)

for resource in settings.x_resources:
if hasattr(resource, "post_processing") and hasattr(
Expand Down
18 changes: 15 additions & 3 deletions ecs_composex/ecs_ingress/ecs_ingress_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from ecs_composex.ecs.ecs_stack import ServiceStack
from ecs_composex.common.settings import ComposeXSettings
from ecs_composex.ecs.ecs_family import ComposeFamily

Expand Down Expand Up @@ -75,16 +76,16 @@ def __init__(self, name: str, settings: ComposeXSettings, **kwargs):
stack_template = build_template(
"Services SG for service-to-service communication"
)
self.services_mappings: dict[str, ServiceSecurityGroup] = {}
self.services_mappings: dict[ComposeFamily, ServiceSecurityGroup] = {}
add_parameters(stack_template, [CLUSTER_NAME, VPC_ID])
super().__init__(name, stack_template, **kwargs)

for family in settings.families.values():
sg = ServiceSecurityGroup(family, self)
self.services_mappings[family.name] = sg
self.services_mappings[family] = sg
add_outputs(stack_template, [sg.output])

def update_vpc_settings(self, vpc_stack: VpcStack):
def update_vpc_settings(self, vpc_stack: VpcStack, root_stack: ComposeXStack):
if vpc_stack.vpc_resource and (
vpc_stack.vpc_resource.cfn_resource or vpc_stack.vpc_resource.mappings
):
Expand All @@ -96,3 +97,14 @@ def update_vpc_settings(self, vpc_stack: VpcStack):
self.Parameters.update(
{VPC_ID.title: FindInMap("Network", VPC_ID.title, VPC_ID.title)}
)
else:
for family, family_sg in self.services_mappings.items():
del family.stack.stack_template.parameters[family_sg.parameter.title]
root_service_stack: ServiceStack = root_stack.stack_template.resources[
family.stack.title
]
del root_service_stack.properties["Parameters"][
family_sg.parameter.title
]

del root_stack.stack_template.resources[self.name]
1,036 changes: 547 additions & 489 deletions poetry.lock

Large diffs are not rendered by default.

Loading