Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use syn-teams.libsonnet for alert routing discovery and alert patching #224

Merged
merged 2 commits into from
Dec 23, 2024
Merged
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
9 changes: 6 additions & 3 deletions component/alert-routing-discovery.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local com = import 'lib/commodore.libjsonnet';
local kap = import 'lib/kapitan.libjsonnet';
local kube = import 'lib/kube.libjsonnet';
local prom = import 'lib/prom.libsonnet';
local syn_teams = import 'syn/syn-teams.libsonnet';

local inv = kap.inventory();
local params = inv.parameters;
Expand All @@ -25,7 +26,7 @@ local discoverNS = function(app)
else if std.isObject(p.namespace) && std.objectHas(p.namespace, 'name') && std.isString(p.namespace.name) then
p.namespace.name;

local ks = prom.appKeys(app);
local ks = syn_teams.appKeys(app);
local aliased = f(ks[0]);
if aliased != null then
aliased
Expand All @@ -44,7 +45,9 @@ local teamToNS = std.mapWithKey(
function(_, a) std.uniq(std.sort(std.prune(a))),
std.foldl(
function(prev, app)
prev { [prom.teamForApplication(app)]+: [ discoverNS(app) ] },
local instance = syn_teams.appKeys(app, true)[0];
local team = syn_teams.teamForApplication(instance);
prev { [team]+: [ discoverNS(app) ] },
inv.applications,
{}
)
Expand Down Expand Up @@ -86,7 +89,7 @@ local alertmanagerConfig =
debugConfigMap: kube.ConfigMap('discovery-debug') {
data: {
local discoveredNamespaces = std.foldl(function(prev, app) prev { [app]: discoverNS(app) }, inv.applications, {}),
local discoveredTeams = std.foldl(function(prev, app) prev { [app]: prom.teamForApplication(app) }, inv.applications, {}),
local discoveredTeams = std.foldl(function(prev, app) prev { [app]: syn_teams.teamForApplication(syn_teams.appKeys(app, true)[0]) }, inv.applications, {}),
applications: std.manifestJsonMinified(inv.applications),
discovered_namespaces: std.manifestYamlDoc(discoveredNamespaces),
apps_without_namespaces: std.manifestYamlDoc(std.foldl(function(prev, app) if discoveredNamespaces[app] == null then prev + [ app ] else prev, std.objectFields(discoveredNamespaces), [])),
Expand Down
10 changes: 10 additions & 0 deletions jsonnetfile.jsonnet
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
version: 1,
dependencies: std.prune([
{
source: {
git: {
remote: 'https://github.com/projectsyn/jsonnet-libs',
subdir: '',
},
},
version: 'main',
name: 'syn',
},
{
source: {
git: {
Expand Down
4 changes: 2 additions & 2 deletions lib/openshift4-monitoring-alert-patching.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// arbitrary alert rules to adhere to the format required by the component's
// approach for allowing us to patch upstream rules.
local com = import 'lib/commodore.libjsonnet';
local prom = import 'lib/prom.libsonnet';
local syn_teams = import 'syn/syn-teams.libsonnet';

local inv = com.inventory();

Expand Down Expand Up @@ -113,7 +113,7 @@ local patchRule(rule, patches={}, patchName=true) =
then
rule.labels.syn_team
else
prom.teamForApplication(inv.parameters._instance);
syn_teams.teamForApplication(inv.parameters._instance);
rule {
// Change alert names so we don't get multiple alerts with the same
// name, as the logging operator deploys its own copy of these
Expand Down
78 changes: 0 additions & 78 deletions lib/openshift4-monitoring-prom.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
* API reference: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md
*/

local com = import 'lib/commodore.libjsonnet';
local kube = import 'lib/kube.libjsonnet';

local alertpatching = import 'lib/alert-patching.libsonnet';

local inv = com.inventory();

// Define Prometheus Operator API versions
local api_version = {
monitoring: 'monitoring.coreos.com/v1',
Expand Down Expand Up @@ -57,81 +54,6 @@ local prometheusRule(name) =
Alertmanager(name):
kube._Object(api_version.monitoring, 'Alertmanager', name),

/**
* \brief Returns an array with the (aliased) application name and if aliased the original name in the second position.
*
* The application name is translated from kebab-case to snake_case, except if the second parameter is set to true.
*
* \arg name
* The application name. Can be `name` or `name as alias`.
* \arg raw
* If set to true, the application name is not translated from kebab-case to snake_case.
* \return
* An array with the (aliased) application name and if aliased the original name in the second position.
*/
appKeys: function(name, raw=false)
local normalized = function(name) if raw then name else std.strReplace(name, '-', '_');
// can be simplified with jsonnet > 0.19 which would support ' as ' as the substring
local parts = std.split(name, ' ');
if std.length(parts) == 1 then
[ normalized(parts[0]) ]
else if std.length(parts) == 3 && parts[1] == 'as' then
[ normalized(parts[2]), normalized(parts[0]) ]
else
error 'invalid application name `%s`' % name,

/**
* \brief Returns the team for the given application or null.
*
* It does so by looking at the top level syn parameter.
* The syn parameter should look roughly like this.
*
* syn:
* owner: clumsy-donkeys
* teams:
* chubby-cockroaches:
* instances:
* - superb-visualization
* lovable-lizards:
* instances:
* - apartment-cats
*
* The application is first looked up in the instances of the teams, if no team is found, owner is used as fallback.
* An error is thrown if the application is found belonging to multiple teams.
*
* \arg app
* The application name. Can be the merged `inventory().params._instance` or an (aliased) application name.
* \return
* The team name or `null` if no team is found.
*/
teamForApplication: function(app)
local params = inv.parameters;
local lookup = function(app)
if std.objectHas(params, 'syn') && std.objectHas(params.syn, 'teams') then
local teams = params.syn.teams;
local teamsForApp = std.foldl(
function(prev, team)
if std.objectHas(teams, team) && std.objectHas(teams[team], 'instances') && std.member(com.renderArray(teams[team].instances), app) then
prev + [ team ]
else
prev,
std.objectFields(teams),
[],
);
if std.length(teamsForApp) == 0 then
null
else if std.length(teamsForApp) == 1 then
teamsForApp[0]
else
error 'application `%s` is in multiple teams: %s' % [ app, std.join(', ', teamsForApp) ];

local teams = std.prune(std.map(lookup, self.appKeys(app, true)));

if std.length(teams) > 0 then
teams[0]
else if std.objectHas(params, 'syn') && std.objectHas(params.syn, 'owner') then
params.syn.owner,

/**
* \brief Function to render rules defined in the hierarchy
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ stringData:
- "continue": true
"matchers":
- "syn_team = \"\""
- "namespace =~ \"ns-string\""
- "namespace =~ \"ns-string|openshift-monitoring\""
"receiver": "team_default_clumsy-donkeys"
- "continue": true
"matchers":
Expand All @@ -54,7 +54,7 @@ stringData:
- "continue": false
"matchers":
- "syn_team = \"\""
- "namespace =~ \"base|overridden|ns-string|ns-object|same-ns\""
- "namespace =~ \"base|overridden|ns-string|openshift-monitoring|ns-object|same-ns\""
"receiver": "__component_openshift4_monitoring_null"
- "matchers":
- "other = \"true\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data:
- "continue": true
"matchers":
- "syn_team = \"\""
- "namespace =~ \"ns-string\""
- "namespace =~ \"ns-string|openshift-monitoring\""
"receiver": "team_default_clumsy-donkeys"
- "continue": true
"matchers":
Expand All @@ -46,14 +46,14 @@ data:
- "continue": false
"matchers":
- "syn_team = \"\""
- "namespace =~ \"base|overridden|ns-string|ns-object|same-ns\""
- "namespace =~ \"base|overridden|ns-string|openshift-monitoring|ns-object|same-ns\""
"receiver": "__component_openshift4_monitoring_null"
- "matchers":
- "other = \"true\""
"receiver": "other"
- "receiver": "team_default_clumsy-donkeys"
applications: '["non-existing","no-ns","ns-string","ns-object","base as ns-in-base","base
as ns-overridden","non-existing as still-non-existing","same-ns-1","same-ns-2"]'
as ns-overridden","non-existing as still-non-existing","same-ns-1","same-ns-2","openshift4-monitoring"]'
apps_without_namespaces: |-
- "no-ns"
- "non-existing"
Expand All @@ -66,6 +66,7 @@ data:
"non-existing as still-non-existing": null
"ns-object": "ns-object"
"ns-string": "ns-string"
"openshift4-monitoring": "openshift-monitoring"
"same-ns-1": "same-ns"
"same-ns-2": "same-ns"
discovered_teams: |-
Expand All @@ -76,6 +77,7 @@ data:
"non-existing as still-non-existing": "clumsy-donkeys"
"ns-object": "lovable-lizards"
"ns-string": "clumsy-donkeys"
"openshift4-monitoring": "clumsy-donkeys"
"same-ns-1": "lovable-lizards"
"same-ns-2": "lovable-lizards"
proposed_routes: |-
Expand All @@ -87,7 +89,7 @@ data:
- "continue": true
"matchers":
- "syn_team = \"\""
- "namespace =~ \"ns-string\""
- "namespace =~ \"ns-string|openshift-monitoring\""
"receiver": "team_default_clumsy-donkeys"
- "continue": true
"matchers":
Expand All @@ -97,7 +99,7 @@ data:
- "continue": false
"matchers":
- "syn_team = \"\""
- "namespace =~ \"base|overridden|ns-string|ns-object|same-ns\""
- "namespace =~ \"base|overridden|ns-string|openshift-monitoring|ns-object|same-ns\""
"receiver": "__component_openshift4_monitoring_null"
kind: ConfigMap
metadata:
Expand Down
1 change: 1 addition & 0 deletions tests/team-routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ applications:
- non-existing as still-non-existing
- same-ns-1
- same-ns-2
- openshift4-monitoring

parameters:
kapitan:
Expand Down
Loading