From 31bef122423abf648fa4fa3e03cd1d9266cfd230 Mon Sep 17 00:00:00 2001 From: Orfeas Kourkakis Date: Tue, 24 Sep 2024 15:56:00 +0300 Subject: [PATCH 1/3] feat: Add terraform modules for katib-{controller,db-manager,ui} (#240) Create a `terraform/` directory for each of the charms that hosts their individual Terraform modules. It follows the structure proposed in [this spec](https://docs.google.com/document/d/1EG71A2pJ244PQRaGVzGj7Mx2B_bzE4U_OSqx4eeVI1E/edit) and it is based on what was done in canonical/argo-operators#198. Ref #236 Ref #237 Ref #239 --- .github/workflows/integrate.yaml | 14 ++++- .gitignore | 2 + charms/katib-controller/terraform/README.md | 60 +++++++++++++++++++ charms/katib-controller/terraform/main.tf | 13 ++++ charms/katib-controller/terraform/outputs.tf | 17 ++++++ .../katib-controller/terraform/variables.tf | 34 +++++++++++ charms/katib-controller/terraform/versions.tf | 9 +++ charms/katib-controller/tox.ini | 7 +++ charms/katib-db-manager/terraform/README.md | 60 +++++++++++++++++++ charms/katib-db-manager/terraform/main.tf | 13 ++++ charms/katib-db-manager/terraform/outputs.tf | 16 +++++ .../katib-db-manager/terraform/variables.tf | 34 +++++++++++ charms/katib-db-manager/terraform/versions.tf | 9 +++ charms/katib-db-manager/tox.ini | 7 +++ charms/katib-ui/terraform/README.md | 60 +++++++++++++++++++ charms/katib-ui/terraform/main.tf | 13 ++++ charms/katib-ui/terraform/outputs.tf | 15 +++++ charms/katib-ui/terraform/variables.tf | 34 +++++++++++ charms/katib-ui/terraform/versions.tf | 9 +++ charms/katib-ui/tox.ini | 7 +++ 20 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 charms/katib-controller/terraform/README.md create mode 100644 charms/katib-controller/terraform/main.tf create mode 100644 charms/katib-controller/terraform/outputs.tf create mode 100644 charms/katib-controller/terraform/variables.tf create mode 100644 charms/katib-controller/terraform/versions.tf create mode 100644 charms/katib-db-manager/terraform/README.md create mode 100644 charms/katib-db-manager/terraform/main.tf create mode 100644 charms/katib-db-manager/terraform/outputs.tf create mode 100644 charms/katib-db-manager/terraform/variables.tf create mode 100644 charms/katib-db-manager/terraform/versions.tf create mode 100644 charms/katib-ui/terraform/README.md create mode 100644 charms/katib-ui/terraform/main.tf create mode 100644 charms/katib-ui/terraform/outputs.tf create mode 100644 charms/katib-ui/terraform/variables.tf create mode 100644 charms/katib-ui/terraform/versions.tf diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index 505d4af2..e11e5958 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -55,7 +55,19 @@ jobs: - uses: actions/checkout@v3 - run: python3 -m pip install tox - run: tox -e ${{ matrix.charm }}-unit - + + terraform-checks: + name: Terraform + uses: canonical/charmed-kubeflow-workflows/.github/workflows/terraform-checks.yaml@main + strategy: + matrix: + charm: + - katib-controller + - katib-db-manager + - katib-ui + with: + charm-path: ./charms/${{ matrix.charm }} + charm-integration: name: Integration tests (microk8s) runs-on: ubuntu-20.04 diff --git a/.gitignore b/.gitignore index 40f89051..b9b0bf03 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ __pycache__/ .tox .vscode venv/ +.terraform* +*.tfstate* diff --git a/charms/katib-controller/terraform/README.md b/charms/katib-controller/terraform/README.md new file mode 100644 index 00000000..486fff7d --- /dev/null +++ b/charms/katib-controller/terraform/README.md @@ -0,0 +1,60 @@ +# Terraform module for katib-controller + +This is a Terraform module facilitating the deployment of the katib-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 "katib-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 "katib-controller" { + source = "" + model_name = data.juju_model.testing.name +} +``` diff --git a/charms/katib-controller/terraform/main.tf b/charms/katib-controller/terraform/main.tf new file mode 100644 index 00000000..dbed6d42 --- /dev/null +++ b/charms/katib-controller/terraform/main.tf @@ -0,0 +1,13 @@ +resource "juju_application" "katib_controller" { + charm { + name = "katib-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/katib-controller/terraform/outputs.tf b/charms/katib-controller/terraform/outputs.tf new file mode 100644 index 00000000..c3586bb8 --- /dev/null +++ b/charms/katib-controller/terraform/outputs.tf @@ -0,0 +1,17 @@ +output "app_name" { + value = juju_application.katib_controller.name +} + +output "provides" { + value = { + metrics_endpoint = "metrics-endpoint", + grafana_dashboard = "grafana-dashboard", + } +} + +output "requires" { + value = { + k8s_service_info = "k8s-service-info" + logging = "logging" + } +} diff --git a/charms/katib-controller/terraform/variables.tf b/charms/katib-controller/terraform/variables.tf new file mode 100644 index 00000000..748451a1 --- /dev/null +++ b/charms/katib-controller/terraform/variables.tf @@ -0,0 +1,34 @@ +variable "app_name" { + description = "Application name" + type = string + default = "katib-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/katib-controller/terraform/versions.tf b/charms/katib-controller/terraform/versions.tf new file mode 100644 index 00000000..eb357ca6 --- /dev/null +++ b/charms/katib-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/katib-controller/tox.ini b/charms/katib-controller/tox.ini index 2b529070..7aabe3f3 100644 --- a/charms/katib-controller/tox.ini +++ b/charms/katib-controller/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} \ diff --git a/charms/katib-db-manager/terraform/README.md b/charms/katib-db-manager/terraform/README.md new file mode 100644 index 00000000..90a8a697 --- /dev/null +++ b/charms/katib-db-manager/terraform/README.md @@ -0,0 +1,60 @@ +# Terraform module for katib-db-manager + +This is a Terraform module facilitating the deployment of the katib-db-manager 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 "katib-db-manager" { + 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 "katib-db-manager" { + source = "" + model_name = data.juju_model.testing.name +} +``` diff --git a/charms/katib-db-manager/terraform/main.tf b/charms/katib-db-manager/terraform/main.tf new file mode 100644 index 00000000..4d5fe552 --- /dev/null +++ b/charms/katib-db-manager/terraform/main.tf @@ -0,0 +1,13 @@ +resource "juju_application" "katib_db_manager" { + charm { + name = "katib-db-manager" + 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/katib-db-manager/terraform/outputs.tf b/charms/katib-db-manager/terraform/outputs.tf new file mode 100644 index 00000000..3b593883 --- /dev/null +++ b/charms/katib-db-manager/terraform/outputs.tf @@ -0,0 +1,16 @@ +output "app_name" { + value = juju_application.katib_db_manager.name +} + +output "provides" { + value = { + k8s_service_info = "k8s-service-info" + } +} + +output "requires" { + value = { + relational_db = "relational-db" + logging = "logging" + } +} diff --git a/charms/katib-db-manager/terraform/variables.tf b/charms/katib-db-manager/terraform/variables.tf new file mode 100644 index 00000000..42f76340 --- /dev/null +++ b/charms/katib-db-manager/terraform/variables.tf @@ -0,0 +1,34 @@ +variable "app_name" { + description = "Application name" + type = string + default = "katib-db-manager" +} + +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/katib-db-manager/terraform/versions.tf b/charms/katib-db-manager/terraform/versions.tf new file mode 100644 index 00000000..eb357ca6 --- /dev/null +++ b/charms/katib-db-manager/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/katib-db-manager/tox.ini b/charms/katib-db-manager/tox.ini index bc882ce9..f629b08e 100644 --- a/charms/katib-db-manager/tox.ini +++ b/charms/katib-db-manager/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} \ diff --git a/charms/katib-ui/terraform/README.md b/charms/katib-ui/terraform/README.md new file mode 100644 index 00000000..72ee9329 --- /dev/null +++ b/charms/katib-ui/terraform/README.md @@ -0,0 +1,60 @@ +# Terraform module for katib-ui + +This is a Terraform module facilitating the deployment of the katib-ui 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 "katib-ui" { + 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 "katib-ui" { + source = "" + model_name = data.juju_model.testing.name +} +``` diff --git a/charms/katib-ui/terraform/main.tf b/charms/katib-ui/terraform/main.tf new file mode 100644 index 00000000..1913e6ea --- /dev/null +++ b/charms/katib-ui/terraform/main.tf @@ -0,0 +1,13 @@ +resource "juju_application" "katib_ui" { + charm { + name = "katib-ui" + 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/katib-ui/terraform/outputs.tf b/charms/katib-ui/terraform/outputs.tf new file mode 100644 index 00000000..eb586793 --- /dev/null +++ b/charms/katib-ui/terraform/outputs.tf @@ -0,0 +1,15 @@ +output "app_name" { + value = juju_application.katib_ui.name +} + +output "provides" { + value = {} +} + +output "requires" { + value = { + ingress = "ingress", + dashboard_links = "dashboard-links", + logging = "logging", + } +} diff --git a/charms/katib-ui/terraform/variables.tf b/charms/katib-ui/terraform/variables.tf new file mode 100644 index 00000000..9a08d4b7 --- /dev/null +++ b/charms/katib-ui/terraform/variables.tf @@ -0,0 +1,34 @@ +variable "app_name" { + description = "Application name" + type = string + default = "katib-ui" +} + +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/katib-ui/terraform/versions.tf b/charms/katib-ui/terraform/versions.tf new file mode 100644 index 00000000..eb357ca6 --- /dev/null +++ b/charms/katib-ui/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/katib-ui/tox.ini b/charms/katib-ui/tox.ini index 1744a980..838d5d73 100644 --- a/charms/katib-ui/tox.ini +++ b/charms/katib-ui/tox.ini @@ -62,3 +62,10 @@ commands = 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 From ad68489f0f3a21c99605b630ee9187035093d042 Mon Sep 17 00:00:00 2001 From: Orfeas Kourkakis Date: Tue, 24 Sep 2024 15:57:42 +0300 Subject: [PATCH 2/3] ci(terraform): Deploy charms from 0.17/stable --- .github/workflows/integrate.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index e11e5958..5c126cf8 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -67,6 +67,7 @@ jobs: - katib-ui with: charm-path: ./charms/${{ matrix.charm }} + channel: 0.17/stable charm-integration: name: Integration tests (microk8s) From 634be05f76c294ce8ebb4f869dddbd443334de3a Mon Sep 17 00:00:00 2001 From: Orfeas Kourkakis Date: Tue, 24 Sep 2024 17:36:11 +0300 Subject: [PATCH 3/3] terraform: set channel variables default to 0.17/stable --- charms/katib-controller/terraform/variables.tf | 2 +- charms/katib-db-manager/terraform/variables.tf | 2 +- charms/katib-ui/terraform/variables.tf | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/charms/katib-controller/terraform/variables.tf b/charms/katib-controller/terraform/variables.tf index 748451a1..72860cac 100644 --- a/charms/katib-controller/terraform/variables.tf +++ b/charms/katib-controller/terraform/variables.tf @@ -7,7 +7,7 @@ variable "app_name" { variable "channel" { description = "Charm channel" type = string - default = null + default = "0.17/stable" } variable "config" { diff --git a/charms/katib-db-manager/terraform/variables.tf b/charms/katib-db-manager/terraform/variables.tf index 42f76340..c56eee9a 100644 --- a/charms/katib-db-manager/terraform/variables.tf +++ b/charms/katib-db-manager/terraform/variables.tf @@ -7,7 +7,7 @@ variable "app_name" { variable "channel" { description = "Charm channel" type = string - default = null + default = "0.17/stable" } variable "config" { diff --git a/charms/katib-ui/terraform/variables.tf b/charms/katib-ui/terraform/variables.tf index 9a08d4b7..14c8375d 100644 --- a/charms/katib-ui/terraform/variables.tf +++ b/charms/katib-ui/terraform/variables.tf @@ -7,7 +7,7 @@ variable "app_name" { variable "channel" { description = "Charm channel" type = string - default = null + default = "0.17/stable" } variable "config" {