Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/managementrouter/health_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (s *healthStubManagementClient) ListRules(ctx context.Context, prOptions ma
return nil, nil
}

func (s *healthStubManagementClient) GetRuleById(ctx context.Context, alertRuleId string) (monitoringv1.Rule, error) {
return monitoringv1.Rule{}, nil
}

func (s *healthStubManagementClient) CreateUserDefinedAlertRule(ctx context.Context, alertRule monitoringv1.Rule, prOptions management.PrometheusRuleOptions) (string, error) {
return "", nil
}
Expand All @@ -128,6 +132,18 @@ func (s *healthStubManagementClient) DeleteUserDefinedAlertRuleById(ctx context.
return nil
}

func (s *healthStubManagementClient) UpdatePlatformAlertRule(ctx context.Context, alertRuleId string, alertRule monitoringv1.Rule) error {
return nil
}

func (s *healthStubManagementClient) DropPlatformAlertRule(ctx context.Context, alertRuleId string) error {
return nil
}

func (s *healthStubManagementClient) RestorePlatformAlertRule(ctx context.Context, alertRuleId string) error {
return nil
}

func (s *healthStubManagementClient) GetAlerts(ctx context.Context, req k8s.GetAlertsRequest) ([]k8s.PrometheusAlert, error) {
return nil, nil
}
Expand Down
16 changes: 16 additions & 0 deletions internal/managementrouter/rules_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func (s *stubManagementClient) ListRules(ctx context.Context, prOptions manageme
return nil, nil
}

func (s *stubManagementClient) GetRuleById(ctx context.Context, alertRuleId string) (monitoringv1.Rule, error) {
return monitoringv1.Rule{}, nil
}

func (s *stubManagementClient) CreateUserDefinedAlertRule(ctx context.Context, alertRule monitoringv1.Rule, prOptions management.PrometheusRuleOptions) (string, error) {
return "", nil
}
Expand All @@ -157,6 +161,18 @@ func (s *stubManagementClient) DeleteUserDefinedAlertRuleById(ctx context.Contex
return nil
}

func (s *stubManagementClient) UpdatePlatformAlertRule(ctx context.Context, alertRuleId string, alertRule monitoringv1.Rule) error {
return nil
}

func (s *stubManagementClient) DropPlatformAlertRule(ctx context.Context, alertRuleId string) error {
return nil
}

func (s *stubManagementClient) RestorePlatformAlertRule(ctx context.Context, alertRuleId string) error {
return nil
}

func (s *stubManagementClient) GetAlerts(ctx context.Context, req k8s.GetAlertsRequest) ([]k8s.PrometheusAlert, error) {
return nil, nil
}
Expand Down
51 changes: 45 additions & 6 deletions pkg/management/delete_user_defined_alert_rule_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *client) deletePlatformAlertRuleById(ctx context.Context, relabeled moni
return fmt.Errorf("failed to get AlertingRule %s: %w", arName, err)
}
if !found || ar == nil {
return &NotFoundError{Resource: "AlertingRule", Id: arName}
return c.deleteUserAlertRuleById(ctx, namespace, name, alertRuleId)
}
// Common preconditions for platform delete
if err := validatePlatformDeletePreconditions(ar); err != nil {
Expand All @@ -84,9 +84,20 @@ func (c *client) deletePlatformAlertRuleById(ctx context.Context, relabeled moni
AdditionalInfo: fmt.Sprintf("alert %q not found in AlertingRule %s", originalRule.Alert, arName),
}
}
ar.Spec.Groups = newGroups
if err := c.k8sClient.AlertingRules().Update(ctx, *ar); err != nil {
return fmt.Errorf("failed to update AlertingRule %s: %w", ar.Name, err)

if len(newGroups) == 0 {
if err := c.k8sClient.AlertingRules().Delete(ctx, ar.Name); err != nil {
return fmt.Errorf("failed to delete AlertingRule %s: %w", ar.Name, err)
}
} else {
ar.Spec.Groups = newGroups
if err := c.k8sClient.AlertingRules().Update(ctx, *ar); err != nil {
return fmt.Errorf("failed to update AlertingRule %s: %w", ar.Name, err)
}
}

if err := c.deleteAssociatedARC(ctx, k8s.ClusterMonitoringNamespace, name, alertRuleId); err != nil {
return fmt.Errorf("failed to clean up ARC for platform rule %s: %w", alertRuleId, err)
}
return nil
}
Expand All @@ -100,6 +111,9 @@ func (c *client) deleteUserAlertRuleById(ctx context.Context, namespace, name, a
if !found {
return &NotFoundError{Resource: "PrometheusRule", Id: fmt.Sprintf("%s/%s", namespace, name)}
}
if gitOpsManaged, _ := k8s.IsExternallyManagedObject(pr); gitOpsManaged {
return notAllowedGitOpsRemove()
}

updated := false
var newGroups []monitoringv1.RuleGroup
Expand All @@ -120,14 +134,39 @@ func (c *client) deleteUserAlertRuleById(ctx context.Context, namespace, name, a
if err := c.k8sClient.PrometheusRules().Delete(ctx, pr.Namespace, pr.Name); err != nil {
return fmt.Errorf("failed to delete PrometheusRule %s/%s: %w", pr.Namespace, pr.Name, err)
}
return nil
return c.cleanupARCForDeletedRule(ctx, namespace, name, alertRuleId)
}

pr.Spec.Groups = newGroups
if err := c.k8sClient.PrometheusRules().Update(ctx, *pr); err != nil {
return fmt.Errorf("failed to update PrometheusRule %s/%s: %w", pr.Namespace, pr.Name, err)
}
return nil
return c.cleanupARCForDeletedRule(ctx, namespace, name, alertRuleId)
}

// cleanupARCForDeletedRule attempts to remove any associated ARC after a rule is deleted.
// It determines the ARC namespace from the rule's namespace and silently skips if no ARC exists.
func (c *client) cleanupARCForDeletedRule(ctx context.Context, namespace, name, alertRuleId string) error {
nn := types.NamespacedName{Namespace: namespace, Name: name}
arcNamespace, err := c.arcNamespaceForRule(nn)
if err != nil {
return nil
}
return c.deleteAssociatedARC(ctx, arcNamespace, name, alertRuleId)
}

// deleteAssociatedARC removes the AlertRelabelConfig associated with an alert rule, if it exists.
// This is best-effort: if the ARC does not exist or is GitOps-managed, it is silently skipped.
func (c *client) deleteAssociatedARC(ctx context.Context, namespace, prName, alertRuleId string) error {
arcName := k8s.GetAlertRelabelConfigName(prName, alertRuleId)
arc, found, err := c.k8sClient.AlertRelabelConfigs().Get(ctx, namespace, arcName)
if err != nil || !found {
return nil
}
if gitOpsManaged, _ := k8s.IsExternallyManagedObject(arc); gitOpsManaged {
return nil
}
return c.k8sClient.AlertRelabelConfigs().Delete(ctx, namespace, arcName)
}

// removeAlertFromAlertingRuleGroups removes all instances of an alert by alert name across groups.
Expand Down
Loading