diff --git a/.github/workflows/test_terraform_webhook_file.yaml b/.github/workflows/test_terraform_webhook_file.yaml new file mode 100644 index 00000000..17beb178 --- /dev/null +++ b/.github/workflows/test_terraform_webhook_file.yaml @@ -0,0 +1,41 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +name: Terraform files test (webhook-gateway) + +on: + pull_request: + paths: + - "**/terraform/**" + +permissions: + contents: read + +jobs: + validate: + name: Validate terraform configuration files + runs-on: ubuntu-latest + env: + WORKING_DIR: "webhook-gateway/charm/terraform" + steps: + - name: Check out code + uses: actions/checkout@v5.0.0 + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v3.1.2 + + - name: Run terraform fmt + run: terraform fmt -check -recursive + working-directory: ${{env.WORKING_DIR}} + + - name: Setup Tflint + uses: terraform-linters/setup-tflint@v6.2.0 + with: + tflint_wrapper_enabled: true + + - name: Run tflint + run: | + tflint --version + tflint --init + tflint -f compact --recursive + working-directory: ${{env.WORKING_DIR}} diff --git a/.github/workflows/test_terraform_webhook_module.yaml b/.github/workflows/test_terraform_webhook_module.yaml new file mode 100644 index 00000000..49365756 --- /dev/null +++ b/.github/workflows/test_terraform_webhook_module.yaml @@ -0,0 +1,57 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +name: Terraform module tests (webhook-gateway) + +on: + pull_request: + paths: + - "**/terraform/**" + +permissions: + contents: read + +jobs: + test-terraform: + name: Test Terraform with Juju + runs-on: ubuntu-latest + env: + WORKING_DIR: "webhook-gateway/charm/terraform/tests" + steps: + - uses: actions/checkout@v5.0.0 + - uses: charmed-kubernetes/actions-operator@main + with: + provider: "microk8s" + channel: 1.33-strict/stable + juju-channel: 3.6/stable + + - name: Prepare juju tf provider environment + run: | + set -e + CONTROLLER=$(juju whoami | yq .Controller) + JUJU_CONTROLLER_ADDRESSES="$(juju show-controller | yq '.[$CONTROLLER]'.details.\"api-endpoints\" | tr -d "[]' "|tr -d '"'|tr -d '\n')" + JUJU_USERNAME="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.user|tr -d '"')" + JUJU_PASSWORD="$(cat ~/.local/share/juju/accounts.yaml | yq .controllers.$CONTROLLER.password|tr -d '"')" + + echo "JUJU_CONTROLLER_ADDRESSES=$JUJU_CONTROLLER_ADDRESSES" >> "$GITHUB_ENV" + echo "JUJU_USERNAME=$JUJU_USERNAME" >> "$GITHUB_ENV" + echo "JUJU_PASSWORD=$JUJU_PASSWORD" >> "$GITHUB_ENV" + { + echo 'JUJU_CA_CERT<> "$GITHUB_ENV" + - uses: hashicorp/setup-terraform@v3.1.2 + - run: terraform init + working-directory: ${{env.WORKING_DIR}} + - run: terraform validate + working-directory: ${{env.WORKING_DIR}} + - run: terraform plan -out=tfplan + working-directory: ${{env.WORKING_DIR}} + - run: terraform show tfplan + working-directory: ${{env.WORKING_DIR}} + - run: | + juju add-model test-deploy-webhook-gateway + set -e # Exit on error + terraform test || { echo "Terraform test failed"; exit 1; } + working-directory: ${{env.WORKING_DIR}} diff --git a/webhook-gateway/charm/terraform/.tflint.hcl b/webhook-gateway/charm/terraform/.tflint.hcl new file mode 100644 index 00000000..01f1384f --- /dev/null +++ b/webhook-gateway/charm/terraform/.tflint.hcl @@ -0,0 +1,6 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +rule "terraform_required_version" { + enabled = false +} diff --git a/webhook-gateway/charm/terraform/README.md b/webhook-gateway/charm/terraform/README.md new file mode 100644 index 00000000..31d577a2 --- /dev/null +++ b/webhook-gateway/charm/terraform/README.md @@ -0,0 +1,56 @@ +# github_runner_webhook_gateway Terraform module + +This folder contains a base [Terraform][Terraform] module for the github_runner_webhook_gateway charm. + +The module uses the [Terraform Juju provider][Terraform Juju provider] to model the charm +deployment onto any Kubernetes environment managed by [Juju][Juju]. + +## Module structure + +- **main.tf** - Defines the Juju application to be deployed. +- **variables.tf** - Allows customization of the deployment. Also models the charm configuration, + except for exposing the deployment options (Juju model name, channel or application name). +- **output.tf** - Integrates the module with other Terraform modules, primarily + by defining potential integration endpoints (charm integrations), but also by exposing + the Juju application name. +- **versions.tf** - Defines the Terraform provider version. + +## Using github_runner_webhook_gateway base module in higher level modules + +If you want to use `github_runner_webhook_gateway` base module as part of your Terraform module, import it +like shown below: + +```text +data "juju_model" "my_model" { + name = var.model +} + +module "github_runner_webhook_gateway" { + source = "git::https://github.com/canonical/github-runner-operators/tree/main/webhook-gateway/charm/terraform" + + model = juju_model.my_model.name + # (Customize configuration variables here if needed) +} +``` + +Create integrations, for instance: + +```text +resource "juju_integration" "webhook_rabbitmq" { + model = juju_model.my_model.name + application { + name = module.github_runner_webhook_gateway.app_name + endpoint = module.github_runner_webhook_gateway.endpoints.logging + } + application { + name = "rabbitmq" + } +} +``` + +The complete list of available integrations can be found [in the Integrations tab][github_runner_webhook_gateway-integrations]. + +[Terraform]: https://developer.hashicorp.com/terraform +[Terraform Juju provider]: https://registry.terraform.io/providers/juju/juju/latest +[Juju]: https://juju.is +[github_runner_webhook_gateway-integrations]: https://charmhub.io/github_runner_webhook_gateway/integrations diff --git a/webhook-gateway/charm/terraform/main.tf b/webhook-gateway/charm/terraform/main.tf new file mode 100644 index 00000000..14f394bd --- /dev/null +++ b/webhook-gateway/charm/terraform/main.tf @@ -0,0 +1,19 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +resource "juju_application" "github_runner_webhook_gateway" { + name = var.app_name + model = var.model + + charm { + name = "github-runner-webhook-gateway" + channel = var.channel + revision = var.revision + base = var.base + } + + config = var.config + constraints = var.constraints + units = var.units + storage_directives = var.storage +} diff --git a/webhook-gateway/charm/terraform/outputs.tf b/webhook-gateway/charm/terraform/outputs.tf new file mode 100644 index 00000000..876201e6 --- /dev/null +++ b/webhook-gateway/charm/terraform/outputs.tf @@ -0,0 +1,13 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +output "app_name" { + description = "Name of the deployed application." + value = juju_application.github_runner_webhook_gateway.name +} + +output "endpoints" { + value = { + rabbitmq = "rabbitmq" + } +} diff --git a/webhook-gateway/charm/terraform/tests/.tflint.hcl b/webhook-gateway/charm/terraform/tests/.tflint.hcl new file mode 100644 index 00000000..01f1384f --- /dev/null +++ b/webhook-gateway/charm/terraform/tests/.tflint.hcl @@ -0,0 +1,6 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +rule "terraform_required_version" { + enabled = false +} diff --git a/webhook-gateway/charm/terraform/tests/main.tf b/webhook-gateway/charm/terraform/tests/main.tf new file mode 100644 index 00000000..06038b98 --- /dev/null +++ b/webhook-gateway/charm/terraform/tests/main.tf @@ -0,0 +1,77 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +locals { + webhook_gateway_app_name = "github-runner-webhook-gateway" + juju_model_name = "test-deploy-webhook-gateway" +} + +variable "channel" { + description = "The channel to use when deploying a charm." + type = string + default = "latest/edge" +} + +variable "revision" { + description = "Revision number of the charm." + type = number + default = null +} + +terraform { + required_version = ">= 1.6.6" + required_providers { + juju = { + source = "juju/juju" + version = ">= 0.23.0" + } + } +} + +resource "juju_application" "rabbitmq" { + name = "rabbitmq" + model = local.juju_model_name + + charm { + name = "rabbitmq-k8s" + channel = "3.12/stable" + } + + trust = true + config = {} + constraints = "" + units = 1 +} + +resource "juju_integration" "webhook_rabbitmq" { + model = local.juju_model_name + application { + name = local.webhook_gateway_app_name + } + application { + name = juju_application.rabbitmq.name + } +} + +provider "juju" {} + +module "github_runner_webhook_gateway" { + source = "./.." + app_name = local.webhook_gateway_app_name + channel = var.channel + model = local.juju_model_name + revision = var.revision + config = { + metrics-port = 9464 + } +} + +output "app_name" { + description = "The name of the deployed github_runner_webhook_gateway charm application." + value = module.github_runner_webhook_gateway.app_name +} + +output "endpoints" { + description = "Integration endpoints exposed by github_runner_webhook_gateway charm." + value = module.github_runner_webhook_gateway.endpoints +} diff --git a/webhook-gateway/charm/terraform/tests/main.tftest.hcl b/webhook-gateway/charm/terraform/tests/main.tftest.hcl new file mode 100644 index 00000000..a05a63d5 --- /dev/null +++ b/webhook-gateway/charm/terraform/tests/main.tftest.hcl @@ -0,0 +1,15 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +variables { + channel = "latest/edge" + # renovate: depName="github_runner_webhook_gateway" + revision = 1 +} + +run "basic_deploy" { + assert { + condition = module.github_runner_webhook_gateway.app_name == "github-runner-webhook-gateway" + error_message = "github-runner-webhook-gateway app_name did not match expected" + } +} diff --git a/webhook-gateway/charm/terraform/variables.tf b/webhook-gateway/charm/terraform/variables.tf new file mode 100644 index 00000000..2ca29802 --- /dev/null +++ b/webhook-gateway/charm/terraform/variables.tf @@ -0,0 +1,55 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +variable "app_name" { + description = "Name of the application in the Juju model." + type = string + default = "github-runner-webhook-gateway" +} + +variable "base" { + description = "The operating system on which to deploy" + type = string + default = "ubuntu@24.04" +} + +variable "channel" { + description = "The channel to use when deploying a charm." + type = string + default = "latest/edge" +} + +variable "config" { + description = "Application config. Details about available options can be found at https://charmhub.io/github-runner-webhook-gateway/configurations." + type = map(string) + default = {} +} + +variable "constraints" { + description = "Juju constraints to apply for this application." + type = string + default = "" +} + +variable "model" { + description = "Reference to a `juju_model`." + type = string +} + +variable "revision" { + description = "Revision number of the charm" + type = number + default = null +} + +variable "storage" { + description = "Map of storage used by the application." + type = map(string) + default = {} +} + +variable "units" { + description = "Number of units to deploy" + type = number + default = 1 +} diff --git a/webhook-gateway/charm/terraform/versions.tf b/webhook-gateway/charm/terraform/versions.tf new file mode 100644 index 00000000..212fa91e --- /dev/null +++ b/webhook-gateway/charm/terraform/versions.tf @@ -0,0 +1,12 @@ +# Copyright 2025 Canonical Ltd. +# See LICENSE file for licensing details. + +terraform { + required_version = ">= 1.6.6" + required_providers { + juju = { + source = "juju/juju" + version = ">= 0.23.0" + } + } +}