diff --git a/.github/workflows/terraform.yaml b/.github/workflows/terraform.yaml new file mode 100644 index 000000000..99bb7311f --- /dev/null +++ b/.github/workflows/terraform.yaml @@ -0,0 +1,15 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +name: Check Terraform Module + +on: + pull_request: + paths: + - '**.tf' + +jobs: + check-terraform-module: + name: Check Terraform Module + uses: canonical/data-platform-workflows/.github/workflows/terraform.yaml@main + secrets: inherit \ No newline at end of file diff --git a/terraform/CONTRIBUTING.md b/terraform/CONTRIBUTING.md deleted file mode 100644 index 9a421a2f7..000000000 --- a/terraform/CONTRIBUTING.md +++ /dev/null @@ -1,86 +0,0 @@ -# Contributing - -## Development environment - -### Prerequisites - -Make sure the following software and tools are installed in the development -environment. - -- `microk8s` -- `juju` -- `terraform` - -### Prepare Development Environment - -Install Microk8s: - -```console -sudo snap install microk8s --channel=1.27-strict/stable -sudo usermod -a -G snap_microk8s $USER -newgrp snap_microk8s -``` - -Enable `storage` plugin for Microk8s: - -```console -sudo microk8s enable hostpath-storage -``` - -Install Juju: - -```console -sudo snap install juju --channel=3.1/stable -``` - -Install Terraform: - -```console -sudo snap install --classic terraform -``` - -Bootstrap the Juju Controller using Microk8s: - -```console -juju bootstrap microk8s -``` - -Add a Juju model: - -```console -juju add model -```` - -### Terraform provider - -The Terraform module uses the Juju provider to provision Juju resources. Please refer to the [Juju provider documentation](https://registry.terraform.io/providers/juju/juju/latest/docs) for more information. - -A Terraform working directory needs to be initialized at the beginning. - -Initialise the provider: - -```console -terraform init -``` - -## Testing - -Terraform CLI provides various ways to do formatting and validation. - -Formats to a canonical format and style: - -```console -terraform fmt -``` - -Check the syntactical validation: - -```console -terraform validate -``` - -Preview the changes: - -```console -terraform plan -``` diff --git a/terraform/README.md b/terraform/README.md index 98f78129b..7a55107fe 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -1,70 +1,54 @@ -# Mongodb-k8s Terraform Module +# MongoDB Operator Terraform module -This mongodb-k8s Terraform module aims to deploy the [mongodb-k8s charm](https://charmhub.io/mongodb-k8s?channel=6/edge) via Terraform. +This folder contains a base [Terraform][Terraform] module for the `mongodb-k8s` charm. -## Getting Started +The module uses the [Terraform Juju provider][Terraform Juju provider] to model the charm deployment onto any Kubernetes environment managed by [Juju][Juju]. -### Prerequisites +The base module is not intended to be deployed in separation (it is possible though), but should rather serve as a building block for higher level modules. -The following software and tools needs to be installed and should be running in the local environment. +## Module structure -- `microk8s` -- `juju 3.x` -- `terrafom` +- **main.tf** - Defines the Juju application to be deployed. +- **variables.tf** - Allows customization of the deployment such as Juju model name, channel or application name and charm configuration. +- **output.tf** - Responsible for integrating the module with other Terraform modules, primarily by defining potential integration endpoints (charm integrations), but also by exposing the application name. +- **terraform.tf** - Defines the Terraform provider. -### Deploy the Mongodb-k8s charm using Terraform +## Using mongodb-k8s base module in higher level modules -Make sure that `storage` plugin is enabled for Microk8s: +If you want to use `mongodb-operator` base module as part of your Terraform module, import it like shown below. -```console -sudo microk8s enable hostpath-storage -``` - -Add a Juju model: - -```console -juju add model -``` - -Initialise the provider: - -```console -terraform init -``` - -Customize the configuration inputs under `terraform.tfvars` file according to requirement. +```text +module "mongodb-operator" { + source = "git::https://github.com/canonical/mongodb-operator.git//terraform" + + model_name = "juju_model_name" + + (Customize configuration variables here if needed) -Replace the values in the `terraform.tfvars` file: - -```yaml -# Mandatory Config Options -model_name = "put your model-name here" -``` - -Run Terraform Plan by providing var-file: - -```console -terraform plan -var-file="terraform.tfvars" +} ``` -Deploy the resources, skip the approval: +Create the integrations, for instance: -```console -terraform apply -auto-approve -``` - -### Check the Output +```text +resource "juju_integration" "amf-db" { + model = var.model_name -Run `juju switch ` to switch to the target Juju model and observe the status of the application. + application { + name = module.amf.app_name + endpoint = module.amf.database_endpoint + } -```console -juju status + application { + name = module.mongodb.app_name + endpoint = module.mongodb.database_endpoint + } +} ``` -### Clean up +Please check the available [integration pairs][integration pairs]. -Remove the application: - -```console -terraform destroy -auto-approve -``` +[Terraform]: https://www.terraform.io/ +[Juju]: https://juju.is +[Terraform Juju provider]: https://registry.terraform.io/providers/juju/juju/latest +[integration pairs]: https://charmhub.io/mongodb-k8s/integrations?channel=6/edge diff --git a/terraform/main.tf b/terraform/main.tf index 72cfbca69..94c37d03c 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,5 +1,8 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + resource "juju_application" "mongodb-k8s" { - name = "mongodb-k8s" + name = var.app_name model = var.model_name charm { @@ -7,7 +10,7 @@ resource "juju_application" "mongodb-k8s" { channel = var.channel base = "ubuntu@22.04" } - config = var.mongo-config + config = var.config units = 1 trust = true } diff --git a/terraform/outputs.tf b/terraform/outputs.tf index bb9fd7ba7..b34939790 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -1,4 +1,51 @@ -output "db_application_name" { +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +output "app_name" { description = "Name of the deployed application." value = juju_application.mongodb-k8s.name +} + +# Provided integration endpoints + +output "database_endpoint" { + description = "Name of the endpoint to provide the mongodb_client interface." + value = "database" +} + +output "obsolete_endpoint" { + description = "Name of the endpoint to provide the mongodb interface." + value = "obsolete" +} + +output "cos_agent_endpoint" { + description = "Name of the endpoint to provide the cos_agent interface." + value = "cos-agent" +} + +output "config_server_endpoint" { + description = "Name of the endpoint to provide the shards interface." + value = "config-server" +} + +output "cluster_endpoint" { + description = "Name of the endpoint to provide the config-server interface." + value = "cluster" +} + +# Required integration endpoints + +output "certificates_endpoint" { + description = "Name of the endpoint to provide the tls-certificates interface." + value = "certificates" +} + +output "s3_credentials_endpoint" { + description = "Name of the endpoint to provide the s3 interface." + value = "s3-credentials" +} + +output "sharding_endpoint" { + description = "Name of the endpoint to provide the shards interface." + value = "sharding" } \ No newline at end of file diff --git a/terraform/terraform.tf b/terraform/terraform.tf index 545a7bb57..4f60bb46b 100644 --- a/terraform/terraform.tf +++ b/terraform/terraform.tf @@ -1,4 +1,4 @@ -# Copyright 2023 Canonical Ltd. +# Copyright 2024 Canonical Ltd. # See LICENSE file for licensing details. terraform { diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars deleted file mode 100644 index 33738f3af..000000000 --- a/terraform/terraform.tfvars +++ /dev/null @@ -1,9 +0,0 @@ -# Mandatory Config Options -model_name = "put your model-name here" - -# Optional Configuration -channel = "put the charm channel here" -mongo-config = { - auto-delete = "put True to remove any relevant databases associated with the relation when a relation is removed" - role = "put role config here as shard, config-server or replication" -} diff --git a/terraform/variables.tf b/terraform/variables.tf index 4794f1702..7a6b2c89f 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,16 +1,26 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + variable "model_name" { description = "Name of Juju model to deploy application to" type = string default = "" } +variable "app_name" { + description = "Name of the application in the Juju model" + type = string + default = "mongodb" +} + variable "channel" { - description = "The channel to use when deploying a charm " + description = "The channel to use when deploying a charm" type = string default = "6/beta" } -variable "mongo-config" { - description = "Additional configuration for the MongoDB" +variable "config" { + description = "Additional configuration for the MongoDB. Details about available options can be found at https://charmhub.io/mongodb-k8s/configure?channel=6/edge." + type = map(string) default = {} } \ No newline at end of file