Skip to content

Commit

Permalink
feat: Added features flags for diego-container-app (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolagospagopa authored Aug 3, 2023
1 parent a586887 commit d7f2ecb
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 141 deletions.
1 change: 0 additions & 1 deletion src/domains/diego-container-apps/01_container_app_0.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ resource "null_resource" "update_az_cli" {
triggers = {
env_name = local.container_app_diego_environment_name
rg = azurerm_resource_group.container_app_diego.name
subnet_id = module.container_apps_snet.id
log_analytics_id = data.azurerm_log_analytics_workspace.log_analytics.workspace_id
log_analytics_workspace_primary_shared_key = data.azurerm_log_analytics_workspace.log_analytics.primary_shared_key
}
Expand Down
23 changes: 15 additions & 8 deletions src/domains/diego-container-apps/01_cosmos_db.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
resource "azurerm_resource_group" "cosmosdb_dapr" {
count = var.is_resource_enabled.mongodb_dapr ? 1 : 0
name = "${local.project}-dapr-cosmos-rg"
location = var.location
tags = var.tags
}

resource "azurerm_cosmosdb_account" "mongodb" {
resource "azurerm_cosmosdb_account" "mongodb_dapr" {
count = var.is_resource_enabled.mongodb_dapr ? 1 : 0

name = "${local.project}-dapr-cosmos"
location = azurerm_resource_group.cosmosdb_dapr.location
resource_group_name = azurerm_resource_group.cosmosdb_dapr.name
location = azurerm_resource_group.cosmosdb_dapr[0].location
resource_group_name = azurerm_resource_group.cosmosdb_dapr[0].name
offer_type = "Standard"
kind = "GlobalDocumentDB"

Expand All @@ -28,16 +31,20 @@ resource "azurerm_cosmosdb_account" "mongodb" {
}

resource "azurerm_cosmosdb_sql_database" "db_sql_dapr" {
count = var.is_resource_enabled.mongodb_dapr ? 1 : 0

name = local.cosmosdb_db_name
resource_group_name = azurerm_resource_group.cosmosdb_dapr.name
account_name = azurerm_cosmosdb_account.mongodb.name
resource_group_name = azurerm_resource_group.cosmosdb_dapr[0].name
account_name = azurerm_cosmosdb_account.mongodb_dapr[0].name
}

resource "azurerm_cosmosdb_sql_container" "collection_dapr" {
count = var.is_resource_enabled.mongodb_dapr ? 1 : 0

name = local.cosmosdb_collection_name
resource_group_name = azurerm_resource_group.cosmosdb_dapr.name
account_name = azurerm_cosmosdb_account.mongodb.name
database_name = azurerm_cosmosdb_sql_database.db_sql_dapr.name
resource_group_name = azurerm_resource_group.cosmosdb_dapr[0].name
account_name = azurerm_cosmosdb_account.mongodb_dapr[0].name
database_name = azurerm_cosmosdb_sql_database.db_sql_dapr[0].name

partition_key_path = "/id"
}
17 changes: 12 additions & 5 deletions src/domains/diego-container-apps/02_container_app_env_dapr.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Subnet to host the api config
module "container_apps_dapr_snet" {
count = var.is_resource_enabled.container_app_dapr ? 1 : 0
source = "git::https://github.com/pagopa/terraform-azurerm-v3.git//subnet?ref=v5.3.0"
name = "${local.project}-container-apps-dapr-snet"
address_prefixes = var.cidr_subnet_container_apps_dapr
Expand All @@ -10,10 +11,12 @@ module "container_apps_dapr_snet" {
}

resource "null_resource" "container_app_dapr_create_env" {
count = var.is_resource_enabled.container_app_dapr ? 1 : 0

triggers = {
env_name = local.container_app_dapr_environment_name
rg = azurerm_resource_group.container_app_diego.name
subnet_id = module.container_apps_dapr_snet.id
subnet_id = module.container_apps_dapr_snet[0].id
log_analytics_id = data.azurerm_log_analytics_workspace.log_analytics.workspace_id
log_analytics_workspace_primary_shared_key = data.azurerm_log_analytics_workspace.log_analytics.primary_shared_key
}
Expand All @@ -24,7 +27,7 @@ resource "null_resource" "container_app_dapr_create_env" {
-n ${local.container_app_dapr_environment_name} \
-g ${azurerm_resource_group.container_app_diego.name} \
--location ${var.location} \
--infrastructure-subnet-resource-id ${module.container_apps_dapr_snet.id} \
--infrastructure-subnet-resource-id ${module.container_apps_dapr_snet[0].id} \
--internal-only false \
--logs-destination log-analytics \
--logs-workspace-id "${data.azurerm_log_analytics_workspace.log_analytics.workspace_id}" \
Expand All @@ -43,26 +46,30 @@ EOD
}

depends_on = [
module.container_apps_dapr_snet,
module.container_apps_dapr_snet[0],
azurerm_resource_group.container_app_diego
]
}

locals {
container_app_env_darp_cosmosdb_yaml_content = templatefile("${path.module}/container-app-env/cosmosdb-component-dapr.yaml.tpl", {
COSMOSDB_KEY = azurerm_cosmosdb_account.mongodb.primary_key
COSMOSDB_ENDPOINT = azurerm_cosmosdb_account.mongodb.endpoint
COSMOSDB_KEY = var.is_resource_enabled.mongodb_dapr ? azurerm_cosmosdb_account.mongodb_dapr[0].primary_key : ""
COSMOSDB_ENDPOINT = var.is_resource_enabled.mongodb_dapr ? azurerm_cosmosdb_account.mongodb_dapr[0].endpoint : ""
COSMOSDB_DATABASE = local.cosmosdb_db_name
COSMOSDB_COLLECTION = local.cosmosdb_collection_name
})
}

resource "local_file" "save_yaml_file_cosmosdb_component" {
count = var.is_resource_enabled.container_app_dapr ? 1 : 0

content = local.container_app_env_darp_cosmosdb_yaml_content
filename = local.container_app_dapr_environment_component_cosmosdb
}

resource "null_resource" "container_app_env_darp_cosmosdb_yaml" {
count = var.is_resource_enabled.container_app_dapr ? 1 : 0


triggers = {
CONTENT_FILE = local.container_app_env_darp_cosmosdb_yaml_content
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Subnet to host the api config
module "container_apps_snet" {
count = var.is_resource_enabled.container_app_diego_env ? 1 : 0

source = "git::https://github.com/pagopa/terraform-azurerm-v3.git//subnet?ref=v5.3.0"
name = "${local.project}-container-apps-snet"
address_prefixes = var.cidr_subnet_container_apps
Expand All @@ -12,9 +14,11 @@ module "container_apps_snet" {


resource "azurerm_container_app_environment" "diego_env" {
count = var.is_resource_enabled.container_app_diego_env ? 1 : 0

name = local.container_app_diego_environment_name
location = azurerm_resource_group.container_app_diego.location
resource_group_name = azurerm_resource_group.container_app_diego.name
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.log_analytics.id
infrastructure_subnet_id = module.container_apps_snet.id
infrastructure_subnet_id = module.container_apps_snet[0].id
}
244 changes: 122 additions & 122 deletions src/domains/diego-container-apps/03_container_app_devops_darp.tf
Original file line number Diff line number Diff line change
@@ -1,122 +1,122 @@
data "azurerm_container_app_environment" "dapr_env" {
name = local.container_app_dapr_environment_name
resource_group_name = azurerm_resource_group.container_app_diego.name

depends_on = [
null_resource.container_app_dapr_create_env
]
}

#
# Frontend
#
resource "azurerm_container_app" "frontend" {
name = "frontend-dapr-showcase"
container_app_environment_id = data.azurerm_container_app_environment.dapr_env.id
resource_group_name = azurerm_resource_group.container_app_diego.name
revision_mode = "Single"

template {
min_replicas = 1
max_replicas = 1

container {
name = "frontend"
image = "ghcr.io/pagopa/devops-webapp-python:beta-comosdb-component-dapr"
cpu = 0.5
memory = "1Gi"

liveness_probe {
failure_count_threshold = 10
initial_delay = 10
interval_seconds = 10
path = "/status"
port = 8000
transport = "HTTP"
}

readiness_probe {
failure_count_threshold = 10
interval_seconds = 10
path = "/status"
port = 8000
transport = "HTTP"
}
}
}

ingress {
external_enabled = true
target_port = 8000
traffic_weight {
latest_revision = true
percentage = 100
}
}

dapr {
app_id = "frontend"
app_port = 8000
}

depends_on = [
data.azurerm_container_app_environment.dapr_env
]
}

#
# backend
#
resource "azurerm_container_app" "backend" {
name = "backend-dapr-showcase"
container_app_environment_id = data.azurerm_container_app_environment.dapr_env.id
resource_group_name = azurerm_resource_group.container_app_diego.name
revision_mode = "Single"

template {
min_replicas = 1
max_replicas = 1

container {
name = "backend"
image = "ghcr.io/diegoitaliait/dapr-showcase:beta-enable-dapr"
cpu = 0.5
memory = "1Gi"

liveness_probe {
failure_count_threshold = 10
initial_delay = 10
interval_seconds = 10
path = "/status"
port = 3000
transport = "HTTP"
}

readiness_probe {
failure_count_threshold = 10
interval_seconds = 10
path = "/status"
port = 3000
transport = "HTTP"
}
}
}

ingress {
external_enabled = false
target_port = 3000
traffic_weight {
latest_revision = true
percentage = 100
}
}

dapr {
app_id = "backend"
app_port = 3000
}

depends_on = [
data.azurerm_container_app_environment.dapr_env
]
}
# data "azurerm_container_app_environment" "dapr_env" {
# name = local.container_app_dapr_environment_name
# resource_group_name = azurerm_resource_group.container_app_diego.name

# depends_on = [
# null_resource.container_app_dapr_create_env
# ]
# }

# #
# # Frontend
# #
# resource "azurerm_container_app" "frontend" {
# name = "frontend-dapr-showcase"
# container_app_environment_id = data.azurerm_container_app_environment.dapr_env.id
# resource_group_name = azurerm_resource_group.container_app_diego.name
# revision_mode = "Single"

# template {
# min_replicas = 1
# max_replicas = 1

# container {
# name = "frontend"
# image = "ghcr.io/pagopa/devops-webapp-python:beta-comosdb-component-dapr"
# cpu = 0.5
# memory = "1Gi"

# liveness_probe {
# failure_count_threshold = 10
# initial_delay = 10
# interval_seconds = 10
# path = "/status"
# port = 8000
# transport = "HTTP"
# }

# readiness_probe {
# failure_count_threshold = 10
# interval_seconds = 10
# path = "/status"
# port = 8000
# transport = "HTTP"
# }
# }
# }

# ingress {
# external_enabled = true
# target_port = 8000
# traffic_weight {
# latest_revision = true
# percentage = 100
# }
# }

# dapr {
# app_id = "frontend"
# app_port = 8000
# }

# depends_on = [
# data.azurerm_container_app_environment.dapr_env
# ]
# }

# #
# # backend
# #
# resource "azurerm_container_app" "backend" {
# name = "backend-dapr-showcase"
# container_app_environment_id = data.azurerm_container_app_environment.dapr_env.id
# resource_group_name = azurerm_resource_group.container_app_diego.name
# revision_mode = "Single"

# template {
# min_replicas = 1
# max_replicas = 1

# container {
# name = "backend"
# image = "ghcr.io/diegoitaliait/dapr-showcase:beta-enable-dapr"
# cpu = 0.5
# memory = "1Gi"

# liveness_probe {
# failure_count_threshold = 10
# initial_delay = 10
# interval_seconds = 10
# path = "/status"
# port = 3000
# transport = "HTTP"
# }

# readiness_probe {
# failure_count_threshold = 10
# interval_seconds = 10
# path = "/status"
# port = 3000
# transport = "HTTP"
# }
# }
# }

# ingress {
# external_enabled = false
# target_port = 3000
# traffic_weight {
# latest_revision = true
# percentage = 100
# }
# }

# dapr {
# app_id = "backend"
# app_port = 3000
# }

# depends_on = [
# data.azurerm_container_app_environment.dapr_env
# ]
# }
8 changes: 8 additions & 0 deletions src/domains/diego-container-apps/99_variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ variable "tags" {
}
}

variable "is_resource_enabled" {
type = object({
mongodb_dapr = bool,
container_app_dapr = bool,
container_app_diego_env = bool,
})
}

variable "terraform_remote_state_core" {
type = object({
resource_group_name = string,
Expand Down
Loading

0 comments on commit d7f2ecb

Please sign in to comment.