1e1adafeec
This adds validating admission hooks to enforce the requirements on AlertRules and RecordingRules that are currently enforced through the provisioning service and storage mechanisms in preparation of a consistent validation in both legacy storage and unified storage. It also adds a mutating admission hook to the app to ensure that folder annotations and folder labels are kept in sync so we can perform label-selector lists.
46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package alertrule
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana-app-sdk/app"
|
|
"github.com/grafana/grafana-app-sdk/simple"
|
|
v1 "github.com/grafana/grafana/apps/alerting/rules/pkg/apis/alerting/v0alpha1"
|
|
"github.com/grafana/grafana/apps/alerting/rules/pkg/app/config"
|
|
)
|
|
|
|
func NewMutator(cfg config.RuntimeConfig) *simple.Mutator {
|
|
return &simple.Mutator{
|
|
MutateFunc: func(ctx context.Context, req *app.AdmissionRequest) (*app.MutatingResponse, error) {
|
|
// Mutate folder label to match folder UID from annotation
|
|
r, ok := req.Object.(*v1.AlertRule)
|
|
if !ok || r == nil {
|
|
// Nothing to do or wrong type; no mutation
|
|
return nil, nil
|
|
}
|
|
|
|
// Read folder UID from annotation
|
|
folderUID := ""
|
|
if r.Annotations != nil {
|
|
folderUID = r.Annotations[v1.FolderAnnotationKey]
|
|
}
|
|
|
|
// Ensure labels map exists and set the folder label if folderUID is present
|
|
if folderUID != "" {
|
|
if r.Labels == nil {
|
|
r.Labels = make(map[string]string)
|
|
}
|
|
// Maintain folder metadata label for downstream systems (alertmanager grouping etc.)
|
|
r.Labels[v1.FolderLabelKey] = folderUID
|
|
}
|
|
|
|
// clamp all duration fields
|
|
if err := r.Spec.ClampDurations(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &app.MutatingResponse{UpdatedObject: r}, nil
|
|
},
|
|
}
|
|
}
|