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

mimirtool: add ability to rule get that output a valid yaml for rule load #7054

Open
edwintye opened this issue Jan 4, 2024 · 0 comments · May be fixed by #8142
Open

mimirtool: add ability to rule get that output a valid yaml for rule load #7054

edwintye opened this issue Jan 4, 2024 · 0 comments · May be fixed by #8142

Comments

@edwintye
Copy link
Contributor

edwintye commented Jan 4, 2024

Is your feature request related to a problem? Please describe.

This is possibly two different feature requests but combining them here in the first instance for clarity.

  1. The current command mimirtool rules get <namespace> <group> prints the output of the rule to the console but it is not a valid yaml. The path /prometheus/config/v1/rules/<namespace>/<group> is printed out here, and even if we ignore this line the output fails mimirtool rules lint since the output is a []rwrulefmt.RuleGroup and should be nested under groups. I would like the option to output a valid yaml, preferably save it to disk directly.
  2. Following on, there is no way to get all the rules from mimir without looping through all the namespaces and groups. This is also partially solved by mimirtool rules print which does print out all the rules, but like above, the output is not a yaml that is ready for consumption by mimirtool itself.

The main driving force here is that using the UI to create rules initially to validate and test it is common, then the rules will be exported into a repo which runs through normal CI/CD (or even use mimirtool diff/sync) at some later point . However, this migration from UI -> repo step is proving to be rather cumbersome. We would like to remove the need to wrap mimirtool inside a bash script, and every developer can just use mimirtool directly to perform simple commands.

Describe the solution you'd like

I have the bare-bone solution, but the UX is really quite poor at this point. A simple solution to item 1. is probably just add the function below to pkg/mimirtool/commands/rules.go

func saveNamespaceRuleGroup(ns string, ruleGroup []rwrulefmt.RuleGroup) {
	rule := map[string]rules.RuleNamespace{ns: rules.RuleNamespace{
		Namespace: ns,
		Filepath:  fmt.Sprintf("%s.yaml", ns),
		Groups:    ruleGroup,
	}}
	if err := save(rule, true); err != nil {
		log.Infof("Failed to save rule to %s.yaml", ns)
	}
}

then code block below to func (r *RuleCommand) getRuleGroup(_ *kingpin.ParseContext) error {} which will write a valid rule file as <namespace>.yaml. This will also preserve the existing behaviour.

	if r.SaveFile {
		log.Debugf("Writing to file %s.yaml", r.Namespace)
		saveNamespaceRuleGroup(r.Namespace, []rwrulefmt.RuleGroup{*group})
       }

Unfortunately, this means we can't change the file path or name. Personally I am not sure whether the option to change the file name is that important, but the path is probably desirable in most cases. We can probably add another flag - yup, I am well aware that adding two flags for one feature is not ideal - say --output-dir="." to allow a different path.

We can re-use the same function for item 2.; either add the write to file ability to mimirtool rules print (seems to be self contradicting now that print also writes), i.e. add to func (r *RuleCommand) printRules(_ *kingpin.ParseContext) error {}

	if r.SaveFile {
		for ns, ruleGroup := range ruleNS {
			saveNamespaceRuleGroup(ns, ruleGroup)
		}
	}

or add a new command (I can hear people screaming at me already) like get-all which would essentially be printRules without the print and instead write to file.

Describe alternatives you've considered

For 1., we can sort of achieve this with bash via a tail -n +2 and yq to massage the existing output.

For 2., we can do an awk on first and third column from the output of mimirtool rules list and loop through the namespace/group combination using the tools we developed to solve for item 1.

Additional context

Adding the code blocks above as a git patch as attachment.

@gotjosh gotjosh linked a pull request May 15, 2024 that will close this issue
4 tasks
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 a pull request may close this issue.

1 participant