Skip to content

Commit

Permalink
Add terraform module (#325)
Browse files Browse the repository at this point in the history
* Add terraform module

* improve README

* add terraform ignores to .gitignore

* update libs

* Revert "update libs"

This reverts commit 03ff3b8.

* avoid checking tester charms

---------

Co-authored-by: Jose C. Massón <[email protected]>
Co-authored-by: Jose C. Massón <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2024
1 parent 5639217 commit 1bb705e
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ __pycache__/
tests/integration/*-tester/lib/
.env
cos-tool*

# Terraform
*.tfstate
*.tfstate.*
*.tfplan
.terraform.lock.hcl
crash.log
.terraform/
terraform.tfvars
terraform.tfvars.json
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ignore-words-list = "assertIn"
# Linting tools configuration
[tool.ruff]
line-length = 99
exclude = ["__pycache__", "*.egg_info"]
exclude = ["__pycache__", "*.egg_info", "prometheus-tester", "loki-tester"]

[tool.ruff.lint]
select = ["E", "W", "F", "C", "N", "R", "D", "I001"]
Expand Down
38 changes: 38 additions & 0 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Terraform module for grafana-agent


This is a Terraform module facilitating the deployment of grafana-agent-k8s charm, using the [Terraform juju provider](https://github.com/juju/terraform-provider-juju/). For more information, refer to the provider [documentation](https://registry.terraform.io/providers/juju/juju/latest/docs).


## Requirements
This module requires a `juju` model to be available. Refer to the [usage section](#usage) below for more details.

## API

### Inputs
The module offers the following configurable inputs:

| Name | Type | Description | Required |
| - | - | - | - |
| `app_name`| string | Application name | mimir-worker |
| `channel`| string | Channel that the charm is deployed from | latest/edge |
| `config`| map(any) | Map of the charm configuration options | {} |
| `constraints`| string | Constraints for the Juju deployment| "" |
| `model_name`| string | Name of the model that the charm is deployed on | |
| `revision`| number | Revision number of the charm name | null |
| `units`| number | Number of units to deploy | 1 |

### Outputs
Upon applied, the module exports the following outputs:

| Name | Description |
| - | - |
| `app_name`| Application name |
| `provides`| Map of `provides` endpoints |
| `requires`| Map of `requires` endpoints |

## Usage

Users should ensure that Terraform is aware of the `juju_model` dependency of the charm module.

To deploy this module with its needed dependency, you can run `terraform apply -var="model_name=<MODEL_NAME>" -auto-approve`
12 changes: 12 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "juju_application" "grafana_agent" {
name = var.app_name
model = var.model_name
trust = true
charm {
name = "grafana-agent-k8s"
channel = var.channel
revision = var.revision
}
units = var.units
config = var.config
}
24 changes: 24 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
output "app_name" {
value = juju_application.grafana_agent.name
}

output "requires" {
value = {
certificates = "certificates",
send_remote_write = "send-remote-write",
metrics_endpoint = "metrics-endpoint",
logging_consumer = "logging-consumer",
grafana_dashboards_consumer = "grafana-dashboards-consumer",
grafana_cloud_config = "grafana-cloud-config",
receive_ca_cert = "receive-ca-cert",
tracing = "tracing",
}
}

output "provides" {
value = {
tracing_provider = "tracing-provider",
logging_provider = "logging-provider",
grafana_dashboards_provider = "grafana-dashboards-provider",
}
}
42 changes: 42 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variable "app_name" {
description = "Application name"
type = string
}

variable "channel" {
description = "Charm channel"
type = string
default = "latest/stable"
}

variable "config" {
description = "Config options as in the ones we pass in juju config"
type = map(string)
default = {}
}

# We use constraints to set AntiAffinity in K8s
# https://discourse.charmhub.io/t/pod-priority-and-affinity-in-juju-charms/4091/13?u=jose
variable "constraints" {
description = "Constraints to be applied"
type = string
default = ""
}

variable "model_name" {
description = "Model name"
type = string
}

variable "revision" {
description = "Charm revision"
type = number
nullable = true
default = null
}

variable "units" {
description = "Number of units"
type = number
default = 1
}
9 changes: 9 additions & 0 deletions terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.5"
required_providers {
juju = {
source = "juju/juju"
version = "~> 0.14"
}
}
}

0 comments on commit 1bb705e

Please sign in to comment.