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

♻️ refactor: use init container for migrations #329

Merged
merged 2 commits into from
Dec 21, 2023

Conversation

larwaa
Copy link
Member

@larwaa larwaa commented Dec 21, 2023

Changes

  • Replace custom migrate container app with init container

Copy link

github-actions bot commented Dec 21, 2023

Planned Changes to Server Infrastructure

Terraform Format and Style 🖌 success

Terraform Initialization ⚙️ success

Terraform Plan 📖 success

Terraform Validation 🤖 success

Show Plan
random_string.resource_code: Refreshing state... [id=3b8qd1il]
module.server.random_password.password: Refreshing state... [id=none]
module.server.random_password.session_secret_value: Refreshing state... [id=none]
github_repository_environment.this: Refreshing state... [id=indok-api:production]
module.server.data.azurerm_key_vault_secret.feide_client_secret: Reading...
module.server.data.azurerm_client_config.current: Reading...
data.azurerm_client_config.current: Reading...
module.server.module.resource_group.azurerm_resource_group.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il]
module.server.data.azurerm_client_config.current: Read complete after 0s [id=Y2xpZW50Q29uZmlncy9jbGllbnRJZD00MmJiMGZiYS0wZmIyLTRjZWItYmRiZi1jNmNjNmFmZTI1MDY7b2JqZWN0SWQ9Y2IxNTc2YmItMjYxOC00NWEwLTgzNjMtMDU3NWQ3ZTdlNDEyO3N1YnNjcmlwdGlvbklkPTBmNmI1YzhlLWNjMTUtNGFmNC1iYTYwLTQwYWE4MzAxOTU1ZTt0ZW5hbnRJZD1mMTdhY2Y1Ni1hYjEzLTQzZmEtYjlkNy1mMGUwZDYyODMyNmM=]
module.server.data.azurerm_key_vault_secret.postmark_api_token: Reading...
data.azurerm_client_config.current: Read complete after 0s [id=Y2xpZW50Q29uZmlncy9jbGllbnRJZD00MmJiMGZiYS0wZmIyLTRjZWItYmRiZi1jNmNjNmFmZTI1MDY7b2JqZWN0SWQ9Y2IxNTc2YmItMjYxOC00NWEwLTgzNjMtMDU3NWQ3ZTdlNDEyO3N1YnNjcmlwdGlvbklkPTBmNmI1YzhlLWNjMTUtNGFmNC1iYTYwLTQwYWE4MzAxOTU1ZTt0ZW5hbnRJZD1mMTdhY2Y1Ni1hYjEzLTQzZmEtYjlkNy1mMGUwZDYyODMyNmM=]
module.server.azurerm_container_app.migrator: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.App/containerApps/migrate-3b8qd1il]
module.server.module.managed_identity.azurerm_user_assigned_identity.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.ManagedIdentity/userAssignedIdentities/managed-identity-3b8qd1il]
module.server.module.container_app_environment.azurerm_log_analytics_workspace.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.OperationalInsights/workspaces/ca-log-workspace-3b8qd1il]
module.server.module.database.azurerm_private_dns_zone.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/privateDnsZones/production-3b8qd1il.postgres.database.azure.com]
module.server.module.vnet.azurerm_virtual_network.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/virtualNetworks/vnet-3b8qd1il]
module.server.module.redis.azurerm_private_dns_zone.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/privateDnsZones/privatelink.redis.cache.windows.net]
module.server.module.redis.azurerm_redis_cache.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Cache/redis/redis3b8qd1il]
module.server.module.database.azurerm_subnet.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/virtualNetworks/vnet-3b8qd1il/subnets/pg-subnet-3b8qd1il]
module.server.module.container_app_environment.azurerm_subnet.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/virtualNetworks/vnet-3b8qd1il/subnets/ca-subnet-3b8qd1il]
module.server.module.redis.azurerm_subnet.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/virtualNetworks/vnet-3b8qd1il/subnets/redis-subnet-3b8qd1il]
module.server.module.database.azurerm_private_dns_zone_virtual_network_link.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/privateDnsZones/production-3b8qd1il.postgres.database.azure.com/virtualNetworkLinks/pg-3b8qd1il.com]
module.server.azurerm_role_assignment.rg_reader: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Authorization/roleAssignments/3c936832-7b1c-77f3-2420-e95122289a6e]
module.server.azurerm_role_assignment.key_vault_user: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Authorization/roleAssignments/d24caaa8-65b4-1d96-c4f6-c069714c52d6]
module.server.module.key_vault.azurerm_key_vault.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.KeyVault/vaults/key-vault-3b8qd1il]
module.server.module.redis.azurerm_private_dns_zone_virtual_network_link.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/privateDnsZones/privatelink.redis.cache.windows.net/virtualNetworkLinks/vnet-private-zone-link]
module.server.module.container_app_environment.azurerm_container_app_environment.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.App/managedEnvironments/ca-env-3b8qd1il]
module.server.module.database.azurerm_postgresql_flexible_server.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.DBforPostgreSQL/flexibleServers/pg-fs-3b8qd1il]
module.server.module.redis.azurerm_private_endpoint.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.Network/privateEndpoints/endpoint-3b8qd1il]
module.server.module.database.azurerm_postgresql_flexible_server_database.this: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.DBforPostgreSQL/flexibleServers/pg-fs-3b8qd1il/databases/pg-fs-db-3b8qd1il]
module.server.azurerm_key_vault_secret.session_secret: Refreshing state... [id=https://key-vault-3b8qd1il.vault.azure.net/secrets/session-secret/886b130b7bad4c7d8e7c8bdcf82d822d]
module.server.azurerm_key_vault_secret.redis_connection_string: Refreshing state... [id=https://key-vault-3b8qd1il.vault.azure.net/secrets/redis-connection-string/d81cf819f622406a9b2c9a2ab4b168ce]
module.server.azurerm_key_vault_secret.db_connection_string: Refreshing state... [id=https://key-vault-3b8qd1il.vault.azure.net/secrets/db-connection-string/fe8162f47d8342528d0aea503fe000eb]
module.server.data.azurerm_key_vault_secret.postmark_api_token: Read complete after 4s [id=https://shared-key-vault-altht.vault.azure.net/secrets/postmark-api-token/e97d2c8704e7419398752b33ece7c9fe]
module.server.data.azurerm_key_vault_secret.feide_client_secret: Read complete after 4s [id=https://shared-key-vault-altht.vault.azure.net/secrets/feide-client-secret/299f08ebe57941dda63f30a792037af0]
module.server.azurerm_container_app.server: Refreshing state... [id=/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.App/containerApps/server-3b8qd1il]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  ~ update in-place
  - destroy

Terraform will perform the following actions:

  # module.server.azurerm_container_app.migrator will be destroyed
  # (because azurerm_container_app.migrator is not in configuration)
  - resource "azurerm_container_app" "migrator" {
      - container_app_environment_id  = "/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.App/managedEnvironments/ca-env-3b8qd1il" -> null
      - custom_domain_verification_id = (sensitive value) -> null
      - id                            = "/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.App/containerApps/migrate-3b8qd1il" -> null
      - latest_revision_name          = "migrate-3b8qd1il--wvlj7et" -> null
      - location                      = "norwayeast" -> null
      - name                          = "migrate-3b8qd1il" -> null
      - outbound_ip_addresses         = [
          - "20.100.54.54",
        ] -> null
      - resource_group_name           = "rg-3b8qd1il" -> null
      - revision_mode                 = "Single" -> null
      - tags                          = {
          - "environment" = "production"
          - "workspace"   = "default"
        } -> null

      - registry {
          - password_secret_name = "docker-registry-password" -> null
          - server               = "ghcr.io" -> null
          - username             = "USERNAME" -> null
        }

      - secret {
          # At least one attribute in this block is (or was) sensitive,
          # so its contents will not be displayed.
        }
      - secret {
          # At least one attribute in this block is (or was) sensitive,
          # so its contents will not be displayed.
        }
      - secret {
          # At least one attribute in this block is (or was) sensitive,
          # so its contents will not be displayed.
        }
      - secret {
          # At least one attribute in this block is (or was) sensitive,
          # so its contents will not be displayed.
        }
      - secret {
          # At least one attribute in this block is (or was) sensitive,
          # so its contents will not be displayed.
        }
      - secret {
          # At least one attribute in this block is (or was) sensitive,
          # so its contents will not be displayed.
        }

      - template {
          - max_replicas = 1 -> null
          - min_replicas = 0 -> null

          - container {
              - args              = [] -> null
              - command           = [
                  - "npm",
                  - "run",
                  - "db:migrate",
                ] -> null
              - cpu               = 0.25 -> null
              - ephemeral_storage = "1Gi" -> null
              - image             = "ghcr.io/rubberdok/server:31cdfbd462db076ad67b09642f046ac1dcb023b9" -> null
              - memory            = "0.5Gi" -> null
              - name              = "migrate" -> null

              - env {
                  - name  = "CORS_ORIGINS" -> null
                  - value = "https://indokntnu.no" -> null
                }
              - env {
                  - name  = "CORS_CREDENTIALS" -> null
                  - value = "true" -> null
                }
              - env {
                  - name  = "NODE_ENV" -> null
                  - value = "production" -> null
                }
              - env {
                  - name  = "NO_REPLY_EMAIL" -> null
                  - value = "[email protected]" -> null
                }
              - env {
                  - name  = "PORT" -> null
                  - value = "4000" -> null
                }
              - env {
                  - name  = "SERVER_URL" -> null
                  - value = "https://server-3b8qd1il.whiteflower-a46872f4.norwayeast.azurecontainerapps.io" -> null
                }
              - env {
                  - name  = "FEIDE_CLIENT_ID" -> null
                  - value = "fcaa9e30-a6d3-4809-8fea-cdd7b3de1c98" -> null
                }
              - env {
                  - name  = "FEIDE_BASE_URL" -> null
                  - value = "https://auth.dataporten.no" -> null
                }
              - env {
                  - name  = "SESSION_COOKIE_NAME" -> null
                  - value = "sessionid" -> null
                }
              - env {
                  - name  = "SESSION_COOKIE_DOMAIN" -> null
                  - value = "server-3b8qd1il.whiteflower-a46872f4.norwayeast.azurecontainerapps.io" -> null
                }
              - env {
                  - name  = "SESSION_COOKIE_HTTP_ONLY" -> null
                  - value = "true" -> null
                }
              - env {
                  - name  = "SESSION_COOKIE_SECURE" -> null
                  - value = "true" -> null
                }
              - env {
                  - name  = "SENTRY_DSN" -> null
                  - value = "https://[email protected]/6553834" -> null
                }
              - env {
                  - name  = "RATE_LIMIT_MAX" -> null
                  - value = "1000" -> null
                }
              - env {
                  - name  = "SENTRY_RELEASE" -> null
                  - value = "31cdfbd462db076ad67b09642f046ac1dcb023b9" -> null
                }
              - env {
                  - name        = "REDIS_CONNECTION_STRING" -> null
                  - secret_name = "redis-connection-string" -> null
                }
              - env {
                  - name        = "DATABASE_CONNECTION_STRING" -> null
                  - secret_name = "database-connection-string" -> null
                }
              - env {
                  - name        = "FEIDE_CLIENT_SECRET" -> null
                  - secret_name = "feide-client-secret" -> null
                }
              - env {
                  - name        = "SESSION_SECRET" -> null
                  - secret_name = "session-secret" -> null
                }
              - env {
                  - name        = "POSTMARK_API_TOKEN" -> null
                  - secret_name = "postmark-api-token" -> null
                }
            }
        }
    }

  # module.server.azurerm_container_app.server will be updated in-place
  ~ resource "azurerm_container_app" "server" {
        id                            = "/subscriptions/0f6b5c8e-cc15-4af4-ba60-40aa8301955e/resourceGroups/rg-3b8qd1il/providers/Microsoft.App/containerApps/server-3b8qd1il"
        name                          = "server-3b8qd1il"
        tags                          = {
            "environment" = "production"
            "workspace"   = "default"
        }
        # (8 unchanged attributes hidden)

      ~ template {
            # (2 unchanged attributes hidden)

          ~ container {
              ~ image             = "ghcr.io/rubberdok/server:31cdfbd462db076ad67b09642f046ac1dcb023b9" -> "ghcr.io/rubberdok/server:latest"
                name              = "server"
                # (5 unchanged attributes hidden)

              ~ env {
                    name  = "SENTRY_RELEASE"
                  ~ value = "31cdfbd462db076ad67b09642f046ac1dcb023b9" -> "0bb49e9c6e137d318234ebc291855168d2714893"
                }

              + liveness_probe {
                  + failure_count_threshold = 3
                  + initial_delay           = 5
                  + interval_seconds        = 10
                  + path                    = "/-/health"
                  + port                    = 4000
                  + timeout                 = 1
                  + transport               = "HTTP"
                }

              + readiness_probe {
                  + failure_count_threshold = 3
                  + interval_seconds        = 10
                  + path                    = "/graphql?query=%7B__typename%7D"
                  + port                    = 4000
                  + success_count_threshold = 3
                  + timeout                 = 1
                  + transport               = "HTTP"

                  + header {
                      + name  = "apollo-require-preflight"
                      + value = "true"
                    }
                }

              ~ startup_probe {
                  ~ path                             = "/-/migration-health" -> "/-/health"
                    # (6 unchanged attributes hidden)
                }

                # (20 unchanged blocks hidden)
            }

          + init_container {
              + command = [
                  + "pnpm",
                  + "run",
                  + "db:migrate",
                ]
              + cpu     = 0.25
              + image   = "ghcr.io/rubberdok/server:latest"
              + memory  = "0.5Gi"
              + name    = "migrate"

              + env {
                  + name  = "CORS_ORIGINS"
                  + value = "https://indokntnu.no"
                }
              + env {
                  + name  = "CORS_CREDENTIALS"
                  + value = "true"
                }
              + env {
                  + name  = "NODE_ENV"
                  + value = "production"
                }
              + env {
                  + name  = "NO_REPLY_EMAIL"
                  + value = "[email protected]"
                }
              + env {
                  + name  = "PORT"
                  + value = "4000"
                }
              + env {
                  + name  = "SERVER_URL"
                  + value = "https://server-3b8qd1il.whiteflower-a46872f4.norwayeast.azurecontainerapps.io"
                }
              + env {
                  + name  = "FEIDE_CLIENT_ID"
                  + value = "fcaa9e30-a6d3-4809-8fea-cdd7b3de1c98"
                }
              + env {
                  + name  = "FEIDE_BASE_URL"
                  + value = "https://auth.dataporten.no"
                }
              + env {
                  + name  = "SESSION_COOKIE_NAME"
                  + value = "sessionid"
                }
              + env {
                  + name  = "SESSION_COOKIE_DOMAIN"
                  + value = "server-3b8qd1il.whiteflower-a46872f4.norwayeast.azurecontainerapps.io"
                }
              + env {
                  + name  = "SESSION_COOKIE_HTTP_ONLY"
                  + value = "true"
                }
              + env {
                  + name  = "SESSION_COOKIE_SECURE"
                  + value = "true"
                }
              + env {
                  + name  = "SENTRY_DSN"
                  + value = "https://[email protected]/6553834"
                }
              + env {
                  + name  = "RATE_LIMIT_MAX"
                  + value = "1000"
                }
              + env {
                  + name  = "SENTRY_RELEASE"
                  + value = "0bb49e9c6e137d318234ebc291855168d2714893"
                }
              + env {
                  + name        = "REDIS_CONNECTION_STRING"
                  + secret_name = "redis-connection-string"
                }
              + env {
                  + name        = "DATABASE_CONNECTION_STRING"
                  + secret_name = "database-connection-string"
                }
              + env {
                  + name        = "FEIDE_CLIENT_SECRET"
                  + secret_name = "feide-client-secret"
                }
              + env {
                  + name        = "SESSION_SECRET"
                  + secret_name = "session-secret"
                }
              + env {
                  + name        = "POSTMARK_API_TOKEN"
                  + secret_name = "postmark-api-token"
                }
            }
        }

        # (8 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 1 to destroy.

─────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.

Pushed by: @larwaa, Action: pull_request

Copy link

sonarcloud bot commented Dec 21, 2023

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

codecov bot commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b46e5f3) 94.86% compared to head (4aa2862) 83.62%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #329       +/-   ##
===========================================
- Coverage   94.86%   83.62%   -11.24%     
===========================================
  Files         118      118               
  Lines        6596     6596               
  Branches      513      497       -16     
===========================================
- Hits         6257     5516      -741     
- Misses        326     1067      +741     
  Partials       13       13               
Flag Coverage Δ
integrationtests 65.46% <ø> (-12.21%) ⬇️
node 83.62% <ø> (-11.24%) ⬇️
unittests 48.99% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@larwaa larwaa merged commit 787f807 into main Dec 21, 2023
17 of 18 checks passed
@larwaa larwaa deleted the infrastructure/init-container branch December 21, 2023 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant