From dadf7df492252eb8ceab9b8ed0637ec981a9823f Mon Sep 17 00:00:00 2001 From: gatici Date: Wed, 31 Jan 2024 13:34:28 +0300 Subject: [PATCH] Add the Terraform module Signed-off-by: gatici --- .gitignore | 57 ++++++++++++++++++++++--- terraform/CONTRIBUTING.md | 86 ++++++++++++++++++++++++++++++++++++++ terraform/README.md | 70 +++++++++++++++++++++++++++++++ terraform/main.tf | 42 +++++++++++++++++++ terraform/outputs.tf | 4 ++ terraform/terraform.tf | 11 +++++ terraform/terraform.tfvars | 10 +++++ terraform/variables.tf | 28 +++++++++++++ 8 files changed, 302 insertions(+), 6 deletions(-) create mode 100644 terraform/CONTRIBUTING.md create mode 100644 terraform/README.md create mode 100644 terraform/main.tf create mode 100644 terraform/outputs.tf create mode 100644 terraform/terraform.tf create mode 100644 terraform/terraform.tfvars create mode 100644 terraform/variables.tf diff --git a/.gitignore b/.gitignore index 8c579e8..4791595 100644 --- a/.gitignore +++ b/.gitignore @@ -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 + diff --git a/terraform/CONTRIBUTING.md b/terraform/CONTRIBUTING.md new file mode 100644 index 0000000..9a421a2 --- /dev/null +++ b/terraform/CONTRIBUTING.md @@ -0,0 +1,86 @@ +# 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 new file mode 100644 index 0000000..52f4669 --- /dev/null +++ b/terraform/README.md @@ -0,0 +1,70 @@ +# 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. + +## Getting Started + +### Prerequisites + +The following software and tools needs to be installed and should be running in the local environment. + +- `microk8s` +- `juju 3.x` +- `terrafom` + +### Deploy the grafana-agent-k8s charm using Terraform + +Make sure that `storage` plugin is enabled for Microk8s: + +```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. + +Replace the values in the `terraform.tfvars` file: + +```yaml +# Mandatory Config Options +model_name = "put your model-name here" +``` + +Run Terraform Plan by providing var-file: + +```console +terraform plan -var-file="terraform.tfvars" +``` + +Deploy the resources, skip the approval: + +```console +terraform apply -auto-approve +``` + +### Check the Output + +Run `juju switch ` to switch to the target Juju model and observe the status of the application. + +```console +juju status --relations +``` + +### Clean Up + +Remove the application: + +```console +terraform destroy -auto-approve +``` diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..bbf31b5 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,42 @@ +resource "juju_application" "grafana-agent-k8s" { + 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 new file mode 100644 index 0000000..5690e7c --- /dev/null +++ b/terraform/outputs.tf @@ -0,0 +1,4 @@ +output "grafana_application_name" { + description = "Name of the deployed application." + value = juju_application.grafana-agent-k8s.name +} \ No newline at end of file diff --git a/terraform/terraform.tf b/terraform/terraform.tf new file mode 100644 index 0000000..545a7bb --- /dev/null +++ b/terraform/terraform.tf @@ -0,0 +1,11 @@ +# Copyright 2023 Canonical Ltd. +# See LICENSE file for licensing details. + +terraform { + required_providers { + juju = { + source = "juju/juju" + version = "~> 0.10.1" + } + } +} diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars new file mode 100644 index 0000000..c825ef1 --- /dev/null +++ b/terraform/terraform.tfvars @@ -0,0 +1,10 @@ +# 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 new file mode 100644 index 0000000..a3020ae --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,28 @@ +variable "model_name" { + description = "Name of Juju model to deploy application to" + type = string + default = "" +} + +variable "channel" { + description = "The channel to use when deploying a charm " + type = string + default = "latest/stable" +} + +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 = "" +}