Mercurial > code > home > repos > victoriametrics
diff tasks.py @ 22:cd115f1ca2a8
use configmaps and a special pod refresh trick
author | drewp@bigasterisk.com |
---|---|
date | Sat, 24 Jun 2023 23:02:04 -0700 |
parents | f5777b65f035 |
children | eec015e90818 |
line wrap: on
line diff
--- a/tasks.py Sat Jun 24 23:00:40 2023 -0700 +++ b/tasks.py Sat Jun 24 23:02:04 2023 -0700 @@ -1,112 +1,30 @@ import json -import sys -import tempfile -import time -import urllib.request -from pathlib import Path -from typing import Dict import yaml from invoke import task from kubernetes import config from k8s_ops import firstPodName, replaceCmap, refreshPodCmaps +import alert_rules -_tfs = [] config.load_kube_config() -def saveTmp(text): - tf = tempfile.NamedTemporaryFile(mode='wt') - _tfs.append(tf) - tf.write(text) - tf.flush() - return Path(tf.name) - - -def writeConfigmap(ctx, files: Dict[str, Path]): - arg = ','.join(f'{k}={v}' for k, v in files.items()) - ctx.run( - f'kubectl create configmap victoriametrics-config --from-file {arg} -o yaml --dry-run=client | kubectl apply -f -' - ) - - -def reload(ctx, svc): - host = ctx.run(f'khost {svc}').stdout - path = {'victoriametrics': '/m/', 'vmalert': '/'}[svc] - reload_url = f'http://{host}{path}-/reload' - print(f'reload with POST {reload_url}') - for workaround in [1]: - print(' -> status', - urllib.request.urlopen(reload_url, data=b'unused').status) - time.sleep(0) - - -def hostsExpectedOnline(ctx): - return ctx.run( - 'cd /my/serv/lanscape; pdm run python hosts_expected_online.py').stdout - +@task +def push_config(ctx): + configObj = scrapeConfig(ctx) + rulesObj = alert_rules.allRules() + rulesObj["groups"] += alert_rules.expectedK8sNodes(ctx)["groups"] + rulesObj["groups"] += alert_rules.hostsExpectedOnline(ctx)["groups"] -def expectedK8sNodes(ctx): - getNode = json.loads(ctx.run("kubectl get node -o json").stdout) - hosts = [item['metadata']['name'] for item in getNode['items']] - return yaml.dump({ - 'groups': [{ - 'name': - 'k8s_expected_nodes', - 'rules': [{ - 'alert': - 'kube_node_log_size_report_' + h, - 'expr': - 'absent(kubelet_container_log_filesystem_used_bytes{instance="%s"})' - % h, - 'for': - '1h', - 'annotations': { - 'summary': f"no recent k8s log size report from host {h}" - } - } for h in hosts] - }] - }) - + replaceCmap("victoriametrics-config", {"scrape_main": configObj, "rules": rulesObj}) -@task -def sync_config(ctx): - config = Path('config') - for workaround in [1]: - writeConfigmap( - ctx, { -# 'scrape_ssl.yaml': saveTmp(httpsCertProber()), - 'rules_expected_nodes.yaml': saveTmp(expectedK8sNodes(ctx)), - 'rules_expected_hosts.yaml': saveTmp(hostsExpectedOnline(ctx)), - }) - reload(ctx, 'victoriametrics') + refreshPodCmaps(firstPodName("app=victoriametrics")) + refreshPodCmaps(firstPodName("app=vmalert")) - # this reload doesn't get the new config- not sure if it's vmalert bug or k8s cm propogation problem - # reload(ctx, 'vmalert') - ctx.run('kubectl rollout restart deploy/vmalert') - - -@task -def build_config(ctx): - with open('rules/build/expected_hosts.yaml', 'w') as out: - out.write(hostsExpectedOnline(ctx)) - - -# -------------------------- + # If the VM reloader isn't fast enough, we could do this too: + # hup(ctx, 'deploy/victoriametrics', 'victoria-metrics-prod') def scrapeConfig(ctx): - return yaml.load(open('config/scrape_main.yaml'), yaml.FullLoader) - - -@task -def updateScrapes(ctx): - configObj = scrapeConfig(ctx) - - replaceCmap('victoriametrics-config', configObj) - - refreshPodCmaps(firstPodName('app=victoriametrics')) - - # If the VM reloader isn't fast enough, we could do this too: - # hup(ctx, 'deploy/victoriametrics', 'victoria-metrics-prod') + return yaml.load(open("config/scrape_main.yaml"), yaml.FullLoader)