diff --git a/.github/workflows/terraform.yaml b/.github/workflows/terraform.yaml new file mode 100644 index 00000000..8946f39e --- /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@v4 + + - 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..dc2feadd 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -6,7 +6,7 @@ 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` @@ -23,7 +23,7 @@ sudo microk8s enable hostpath-storage Add a Juju model: ```console -juju add model +juju add-model ``` Initialise the provider: @@ -32,22 +32,20 @@ Initialise the provider: terraform init ``` -Customize the configuration inputs under `terraform.tfvars` file according to requirement. - -Replace the values in the `terraform.tfvars` file: +Fill the mandatory config options in the `terraform.tfvars` file: ```yaml # Mandatory Config Options model_name = "put your model-name here" ``` -Run Terraform Plan by providing var-file: +Create the Terraform Plan: ```console terraform plan -var-file="terraform.tfvars" ``` -Deploy the resources, skip the approval: +Deploy the resources: ```console terraform apply -auto-approve @@ -63,7 +61,7 @@ juju status --relations ### Clean Up -Remove the application: +Destroy the deployment: ```console terraform destroy -auto-approve diff --git a/terraform/main.tf b/terraform/main.tf index bbf31b58..0d3476cb 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 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..d4b7d158 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 used by Grafana to get the X.509 certificate." + value = "certificates" +} + +output "send-remote-write-endpoint" { + description = "Name of the endpoint used by Grafana to forward client charms metrics and associated alert rules to Prometheus using prometheus_remote_write interface." + value = "send-remote-write" +} + +output "metrics-endpoint" { + description = "Exposes 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 used by Grafana to send the logs to Loki using loki_push_api interface." + value = "logging-consumer" +} + +output "grafana-dashboards-consumer-endpoint" { + description = "Name of the endpoint used by Grafana 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 used by Grafana 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 used by Grafana 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..5dd73891 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 @@ -14,15 +17,3 @@ variable "grafana-config" { description = "Additional configuration for the Grafana" 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 = "" -}