Skip to content

Commit

Permalink
[kube-prometheus-stack] Generate windows dashboard from hack/sync (#4013
Browse files Browse the repository at this point in the history
)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
Co-authored-by: Gabriel Martinez <19713226+GMartinez-Sisti@users.noreply.github.com>
  • Loading branch information
jkroepke and GMartinez-Sisti committed Nov 17, 2023
1 parent ffded69 commit 1bc07da
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 60 deletions.
2 changes: 1 addition & 1 deletion charts/kube-prometheus-stack/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ name: kube-prometheus-stack
sources:
- https://github.com/prometheus-community/helm-charts
- https://github.com/prometheus-operator/kube-prometheus
version: 54.0.1
version: 54.1.0
appVersion: v0.69.1
kubeVersion: ">=1.19.0-0"
home: https://github.com/prometheus-operator/kube-prometheus
Expand Down
52 changes: 43 additions & 9 deletions charts/kube-prometheus-stack/hack/sync_grafana_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,22 @@ def new_representer(dumper, data):
'min_kubernetes': '1.14.0-0',
'multicluster_key': '.Values.grafana.sidecar.dashboards.multicluster.global.enabled',
},
{
'git': 'https://github.com/kubernetes-monitoring/kubernetes-mixin.git',
'branch': 'master',
'content': "(import 'dashboards/windows.libsonnet') + (import 'config.libsonnet') + { _config+:: { windowsExporterSelector: 'job=\"windows-exporter\"', }}",
'cwd': '.',
'destination': '../templates/grafana/dashboards-1.14',
'min_kubernetes': '1.14.0-0',
'type': 'jsonnet_mixin',
'mixin_vars': {},
'multicluster_key': '.Values.grafana.sidecar.dashboards.multicluster.global.enabled',
},
{
'git': 'https://github.com/etcd-io/etcd.git',
'source': 'contrib/mixin/mixin.libsonnet',
'branch': 'main',
'source': 'mixin.libsonnet',
'cwd': 'contrib/mixin',
'destination': '../templates/grafana/dashboards-1.14',
'min_kubernetes': '1.14.0-0',
'type': 'jsonnet_mixin',
Expand All @@ -60,7 +73,12 @@ def new_representer(dumper, data):
'node-cluster-rsrc-use': ' .Values.nodeExporter.enabled',
'nodes': ' (and .Values.nodeExporter.enabled .Values.nodeExporter.operatingSystems.linux.enabled)',
'nodes-darwin': ' (and .Values.nodeExporter.enabled .Values.nodeExporter.operatingSystems.darwin.enabled)',
'prometheus-remote-write': ' .Values.prometheus.prometheusSpec.remoteWriteDashboards'
'prometheus-remote-write': ' .Values.prometheus.prometheusSpec.remoteWriteDashboards',
'k8s-windows-cluster-rsrc-use': ' .Values.windowsMonitoring.enabled',
'k8s-windows-node-rsrc-use': ' .Values.windowsMonitoring.enabled',
'k8s-resources-windows-cluster': ' .Values.windowsMonitoring.enabled',
'k8s-resources-windows-namespace': ' .Values.windowsMonitoring.enabled',
'k8s-resources-windows-pod': ' .Values.windowsMonitoring.enabled',
}

# standard header
Expand Down Expand Up @@ -195,24 +213,39 @@ def main():
# read the rules, create a new template file per group
for chart in charts:
if 'git' in chart:
if 'source' not in chart:
chart['source'] = '_mixin.jsonnet'

url = chart['git']

print("Clone %s" % chart['git'])
checkout_dir = os.path.basename(chart['git'])
shutil.rmtree(checkout_dir, ignore_errors=True)
subprocess.run(["git", "clone", chart['git'], "--branch", "main", "--single-branch", "--depth", "1", checkout_dir])

branch = "main"
if 'branch' in chart:
branch = chart['branch']

subprocess.run(["git", "clone", chart['git'], "--branch", branch, "--single-branch", "--depth", "1", checkout_dir])
print("Generating rules from %s" % chart['source'])

