Skip to content

Commit

Permalink
Merge pull request #186 from appuio/feat/exclude-namespaces
Browse files Browse the repository at this point in the history
Add parameter `alerts.excludeNamespaces` to exclude specific namespaces from base alerts
  • Loading branch information
simu authored Oct 18, 2023
2 parents f96735b + f4c5a1c commit 5a72ed3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 59 deletions.
1 change: 1 addition & 0 deletions class/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ parameters:
- kube-.*
- openshift-.*
- syn.*
excludeNamespaces: []
ignoreNames:
- AlertmanagerReceiversNotConfigured
- CannotRetrieveUpdates
Expand Down
21 changes: 20 additions & 1 deletion component/rules.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,30 @@ local annotateRules = {
local renderedNamespaceSelector =
std.join('|', com.renderArray(params.alerts.includeNamespaces));

local renderedExcludesSelector =
std.join('|', com.renderArray(params.alerts.excludeNamespaces));

local renderedSelector =
local hasNsSel = std.length(renderedNamespaceSelector) > 0;
local hasExcSel = std.length(renderedExcludesSelector) > 0;
(
if hasNsSel then
'namespace=~"(%s)"' % renderedNamespaceSelector
else ''

) + (
if hasNsSel && hasExcSel then ',' else ''
) + (
if std.length(renderedExcludesSelector) > 0 then
'namespace!~"(%s)"' % renderedExcludesSelector
else ''
);

local patchExpr(expr) =
std.strReplace(
expr,
'namespace=~"(openshift-.*|kube-.*|default)"',
'namespace=~"(%s)"' % renderedNamespaceSelector
renderedSelector
);

local rulePatches =
Expand Down
32 changes: 31 additions & 1 deletion docs/modules/ROOT/pages/references/parameters.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ default:: https://github.com/appuio/component-openshift4-monitoring/blob/master/

List of namespace patterns to use for alerts which have `namespace=~"(openshift-.\*|kube-.*|default)"` in the upstream rule.
The component generates a regex pattern from the list by concatenating all elements into a large OR-regex.
To inject the custom regex, the component searches for the exact string `namespace=~"(openshift-.\*|kube-.*|default)"` in field `expr` of each alert rule and replaces it with the regex generated from the parameter.
To inject the custom regex, the component searches for the exact string `namespace=~"(openshift-.\*|kube-.*|default)"` in field `expr` of each alert rule and replaces it with the regex generated from this parameter and parameter `excludeNamespaces`.

The component processes the list with `com.renderArray()` to allow users to drop entries in the hierarchy.

Expand All @@ -372,6 +372,36 @@ includeNamespaces:

The component will generate namespace selector `namespace=~"(default|syn.*)"` from this input configuration.

=== `excludeNamespaces`

[horizontal]
type:: list
default:: `[]`

List of namespace patterns to exclude for alerts which have `namespace=~"(openshift-.\*|kube-.*|default)"` in the upstream rule.
The component generates a regex pattern from the list by concatenating all elements into a large OR-regex.
To inject the custom regex, the component searches for the exact string `namespace=~"(openshift-.\*|kube-.*|default)"` in field `expr` of each alert rule and replaces it with the regex generated from this parameter and parameter `includeNamespaces`.

The component processes the list with `com.renderArray()` to allow users to drop entries in the hierarchy.

IMPORTANT: The component doesn't validate that the list entries are valid regex patterns.

==== Example

We assume that the input config has patterns `default` and `openshift.*` and `syn.*` for `includeNamespaces` and `openshift-adp` for `excludeNamespaces`:

[source,yaml]
----
includeNamespaces:
- default
- openshift.*
- syn.*
excludeNamespaces:
- openshift-adp
----

The component will generate namespace selector `namespace=~"(default|openshift.*|syn.*)",namespace!~"(openshift-adp)"` from this input configuration.

=== `ignoreNames`

[horizontal]
Expand Down
2 changes: 2 additions & 0 deletions tests/custom-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ parameters:
manifests_version: release-4.13

alerts:
excludeNamespaces:
- openshift-adp
patchRules:
'*':
HighOverallControlPlaneMemory:
Expand Down
Loading

0 comments on commit 5a72ed3

Please sign in to comment.