Files
grafana/pkg/services/ngalert/provisioning/validation/provenance.go
T
Alexander Akhmetov 85b0b47efd Alerting: Allow disabling provenance in the Prometheus conversion API (#101573)
When creating Grafana-managed alerts from Prometheus rule definitions with mimirtool or cortextool, the rules are marked as "provisioned" and are not editable in the Grafana UI. This PR allows changing this by providing an extra header: --extra-header="X-Disable-Provenance=true".

When provenance is disabled, we do not keep the original rule definition in YAML, so it is impossible to read it back using the Prometheus conversion API (mimirtool/cortextool). This is intentional because if we did keep it and the rule was later changed in the UI, its Prometheus YAML definition would no longer reflect the latest version of the alert rule, as it would be unchanged.
2025-03-11 19:53:28 +01:00

51 lines
1.9 KiB
Go

package validation
import (
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
// CanUpdateProvenanceInRuleGroup checks if a provenance can be updated for a rule group and its alerts.
// ReplaceRuleGroup function intends to replace an entire rule group: inserting, updating, and removing rules.
func CanUpdateProvenanceInRuleGroup(storedProvenance, provenance models.Provenance) bool {
// Same provenance is always allowed
if storedProvenance == provenance {
return true
}
// Can always update stored ProvenanceNone
if storedProvenance == models.ProvenanceNone {
return true
}
// Can reset to ProvenanceNone from specific provenances
if provenance == models.ProvenanceNone {
return storedProvenance == models.ProvenanceAPI ||
storedProvenance == models.ProvenanceConvertedPrometheus
}
return false
}
type ProvenanceStatusTransitionValidator = func(from, to models.Provenance) error
// ValidateProvenanceRelaxed checks if the transition of provenance status from `from` to `to` is allowed.
// Applies relaxed checks that prevents only transition from any status to `none`.
// Returns ErrProvenanceChangeNotAllowed if transition is not allowed
func ValidateProvenanceRelaxed(from, to models.Provenance) error {
if from == models.ProvenanceNone { // allow any transition from none
return nil
}
if to == models.ProvenanceNone { // allow any transition to none unless it's from "none" either
return MakeErrProvenanceChangeNotAllowed(from, to)
}
return nil
}
// ValidateProvenanceOfDependentResources returns a list of allowed provenance statuses for dependent resources
// in the case when they need to be updated when the resource they depend on is changed.
func ValidateProvenanceOfDependentResources(parentProvenance models.Provenance) func(childProvenance models.Provenance) bool {
return func(childProvenance models.Provenance) bool {
return parentProvenance == childProvenance || childProvenance == models.ProvenanceNone
}
}