Load additional modules dynamically from vars module definition

This commit is contained in:
CarlosEDP 2019-05-15 17:28:47 -03:00
parent ad1677dad5
commit 87ed1bf665
6 changed files with 30 additions and 54 deletions

View File

@ -1,6 +1,5 @@
local k = import 'ksonnet/ksonnet.beta.3/k.libsonnet';
local vars = import 'vars.jsonnet';
local enabledModules = [module.name for module in vars.modules if module.enabled];
local join_objects(objs) =
local aux(arr, i, running) =
@ -18,49 +17,26 @@ local kp = (import 'kube-prometheus/kube-prometheus.libsonnet')
+ (import 'kube-prometheus/kube-prometheus-insecure-kubelet.libsonnet')
+ (import 'base_operator_stack.jsonnet')
+ (import 'smtp_server.jsonnet')
// Additional Modules
// Additional modules are loaded dynamically from vars.jsonnet
+ join_objects([module.file for module in vars.modules if module.enabled])
// Load image versions last to override default from modules
+ (import 'image_sources_versions.jsonnet');
// Generate core modules
{ ['00namespace-' + name]: kp.kubePrometheus[name] for name in std.objectFields(kp.kubePrometheus) }
{ ['0prometheus-operator-' + name]: kp.prometheusOperator[name] for name in std.objectFields(kp.prometheusOperator) } +
{ ['prometheus-adapter-' + name]: kp.prometheusAdapter[name] for name in std.objectFields(kp.prometheusAdapter) } +
{ ['prometheus-' + name]: kp.prometheus[name] for name in std.objectFields(kp.prometheus) } +
{ ['alertmanager-' + name]: kp.alertmanager[name] for name in std.objectFields(kp.alertmanager) } +
{ ['kube-state-metrics-' + name]: kp.kubeStateMetrics[name] for name in std.objectFields(kp.kubeStateMetrics) } +
{ ['node-exporter-' + name]: kp.nodeExporter[name] for name in std.objectFields(kp.nodeExporter) } +
{ ['grafana-' + name]: kp.grafana[name] for name in std.objectFields(kp.grafana) } +
{ ['ingress-' + name]: kp.ingress[name] for name in std.objectFields(kp.ingress) } +
{ ['0prometheus-operator-' + name]: kp.prometheusOperator[name] for name in std.objectFields(kp.prometheusOperator) }
{ ['prometheus-adapter-' + name]: kp.prometheusAdapter[name] for name in std.objectFields(kp.prometheusAdapter) }
{ ['prometheus-' + name]: kp.prometheus[name] for name in std.objectFields(kp.prometheus) }
{ ['alertmanager-' + name]: kp.alertmanager[name] for name in std.objectFields(kp.alertmanager) }
{ ['kube-state-metrics-' + name]: kp.kubeStateMetrics[name] for name in std.objectFields(kp.kubeStateMetrics) }
{ ['node-exporter-' + name]: kp.nodeExporter[name] for name in std.objectFields(kp.nodeExporter) }
{ ['grafana-' + name]: kp.grafana[name] for name in std.objectFields(kp.grafana) }
{ ['ingress-' + name]: kp.ingress[name] for name in std.objectFields(kp.ingress) }
{ ['smtp-server-' + name]: kp.smtpServer[name] for name in std.objectFields(kp.smtpServer) }
// Additional Modules
{
[std.asciiLower(moduleName) + '-' + objName]: {kp.moduleName[objName]}
for objName in [std.objectFields(kp[moduleName]] for moduleName in enabledModules
{ // Dynamically generate additional modules from vars.jsonnet
[std.asciiLower(module.name) + '-' + name]: kp[module.name][name]
for module in vars.modules
if module.enabled
for name in std.objectFields(kp[module.name])
}
// {
// local items = ["a", "b", "c"],
// joined: {
// [x + i]: {
// data: "x = %s and i = %s" % [x, i],
// } for i in ["1", "2"] for x in items
// }
// }
// { ['arm-exporter-' + name]: kp.armExporter[name] for name in std.objectFields(kp.armExporter) }
// {[std.asciiLower(moduleName) + '-' + objName]: kp.moduleName[objName] for objName in [std.objectFields(kp[moduleName]) for moduleName in enabledModules]}
// (if vars.installModules['arm-exporter'] then
// { ['arm-exporter-' + name]: kp.armExporter[name] for name in std.objectFields(kp.armExporter) } else {}) +
// (if vars.installModules.metallb then
// { ['metallb-' + name]: kp.metallb[name] for name in std.objectFields(kp.metallb) } else {}) +
// (if vars.installModules.traefik then
// { ['traefik-' + name]: kp.traefik[name] for name in std.objectFields(kp.traefik) } else {}) +
// (if vars.installModules['ups-exporter'] then
// { ['ups-exporter-' + name]: kp.upsExporter[name] for name in std.objectFields(kp.upsExporter) } else {}) +
// (if vars.installModules['elastic-exporter'] then
// { ['elasticexporter-' + name]: kp.elasticExporter[name] for name in std.objectFields(kp.elasticExporter) } else {})

View File

@ -5,7 +5,7 @@ metadata:
namespace: monitoring
spec:
rules:
- host: alertmanager.192.168.99.102.nip.io
- host: alertmanager.192.168.99.100.nip.io
http:
paths:
- backend:

View File

@ -5,7 +5,7 @@ metadata:
namespace: monitoring
spec:
rules:
- host: grafana.192.168.99.102.nip.io
- host: grafana.192.168.99.100.nip.io
http:
paths:
- backend:

View File

@ -5,7 +5,7 @@ metadata:
namespace: monitoring
spec:
rules:
- host: prometheus.192.168.99.102.nip.io
- host: prometheus.192.168.99.100.nip.io
http:
paths:
- backend:

View File

@ -26,7 +26,7 @@ spec:
namespace: monitoring
port: web
baseImage: carlosedp/prometheus
externalUrl: http://prometheus.192.168.99.102.nip.io
externalUrl: http://prometheus.192.168.99.100.nip.io
nodeSelector:
beta.kubernetes.io/os: linux
replicas: 1

View File

@ -3,27 +3,27 @@
modules: [
{
name: 'armExporter',
enabled: true,
enabled: false,
file: import 'arm_exporter.jsonnet',
},
{
name: 'upsExporter',
enabled: true,
enabled: false,
file: import 'ups_exporter.jsonnet',
},
{
name: 'metallbExporter',
enabled: true,
enabled: false,
file: import 'metallb.jsonnet',
},
{
name: 'traefikExporter',
enabled: true,
enabled: false,
file: import 'traefik.jsonnet',
},
{
name: 'elasticExporter',
enabled: true,
enabled: false,
file: import 'elasticsearch_exporter.jsonnet',
},
],