Alerting: Add validation to check updates on rule groups (#113669)

This moves some of the validation logic for rule groups from the legacy storage layer to the validator.
This commit is contained in:
Moustafa Baiou
2025-11-10 14:40:35 -05:00
committed by GitHub
parent 142340e0ff
commit dd0a2d4cff
6 changed files with 198 additions and 54 deletions
@@ -126,11 +126,6 @@ func (s *legacyStorage) Create(ctx context.Context, obj runtime.Object, createVa
if p.GenerateName != "" {
return nil, fmt.Errorf("generate-name is not supported in legacy storage mode")
}
// TODO: move this to the validation function
if p.Labels[model.GroupLabelKey] != "" || p.Labels[model.GroupIndexLabelKey] != "" {
return nil, k8serrors.NewBadRequest("cannot set group when creating alert rule")
}
model, provenance, err := convertToDomainModel(info.OrgID, p)
if err != nil {
return nil, err
@@ -160,12 +155,6 @@ func (s *legacyStorage) Update(ctx context.Context, name string, objInfo rest.Up
return old, false, err
}
current, ok := old.(*model.AlertRule)
if !ok {
// this shouldn't really be possible
return nil, false, k8serrors.NewBadRequest("expected valid alert rule object")
}
obj, err := objInfo.UpdatedObject(ctx, old)
if err != nil {
return old, false, err
@@ -180,9 +169,6 @@ func (s *legacyStorage) Update(ctx context.Context, name string, objInfo rest.Up
if !ok {
return nil, false, k8serrors.NewBadRequest("expected valid alert rule object")
}
if current.Labels[model.GroupLabelKey] == "" && new.Labels[model.GroupLabelKey] != "" {
return nil, false, k8serrors.NewBadRequest("cannot set group label when updating un-grouped alert rule")
}
model, provenance, err := convertToDomainModel(info.OrgID, new)
if err != nil {
@@ -162,11 +162,6 @@ func (s *legacyStorage) Update(ctx context.Context, name string, objInfo rest.Up
return nil, false, err
}
current, ok := old.(*model.RecordingRule)
if !ok {
return nil, false, k8serrors.NewBadRequest("expected valid recording rule object")
}
obj, err := objInfo.UpdatedObject(ctx, old)
if err != nil {
return old, false, err
@@ -185,10 +180,6 @@ func (s *legacyStorage) Update(ctx context.Context, name string, objInfo rest.Up
if new.Name != "" {
new.UID = types.UID(new.Name)
}
// TODO: move to validation function
if current.Labels[model.GroupLabelKey] == "" && new.Labels[model.GroupLabelKey] != "" {
return nil, false, k8serrors.NewBadRequest("cannot set group label when updating un-grouped recording rule")
}
model, provenance, err := convertToDomainModel(info.OrgID, new)
if err != nil {