jsonnet: create function responsible for prometheus objects

Signed-off-by: paulfantom <pawel@krupa.net.pl>
This commit is contained in:
paulfantom 2021-01-13 09:53:03 +01:00
parent 6b11d79c4b
commit 9cf2ce9ffc
No known key found for this signature in database
GPG Key ID: 12AE0185401674E7
2 changed files with 483 additions and 476 deletions

View File

@ -5,16 +5,15 @@ local blackboxExporter = import './blackbox-exporter/blackbox-exporter.libsonnet
local kubeStateMetrics = import './kube-state-metrics/kube-state-metrics.libsonnet'; local kubeStateMetrics = import './kube-state-metrics/kube-state-metrics.libsonnet';
local nodeExporter = import './node-exporter/node-exporter.libsonnet'; local nodeExporter = import './node-exporter/node-exporter.libsonnet';
local prometheusAdapter = import './prometheus-adapter/prometheus-adapter.libsonnet'; local prometheusAdapter = import './prometheus-adapter/prometheus-adapter.libsonnet';
local prometheus = import './prometheus/prometheus.libsonnet';
local monitoringMixins = import './mixins/monitoring-mixins.libsonnet'; local monitoringMixins = import './mixins/monitoring-mixins.libsonnet';
(import 'github.com/brancz/kubernetes-grafana/grafana/grafana.libsonnet') + (import 'github.com/brancz/kubernetes-grafana/grafana/grafana.libsonnet') +
(import 'github.com/prometheus-operator/prometheus-operator/jsonnet/prometheus-operator/prometheus-operator.libsonnet') + (import 'github.com/prometheus-operator/prometheus-operator/jsonnet/prometheus-operator/prometheus-operator.libsonnet') +
(import './prometheus/prometheus.libsonnet') +
{ {
alertmanager: alertmanager({ alertmanager: alertmanager({
name: 'main', name: $._config.alertmanagerName,
namespace: $._config.namespace, namespace: $._config.namespace,
version: '0.21.0', version: '0.21.0',
image: 'quay.io/prometheus/alertmanager:v0.21.0', image: 'quay.io/prometheus/alertmanager:v0.21.0',
@ -34,6 +33,14 @@ local monitoringMixins = import './mixins/monitoring-mixins.libsonnet';
version: '1.0.1', version: '1.0.1',
image: 'quay.io/prometheus/node-exporter:v1.0.1', image: 'quay.io/prometheus/node-exporter:v1.0.1',
}), }),
prometheus: prometheus({
namespace: $._config.namespace,
version: '2.24.0',
image: 'quay.io/prometheus/prometheus:v2.24.0',
name: $._config.prometheusName,
alertmanagerName: $._config.alertmanagerName,
rules: $.allRules,
}),
prometheusAdapter: prometheusAdapter({ prometheusAdapter: prometheusAdapter({
namespace: $._config.namespace, namespace: $._config.namespace,
version: '0.8.2', version: '0.8.2',
@ -42,9 +49,24 @@ local monitoringMixins = import './mixins/monitoring-mixins.libsonnet';
}), }),
mixins+:: monitoringMixins({ mixins+:: monitoringMixins({
namespace: $._config.namespace, namespace: $._config.namespace,
alertmanagerName: 'main', alertmanagerName: $._config.alertmanagerName,
prometheusName: 'k8s', prometheusName: $._config.prometheusName,
}), }),
// FIXME(paulfantom) Remove this variable by moving each mixin to its own component
// Example: node_exporter mixin could be added in ./node-exporter/node-exporter.libsonnet
allRules::
$.mixins.nodeExporter.prometheusRules +
$.mixins.kubernetes.prometheusRules +
$.mixins.base.prometheusRules +
$.mixins.kubeStateMetrics.prometheusAlerts +
$.mixins.nodeExporter.prometheusAlerts +
$.mixins.alertmanager.prometheusAlerts +
$.mixins.prometheusOperator.prometheusAlerts +
$.mixins.kubernetes.prometheusAlerts +
$.mixins.prometheus.prometheusAlerts +
$.mixins.base.prometheusAlerts,
kubePrometheus+:: { kubePrometheus+:: {
namespace: { namespace: {
apiVersion: 'v1', apiVersion: 'v1',
@ -143,6 +165,8 @@ local monitoringMixins = import './mixins/monitoring-mixins.libsonnet';
} + { } + {
_config+:: { _config+:: {
namespace: 'default', namespace: 'default',
prometheusName: 'k8s',
alertmanagerName: 'main',
versions+:: { grafana: '7.3.5', kubeRbacProxy: 'v0.8.0' }, versions+:: { grafana: '7.3.5', kubeRbacProxy: 'v0.8.0' },
imageRepos+:: { kubeRbacProxy: 'quay.io/brancz/kube-rbac-proxy' }, imageRepos+:: { kubeRbacProxy: 'quay.io/brancz/kube-rbac-proxy' },
@ -187,24 +211,6 @@ local monitoringMixins = import './mixins/monitoring-mixins.libsonnet';
}, },
}, },
local allRules =
$.mixins.nodeExporter.prometheusRules +
$.mixins.kubernetes.prometheusRules +
$.mixins.base.prometheusRules +
$.mixins.kubeStateMetrics.prometheusAlerts +
$.mixins.nodeExporter.prometheusAlerts +
$.mixins.alertmanager.prometheusAlerts +
$.mixins.prometheusOperator.prometheusAlerts +
$.mixins.kubernetes.prometheusAlerts +
$.mixins.prometheus.prometheusAlerts +
$.mixins.base.prometheusAlerts,
local allDashboards =
$.mixins.nodeExporter.grafanaDashboards +
$.mixins.kubernetes.grafanaDashboards +
$.mixins.prometheus.grafanaDashboards,
prometheus+:: { rules: allRules },
grafana+:: { grafana+:: {
labels: { labels: {
'app.kubernetes.io/name': 'grafana', 'app.kubernetes.io/name': 'grafana',
@ -212,7 +218,12 @@ local monitoringMixins = import './mixins/monitoring-mixins.libsonnet';
'app.kubernetes.io/component': 'grafana', 'app.kubernetes.io/component': 'grafana',
'app.kubernetes.io/part-of': 'kube-prometheus', 'app.kubernetes.io/part-of': 'kube-prometheus',
}, },
dashboards: allDashboards, // FIXME(paulfantom): Same as with rules and alerts.
// This should be gathering all dashboards from components without having to enumerate all dashboards.
dashboards:
$.mixins.nodeExporter.grafanaDashboards +
$.mixins.kubernetes.grafanaDashboards +
$.mixins.prometheus.grafanaDashboards,
}, },
}, },
} }

