From 2b303196114d787aecc3370a2be2b4d86056dedd Mon Sep 17 00:00:00 2001 From: Manos Vlassis <57320708+mvlassis@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:00:29 +0300 Subject: [PATCH 1/3] Feat: Add Terraform module (#271) * Add Terraform module * Update README.md * Remove channel from terraform action * Update default Terraform channel * Update default Terraform channel --- .github/workflows/integrate.yaml | 6 ++ .gitignore | 3 + charms/kserve-controller/terraform/README.md | 60 +++++++++++++++++++ charms/kserve-controller/terraform/main.tf | 13 ++++ charms/kserve-controller/terraform/outputs.tf | 20 +++++++ .../kserve-controller/terraform/variables.tf | 34 +++++++++++ .../kserve-controller/terraform/versions.tf | 9 +++ charms/kserve-controller/tox.ini | 7 +++ 8 files changed, 152 insertions(+) create mode 100644 charms/kserve-controller/terraform/README.md create mode 100644 charms/kserve-controller/terraform/main.tf create mode 100644 charms/kserve-controller/terraform/outputs.tf create mode 100644 charms/kserve-controller/terraform/variables.tf create mode 100644 charms/kserve-controller/terraform/versions.tf diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index 9c0edcf0..7f2ba1ad 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -41,6 +41,12 @@ jobs: - run: sudo apt update && sudo apt install tox - run: tox -e ${{ matrix.charm }}-unit + terraform-checks: + name: Terraform + uses: canonical/charmed-kubeflow-workflows/.github/workflows/terraform-checks.yaml@main + with: + charm-path: ./charms/kserve-controller + charm-integration: name: Individual Integration Tests runs-on: ubuntu-20.04 diff --git a/.gitignore b/.gitignore index f99df7cf..d0452a1e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ build/ .vscode .python-version +venv/ +.terraform* +*.tfstate* diff --git a/charms/kserve-controller/terraform/README.md b/charms/kserve-controller/terraform/README.md new file mode 100644 index 00000000..c142826f --- /dev/null +++ b/charms/kserve-controller/terraform/README.md @@ -0,0 +1,60 @@ +# Terraform module for kserve-controller + +This is a Terraform module facilitating the deployment of the kserve-controller 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 "kserve-controller" { + 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 "kserve-controller" { + source = "" + model_name = data.juju_model.testing.name +} +``` diff --git a/charms/kserve-controller/terraform/main.tf b/charms/kserve-controller/terraform/main.tf new file mode 100644 index 00000000..4280a154 --- /dev/null +++ b/charms/kserve-controller/terraform/main.tf @@ -0,0 +1,13 @@ +resource "juju_application" "kserve_controller" { + charm { + name = "kserve-controller" + 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/charms/kserve-controller/terraform/outputs.tf b/charms/kserve-controller/terraform/outputs.tf new file mode 100644 index 00000000..863346d6 --- /dev/null +++ b/charms/kserve-controller/terraform/outputs.tf @@ -0,0 +1,20 @@ +output "app_name" { + value = juju_application.kserve_controller.name +} + +output "provides" { + value = { + metrics_endpoint = "metrics-endpoint" + } +} + +output "requires" { + value = { + object_storage = "object-storage", + ingress_gateway = "ingress-gateway", + local_gateway = "local-gateway", + secrets = "secrets", + service_accounts = "service-accounts", + logging = "logging", + } +} diff --git a/charms/kserve-controller/terraform/variables.tf b/charms/kserve-controller/terraform/variables.tf new file mode 100644 index 00000000..f02c8705 --- /dev/null +++ b/charms/kserve-controller/terraform/variables.tf @@ -0,0 +1,34 @@ +variable "app_name" { + description = "Application name" + type = string + default = "kserve-controller" +} + +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/charms/kserve-controller/terraform/versions.tf b/charms/kserve-controller/terraform/versions.tf new file mode 100644 index 00000000..eb357ca6 --- /dev/null +++ b/charms/kserve-controller/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/charms/kserve-controller/tox.ini b/charms/kserve-controller/tox.ini index d1c2accb..b8f7f2ac 100644 --- a/charms/kserve-controller/tox.ini +++ b/charms/kserve-controller/tox.ini @@ -71,6 +71,13 @@ commands = -m pytest --ignore={[vars]tst_path}integration -vv --tb native {posargs} coverage report +[testenv:tflint] +allowlist_externals = + tflint +commands = + tflint --chdir=terraform --recursive +description = Check Terraform code against coding style standards + [testenv:integration] description = Run integration tests deps = From cb80c55c0f9d8927f4c09d53e963bf313819145f Mon Sep 17 00:00:00 2001 From: mvlassis Date: Thu, 26 Sep 2024 13:04:10 +0300 Subject: [PATCH 2/3] Update default Terraform channel --- charms/kserve-controller/terraform/variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charms/kserve-controller/terraform/variables.tf b/charms/kserve-controller/terraform/variables.tf index f02c8705..426f113e 100644 --- a/charms/kserve-controller/terraform/variables.tf +++ b/charms/kserve-controller/terraform/variables.tf @@ -7,7 +7,7 @@ variable "app_name" { variable "channel" { description = "Charm channel" type = string - default = null + default = "0.13/stable" } variable "config" { From 019ac2d7f6639d1733e7c3ef1950424ed6d23b6e Mon Sep 17 00:00:00 2001 From: mvlassis Date: Thu, 26 Sep 2024 14:10:41 +0300 Subject: [PATCH 3/3] Github action --- .github/workflows/integrate.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index 7f2ba1ad..0fb95167 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -46,6 +46,7 @@ jobs: uses: canonical/charmed-kubeflow-workflows/.github/workflows/terraform-checks.yaml@main with: charm-path: ./charms/kserve-controller + channel: 0.13/stable charm-integration: name: Individual Integration Tests