From 9d80adaec0b056dd6b41dfcab72d353759dc9e2c Mon Sep 17 00:00:00 2001 From: gatici Date: Mon, 5 Feb 2024 15:35:05 +0300 Subject: [PATCH] Add the Github workflow for the Terraform checks Signed-off-by: gatici --- .github/workflows/terraform.yaml | 30 +++++++++++ terraform/CONTRIBUTING.md | 86 -------------------------------- terraform/README.md | 79 ++++++++++++++--------------- terraform/main.tf | 36 ++----------- terraform/outputs.tf | 54 ++++++++++++++++++-- terraform/terraform.tf | 2 +- terraform/terraform.tfvars | 10 ---- terraform/variables.tf | 19 ++----- 8 files changed, 129 insertions(+), 187 deletions(-) create mode 100644 .github/workflows/terraform.yaml delete mode 100644 terraform/CONTRIBUTING.md delete mode 100644 terraform/terraform.tfvars diff --git a/.github/workflows/terraform.yaml b/.github/workflows/terraform.yaml new file mode 100644 index 00000000..f161c712 --- /dev/null +++ b/.github/workflows/terraform.yaml @@ -0,0 +1,30 @@ +name: Check the Terraform Module + +on: + pull_request: + paths: + - '**.tf' + +jobs: + build: + name: Terraform Checks and Plans + runs-on: ubuntu-latest + steps: + - name: Checkout the repository to the runner + uses: actions/checkout@v3 + + - name: HashiCorp - Setup Terraform + uses: hashicorp/setup-terraform@v3 + + - name: Terraform Format + id: fmt + run: terraform fmt -recursive -check + continue-on-error: true + + - name: Terraform Init + id: init + run: terraform init + + - name: Terraform Validate + id: validate + run: terraform validate -no-color \ No newline at end of file diff --git a/terraform/CONTRIBUTING.md b/terraform/CONTRIBUTING.md deleted file mode 100644 index 9a421a2f..00000000 --- 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 52f4669a..63dcf930 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -6,65 +6,60 @@ This Grafana-agent-k8s Terraform module aims to deploy the [grafana-agent-k8s ch ### 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 grafana-agent-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 -``` - -Customize the configuration inputs under `terraform.tfvars` file according to requirement. +## Using Grafana-agent-k8s base module in higher level modules -Replace the values in the `terraform.tfvars` file: +If you want to use `grafana-agent-k8s` base module as part of your Terraform module, import it like shown below. -```yaml -# Mandatory Config Options -model_name = "put your model-name here" +```text +module "grafana-agent-k8s" { + source = "git::https://github.com/canonical/grafana-agent-k8s-operator//terraform" + + model_name = "juju_model_name" + # Optional Configurations + # channel = "put the Charm channel here" + # grafana-config = { + # tls_insecure_skip_verify = "put True not to skip the TLS verification" + # } +} ``` -Run Terraform Plan by providing var-file: - -```console -terraform plan -var-file="terraform.tfvars" -``` +Please see the link to customize the Grafana configuration variables if needed. -Deploy the resources, skip the approval: +- [Grafana configuration option](https://charmhub.io/grafana-agent-k8s/configure) -```console -terraform apply -auto-approve -``` +Create the integrations, for instance: -### Check the Output +```text +resource "juju_integration" "amf-metrics" { + 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.grafana.metrics_endpoint + } -```console -juju status --relations + application { + name = module.grafana.app_name + endpoint = module.grafana.metrics_endpoint + } +} ``` -### Clean Up +Please check the available [integration pairs](https://charmhub.io/grafana-agent-k8s/integrations). -Remove the application: +[Terraform](https://www.terraform.io/) -```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 bbf31b58..369b0753 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,42 +1,16 @@ -resource "juju_application" "grafana-agent-k8s" { +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +resource "juju_application" "grafana" { name = "grafana-agent-k8s" model = var.model_name charm { name = "grafana-agent-k8s" channel = var.channel - base = "ubuntu@22.04" } - config = var.grafana-config + config = var.grafana_config units = 1 trust = true } -resource "juju_integration" "prometheus-remote-write" { - count = var.metrics_remote_write_offer_url != "" ? 1 : 0 - model = var.model_name - - application { - name = juju_application.grafana-agent-k8s.name - endpoint = "send-remote-write" - } - - application { - offer_url = var.metrics_remote_write_offer_url - } -} - -resource "juju_integration" "loki-logging" { - count = var.logging_offer_url != "" ? 1 : 0 - model = var.model_name - - application { - name = juju_application.grafana-agent-k8s.name - endpoint = "logging-consumer" - } - - application { - offer_url = var.logging_offer_url - } -} - diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 5690e7ce..dc093589 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -1,4 +1,52 @@ -output "grafana_application_name" { +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +output "app_name" { description = "Name of the deployed application." - value = juju_application.grafana-agent-k8s.name -} \ No newline at end of file + value = juju_application.grafana.name +} + +output "certificates_endpoint" { + description = "Name of the endpoint to get the X.509 certificate using tls-certificates interface." + value = "certificates" +} + +output "send_remote_write_endpoint" { + description = "Name of the endpoint to forward client charms metrics and associated alert rules to Prometheus using prometheus_remote_write interface." + value = "send-remote-write" +} + +output "metrics_endpoint" { + description = "Name of the endpoint to expose the Prometheus metrics endpoint providing telemetry about the Grafana instance using prometheus_scrape interface." + value = "metrics-endpoint" +} + +output "logging_consumer_endpoint" { + description = "Name of the endpoint to send the logs to Loki using loki_push_api interface." + value = "logging-consumer" +} + +output "grafana_dashboards_consumer_endpoint" { + description = "Name of the endpoint to provide meaningful dashboards about it's metrics using grafana_dashboard interface." + value = "grafana-dashboards-consumer" +} + +output "grafana_cloud_config_endpoint" { + description = "Name of the endpoint to forward telemetry to any Prometheus(or Loki) compatible endpoint using grafana_cloud_config interface." + value = "grafana-cloud-config" +} + +output "receive_ca_cert_endpoint" { + description = "Name of the endpoint to get the Self signed X.509 Certificates through the relation with Self Signed Certificates Charm using certificate_transfer interface." + value = "receive-ca-cert" +} + +output "logging_provider_endpoint" { + description = "Name of the endpoint provided by Grafana to receive logs from any charm that supports the loki_push_api relation interface." + value = "logging-provider" +} + +output "grafana_dashboards_provider_endpoint" { + description = "Name of the endpoint provided by Grafana to provide meaningful dashboards about its metrics to be shown in a Grafana Charm over the grafana-dashboard relation using the grafana-dashboard interface." + value = "grafana-dashboards-provider" +} diff --git a/terraform/terraform.tf b/terraform/terraform.tf index 545a7bb5..4f60bb46 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 c825ef1c..00000000 --- a/terraform/terraform.tfvars +++ /dev/null @@ -1,10 +0,0 @@ -# Mandatory Config Options -model_name = "put your model-name here" - -# Optional Configuration -channel = "put the charm channel here" -metrics_remote_write_offer_url = "Put the URL here" -logging_offer_url = "Put the URL here" -grafana-config = { - tls_insecure_skip_verify = "put True not to skip the TLS verification" -} \ No newline at end of file diff --git a/terraform/variables.tf b/terraform/variables.tf index a3020ae6..4fa3a0c5 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,19 +13,7 @@ variable "channel" { default = "latest/stable" } -variable "grafana-config" { - description = "Additional configuration for the Grafana" +variable "grafana_config" { + description = "Additional configurations for the Grafana. Please see the available options: https://charmhub.io/grafana-agent-k8s/configure" default = {} } - -variable "metrics_remote_write_offer_url" { - description = "Prometheus offer URL for `send-remote-write` endpoint" - type = string - default = "" -} - -variable "logging_offer_url" { - description = "Loki offer URL for `logging-consumer` endpoint" - type = string - default = "" -}