Skip to content

Commit

Permalink
Add the Github workflow for the Terraform checks
Browse files Browse the repository at this point in the history
Signed-off-by: gatici <[email protected]>
  • Loading branch information
gatici committed Feb 9, 2024
1 parent becd9d3 commit 9d80ada
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 187 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/terraform.yaml
Original file line number Diff line number Diff line change
@@ -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
86 changes: 0 additions & 86 deletions terraform/CONTRIBUTING.md

This file was deleted.

79 changes: 37 additions & 42 deletions terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <model-name>
```

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 <juju model>` 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)
36 changes: 5 additions & 31 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
}
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
}
}

54 changes: 51 additions & 3 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
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"
}
2 changes: 1 addition & 1 deletion terraform/terraform.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Canonical Ltd.
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

terraform {
Expand Down
10 changes: 0 additions & 10 deletions terraform/terraform.tfvars

This file was deleted.

19 changes: 5 additions & 14 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = ""
}

0 comments on commit 9d80ada

Please sign in to comment.