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

YAML format of rules get should match rules sync #137

Open
lukebigum opened this issue Jan 13, 2021 · 2 comments
Open

YAML format of rules get should match rules sync #137

lukebigum opened this issue Jan 13, 2021 · 2 comments
Labels

Comments

@lukebigum
Copy link

It would be helpful if the output format of cortextool rules get <namespace> <group> matched the exact format expected to cortextool rules sync. This would make it easier to get down all rules and commit them into Git, then sync them back again. Right now I have to munge the YAML slightly to make it compatible to sync.

Right now the format is this:

$ cortextool rules get somenamespace anygroup
name: anygroup
rules:
    - alert: FrontEnd Prometheus
      expr: .......

Ideally it would be this:

namespace: somenamespace
groups:
    - name: anygroup
      rules:
        - alert: FrontEnd Prometheus
          expr: ........
@lukebigum
Copy link
Author

A simper test:

$ cortextool rules print --disable-color > tmp.yml
$ cortextool rules sync tmp.yml 
ERRO[0000] unable parse rules file                       error="yaml: unmarshal errors:\n  line 1: field group1 not found in type rules.RuleNamespace\n  line 13: field group2 not found in type rules.RuleNamespace" file=tmp.yml
cortextool: error: sync operation unsuccessful, unable to parse rules files: file read error, try --help

@gotjosh gotjosh added command/cortex-tool enhancement New feature or request labels Feb 4, 2021
@Granddave
Copy link

To circumvent this annoyance I've written a small script to convert the content generated by coretextool rules print.

$ cortextool rules print --disable-color > tmp.yml
$ ./split.py tmp.yml
$ rules check --rule-dirs=rules && echo OK
OK
#!/usr/bin/env python3

import argparse
import os
import yaml


def _load_yaml(path):
    with open(path) as file:
        return yaml.load(file, Loader=yaml.FullLoader)


def _write_yaml(data, path):
    with open(path, "w") as file:
        yaml.dump(data, file, sort_keys=False, width=9999)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("namespaces_file", type=str)
    args = parser.parse_args()

    base_dir = os.path.dirname(os.path.abspath(args.namespaces_file))
    rules_dir = os.path.join(base_dir, "rules")
    os.makedirs(rules_dir)

    for namespace, groups in _load_yaml(args.namespaces_file).items():
        _write_yaml(
            data={"namespace": namespace, "groups": groups},
            path=os.path.join(rules_dir, f"{namespace}.yaml"),
        )


if __name__ == "__main__":
    main()

friedrichg pushed a commit to cortexproject/cortex-tools that referenced this issue Aug 1, 2023
… size (grafana#137)

Signed-off-by: Akarsh <akarsh.satija@gmail.com>

Co-authored-by: Ken Haines <khaines@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants