From cc28b4ed58297af70d6d78240fa91d1cf12f6d1e Mon Sep 17 00:00:00 2001 From: gatici Date: Mon, 5 Feb 2024 15:40:55 +0300 Subject: [PATCH] Add the Github workflow for the Terraform checks Signed-off-by: gatici --- .github/ISSUE_TEMPLATE/bug_report.md | 1 + .github/workflows/main.yaml | 3 + terraform/CONTRIBUTING.md | 86 ---------------------------- terraform/README.md | 78 +++++++++++-------------- terraform/main.tf | 34 ++--------- terraform/outputs.tf | 24 +++++++- terraform/terraform.tfvars | 7 --- terraform/variables.tf | 17 +++--- 8 files changed, 69 insertions(+), 181 deletions(-) delete mode 100644 terraform/CONTRIBUTING.md delete mode 100644 terraform/terraform.tfvars diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 584d35d..1d9e946 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -32,6 +32,7 @@ assignees: '' - Juju version (output from `juju --version`): - Cloud Environment: - Kubernetes version (output from `kubectl version --short`): +- Terraform version (output from `terraform version`): #### Additional context diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 097cc01..08050c2 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -18,6 +18,9 @@ jobs: lint-report: uses: canonical/sdcore-github-workflows/.github/workflows/lint-report.yaml@main + terraform-check: + uses: canonical/sdcore-github-workflows/.github/workflows/terraform.yaml@main + static-analysis: uses: canonical/sdcore-github-workflows/.github/workflows/static-analysis.yaml@main diff --git a/terraform/CONTRIBUTING.md b/terraform/CONTRIBUTING.md deleted file mode 100644 index 9a421a2..0000000 --- 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 0cddc60..08cdebe 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -6,67 +6,53 @@ This SD-Core NRF K8s Terraform module aims to deploy the [sdcore-nrf-k8s charm]( ### Prerequisites -The following software and tools needs to be installed and should be running in the local environment. +The following software and tools needs to be installed and should be running in the local environment. Please [set up your environment](https://discourse.charmhub.io/t/set-up-your-development-environment-with-microk8s-for-juju-terraform-provider/13109) before deployment. - `microk8s` - `juju 3.x` - `terrafom` -### Deploy the sdcore-nrf-k8s charm using Terraform +### Module structure -Make sure that `storage` plugin is enabled for Microk8s: +- **main.tf** - Defines the Juju application to be deployed. +- **variables.tf** - Allows customization of the deployment. Except for exposing the deployment options (Juju model name, channel or application name) also models the 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. -```console -sudo microk8s enable hostpath-storage -``` - -Add a Juju model: - -```console -juju add model -``` - -Initialise the provider: - -```console -terraform init -``` +## Using sdcore-nrf-k8s base module in higher level modules -Customize the configuration inputs under `terraform.tfvars` file according to requirement. +If you want to use `sdcore-nrf-k8s` base module as part of your Terraform module, import it like shown below. -Replace the values in the `terraform.tfvars` file: - -```yaml -# Mandatory Config Options -model_name = "put your model-name here" -db_application_name = "put your mongodb app name here" -certs_application_name = "put your self-signed-certificates app name here" +```text +module "sdcore-nrf-k8s" { + source = "git::https://github.com/canonical/sdcore-nrf-k8s-operator//terraform" + model_name = "juju_model_name" + # Optional Configurations + # channel = "put the Charm channel here" + # app_name = "put the application name here" +} ``` -Run Terraform Plan by providing var-file: +Create the integrations, for instance: -```console -terraform plan -var-file="terraform.tfvars" -``` +```text +resource "juju_integration" "nrf-db" { + model = var.model_name -Deploy the resources, skip the approval: + application { + name = module.nrf.app_name + endpoint = module.nrf.database_endpoint + } -```console -terraform apply -auto-approve + application { + name = module.mongodb.app_name + endpoint = module.mongodb.database_endpoint + } +} ``` -### Check the Output +Please check the available [integration pairs](https://charmhub.io/sdcore-nrf-k8s/integrations). -Run `juju switch ` to switch to the target Juju model and observe the status of the application. - -```console -juju status --relations -``` +[Terraform](https://www.terraform.io/) -### Clean up - -Remove the application: - -```console -terraform destroy -auto-approve -``` +[Terraform Juju provider](https://registry.terraform.io/providers/juju/juju/latest) diff --git a/terraform/main.tf b/terraform/main.tf index c89bab8..3332591 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,5 +1,8 @@ -resource "juju_application" "nrf" { - name = "nrf" +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +resource "juju_application" "sdcore-nrf-k8s" { + name = var.app_name model = var.model_name charm { @@ -10,31 +13,4 @@ resource "juju_application" "nrf" { trust = true } -resource "juju_integration" "nrf-db" { - model = var.model_name - - application { - name = juju_application.nrf.name - endpoint = "database" - } - - application { - name = var.db_application_name - endpoint = "database" - } -} - -resource "juju_integration" "nrf-certs" { - model = var.model_name - - application { - name = juju_application.nrf.name - endpoint = "certificates" - } - - application { - name = var.certs_application_name - endpoint = "certificates" - } -} diff --git a/terraform/outputs.tf b/terraform/outputs.tf index cbfbbb9..9d5ec52 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -1,4 +1,22 @@ -output "nrf_application_name" { +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +output "app_name" { description = "Name of the deployed application." - value = juju_application.nrf.name -} \ No newline at end of file + value = juju_application.sdcore-nrf-k8s.name +} + +output "database_endpoint" { + description = "Name of the endpoint to integrate with MongoDB using mongodb_client interface." + value = "database" +} + +output "certificates_endpoint" { + description = "Name of the endpoint to get the X.509 certificate using tls-certificates interface." + value = "certificates" +} + +output "fiveg_nrf_endpoint" { + description = "Name of the endpoint to provide fiveg_nrf interface." + value = "fiveg-nrf" +} diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars deleted file mode 100644 index fa5483c..0000000 --- a/terraform/terraform.tfvars +++ /dev/null @@ -1,7 +0,0 @@ -# Mandatory Config Options -model_name = "put your model-name here" -db_application_name = "put your mongodb app name here" -certs_application_name = "put your self-signed-certificates app name here" - -# Optional Configuration -channel = "put the charm channel here" diff --git a/terraform/variables.tf b/terraform/variables.tf index 542ec02..67614d7 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,3 +1,6 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + variable "model_name" { description = "Name of Juju model to deploy application to." type = string @@ -10,14 +13,8 @@ variable "channel" { default = "1.3/edge" } -variable "db_application_name" { - description = "The name of the application providing the `database` endpoint." +variable "app_name" { + description = "Name of the application in the Juju model" type = string - default = "" -} - -variable "certs_application_name" { - description = "Name of the application providing the `certificates` integration endpoint." - type = string - default = "" -} + default = "nrf" +} \ No newline at end of file