View File

@ -1,49 +1,48 @@
local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabelings.libsonnet'; local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabelings.libsonnet';
{ local defaults = {
_config+:: { local defaults = self,
namespace: 'default', namespace: error 'must provide namespace',
version: error 'must provide version',
image: error 'must provide image',
resources: {
requests: { memory: '400Mi' },
},
versions+:: { prometheus: 'v2.22.1' }, name: error 'must provide name',
imageRepos+:: { prometheus: 'quay.io/prometheus/prometheus' }, alertmanagerName: error 'must provide alertmanagerName',
alertmanager+:: { name: 'main' }, namespaces: ['default', 'kube-system', defaults.namespace],
prometheus+:: {
name: 'k8s',
replicas: 2, replicas: 2,
rules: {}, rules: {
namespaces: ['default', 'kube-system', $._config.namespace], groups: [],
labels: { },
commonLabels:: {
'app.kubernetes.io/name': 'prometheus', 'app.kubernetes.io/name': 'prometheus',
'app.kubernetes.io/version': $._config.versions.prometheus, 'app.kubernetes.io/version': defaults.version,
'app.kubernetes.io/component': 'prometheus', 'app.kubernetes.io/component': 'prometheus',
'app.kubernetes.io/part-of': 'kube-prometheus', 'app.kubernetes.io/part-of': 'kube-prometheus',
}, },
selectorLabels: { selectorLabels:: {
[labelName]: $._config.prometheus.labels[labelName] [labelName]: defaults.commonLabels[labelName]
for labelName in std.objectFields($._config.prometheus.labels) for labelName in std.objectFields(defaults.commonLabels)
if !std.setMember(labelName, ['app.kubernetes.io/version']) if !std.setMember(labelName, ['app.kubernetes.io/version'])
}, } + { prometheus: defaults.name },
}, };
},
prometheus+:: {
function(params) {
local p = self, local p = self,
config:: defaults + params,
name:: $._config.prometheus.name, // Safety check
namespace:: $._config.namespace, assert std.isObject(p.config.resources),
roleBindingNamespaces:: $._config.prometheus.namespaces,
replicas:: $._config.prometheus.replicas,
prometheusRules:: $._config.prometheus.rules,
alertmanagerName:: $.alertmanager.service.metadata.name,
serviceAccount: { serviceAccount: {
apiVersion: 'v1', apiVersion: 'v1',
kind: 'ServiceAccount', kind: 'ServiceAccount',
metadata: { metadata: {
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: p.namespace, namespace: p.config.namespace,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
}, },
@ -51,15 +50,15 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
apiVersion: 'v1', apiVersion: 'v1',
kind: 'Service', kind: 'Service',
metadata: { metadata: {
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: p.namespace, namespace: p.config.namespace,
labels: { prometheus: p.name } + $._config.prometheus.labels, labels: { prometheus: p.config.name } + p.config.commonLabels,
}, },
spec: { spec: {
ports: [ ports: [
{ name: 'web', targetPort: 'web', port: 9090 }, { name: 'web', targetPort: 'web', port: 9090 },
], ],
selector: { app: 'prometheus', prometheus: p.name } + $._config.prometheus.selectorLabels, selector: { app: 'prometheus' } + p.config.selectorLabels,
sessionAffinity: 'ClientIP', sessionAffinity: 'ClientIP',
}, },
}, },
@ -69,14 +68,14 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'PrometheusRule', kind: 'PrometheusRule',
metadata: { metadata: {
labels: { labels: {
prometheus: p.name, prometheus: p.config.name,
role: 'alert-rules', role: 'alert-rules',
} + $._config.prometheus.labels, } + p.config.commonLabels,
name: 'prometheus-' + p.name + '-rules', name: 'prometheus-' + p.config.name + '-rules',
namespace: p.namespace, namespace: p.config.namespace,
}, },
spec: { spec: {
groups: p.prometheusRules.groups, groups: p.config.rules.groups,
}, },
}, },
@ -85,33 +84,33 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleBinding', kind: 'RoleBinding',
metadata: { metadata: {
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: namespace, namespace: namespace,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
roleRef: { roleRef: {
apiGroup: 'rbac.authorization.k8s.io', apiGroup: 'rbac.authorization.k8s.io',
kind: 'Role', kind: 'Role',
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
}, },
subjects: [{ subjects: [{
kind: 'ServiceAccount', kind: 'ServiceAccount',
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: p.namespace, namespace: p.config.namespace,
}], }],
}; };
{ {
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleBindingList', kind: 'RoleBindingList',
items: [newSpecificRoleBinding(x) for x in p.roleBindingNamespaces], items: [newSpecificRoleBinding(x) for x in p.config.namespaces],
}, },
clusterRole: { clusterRole: {
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'ClusterRole', kind: 'ClusterRole',
metadata: { metadata: {
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
rules: [ rules: [
{ {
@ -130,9 +129,9 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'Role', kind: 'Role',
metadata: { metadata: {
name: 'prometheus-' + p.name + '-config', name: 'prometheus-' + p.config.name + '-config',
namespace: p.namespace, namespace: p.config.namespace,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
rules: [{ rules: [{
apiGroups: [''], apiGroups: [''],
@ -145,19 +144,19 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleBinding', kind: 'RoleBinding',
metadata: { metadata: {
name: 'prometheus-' + p.name + '-config', name: 'prometheus-' + p.config.name + '-config',
namespace: p.namespace, namespace: p.config.namespace,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
roleRef: { roleRef: {
apiGroup: 'rbac.authorization.k8s.io', apiGroup: 'rbac.authorization.k8s.io',
kind: 'Role', kind: 'Role',
name: 'prometheus-' + p.name + '-config', name: 'prometheus-' + p.config.name + '-config',
}, },
subjects: [{ subjects: [{
kind: 'ServiceAccount', kind: 'ServiceAccount',
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: p.namespace, namespace: p.config.namespace,
}], }],
}, },
@ -165,18 +164,18 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'ClusterRoleBinding', kind: 'ClusterRoleBinding',
metadata: { metadata: {
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
roleRef: { roleRef: {
apiGroup: 'rbac.authorization.k8s.io', apiGroup: 'rbac.authorization.k8s.io',
kind: 'ClusterRole', kind: 'ClusterRole',
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
}, },
subjects: [{ subjects: [{
kind: 'ServiceAccount', kind: 'ServiceAccount',
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: p.namespace, namespace: p.config.namespace,
}], }],
}, },
@ -185,9 +184,9 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'Role', kind: 'Role',
metadata: { metadata: {
name: 'prometheus-' + p.name, name: 'prometheus-' + p.config.name,
namespace: namespace, namespace: namespace,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
rules: [ rules: [
{ {
@ -205,25 +204,25 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
{ {
apiVersion: 'rbac.authorization.k8s.io/v1', apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'RoleList', kind: 'RoleList',
items: [newSpecificRole(x) for x in p.roleBindingNamespaces], items: [newSpecificRole(x) for x in p.config.namespaces],
}, },
prometheus: { prometheus: {
apiVersion: 'monitoring.coreos.com/v1', apiVersion: 'monitoring.coreos.com/v1',
kind: 'Prometheus', kind: 'Prometheus',
metadata: { metadata: {
name: p.name, name: p.config.name,
namespace: p.namespace, namespace: p.config.namespace,
labels: { prometheus: p.name } + $._config.prometheus.labels, labels: { prometheus: p.config.name } + p.config.commonLabels,
}, },
spec: { spec: {
replicas: p.replicas, replicas: p.config.replicas,
version: $._config.versions.prometheus, version: p.config.version,
image: $._config.imageRepos.prometheus + ':' + $._config.versions.prometheus, image: p.config.image,
podMetadata: { podMetadata: {
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
serviceAccountName: 'prometheus-' + p.name, serviceAccountName: 'prometheus-' + p.config.name,
serviceMonitorSelector: {}, serviceMonitorSelector: {},
podMonitorSelector: {}, podMonitorSelector: {},
probeSelector: {}, probeSelector: {},
@ -234,16 +233,14 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
ruleSelector: { ruleSelector: {
matchLabels: { matchLabels: {
role: 'alert-rules', role: 'alert-rules',
prometheus: p.name, prometheus: p.config.name,
}, },
}, },
resources: { resources: p.config.resources,
requests: { memory: '400Mi' },
},
alerting: { alerting: {
alertmanagers: [{ alertmanagers: [{
namespace: p.namespace, namespace: p.config.namespace,
name: p.alertmanagerName, name: 'alertmanager-' + p.config.alertmanagerName,
port: 'web', port: 'web',
}], }],
}, },
@ -260,12 +257,12 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: 'prometheus', name: 'prometheus',
namespace: p.namespace, namespace: p.config.namespace,
labels: $._config.prometheus.labels, labels: p.config.commonLabels,
}, },
spec: { spec: {
selector: { selector: {
matchLabels: { prometheus: p.name } + $._config.prometheus.selectorLabels, matchLabels: p.config.selectorLabels,
}, },
endpoints: [{ endpoints: [{
port: 'web', port: 'web',
@ -279,7 +276,7 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: 'kube-scheduler', name: 'kube-scheduler',
namespace: p.namespace, namespace: p.config.namespace,
labels: { 'app.kubernetes.io/name': 'kube-scheduler' }, labels: { 'app.kubernetes.io/name': 'kube-scheduler' },
}, },
spec: { spec: {
@ -305,7 +302,7 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: 'kubelet', name: 'kubelet',
namespace: p.namespace, namespace: p.config.namespace,
labels: { 'app.kubernetes.io/name': 'kubelet' }, labels: { 'app.kubernetes.io/name': 'kubelet' },
}, },
spec: { spec: {
@ -377,7 +374,7 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: 'kube-controller-manager', name: 'kube-controller-manager',
namespace: p.namespace, namespace: p.config.namespace,
labels: { 'app.kubernetes.io/name': 'kube-controller-manager' }, labels: { 'app.kubernetes.io/name': 'kube-controller-manager' },
}, },
spec: { spec: {
@ -412,7 +409,7 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: 'kube-apiserver', name: 'kube-apiserver',
namespace: p.namespace, namespace: p.config.namespace,
labels: { 'app.kubernetes.io/name': 'apiserver' }, labels: { 'app.kubernetes.io/name': 'apiserver' },
}, },
spec: { spec: {
@ -466,7 +463,7 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
kind: 'ServiceMonitor', kind: 'ServiceMonitor',
metadata: { metadata: {
name: 'coredns', name: 'coredns',
namespace: p.namespace, namespace: p.config.namespace,
labels: { 'app.kubernetes.io/name': 'coredns' }, labels: { 'app.kubernetes.io/name': 'coredns' },
}, },
spec: { spec: {
@ -484,5 +481,4 @@ local relabelings = import 'kube-prometheus/dropping-deprecated-metrics-relabeli
}], }],
}, },
}, },
},
} }