Skip to content

fgouteroux/mimir2tf

Repository files navigation

mimir2tf - Mimir YAML Prometheus Rules to Terraform HCL converter

A tool for converting Mimir Prometheus Rules (in YAML format) into HashiCorp's Terraform configuration language.

The converted .tf files are suitable for use with the Terraform Mimir Provider

Installation

Pre-built Binaries

Download Binary from GitHub releases page.

Usage

Usage of mimir2tf:
  -d, --debug                enable debug output
  -f, --filepath string      file or directory that contains the YAML configuration to convert. Use "-" to read from stdin (default "-")
  -o, --output string        file or directory where Terraform config will be written (default "-")
  -x, --overwrite-existing   allow overwriting existing output file(s)
  -r, --reverse              Reverse mode (hcl to yaml)
  -F, --tf12format           Use Terraform 0.12 formatter
  -v, --version              Print mimir2tf version

YAML to HCL

Convert a single YAML file and write generated Terraform config to Stdout

$ mimir2tf -f test-fixtures/rules.yaml

resource "mimir_rule_group_alerting" "example" {
  name      = "example"
  namespace = "default"

  rule {
    alert = "HighRequestLatency"
    expr  = "sum by (job) (http_inprogress_requests)"
    for   = "5m"

    labels = {
      severity = "critical"
    }

    annotations = {
      test = "annotation value"
    }
  }
}

resource "mimir_rule_group_recording" "mimir_ingester_rules" {
  name      = "mimir_ingester_rules"
  namespace = "default"

  rule {
    record = "cluster_namespace_pod:cortex_ingester_ingested_samples_total:rate1m"
    expr   = "sum by(cluster, namespace, pod) (rate(cortex_ingester_ingested_samples_total[1m]))"
  }
}

Convert a single YAML file and write output to file

$ mimir2tf -f test-fixtures/rules.yaml -o rules.tf

Convert a directory of Mimir YAML files and write output to stdout

$ mimir2tf -f test-fixtures/

Convert a directory of Mimir YAML files and write output to file

$ mimir2tf -f test-fixtures/ -o /tmp/mimir-alerts-rules.tf

HCL to YAML

Convert a single HCL file and write yaml output to file

$ mimir2tf -r -f test-fixtures/mimir-rules.tf -o /tmp/mimir-rules.yaml

Convert a directory of Mimir HCL files to YAML file

$ mimir2tf -r -f test-fixtures -o /tmp/mimir-alert-rules.yaml

Then validate with promtool

$ promtool check rules /tmp/mimir-alert-rules.yaml
Checking /tmp/mimir-alert-rules.yaml
2 duplicate rule(s) found.
Metric: MimirCompactorHasNotUploadedBlocks
Label(s):
  severity: critical
Metric: MimirRolloutStuck
Label(s):
  severity: warning
Might cause inconsistency while recording expressions.
  SUCCESS: 194 rules found

Building

NOTE Requires a working Golang build environment.

This project uses Golang modules for dependency management, so it can be cloned outside of the $GOPATH.

Clone the repository

$ git clone https://github.com/fgouteroux/mimir2tf.git

Build

$ cd mimir2tf
$ make build