Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding the Terraform module #274

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
gatici marked this conversation as resolved.
Show resolved Hide resolved

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
gatici marked this conversation as resolved.
Show resolved Hide resolved

- 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
57 changes: 51 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
*.idea
.vscode/
.coverage
.tox/
venv/
build/
*.charm
.coverage
tests/integration/*-tester/lib/

# Python
**/venv/**
*.pyc
.python-version
.mypy_cache/
__pycache__/
*.py[cod]
.tox
.idea/
tests/integration/*-tester/lib/
.env

# Charmcraft
*.charm

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
.terraform.lock.hcl

65 changes: 65 additions & 0 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Grafana-agent-k8s Terraform Module

This Grafana-agent-k8s Terraform module aims to deploy the [grafana-agent-k8s charm](https://charmhub.io/grafana-agent-k8s) via Terraform.
gatici marked this conversation as resolved.
Show resolved Hide resolved

## Getting Started
gatici marked this conversation as resolved.
Show resolved Hide resolved

### Prerequisites
gatici marked this conversation as resolved.
Show resolved Hide resolved

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`
gatici marked this conversation as resolved.
Show resolved Hide resolved
- `juju 3.x`
gatici marked this conversation as resolved.
Show resolved Hide resolved
- `terrafom`
gatici marked this conversation as resolved.
Show resolved Hide resolved

### Module structure

- **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.

## Using Grafana-agent-k8s base module in higher level modules

If you want to use `grafana-agent-k8s` base module as part of your Terraform module, import it like shown below.

```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"
# }
gatici marked this conversation as resolved.
Show resolved Hide resolved
gatici marked this conversation as resolved.
Show resolved Hide resolved
}
```

Please see the link to customize the Grafana configuration variables if needed.

- [Grafana configuration option](https://charmhub.io/grafana-agent-k8s/configure)
gatici marked this conversation as resolved.
Show resolved Hide resolved

Create the integrations, for instance:

```text
resource "juju_integration" "amf-metrics" {
gatici marked this conversation as resolved.
Show resolved Hide resolved
model = var.model_name

application {
name = module.amf.app_name
endpoint = module.grafana.metrics_endpoint
gatici marked this conversation as resolved.
Show resolved Hide resolved
}

application {
name = module.grafana.app_name
endpoint = module.grafana.metrics_endpoint
gatici marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

Please check the available [integration pairs](https://charmhub.io/grafana-agent-k8s/integrations).

[Terraform](https://www.terraform.io/)

[Terraform Juju provider](https://registry.terraform.io/providers/juju/juju/latest)
16 changes: 16 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

resource "juju_application" "grafana-agent-k8s" {
name = var.app_name
model = var.model_name

charm {
name = "grafana-agent-k8s"
channel = var.channel
}
config = var.grafana_config
gatici marked this conversation as resolved.
Show resolved Hide resolved
units = 1
trust = true
}

52 changes: 52 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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
}

gatici marked this conversation as resolved.
Show resolved Hide resolved
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"
}
11 changes: 11 additions & 0 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

terraform {
required_providers {
juju = {
source = "juju/juju"
version = "~> 0.10.1"
}
}
}
25 changes: 25 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

variable "model_name" {
description = "Name of Juju model to deploy application to"
type = string
default = ""
}

variable "app_name" {
description = "Name of the application in the Juju model"
type = string
default = "grafana"
}

variable "channel" {
description = "The channel to use when deploying a charm "
type = string
default = "latest/stable"
}

variable "grafana_config" {
gatici marked this conversation as resolved.
Show resolved Hide resolved
description = "Additional configurations for the Grafana. Please see the available options: https://charmhub.io/grafana-agent-k8s/configure"
gatici marked this conversation as resolved.
Show resolved Hide resolved
default = {}
}
Loading