ovh/terraform-provider-mimirtool

Renaming a namespace does not work

mlcdf opened this issue · 0 comments

mlcdf commented

Hello,

I had an error trying to rename a namespace.

The change:

resource "mimirtool_ruler_namespace" "alerts" {
+  namespace                   = "alerts_infra"
-  namespace                   = "infra"
  config_yaml                 = file("alerts/infra.yaml")
  strict_recording_rule_check = true
}

The error:
image

Root cause

The error is raised by

ruleNamespaces, err := rules.ParseBytes([]byte(configYAML))

After adding logs everywhere, I realized that the error occurs when the configYAML variable is equal to:

- name: mimir_api_1
  rules:
  - expr: histogram_quantile(0.99, sum(rate(cortex_request_duration_seconds_bucket[1m]))
      by (le, cluster, job))
    record: cluster_job:cortex_request_duration_seconds:99quantile
  - expr: histogram_quantile(0.50, sum(rate(cortex_request_duration_seconds_bucket[1m]))
      by (le, cluster, job))
    record: cluster_job:cortex_request_duration_seconds:50quantile

But it works when configYAML equals:

groups:
- name: mimir_api_1
  rules:
  - expr: histogram_quantile(0.99, sum(rate(cortex_request_duration_seconds_bucket[1m]))
      by (le, cluster, job))
    record: cluster_job:cortex_request_duration_seconds:99quantile
  - expr: histogram_quantile(0.50, sum(rate(cortex_request_duration_seconds_bucket[1m]))
      by (le, cluster, job))
    record: cluster_job:cortex_request_duration_seconds:50quantile

=> Sometimes, configYAML does not contain the groups item.

This is due to

namespaceBytes, _ := yaml.Marshal(ruleNamespace.Groups)
which removes the groups part.

So ok, but what this parseBytes function is actually doing? Well it reads file like this one:

namespace: foo
groups:
  - name: testgrp2
    interval: 0s
    rules:
      - alert: HTTPCredentialsLeaked
        expr: sum by (cluster, job, pod) (rate({namespace=~"%s"} |~ "http(s?)://(\\w+):(\\w+)@" [5m]) > 0)
        for: 2m
        labels:
            severity: page
        annotations:
            summary: High request latency
---
namespace: other_foo
groups:
  - name: other_testgrp2
    interval: 0s
    rules:
      - alert: HTTPCredentialsLeaked
        expr: sum by (cluster, job, pod) (rate({namespace=~"%s"} |~ "http(s?)://(\\w+):(\\w+)@" [5m]) > 0)
        for: 2m
        labels:
            severity: page
        annotations:
            summary: High request latency

Oh. It expected a groups.

Steps to Reproduce

You can reproduce the issue by adding this test:

const testAccResourceNamespaceSuppress = `
resource "mimirtool_ruler_namespace" "alerts" {
	namespace = "alerts_infra"
	config_yaml = file("testdata/rules.yaml")
  }
`

const testAccResourceNamespaceSuppress2 = `
resource "mimirtool_ruler_namespace" "alerts" {
	namespace = "infra"
	config_yaml = file("testdata/rules.yaml")
  }
`

func TestAccResourceNamespaceSuppress(t *testing.T) {
	resource.UnitTest(t, resource.TestCase{
		PreCheck:          func() { testAccPreCheck(t) },
		ProviderFactories: testAccProviderFactories,
		Steps: []resource.TestStep{
			{
				Config: testAccResourceNamespaceSuppress,
				Check: resource.ComposeTestCheckFunc(
					resource.TestCheckResourceAttr(
						"mimirtool_ruler_namespace.alerts", "namespace", "alerts_infra"),
				),
			},
			{
				Config: testAccResourceNamespaceSuppress2,
				Check: resource.ComposeTestCheckFunc(
					resource.TestCheckResourceAttr(
						"mimirtool_ruler_namespace.alerts", "namespace", "infra"),
				),
			},
		},
	})
}

I'll submit a PR soon :)