mixin_file = os.path.basename(chart['source'])
mixin_dir = checkout_dir + '/' + os.path.dirname(chart['source']) + '/'
mixin_file = chart['source']
mixin_dir = checkout_dir + '/' + chart['cwd'] + '/'
if os.path.exists(mixin_dir + "jsonnetfile.json"):
print("Running jsonnet-bundler, because jsonnetfile.json exists")
subprocess.run(["jb", "install"], cwd=mixin_dir)

if 'content' in chart:
f = open(mixin_dir + mixin_file, "w")
f.write(chart['content'])
f.close()

mixin_vars = json.dumps(chart['mixin_vars'])

cwd = os.getcwd()
os.chdir(mixin_dir)
raw_text = '((import "%s") + %s)' % (mixin_file, mixin_vars)
source = mixin_file
source = os.path.basename(mixin_file)
else:
print("Generating rules from %s" % chart['source'])
response = requests.get(chart['source'])
Expand All @@ -221,6 +254,7 @@ def main():
continue
raw_text = response.text
source = chart['source']
url = chart['source']

if ('max_kubernetes' not in chart):
chart['max_kubernetes']="9.9.9-9"
Expand All @@ -230,7 +264,7 @@ def main():
groups = yaml_text['items']
for group in groups:
for resource, content in group['data'].items():
write_group_to_file(resource.replace('.json', ''), content, chart['source'], chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'], chart['multicluster_key'])
write_group_to_file(resource.replace('.json', ''), content, url, chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'], chart['multicluster_key'])
elif chart['type'] == 'jsonnet_mixin':
json_text = json.loads(_jsonnet.evaluate_snippet(source, raw_text + '.grafanaDashboards', import_callback=jsonnet_import_callback))

Expand All @@ -240,10 +274,10 @@ def main():
flat_structure = bool(json_text.get('annotations'))
if flat_structure:
resource = os.path.basename(chart['source']).replace('.json', '')
write_group_to_file(resource, json.dumps(json_text, indent=4), chart['source'], chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'], chart['multicluster_key'])
write_group_to_file(resource, json.dumps(json_text, indent=4), url, chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'], chart['multicluster_key'])
else:
for resource, content in json_text.items():
write_group_to_file(resource.replace('.json', ''), json.dumps(content, indent=4), chart['source'], chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'], chart['multicluster_key'])
write_group_to_file(resource.replace('.json', ''), json.dumps(content, indent=4), url, chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'], chart['multicluster_key'])

print("Finished")

Expand Down
68 changes: 56 additions & 12 deletions charts/kube-prometheus-stack/hack/sync_prometheus_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,23 @@ def new_representer(dumper, data):
'destination': '../templates/prometheus/rules-1.14',
'min_kubernetes': '1.14.0-0'
},
{
'git': 'https://github.com/kubernetes-monitoring/kubernetes-mixin.git',
'branch': 'master',
'source': 'windows.libsonnet',
'cwd': 'rules',
'destination': '../templates/prometheus/rules-1.14',
'min_kubernetes': '1.14.0-0',
'is_mixin': True,
'mixin_vars': {'_config': {
'windowsExporterSelector': 'job="windows-exporter"',
'kubeStateMetricsSelector': 'job="kube-state-metrics"',
}}
},
{
'git': 'https://github.com/etcd-io/etcd.git',
'source': 'contrib/mixin/mixin.libsonnet',
'source': 'mixin.libsonnet',
'cwd': 'contrib/mixin',
'destination': '../templates/prometheus/rules-1.14',
'min_kubernetes': '1.14.0-0',
'is_mixin': True,
Expand Down Expand Up @@ -105,6 +119,8 @@ def new_representer(dumper, data):
'node-network': ' .Values.defaultRules.rules.network',
'prometheus-operator': ' .Values.defaultRules.rules.prometheusOperator',
'prometheus': ' .Values.defaultRules.rules.prometheus', # kube-prometheus >= 1.14 uses prometheus as group instead of prometheus.rules
'windows.node.rules': ' .Values.windowsMonitoring.enabled .Values.defaultRules.rules.windows',
'windows.pod.rules': ' .Values.windowsMonitoring.enabled .Values.defaultRules.rules.windows',
}

alert_condition_map = {
Expand Down Expand Up @@ -444,26 +460,53 @@ def main():
# read the rules, create a new template file per group
for chart in charts:
if 'git' in chart:
if 'source' not in chart:
chart['source'] = '_mixin.jsonnet'

url = chart['git']

print("Clone %s" % chart['git'])
checkout_dir = os.path.basename(chart['git'])
shutil.rmtree(checkout_dir, ignore_errors=True)
subprocess.run(["git", "clone", chart['git'], "--branch", "main", "--single-branch", "--depth", "1", checkout_dir])
print("Generating rules from %s" % chart['source'])

branch = "main"
if 'branch' in chart:
branch = chart['branch']

subprocess.run(["git", "clone", chart['git'], "--branch", branch, "--single-branch", "--depth", "1", checkout_dir])

if chart.get('is_mixin'):
mixin_file = os.path.basename(chart['source'])
mixin_dir = checkout_dir + '/' + os.path.dirname(chart['source']) + '/'
cwd = os.getcwd()

source_cwd = chart['cwd']
mixin_file = chart['source']

mixin_dir = cwd + '/' + checkout_dir + '/' + source_cwd + '/'
if os.path.exists(mixin_dir + "jsonnetfile.json"):
print("Running jsonnet-bundler, because jsonnetfile.json exists")
subprocess.run(["jb", "install"], cwd=mixin_dir)

if 'content' in chart:
f = open(mixin_dir + mixin_file, "w")
f.write(chart['content'])
f.close()

mixin_vars = json.dumps(chart['mixin_vars'])

print("Generating rules from %s" % mixin_file)
print("Change cwd to %s" % checkout_dir + '/' + os.path.dirname(chart['source']))
cwd = os.getcwd()
print("Change cwd to %s" % checkout_dir + '/' + source_cwd)
os.chdir(mixin_dir)
alerts = json.loads(_jsonnet.evaluate_snippet(mixin_file, '((import "%s") + %s).prometheusAlerts' % (mixin_file, mixin_vars), import_callback=jsonnet_import_callback))

mixin = """
local kp =
{ prometheusAlerts+:: {}, prometheusRules+:: {}} +
(import "%s") +
%s;
kp.prometheusAlerts + kp.prometheusRules
"""

alerts = json.loads(_jsonnet.evaluate_snippet(mixin_file, mixin % (mixin_file, mixin_vars), import_callback=jsonnet_import_callback))
os.chdir(cwd)
else:
with open(checkout_dir + '/' + chart['source'], "r") as f:
Expand All @@ -472,14 +515,15 @@ def main():
alerts = yaml.full_load(raw_text)

else:
print("Generating rules from %s" % chart['source'])
response = requests.get(chart['source'])
url = chart['source']
print("Generating rules from %s" % url)
response = requests.get(url)
if response.status_code != 200:
print('Skipping the file, response code %s not equals 200' % response.status_code)
continue
raw_text = response.text
if chart.get('is_mixin'):
alerts = json.loads(_jsonnet.evaluate_snippet(chart['source'], raw_text + '.prometheusAlerts'))
alerts = json.loads(_jsonnet.evaluate_snippet(url, raw_text + '.prometheusAlerts'))
else:
alerts = yaml.full_load(raw_text)

Expand All @@ -489,7 +533,7 @@ def main():
# etcd workaround, their file don't have spec level
groups = alerts['spec']['groups'] if alerts.get('spec') else alerts['groups']
for group in groups:
write_group_to_file(group, chart['source'], chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'])
write_group_to_file(group, url, chart['destination'], chart['min_kubernetes'], chart['max_kubernetes'])

# write rules.names named template
write_rules_names_template()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{- /*
Generated from 'etcd' from contrib/mixin/mixin.libsonnet
Generated from 'etcd' from https://github.com/etcd-io/etcd.git
Do not change in-place! In order to change this file first read following link:
https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack/hack
*/ -}}
Expand Down

Large diffs are not rendered by default.

0 comments on commit 1bc07da

Please sign in to comment.