Skip to content

Commit d416dd5

Browse files
authored
Merge pull request #1 from cruxstack/dev
feat: handle node termination gracefully
2 parents 5296f6c + 9b1b51d commit d416dd5

File tree

23 files changed

+7769
-10
lines changed

23 files changed

+7769
-10
lines changed

.devcontainer/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ RUN curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/in
2525
# install docker
2626
COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/
2727

28+
# install node.js
29+
ENV NODE_VERSION=18.x
30+
RUN curl -sL https://deb.nodesource.com/setup_${NODE_VERSION} | bash - && \
31+
apt-get install -y nodejs
32+
33+
# install typescript
34+
RUN npm install -g typescript
35+
2836
# install pip
2937
RUN apt-get update
3038
RUN apt-get install -y \

.devcontainer/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"extensions": [
3434
"darkriszty.markdown-table-prettify",
35+
"dbaeumer.vscode-eslint",
3536
"editorconfig.editorconfig",
3637
"github.copilot",
3738
"github.copilot-chat",

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,3 @@ tmp/
5959

6060
# opa
6161
bundle.tar.gz
62-
63-
# lifecycle
64-
**/lifecycle*

main.tf

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ locals {
33
enabled = module.this.enabled
44
self_hosted_enabled = local.enabled && var.self_hosted
55

6-
cluster_node_types = toset(local.self_hosted_enabled ? ["spot", "ondemand"] : [])
7-
86
aws_account_id = var.aws_account_id != "" ? var.aws_account_id : try(data.aws_caller_identity.current[0].account_id, "")
97
aws_region_name = var.aws_region_name != "" ? var.aws_region_name : try(data.aws_region.current[0].name, "")
108
aws_kv_namespace = trim(coalesce(var.aws_kv_namespace, "ecs-cluster/${module.cluster_label.id}"), "/")
9+
10+
cluster_node_types = local.self_hosted_enabled ? ["spot", "ondemand"] : []
11+
tag_name_ecs_cluster_name = "${local.aws_kv_namespace}/cluster-name"
1112
}
1213

1314
data "aws_caller_identity" "current" {
@@ -50,15 +51,15 @@ resource "aws_ecs_cluster" "this" {
5051
module "cluster_node_label" {
5152
source = "cloudposse/label/null"
5253
version = "0.25.0"
53-
for_each = local.cluster_node_types
54+
for_each = toset(local.cluster_node_types)
5455

5556
attributes = [each.key]
5657
tags = { ("${local.aws_kv_namespace}/node-type") : each.key }
5758
context = module.cluster_label.context
5859
}
5960

6061
resource "aws_autoscaling_group" "this" {
61-
for_each = local.cluster_node_types
62+
for_each = toset(local.cluster_node_types)
6263

6364
name = module.cluster_node_label[each.key].id
6465
vpc_zone_identifier = var.vpc_subnet_ids
@@ -105,7 +106,10 @@ resource "aws_autoscaling_group" "this" {
105106
}
106107

107108
dynamic "tag" {
108-
for_each = merge(module.cluster_node_label[each.key].tags, { Name = module.cluster_node_label[each.key].id })
109+
for_each = merge(
110+
module.cluster_node_label[each.key].tags,
111+
{ Name = module.cluster_node_label[each.key].id },
112+
)
109113

110114
content {
111115
key = tag.key
@@ -324,7 +328,7 @@ data "aws_iam_policy_document" "cluster_instance_access" {
324328
module "capacity_provider_label" {
325329
source = "cloudposse/label/null"
326330
version = "0.25.0"
327-
for_each = local.cluster_node_types
331+
for_each = toset(local.cluster_node_types)
328332

329333
delimiter = "_"
330334
label_order = ["name", "attributes"]
@@ -347,7 +351,7 @@ resource "aws_ecs_cluster_capacity_providers" "this" {
347351
}
348352

349353
resource "aws_ecs_capacity_provider" "this" {
350-
for_each = local.cluster_node_types
354+
for_each = toset(local.cluster_node_types)
351355

352356
name = replace(upper(module.capacity_provider_label[each.key].id), "-", "_")
353357

@@ -364,6 +368,18 @@ resource "aws_ecs_capacity_provider" "this" {
364368
}
365369
}
366370

371+
# ======================================================== lifecycle-manager ===
372+
373+
module "lifecycle_manager" {
374+
source = "./modules/lifecycle-manager"
375+
376+
enabled = local.self_hosted_enabled
377+
asg_names = [for x in local.cluster_node_types : aws_autoscaling_group.this[x].name]
378+
tag_name_ecs_cluster_name = local.tag_name_ecs_cluster_name
379+
380+
context = module.cluster_label.context
381+
}
382+
367383
# ================================================================== lookups ===
368384

369385
data "aws_ssm_parameter" "ecs_optimized_ami_id" {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.gitignore
2+
.git
3+
4+
*Dockerfile*
5+
*docker-compose*
6+
.dockerignore
7+
8+
.vscode/
9+
node_modules/
10+
11+
*.env
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
plugins: [
3+
'import',
4+
'jest',
5+
],
6+
rules: {
7+
'func-style': 'error',
8+
},
9+
overrides: [
10+
{
11+
files: [
12+
'**/*.{ts,tsx}',
13+
],
14+
parser: '@typescript-eslint/parser',
15+
parserOptions: {
16+
ecmaVersion: 2019,
17+
sourceType: 'module',
18+
tsconfigRootDir: __dirname,
19+
project: [
20+
'./tsconfig.json',
21+
],
22+
},
23+
plugins: [
24+
'@typescript-eslint',
25+
],
26+
extends: [
27+
'airbnb-typescript/base',
28+
],
29+
rules: {
30+
'@typescript-eslint/no-unused-vars': 'warn',
31+
'@typescript-eslint/no-floating-promises': 'error',
32+
},
33+
},
34+
],
35+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ------------------------------------------------------------------- base ---
2+
3+
FROM node:18 as base
4+
5+
RUN mkdir -p /opt/app
6+
WORKDIR /opt/app
7+
8+
ENV NODE_ENV development
9+
10+
ADD package*.json /opt/
11+
RUN npm ci
12+
13+
ADD ./ ./
14+
15+
# -------------------------------------------------------------------- test ---
16+
17+
FROM base as test
18+
19+
RUN npm run lint
20+
21+
# -------------------------------------------------------------------- build ---
22+
23+
FROM test as build
24+
25+
RUN npm run build
26+
27+
# ----------------------------------------------------------------- package ---
28+
29+
FROM alpine:latest as package
30+
31+
COPY --from=build /opt/app/dist /opt/app/dist
32+
COPY --from=build /opt/package-lock.json /opt/app/dist/
33+
34+
RUN apk add zip \
35+
&& cd /opt/app/dist \
36+
&& zip -r /tmp/package.zip .
37+

0 commit comments

Comments
 (0)