Skip to content

Commit 910f863

Browse files
authored
[Rob 1901] custom rule label (#1957)
* add changes to rules * added docs * added update to labels/rules on helm upgrade * bugfix * updates labels correctly after helm upgrade
1 parent 621ad02 commit 910f863

5 files changed

Lines changed: 56 additions & 9 deletions

File tree

docs/setup-robusta/alertsui.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ Choose the appropriate instructions below, based on whether you use the Promethe
6666
6767
Then perform a :ref:`Helm Upgrade <Simple Upgrade>`.
6868

69+
70+
Customizing PrometheusRule Labels and Group Name
71+
**************************************************
72+
73+
When you create alerts in the Robusta Alerts UI, they are synced to your cluster as PrometheusRule CRDs. You can customize the labels and group name that are applied to these UI-created rules.
74+
75+
Add the following to your ``globalConfig`` section in ``generated_values.yaml``:
76+
77+
.. code-block:: yaml
78+
79+
globalConfig:
80+
# Custom group name for PrometheusRule alerts created from the Robusta Alerts UI
81+
prometheus_rule_group_name: "robusta-rule-group"
82+
83+
# Custom labels to add to PrometheusRule metadata for rules created from the UI
84+
prometheus_rule_custom_labels:
85+
my_label: "my_value"
86+
87+
Then perform a :ref:`Helm Upgrade <Simple Upgrade>`.
88+
89+
.. note::
90+
These settings only apply to PrometheusRules created from the Robusta Alerts UI.
91+
6992

7093
Disabling the Feature
7194
---------------------------------

helm/robusta/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ globalConfig:
3434
signing_key: ""
3535
custom_annotations: []
3636
custom_severity_map: {}
37+
38+
# Custom group name for PrometheusRule alerts managed by Robusta UI
39+
prometheus_rule_group_name: "kubernetes-apps"
40+
# Custom labels to add to PrometheusRule metadata for alert selection
41+
prometheus_rule_custom_labels: {}
3742

3843
# see https://docs.robusta.dev/master/user-guide/configuration/additional-settings.html#relabel-prometheus-alerts
3944
alertRelabel: []

src/robusta/core/sinks/robusta/robusta_sink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(self, sink_config: RobustaSinkConfigWrapper, registry):
9595
self.__prometheus_discovery_util = PrometheusDiscoveryUtils(
9696
discovery_period_sec=self.__discovery_period_sec, registry=registry
9797
)
98-
self.__rrm_checker = RRM(dal=self.dal, cluster=self.cluster_name, account_id=self.account_id)
98+
self.__rrm_checker = RRM(dal=self.dal, cluster=self.cluster_name, account_id=self.account_id, registry=registry)
9999
self.__pods_running_count: int = 0
100100

101101
self.registry.subscribe("scan_updated", self)

src/robusta/core/sinks/robusta/rrm/prometheus_alert_resource_manager.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def __init__(
1919
self,
2020
cluster: str,
2121
resource_kind: ResourceKind,
22+
custom_labels: Dict[str, str],
23+
group_name: Optional[str] = None,
2224
):
2325
super().__init__(resource_kind, cluster)
2426
self.__sleep = RRM_PERIOD_SEC
@@ -30,7 +32,8 @@ def __init__(
3032
self.__label_selector = f"{self.__identification_label}={self.__identification_label_value}"
3133
self.__plural = "prometheusrules"
3234
self.__group = "monitoring.coreos.com"
33-
self.__group_name = "kubernetes-apps"
35+
self.__group_name = group_name or "kubernetes-apps"
36+
self.__custom_labels = custom_labels
3437
self.__version = "v1"
3538
self.__k8_apiVersion = f"{self.__group}/{self.__version}"
3639
self.__kind = "PrometheusRule"
@@ -72,17 +75,22 @@ def __exisiting_rules_objects_map(self) -> Optional[Dict[str, Dict]]:
7275

7376
return result
7477

78+
def __get_labels(self) -> Dict:
79+
labels = {
80+
"release": RELEASE_NAME,
81+
"role": "alert-rules",
82+
self.__identification_label: self.__identification_label_value,
83+
}
84+
labels.update(self.__custom_labels)
85+
return labels
86+
7587
def __get_snapshot_body(self, name: str, rules: List[dict]):
7688
return {
7789
"apiVersion": self.__k8_apiVersion,
7890
"kind": self.__kind,
7991
"metadata": {
8092
"name": name,
81-
"labels": {
82-
"release": RELEASE_NAME,
83-
"role": "alert-rules",
84-
self.__identification_label: self.__identification_label_value,
85-
},
93+
"labels": self.__get_labels(),
8694
},
8795
"spec": {"groups": [{"name": self.__group_name, "rules": rules}]},
8896
}
@@ -100,6 +108,12 @@ def __create_cr_rules(
100108
existing_cr_obj["spec"]["groups"] = [{}]
101109

102110
existing_cr_obj["spec"]["groups"][0]["rules"] = rules
111+
# Update group name and labels - if helm chart is upgraded with new group name + labels, it will be updated here
112+
existing_cr_obj["spec"]["groups"][0]["name"] = self.__group_name
113+
if not existing_cr_obj.get("metadata"):
114+
existing_cr_obj["metadata"] = {}
115+
# Overwrite labels - if helm chart is upgraded with new custom labels, existing labels will be updated
116+
existing_cr_obj["metadata"]["labels"] = self.__get_labels()
103117

104118
# If a custom object with the given name already exists then replace the existing custom object with
105119
# the updated one.

src/robusta/core/sinks/robusta/rrm/rrm.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313

1414
# robusta resource management
1515
class RRM:
16-
def __init__(self, dal: AccountResourceFetcher, cluster: str, account_id: str):
16+
def __init__(self, dal: AccountResourceFetcher, cluster: str, account_id: str, registry):
1717
self.dal = dal
1818
self.cluster = cluster
1919
self.__sleep = RRM_PERIOD_SEC
2020
self.__latest_revision: Optional[datetime] = None
21+
global_config = registry.get_global_config()
22+
custom_labels = global_config.get("prometheus_rule_custom_labels", {})
23+
group_name = global_config.get("prometheus_rule_group_name")
2124
self.__resource_handler_map: Dict[ResourceKind, BaseResourceHandler] = {
2225
ResourceKind.PrometheusAlert: PrometheusAlertResourceHandler(
2326
cluster=self.cluster,
24-
resource_kind=ResourceKind.PrometheusAlert
27+
resource_kind=ResourceKind.PrometheusAlert,
28+
custom_labels=custom_labels,
29+
group_name=group_name
2530
)
2631
}
2732

0 commit comments

Comments
 (0)