From 7fc5b7fb164821923bda84155a9ac4f9c5056510 Mon Sep 17 00:00:00 2001 From: Manos Vlassis <57320708+mvlassis@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:47:40 +0300 Subject: [PATCH] Feat: Add Terraform module (#64) * Add Terraform module * Update format * Don't run terraform-checks since the charm is missing CRDs on its own * Update terraform/README.md Co-authored-by: Noha Ihab <49988746+NohaIhab@users.noreply.github.com> * Update format for empty map * Add note on why we are skipping the Terraform apply test * Update README.md * Update README.md * Remove channel from terraform action * Update default Terraform channel * Update default Terraform channel * Change the version of the setup operator environment * Change setup operator action tag --------- Co-authored-by: Noha Ihab <49988746+NohaIhab@users.noreply.github.com> --- .github/workflows/integrate.yaml | 9 +++++ .gitignore | 3 ++ terraform/README.md | 60 ++++++++++++++++++++++++++++++++ terraform/main.tf | 13 +++++++ terraform/outputs.tf | 15 ++++++++ terraform/variables.tf | 34 ++++++++++++++++++ terraform/versions.tf | 9 +++++ tox.ini | 7 ++++ 8 files changed, 150 insertions(+) create mode 100644 terraform/README.md create mode 100644 terraform/main.tf create mode 100644 terraform/outputs.tf create mode 100644 terraform/variables.tf create mode 100644 terraform/versions.tf diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index 7b2d452..e436699 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -51,6 +51,15 @@ jobs: - name: Run unit tests run: tox -e unit + terraform-checks: + name: Terraform + uses: canonical/charmed-kubeflow-workflows/.github/workflows/terraform-checks.yaml@main + with: + charm-path: . + # Don't run this workflow since the charm goes to `Error` state when deployed on its own + # See: https://github.com/canonical/resource-dispatcher/issues/66 + apply: false + integration: name: Integration Test (build and deploy) runs-on: ubuntu-20.04 diff --git a/.gitignore b/.gitignore index 30d24e6..e09e03d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ __pycache__/ *.py[cod] .idea .vscode/ +venv/ +.terraform* +*.tfstate* kubeconfig.tmp new_config diff --git a/terraform/README.md b/terraform/README.md new file mode 100644 index 0000000..b99b6b2 --- /dev/null +++ b/terraform/README.md @@ -0,0 +1,60 @@ +# Terraform module for resource-dispatcher + +This is a Terraform module facilitating the deployment of the resource-dispatcher charm, using the [Terraform juju provider](https://github.com/juju/terraform-provider-juju/). For more information, refer to the provider [documentation](https://registry.terraform.io/providers/juju/juju/latest/docs) + +## Requirements +This module requires a `juju` model to be available. Refer to the [usage section](#usage) below for more details. + +## API + +### Inputs +The module offers the following configurable inputs: + +| Name | Type | Description | Required | +| - | - | - | - | +| `app_name`| string | Application name | False | +| `channel`| string | Channel that the charm is deployed from | False | +| `config`| map(string) | Map of the charm configuration options | False | +| `model_name`| string | Name of the model that the charm is deployed on | True | +| `resources`| map(string) | Map of the charm resources | False | +| `revision`| number | Revision number of the charm name | False | + +### Outputs +Upon applied, the module exports the following outputs: + +| Name | Description | +| - | - | +| `app_name`| Application name | +| `provides`| Map of `provides` endpoints | +| `requires`| Map of `reqruires` endpoints | + +## Usage + +This module is intended to be used as part of a higher-level module. When defining one, users should ensure that Terraform is aware of the `juju_model` dependency of the charm module. There are two options to do so when creating a high-level module: + +### Define a `juju_model` resource +Define a `juju_model` resource and pass to the `model_name` input a reference to the `juju_model` resource's name. For example: + +``` +resource "juju_model" "testing" { + name = kubeflow +} + +module "resource-dispatcher" { + source = "" + model_name = juju_model.testing.name +} +``` + +### Define a `data` source +Define a `data` source and pass to the `model_name` input a reference to the `data.juju_model` resource's name. This will enable Terraform to look for a `juju_model` resource with a name attribute equal to the one provided, and apply only if this is present. Otherwise, it will fail before applying anything. +``` +data "juju_model" "testing" { + name = var.model_name +} + +module "resource-dispatcher" { + source = "" + model_name = data.juju_model.testing.name +} +``` diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..2bf732e --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,13 @@ +resource "juju_application" "resource_dispatcher" { + charm { + name = "resource-dispatcher" + channel = var.channel + revision = var.revision + } + config = var.config + model = var.model_name + name = var.app_name + resources = var.resources + trust = true + units = 1 +} diff --git a/terraform/outputs.tf b/terraform/outputs.tf new file mode 100644 index 0000000..39cb368 --- /dev/null +++ b/terraform/outputs.tf @@ -0,0 +1,15 @@ +output "app_name" { + value = juju_application.resource_dispatcher.name +} + +output "provides" { + value = { + secrets = "secrets", + service_accounts = "service-accounts", + pod_defaults = "pod-defaults", + } +} + +output "requires" { + value = {} +} diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..43a9bdf --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,34 @@ +variable "app_name" { + description = "Application name" + type = string + default = "resource-dispatcher" +} + +variable "channel" { + description = "Charm channel" + type = string + default = null +} + +variable "config" { + description = "Map of charm configuration options" + type = map(string) + default = {} +} + +variable "model_name" { + description = "Model name" + type = string +} + +variable "resources" { + description = "Map of resources" + type = map(string) + default = null +} + +variable "revision" { + description = "Charm revision" + type = number + default = null +} diff --git a/terraform/versions.tf b/terraform/versions.tf new file mode 100644 index 0000000..eb357ca --- /dev/null +++ b/terraform/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.6" + required_providers { + juju = { + source = "juju/juju" + version = "~> 0.14.0" + } + } +} diff --git a/tox.ini b/tox.ini index ce52f47..bb797c3 100644 --- a/tox.ini +++ b/tox.ini @@ -64,6 +64,13 @@ deps = -r requirements-lint.txt description = Check code against coding style standards +[testenv:tflint] +allowlist_externals = + tflint +commands = + tflint --chdir=terraform --recursive +description = Check Terraform code against coding style standards + [testenv:unit] commands = coverage run --source={[vars]src_path} \