Skip to content

Conversation

ldvc
Copy link
Contributor

@ldvc ldvc commented Aug 29, 2025

Changes

The config_yaml now contains the exact value submitted by the user. Additionally, a new attribute, remote_config_yaml, has been introduced to reflect the configuration as stored in the backend.

Here the alerts/node-exporter.yaml used for this demo:

namespace: node-exporter
groups:
  - name: NodeExporter
    rules:
      - alert: HostMemoryUnderMemoryPressure
        expr: "rate(node_vmstat_pgmajfault[1m]) > 1000"
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: Host memory under memory pressure (instance {{ $labels.instance }})
          description: "The node is under heavy memory pressure. High rate of major page faults\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

      - alert: HostSystemdServiceCrashed
        expr: 'node_systemd_unit_state{state="failed"} == 1'
        for: 0m
        labels:
          severity: warning
        annotations:
          summary: Host systemd service crashed (instance {{ $labels.instance }})
          description: "systemd service crashed\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
➜ terraform apply
mimirtool_ruler_namespace.alerts_node_exporter[0]: Refreshing state... [id=b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # mimirtool_ruler_namespace.alerts_node_exporter[0] will be updated in-place
  ~ resource "mimirtool_ruler_namespace" "alerts_node_exporter" {
      ~ config_yaml                 = <<-EOT
          - groups: []
          + namespace: node-exporter
          + groups:
          +     - name: NodeExporter
          +       rules:
          +         - alert: HostMemoryUnderMemoryPressure
          +           expr: "rate(node_vmstat_pgmajfault[1m]) > 1000"
          +           for: 2m
          +           labels:
          +             severity: warning
          +           annotations:
          +             description: |-
          +                 The node is under heavy memory pressure. High rate of major page faults
          +                   VALUE = {{ $value }}
          +                   LABELS = {{ $labels }}
          +             summary: Host memory under memory pressure (instance {{ $labels.instance }})
          +         - alert: HostSystemdServiceCrashed
          +           expr: 'node_systemd_unit_state{state="failed"} == 1'
          +           labels:
          +             severity: warning
          +           annotations:
          +             description: |-
          +                 systemd service crashed
          +                   VALUE = {{ $value }}
          +                   LABELS = {{ $labels }}
          +             summary: Host systemd service crashed (instance {{ $labels.instance }})
        EOT
        id                          = "b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321"
        # (3 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

mimirtool_ruler_namespace.alerts_node_exporter[0]: Modifying... [id=b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321]
mimirtool_ruler_namespace.alerts_node_exporter[0]: Modifications complete after 0s [id=b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

With the rewrite:

➜ terraform apply
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - ovh/mimirtool in /Users/lterrier/Code/github.com/ldvc/terraform-provider-mimirtool
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
mimirtool_ruler_namespace.alerts_node_exporter[0]: Refreshing state... [id=b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # mimirtool_ruler_namespace.alerts_node_exporter[0] will be updated in-place
  ~ resource "mimirtool_ruler_namespace" "alerts_node_exporter" {
      ~ config_yaml                 = <<-EOT
          + namespace: node-exporter
            groups:
          -     - name: NodeExporter
          -       rules:
          -         - alert: HostMemoryUnderMemoryPressure
          -           expr: rate(node_vmstat_pgmajfault[1m]) > 1000
          -           for: 2m
          -           labels:
          -             severity: warning
          -           annotations:
          -             description: |-
          -                 The node is under heavy memory pressure. High rate of major page faults
          -                   VALUE = {{ $value }}
          -                   LABELS = {{ $labels }}
          -             summary: Host memory under memory pressure (instance {{ $labels.instance }})
          -         - alert: HostSystemdServiceCrashed
          -           expr: node_systemd_unit_state{state="failed"} == 1
          -           labels:
          -             severity: warning
          -           annotations:
          -             description: |-
          -                 systemd service crashed
          -                   VALUE = {{ $value }}
          -                   LABELS = {{ $labels }}
          -             summary: Host systemd service crashed (instance {{ $labels.instance }})
          +   - name: NodeExporter
          +     rules:
          +       - alert: HostMemoryUnderMemoryPressure
          +         expr: "rate(node_vmstat_pgmajfault[1m]) > 1000"
          +         for: 2m
          +         labels:
          +           severity: warning
          +         annotations:
          +           summary: Host memory under memory pressure (instance {{ $labels.instance }})
          +           description: "The node is under heavy memory pressure. High rate of major page faults\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
          +
          +       - alert: HostSystemdServiceCrashed
          +         expr: 'node_systemd_unit_state{state="failed"} == 1'
          +         for: 0m
          +         labels:
          +           severity: warning
          +         annotations:
          +           summary: Host systemd service crashed (instance {{ $labels.instance }})
          +           description: "systemd service crashed\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
        EOT
        id                          = "b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321"
      ~ remote_config_yaml          = <<-EOT
            groups:
                - name: NodeExporter
                  rules:
                    - alert: HostMemoryUnderMemoryPressure
                      expr: rate(node_vmstat_pgmajfault[1m]) > 1000
                      for: 2m
                      labels:
                        severity: warning
                      annotations:
                        description: |-
                            The node is under heavy memory pressure. High rate of major page faults
                              VALUE = {{ $value }}
                              LABELS = {{ $labels }}
                        summary: Host memory under memory pressure (instance {{ $labels.instance }})
                    - alert: HostSystemdServiceCrashed
                      expr: node_systemd_unit_state{state="failed"} == 1
                      labels:
                        severity: warning
                      annotations:
                        description: |-
                            systemd service crashed
                              VALUE = {{ $value }}
                              LABELS = {{ $labels }}
                        summary: Host systemd service crashed (instance {{ $labels.instance }})
        EOT -> (known after apply)
        # (3 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

mimirtool_ruler_namespace.alerts_node_exporter[0]: Modifying... [id=b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321]
mimirtool_ruler_namespace.alerts_node_exporter[0]: Modifications complete after 0s [id=b7ae971e183b39f3a6cbe93a538b5c010fb6c0bd478eeca4513047702889a321]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

@ldvc ldvc requested a review from a team as a code owner August 29, 2025 14:16
}

// mapStringFromTypesMap converts a types.Map to map[string]string for template handling.
func mapStringFromTypesMap(m types.Map) map[string]string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be in a helper/util file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to the existing common.go file.

alertmanagerConfig := plan.ConfigYAML.ValueString()
templates := mapStringFromTypesMap(plan.TemplatesConfigYAML)

err := cli.CreateAlertmanagerConfig(ctx, alertmanagerConfig, templates)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err := cli.CreateAlertmanagerConfig(ctx, alertmanagerConfig, templates)
err := r.client.CreateAlertmanagerConfig(ctx, alertmanagerConfig, templates)


state.ConfigYAML = types.StringValue(alertmanagerConfig)

// Convert templates map[string]string to types.Map
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why not using mapStringFromTypesMap here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is the opposite, we have a map of string and we want a Terraform types.Map value.
Added a new dedicated function typeMapFromMapString.

alertmanagerConfig := plan.ConfigYAML.ValueString()
templates := mapStringFromTypesMap(plan.TemplatesConfigYAML)

err := client.CreateAlertmanagerConfig(ctx, alertmanagerConfig, templates)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err := client.CreateAlertmanagerConfig(ctx, alertmanagerConfig, templates)
err := r.client.CreateAlertmanagerConfig(ctx, alertmanagerConfig, templates)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks.

@wilfriedroset wilfriedroset merged commit b9bc262 into ovh:main Sep 17, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants