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.
80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana-app-sdk/app"
|
|
"github.com/grafana/grafana-app-sdk/logging"
|
|
"github.com/grafana/grafana-app-sdk/operator"
|
|
"github.com/grafana/grafana-app-sdk/resource"
|
|
"github.com/grafana/grafana-app-sdk/simple"
|
|
|
|
"github.com/grafana/grafana/apps/alerting/rules/pkg/apis"
|
|
"github.com/grafana/grafana/apps/alerting/rules/pkg/app/alertrule"
|
|
"github.com/grafana/grafana/apps/alerting/rules/pkg/app/config"
|
|
"github.com/grafana/grafana/apps/alerting/rules/pkg/app/recordingrule"
|
|
)
|
|
|
|
func New(cfg app.Config) (app.App, error) {
|
|
managedKinds := make([]simple.AppManagedKind, 0)
|
|
runtimeCfg, ok := cfg.SpecificConfig.(config.RuntimeConfig)
|
|
if !ok {
|
|
return nil, config.ErrInvalidRuntimeConfig
|
|
}
|
|
for _, kinds := range apis.GetKinds() {
|
|
for _, kind := range kinds {
|
|
managedKind := simple.AppManagedKind{
|
|
Kind: kind,
|
|
Validator: buildKindValidator(kind, runtimeCfg),
|
|
Mutator: buildKindMutator(kind, runtimeCfg),
|
|
}
|
|
managedKinds = append(managedKinds, managedKind)
|
|
}
|
|
}
|
|
|
|
c := simple.AppConfig{
|
|
Name: "alerting.rules",
|
|
KubeConfig: cfg.KubeConfig,
|
|
InformerConfig: simple.AppInformerConfig{
|
|
InformerOptions: operator.InformerOptions{
|
|
ErrorHandler: func(ctx context.Context, err error) {
|
|
logging.DefaultLogger.With("error", err).Error("Informer processing error")
|
|
},
|
|
},
|
|
},
|
|
ManagedKinds: managedKinds,
|
|
}
|
|
|
|
a, err := simple.NewApp(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = a.ValidateManifest(cfg.ManifestData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return a, nil
|
|
}
|
|
|
|
func buildKindValidator(kind resource.Kind, cfg config.RuntimeConfig) *simple.Validator {
|
|
switch kind.Kind() {
|
|
case "AlertRule":
|
|
return alertrule.NewValidator(cfg)
|
|
case "RecordingRule":
|
|
return recordingrule.NewValidator(cfg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func buildKindMutator(kind resource.Kind, cfg config.RuntimeConfig) *simple.Mutator {
|
|
switch kind.Kind() {
|
|
case "AlertRule":
|
|
return alertrule.NewMutator(cfg)
|
|
case "RecordingRule":
|
|
return recordingrule.NewMutator(cfg)
|
|
}
|
|
return nil
|
|
}
|