-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.jsonnet
More file actions
139 lines (132 loc) · 4.69 KB
/
Copy pathmain.jsonnet
File metadata and controls
139 lines (132 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// main template for loki
local kap = import 'lib/kapitan.libjsonnet';
local kube = import 'lib/kube.libjsonnet';
local inv = kap.inventory();
local com = import 'lib/commodore.libjsonnet';
// `prom.libsonnet` (with `generateRules`) is provided by
// component-openshift4-monitoring via a library alias. On non-OpenShift
// clusters fall back to component-prometheus, which exports
// `prometheus.libsonnet`, and reimplement the small `generateRules` helper.
// Keep behaviour identical to the OpenShift `prom.libsonnet`: `generateRules`
// only shapes the rules, the syn labels are stamped later by `patch-alerts`.
local prom =
if std.member(inv.applications, 'openshift4-monitoring') then
import 'lib/prom.libsonnet'
else if std.member(inv.applications, 'prometheus') then
local p = import 'lib/prometheus.libsonnet';
{
generateRules(name, rules): p.PrometheusRule(name) {
spec: {
groups: std.filter(
function(g) std.length(g.rules) > 0,
[
{
name: group_name,
rules: [
local rnamekey = std.splitLimit(rname, ':', 1);
rules[group_name][rname] {
// transform source key into "alert: alertname" or
// "record: recordname"
[rnamekey[0]]: rnamekey[1],
}
for rname in std.objectFields(rules[group_name])
if rules[group_name][rname] != null
],
}
for group_name in std.objectFields(rules)
if rules[group_name] != null
]
),
},
},
}
else
error 'component requires one of component-openshift4-monitoring or component-prometheus to be present';
// The hiera parameters for the component
local params = inv.parameters.loki;
// Prevent using non-instantiated configuration of this component
assert inv.parameters._instance != 'loki' : "configuring non-instantiated component isn't allowed";
local secrets = com.generateResources(
{
[if params.ingress.tls.enabled && params.ingress.tls.key != null && params.ingress.tls.cert != null then '%s-tls' % std.strReplace(params.ingress.url, '.', '-')]:
{
stringData: {
'tls.key': params.ingress.tls.key,
'tls.cert': params.ingress.tls.cert,
},
},
[if params.basicAuth.enabled && params.basicAuth.htpasswd != null then '%s-nginx-htpasswd' % inv.parameters._instance]:
{
stringData: {
'.htpasswd': params.basicAuth.htpasswd,
},
},
['%s-bucket-secret' % inv.parameters._instance]: {
stringData: {
S3_ACCESS_KEY_ID: params.s3.auth.accessKeyId,
S3_SECRET_ACCESS_KEY: params.s3.auth.secretAccessKey,
},
},
} + com.makeMergeable(params.secrets),
function(name) kube.Secret(name) {
metadata+: {
labels+: {
'app.kubernetes.io/managed-by': 'commodore',
'app.kubernetes.io/name': name,
},
namespace: params.namespace.name,
},
}
);
local netpols =
local exposedComponents = com.renderArray(params.networkPolicy.exposedComponents);
local allowedNamespaces = com.renderArray(params.networkPolicy.allowedNamespaces);
if
params.networkPolicy.enabled
&& std.length(exposedComponents) > 0
&& std.length(allowedNamespaces) > 0
then kube.NetworkPolicy('allow-from-other-namespaces') {
metadata+: {
labels+: {
'app.kubernetes.io/managed-by': 'commodore',
'app.kubernetes.io/name': 'allow-from-other-namespaces',
},
namespace: params.namespace.name,
},
spec: {
policyTypes: [ 'Ingress' ],
podSelector: {
matchExpressions: [ {
key: 'app.kubernetes.io/component',
operator: 'In',
values: exposedComponents,
} ],
},
ingress: [ {
from: [ {
namespaceSelector: {
matchExpressions: [ {
key: 'kubernetes.io/metadata.name',
operator: 'In',
values: allowedNamespaces,
} ],
},
} ],
} ],
},
} else {};
// Define outputs below
{
[if params.namespace.create then '00_namespace']: kube.Namespace(params.namespace.name) {
metadata+: com.makeMergeable(params.namespace.metadata),
},
'01_secrets': secrets,
// Empty file to make sure the directory is created. Later used in patching alerts.
'10_helm_loki/loki/templates/monitoring/.keep': {},
'20_prometheus_rule': prom.generateRules('loki-custom', { 'loki-custom.rules': params.alerts.additionalRules }) {
metadata+: {
namespace: params.namespace.name,
},
},
[if std.length(netpols) > 0 then '30_network_policies']: netpols,
}