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

create an ingress to expose the legacy v0 API for Episerver #192

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions cdk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"prod": ("prod-onsgeodata-buckete75ea64c-phllx3dqnkmx", "geo_postcodes_prod.csv"),
}

API_V0_HOSTNAMES = {
"dev": "bureaudetails.qa.citizensadvice.org.uk",
"prod": "bureaudetails.prod.content.citizensadvice.org.uk",
}

STAGES = [
Stage(app, "dev", env=Environment(account=ACCOUNT_IDS["devops"], region="eu-west-1")),
Stage(app, "prod", env=Environment(account=ACCOUNT_IDS["prod2"], region="eu-west-1")),
Expand All @@ -40,6 +45,7 @@
lss_bucket_name=LSS_FILES[stage.stage_name],
geo_data_bucket_name=GEO_DATA_FILES[stage.stage_name][0],
geo_data_postcode_file=GEO_DATA_FILES[stage.stage_name][1],
api_v0_host=API_V0_HOSTNAMES[stage.stage_name],
)

for child in app.node.children:
Expand Down
64 changes: 58 additions & 6 deletions cdk/app/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
MetricTarget,
EnvFieldPaths,
NetworkPolicyIpBlock,
Secret, ServiceAccount,
Secret,
ServiceAccount,
ServiceType,
Service,
Ingress,
IngressBackend,
)
from constructs import Construct

Expand All @@ -58,6 +63,7 @@ def __init__(
service_account_name: str,
rds_secret_name: str,
app_secret_name: str,
api_v0_host: str,
):
self._labels = {
"app": self._APP_NAME,
Expand Down Expand Up @@ -93,7 +99,8 @@ def __init__(
self._geo_data_postcode_file = geo_data_postcode_file

deployment = self._create_deployment()
self._expose_services(deployment)
app_service = self._expose_services(deployment)
self._expose_v0_api(api_v0_host, app_service)

self._create_scheduled_import()
self._configure_autoscaler(deployment)
Expand Down Expand Up @@ -170,7 +177,9 @@ def _create_scheduled_import(self):

self._add_labels(scheduled_job.metadata)
self._add_labels(scheduled_job.pod_metadata)
scheduled_job.pod_metadata.add_label("component", "local-office-search-api-scheduled-import")
scheduled_job.pod_metadata.add_label(
"component", "local-office-search-api-scheduled-import"
)

scheduled_job.metadata.add_annotation(
"ad.datadoghq.com/local-office-search-api-scheduled-import.logs",
Expand Down Expand Up @@ -209,7 +218,9 @@ def _server_container_props(self, name: str, command_line: typing.List[str]):
cpu=CpuResources(request=Cpu.millis(400), limit=Cpu.millis(800)),
memory=MemoryResources(request=Size.mebibytes(512), limit=Size.gibibytes(1)),
),
security_context=ContainerSecurityContextProps(user=1000, read_only_root_filesystem=False),
security_context=ContainerSecurityContextProps(
user=1000, read_only_root_filesystem=False
),
)

def _app_env_vars(self):
Expand Down Expand Up @@ -251,8 +262,10 @@ def _app_env_vars(self):
}

def _expose_services(self, deployment: Deployment):
deployment.expose_via_service(
name=self._APP_NAME, ports=[ServicePort(name="http", port=self._HTTP_PORT)]
service = deployment.expose_via_service(
name=self._APP_NAME,
ports=[ServicePort(name="http", port=self._HTTP_PORT)],
service_type=ServiceType.NODE_PORT,
)

metrics_service = deployment.expose_via_service(
Expand All @@ -261,6 +274,45 @@ def _expose_services(self, deployment: Deployment):
)
metrics_service.metadata.add_label("custom-metrics-enabled", "true")

return service

def _expose_v0_api(self, host: str, app_service: Service):
ingress = Ingress(self, "LocalOfficeSearchApiV0Ingress", class_name="alb")
ingress.add_host_rule(host, "/api/v0/", IngressBackend.from_service(app_service))

ingress.metadata.add_annotation("alb.ingress.kubernetes.io/scheme", "internet-facing")
ingress.metadata.add_annotation(
"alb.ingress.kubernetes.io/healthcheck-path", "/status"
)
ingress.metadata.add_annotation(
"alb.ingress.kubernetes.io/actions.ssl-redirect",
json.dumps(
{
"Type": "redirect",
"RedirectConfig": {
"Protocol": "HTTPS",
"Port": "443",
"StatusCode": "HTTP_301",
},
}
),
)
ingress.metadata.add_annotation(
"alb.ingress.kubernetes.io/ssl-policy", "ELBSecurityPolicy-TLS-1-2-2017-01"
)
ingress.metadata.add_annotation(
"alb.ingress.kubernetes.io/tags",
",".join(
f"{key}={value}"
for key, value in {
"Environment": self._labels["env"],
"Product": "corporate_site",
"Component": "local_office_search_api",
"TechnicalOwner": "contentplatform@citizensadvice.org.uk",
}.items()
),
)

def _configure_autoscaler(self, deployment: Deployment):
HorizontalPodAutoscaler(
self,
Expand Down
2 changes: 2 additions & 0 deletions cdk/app/local_office_search_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
lss_bucket_name: str,
geo_data_bucket_name: str,
geo_data_postcode_file: str,
api_v0_host: str,
**kwargs,
) -> None:
super().__init__(scope, construct_id, **kwargs)
Expand Down Expand Up @@ -142,5 +143,6 @@ def __init__(
service_account_name=self._service_account.service_account_name,
rds_secret_name=rds_secret_source.k8s_secret_name,
app_secret_name=app_secret_source.k8s_secret_name,
api_v0_host=api_v0_host,
),
)