From 8099ea6dc92fe87ffaa8a1d074bcee5f31e9d42c Mon Sep 17 00:00:00 2001 From: Jared Holgate Date: Sun, 4 Aug 2024 13:56:30 +0100 Subject: [PATCH] Replace cron job and add placeholder agent name (#48) * Replace cron job and add plkaceholder agent name * Add depends_on in examples for safety --- README.md | 17 ++++++------ .../azure_devops_container_instance/README.md | 1 + .../azure_devops_container_instance/main.tf | 1 + .../azure_devops_public_networking/README.md | 1 + .../azure_devops_public_networking/main.tf | 1 + examples/default/README.md | 1 + examples/default/main.tf | 1 + examples/github_basic/README.md | 1 + examples/github_basic/main.tf | 1 + examples/github_container_instance/README.md | 1 + examples/github_container_instance/main.tf | 1 + examples/github_public_networking/README.md | 1 + examples/github_public_networking/main.tf | 1 + examples/multi_region/README.md | 2 ++ examples/multi_region/main.tf | 2 ++ locals.container.app.job.tf | 8 +++--- locals.tf | 1 + main.container.app.job.tf | 19 +------------ modules/container-app-job/README.md | 27 +++++++++++-------- modules/container-app-job/main.tf | 18 ++++++++++--- .../variables.placeholder.tf | 6 ----- variables.container.app.tf | 6 ----- variables.version.control.system.tf | 6 +++++ 23 files changed, 67 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 669dbb6..e00f258 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,6 @@ The following resources are used by this module: - [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) (resource) - [modtm_telemetry.telemetry](https://registry.terraform.io/providers/azure/modtm/latest/docs/resources/telemetry) (resource) - [random_uuid.telemetry](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/uuid) (resource) -- [time_offset.placeholder_job](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/offset) (resource) - [time_sleep.delay_after_container_image_build](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) (resource) - [azurerm_client_config.current](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config) (data source) - [azurerm_client_config.telemetry](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config) (data source) @@ -261,14 +260,6 @@ Type: `number` Default: `300` -### [container\_app\_placeholder\_schedule\_offset\_minutes](#input\_container\_app\_placeholder\_schedule\_offset\_minutes) - -Description: The offset in minutes for the placeholder job. - -Type: `number` - -Default: `5` - ### [container\_app\_polling\_interval\_seconds](#input\_container\_app\_polling\_interval\_seconds) Description: How often should the pipeline queue be checked for new events, in seconds. @@ -853,6 +844,14 @@ Type: `string` Default: `null` +### [version\_control\_system\_placeholder\_agent\_name](#input\_version\_control\_system\_placeholder\_agent\_name) + +Description: The version control system placeholder agent name. + +Type: `string` + +Default: `null` + ### [version\_control\_system\_pool\_name](#input\_version\_control\_system\_pool\_name) Description: The name of the agent pool in the version control system. diff --git a/examples/azure_devops_container_instance/README.md b/examples/azure_devops_container_instance/README.md index 06d44ec..ee12cc6 100644 --- a/examples/azure_devops_container_instance/README.md +++ b/examples/azure_devops_container_instance/README.md @@ -146,6 +146,7 @@ module "azure_devops_agents" { version_control_system_pool_name = azuredevops_agent_pool.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "container_instance_resource_ids" { diff --git a/examples/azure_devops_container_instance/main.tf b/examples/azure_devops_container_instance/main.tf index 17c14ee..538b9ba 100644 --- a/examples/azure_devops_container_instance/main.tf +++ b/examples/azure_devops_container_instance/main.tf @@ -140,6 +140,7 @@ module "azure_devops_agents" { version_control_system_pool_name = azuredevops_agent_pool.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "container_instance_resource_ids" { diff --git a/examples/azure_devops_public_networking/README.md b/examples/azure_devops_public_networking/README.md index 6bbc936..0057b98 100644 --- a/examples/azure_devops_public_networking/README.md +++ b/examples/azure_devops_public_networking/README.md @@ -145,6 +145,7 @@ module "azure_devops_agents" { version_control_system_pool_name = azuredevops_agent_pool.this.name use_private_networking = false tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "container_app_environment_resource_id" { diff --git a/examples/azure_devops_public_networking/main.tf b/examples/azure_devops_public_networking/main.tf index 160faaf..b08f248 100644 --- a/examples/azure_devops_public_networking/main.tf +++ b/examples/azure_devops_public_networking/main.tf @@ -139,6 +139,7 @@ module "azure_devops_agents" { version_control_system_pool_name = azuredevops_agent_pool.this.name use_private_networking = false tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "container_app_environment_resource_id" { diff --git a/examples/default/README.md b/examples/default/README.md index b6f7c48..8b72a7f 100644 --- a/examples/default/README.md +++ b/examples/default/README.md @@ -145,6 +145,7 @@ module "azure_devops_agents" { version_control_system_pool_name = azuredevops_agent_pool.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "container_app_environment_resource_id" { diff --git a/examples/default/main.tf b/examples/default/main.tf index ad5306d..a7b4a3a 100644 --- a/examples/default/main.tf +++ b/examples/default/main.tf @@ -139,6 +139,7 @@ module "azure_devops_agents" { version_control_system_pool_name = azuredevops_agent_pool.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "container_app_environment_resource_id" { diff --git a/examples/github_basic/README.md b/examples/github_basic/README.md index e4f84b8..311fd78 100644 --- a/examples/github_basic/README.md +++ b/examples/github_basic/README.md @@ -108,6 +108,7 @@ module "github_runners" { version_control_system_repository = github_repository.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [github_repository_file.this] } # Region helpers diff --git a/examples/github_basic/main.tf b/examples/github_basic/main.tf index fa3dcfb..8082363 100644 --- a/examples/github_basic/main.tf +++ b/examples/github_basic/main.tf @@ -102,6 +102,7 @@ module "github_runners" { version_control_system_repository = github_repository.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [github_repository_file.this] } # Region helpers diff --git a/examples/github_container_instance/README.md b/examples/github_container_instance/README.md index 5896026..2149682 100644 --- a/examples/github_container_instance/README.md +++ b/examples/github_container_instance/README.md @@ -109,6 +109,7 @@ module "github_runners" { version_control_system_repository = github_repository.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [github_repository_file.this] } # Region helpers diff --git a/examples/github_container_instance/main.tf b/examples/github_container_instance/main.tf index 24a883e..4bf08cc 100644 --- a/examples/github_container_instance/main.tf +++ b/examples/github_container_instance/main.tf @@ -103,6 +103,7 @@ module "github_runners" { version_control_system_repository = github_repository.this.name virtual_network_address_space = "10.0.0.0/16" tags = local.tags + depends_on = [github_repository_file.this] } # Region helpers diff --git a/examples/github_public_networking/README.md b/examples/github_public_networking/README.md index af77819..7b573bd 100644 --- a/examples/github_public_networking/README.md +++ b/examples/github_public_networking/README.md @@ -108,6 +108,7 @@ module "github_runners" { version_control_system_repository = github_repository.this.name use_private_networking = false tags = local.tags + depends_on = [github_repository_file.this] } # Region helpers diff --git a/examples/github_public_networking/main.tf b/examples/github_public_networking/main.tf index 962f9fd..5c96894 100644 --- a/examples/github_public_networking/main.tf +++ b/examples/github_public_networking/main.tf @@ -102,6 +102,7 @@ module "github_runners" { version_control_system_repository = github_repository.this.name use_private_networking = false tags = local.tags + depends_on = [github_repository_file.this] } # Region helpers diff --git a/examples/multi_region/README.md b/examples/multi_region/README.md index 22ca746..926e0fe 100644 --- a/examples/multi_region/README.md +++ b/examples/multi_region/README.md @@ -153,6 +153,7 @@ module "azure_devops_agents_primary" { virtual_network_address_space = "10.0.0.0/16" container_app_polling_interval_seconds = local.primary_polling_interval_prime_number tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } module "azure_devops_agents_secondary" { @@ -166,6 +167,7 @@ module "azure_devops_agents_secondary" { virtual_network_address_space = "10.1.0.0/16" container_app_polling_interval_seconds = local.secondary_polling_interval_prime_number tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "primary_region" { diff --git a/examples/multi_region/main.tf b/examples/multi_region/main.tf index 56a13db..bfafb8f 100644 --- a/examples/multi_region/main.tf +++ b/examples/multi_region/main.tf @@ -145,6 +145,7 @@ module "azure_devops_agents_primary" { virtual_network_address_space = "10.0.0.0/16" container_app_polling_interval_seconds = local.primary_polling_interval_prime_number tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } module "azure_devops_agents_secondary" { @@ -158,6 +159,7 @@ module "azure_devops_agents_secondary" { virtual_network_address_space = "10.1.0.0/16" container_app_polling_interval_seconds = local.secondary_polling_interval_prime_number tags = local.tags + depends_on = [azuredevops_pipeline_authorization.this] } output "primary_region" { diff --git a/locals.container.app.job.tf b/locals.container.app.job.tf index 6b8ecd9..0ef2225 100644 --- a/locals.container.app.job.tf +++ b/locals.container.app.job.tf @@ -61,6 +61,10 @@ locals { locals { environment_variables_placeholder = tolist(jsondecode(local.environment_variables_placeholder_final)) environment_variables_placeholder_azure_devops = [ + { + name = "AZP_AGENT_NAME" + value = local.version_control_system_placeholder_agent_name + }, { name = "AZP_PLACEHOLDER" value = "true" @@ -96,7 +100,3 @@ locals { } ] } - -locals { - cron_expression = local.deploy_container_app ? "${time_offset.placeholder_job[0].minute} ${time_offset.placeholder_job[0].hour} ${time_offset.placeholder_job[0].day} ${time_offset.placeholder_job[0].month} *" : "" -} diff --git a/locals.tf b/locals.tf index b1de33b..b79a7fd 100644 --- a/locals.tf +++ b/locals.tf @@ -29,6 +29,7 @@ locals { user_assigned_managed_identity_id = var.user_assigned_managed_identity_id != null ? var.user_assigned_managed_identity_id : module.user_assigned_managed_identity[0].resource_id user_assigned_managed_identity_name = var.user_assigned_managed_identity_name != null ? var.user_assigned_managed_identity_name : "uami-${var.postfix}" version_control_system_agent_name_prefix = var.version_control_system_agent_name_prefix != null ? var.version_control_system_agent_name_prefix : (var.version_control_system_type == local.version_control_system_azure_devops ? "agent-${var.postfix}" : "runner-${var.postfix}") + version_control_system_placeholder_agent_name = var.version_control_system_placeholder_agent_name != null ? var.version_control_system_placeholder_agent_name : "placeholder-${var.postfix}" virtual_network_name = var.virtual_network_name != null ? var.virtual_network_name : "vnet-${var.postfix}" } diff --git a/main.container.app.job.tf b/main.container.app.job.tf index 4af3fcf..a1a26a1 100644 --- a/main.container.app.job.tf +++ b/main.container.app.job.tf @@ -1,19 +1,3 @@ -resource "time_offset" "placeholder_job" { - count = local.deploy_container_app ? 1 : 0 - - offset_minutes = var.container_app_placeholder_schedule_offset_minutes - triggers = { - container_image = local.container_images["container_app"].image_names[0] - offset_minutes = var.container_app_placeholder_schedule_offset_minutes - environment_variables = jsonencode(local.environment_variables) - sensitive_environment_variables = jsonencode(local.sensitive_environment_variables) - environment_variables_placeholder = jsonencode(local.environment_variables_placeholder) - image_version = var.default_image_repository_commit - } - - depends_on = [module.container_registry, azurerm_container_app_environment.this, time_sleep.delay_after_container_image_build] -} - module "container_app_job" { count = local.deploy_container_app ? 1 : 0 source = "./modules/container-app-job" @@ -45,7 +29,6 @@ module "container_app_job" { placeholder_container_name = var.container_app_placeholder_container_name placeholder_replica_retry_limit = var.container_app_placeholder_replica_retry_limit placeholder_replica_timeout = var.container_app_placeholder_replica_timeout - placeholder_cron_expression = local.cron_expression polling_interval_seconds = var.container_app_polling_interval_seconds container_cpu = var.container_app_container_cpu @@ -57,4 +40,4 @@ module "container_app_job" { user_assigned_managed_identity_id = local.user_assigned_managed_identity_id depends_on = [module.container_registry, azurerm_private_dns_zone_virtual_network_link.container_registry, time_sleep.delay_after_container_image_build] -} \ No newline at end of file +} diff --git a/modules/container-app-job/README.md b/modules/container-app-job/README.md index de8272d..e248307 100644 --- a/modules/container-app-job/README.md +++ b/modules/container-app-job/README.md @@ -53,13 +53,12 @@ resource "azapi_resource" "placeholder" { replicaRetryLimit = var.placeholder_replica_retry_limit replicaTimeout = var.placeholder_replica_timeout registries = local.container_registies - scheduleTriggerConfig = { - cronExpression = var.placeholder_cron_expression + manualTriggerConfig = { parallelism = 1 replicaCompletionCount = 1 } secrets = local.secrets - triggerType = "Schedule" + triggerType = "Manual" } template = { containers = [local.container_placeholder] @@ -76,6 +75,19 @@ resource "azapi_resource" "placeholder" { identity_ids = [var.user_assigned_managed_identity_id] } } + +resource "azapi_resource_action" "placeholder_trigger" { + count = var.placeholder_job_creation_enabled ? 1 : 0 + + resource_id = azapi_resource.placeholder[0].id + type = "Microsoft.App/jobs@2024-03-01" + action = "start" + body = {} + + lifecycle { + replace_triggered_by = [azapi_resource.placeholder] + } +} ``` @@ -99,6 +111,7 @@ The following resources are used by this module: - [azapi_resource.job](https://registry.terraform.io/providers/azure/azapi/latest/docs/resources/resource) (resource) - [azapi_resource.placeholder](https://registry.terraform.io/providers/azure/azapi/latest/docs/resources/resource) (resource) +- [azapi_resource_action.placeholder_trigger](https://registry.terraform.io/providers/azure/azapi/latest/docs/resources/resource_action) (resource) ## Required Inputs @@ -268,14 +281,6 @@ Type: `string` Default: `null` -### [placeholder\_cron\_expression](#input\_placeholder\_cron\_expression) - -Description: The cron expression for the placeholder Container Apps job. - -Type: `string` - -Default: `null` - ### [placeholder\_job\_creation\_enabled](#input\_placeholder\_job\_creation\_enabled) Description: Whether or not to create a placeholder job. diff --git a/modules/container-app-job/main.tf b/modules/container-app-job/main.tf index b5a896c..5072398 100644 --- a/modules/container-app-job/main.tf +++ b/modules/container-app-job/main.tf @@ -47,13 +47,12 @@ resource "azapi_resource" "placeholder" { replicaRetryLimit = var.placeholder_replica_retry_limit replicaTimeout = var.placeholder_replica_timeout registries = local.container_registies - scheduleTriggerConfig = { - cronExpression = var.placeholder_cron_expression + manualTriggerConfig = { parallelism = 1 replicaCompletionCount = 1 } secrets = local.secrets - triggerType = "Schedule" + triggerType = "Manual" } template = { containers = [local.container_placeholder] @@ -70,3 +69,16 @@ resource "azapi_resource" "placeholder" { identity_ids = [var.user_assigned_managed_identity_id] } } + +resource "azapi_resource_action" "placeholder_trigger" { + count = var.placeholder_job_creation_enabled ? 1 : 0 + + resource_id = azapi_resource.placeholder[0].id + type = "Microsoft.App/jobs@2024-03-01" + action = "start" + body = {} + + lifecycle { + replace_triggered_by = [azapi_resource.placeholder] + } +} diff --git a/modules/container-app-job/variables.placeholder.tf b/modules/container-app-job/variables.placeholder.tf index 461db46..876128a 100644 --- a/modules/container-app-job/variables.placeholder.tf +++ b/modules/container-app-job/variables.placeholder.tf @@ -27,9 +27,3 @@ variable "placeholder_replica_timeout" { description = "The timeout in seconds for the placeholder Container Apps job." default = 300 } - -variable "placeholder_cron_expression" { - type = string - description = "The cron expression for the placeholder Container Apps job." - default = null -} diff --git a/variables.container.app.tf b/variables.container.app.tf index 95e898d..da058b6 100644 --- a/variables.container.app.tf +++ b/variables.container.app.tf @@ -120,9 +120,3 @@ variable "container_app_infrastructure_resource_group_name" { default = null description = "The name of the resource group where the Container Apps infrastructure is deployed." } - -variable "container_app_placeholder_schedule_offset_minutes" { - type = number - default = 5 - description = "The offset in minutes for the placeholder job." -} diff --git a/variables.version.control.system.tf b/variables.version.control.system.tf index 212642d..3026419 100644 --- a/variables.version.control.system.tf +++ b/variables.version.control.system.tf @@ -26,6 +26,12 @@ variable "version_control_system_agent_name_prefix" { description = "The version control system agent name prefix." } +variable "version_control_system_placeholder_agent_name" { + type = string + default = null + description = "The version control system placeholder agent name." +} + variable "version_control_system_agent_target_queue_length" { type = number default = 1