Skip to content

Commit 6f3a91c

Browse files
committed
chore(controller): add measure loalancers
1 parent c9f8bb2 commit 6f3a91c

5 files changed

Lines changed: 98 additions & 1 deletion

File tree

charts/controller/templates/_helpers.tpl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ env:
129129
secretKeyRef:
130130
name: redis-creds
131131
key: password
132+
{{- if eq .Values.global.rabbitmqLocation "off-cluster" }}
133+
- name: "DRYCC_PROMETHEUS_URL"
134+
valueFrom:
135+
secretKeyRef:
136+
name: prometheus-creds
137+
key: url
138+
{- else }
139+
- name: "DRYCC_PROMETHEUS_USERNAME"
140+
valueFrom:
141+
secretKeyRef:
142+
name: prometheus-creds
143+
key: username
144+
- name: "DRYCC_PROMETHEUS_PASSWORD"
145+
valueFrom:
146+
secretKeyRef:
147+
name: prometheus-creds
148+
key: password
149+
- name: "DRYCC_PROMETHEUS_URL"
150+
value: "http://$(DRYCC_PROMETHEUS_USERNAME):$(DRYCC_PROMETHEUS_PASSWORD)@drycc-prometheus.{{$.Release.Namespace}}.svc.{{$.Values.global.clusterDomain}}:9090//api/v1/query"
151+
{{- end }}
132152
{{- if (.Values.rabbitmqUrl) }}
133153
- name: DRYCC_RABBITMQ_URL
134154
valueFrom:

charts/controller/templates/controller-cronjob-hourly.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ spec:
4242
- -c
4343
- python -u /workspace/manage.py measure_networks
4444
{{- include "controller.envs" . | indent 12 }}
45+
- image: {{.Values.imageRegistry}}/{{.Values.imageOrg}}/controller:{{.Values.imageTag}}
46+
imagePullPolicy: {{.Values.pull_policy}}
47+
name: drycc-controller-measure-loadbalancer
48+
args:
49+
- /bin/bash
50+
- -c
51+
- python -u /workspace/manage.py measure_loadbalancers
52+
{{- include "controller.envs" . | indent 12 }}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import uuid
2+
import time
3+
import logging
4+
from django.utils import timezone
5+
from django.core.management.base import BaseCommand
6+
from django.conf import settings
7+
from api import monitor
8+
from api.models.app import App
9+
from api.tasks import send_measurements
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
class Command(BaseCommand):
15+
"""Management command for push data to manager"""
16+
17+
def _measure_loadbalancers(self, app_map, timestamp):
18+
stop = timestamp - (timestamp % 3600)
19+
start = stop - 3600
20+
loadbalancers = []
21+
for item in monitor.query_loadbalancer(app_map.keys(), start, stop):
22+
name = item["ip"]
23+
namespace = item["namespace"]
24+
owner_id = app_map[namespace].owner_id
25+
loadbalancers.append({
26+
"app_id": str(app_map[namespace].uuid),
27+
"owner": owner_id,
28+
"name": name,
29+
"type": "loadbalancer",
30+
"unit": "number",
31+
"usage": 1,
32+
"timestamp": start
33+
})
34+
send_measurements.delay(loadbalancers)
35+
36+
def handle(self, *args, **options):
37+
if settings.WORKFLOW_MANAGER_URL and settings.DRYCC_PROMETHEUS_URL:
38+
timestamp = int(time.time())
39+
task_id = uuid.uuid4().hex
40+
logger.info(f"pushing {task_id} limits to workflow_manager when {timezone.now()}")
41+
app_map = {}
42+
for app in App.objects.all():
43+
app_map[app.id] = app
44+
if len(app_map) % 1000 == 0:
45+
self._measure_loadbalancers(app_map, timestamp)
46+
app_map = {}
47+
if len(app_map) > 0:
48+
self._measure_loadbalancers(app_map, timestamp)
49+
logger.info(f"pushed {task_id} limits to workflow_manager when {timezone.now()}")
50+
self.stdout.write("done")

rootfs/api/monitor.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from typing import Iterator
1+
import requests
2+
from typing import Iterator, Dict
23
from contextlib import closing
34
from django.db import connections
5+
from django.conf import settings
46

57

68
query_network_flow_sql_tpl = """
@@ -101,6 +103,21 @@
101103
"""
102104

103105

106+
query_loadbalancer_promql_tpl = """
107+
max_over_time(kube_service_status_load_balancer_ingress{namespace=~"%s"}[60m])
108+
"""
109+
110+
111+
def query_loadbalancer(namespaces: Iterator[str],
112+
start: int, stop: int) -> Iterator[Dict[str, str]]:
113+
promql = query_loadbalancer_promql_tpl % "|".join(namespaces)
114+
params = {"query": promql, "start": start, "end": stop}
115+
response = requests.get(settings.DRYCC_PROMETHEUS_URL, params=params)
116+
if response.status_code != 200:
117+
return StopIteration
118+
yield from (metric["metric"] for metric in response.json()["data"]["result"])
119+
120+
104121
def query_network_flow(namespaces: Iterator[str],
105122
start: int, stop: int) -> Iterator[tuple[str, str, int, int]]:
106123
with closing(connections['monitor'].cursor()) as cursor:

rootfs/api/settings/production.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@
269269
)
270270

271271
K8S_API_VERIFY_TLS = bool(strtobool(os.environ.get('K8S_API_VERIFY_TLS', 'true')))
272+
# drycc prometheus url
273+
DRYCC_PROMETHEUS_URL = os.environ.get('DRYCC_PROMETHEUS_URL', '')
272274

273275
# security keys and auth tokens
274276
random_secret = 'CHANGEME_sapm$s%upvsw5l_zuy_&29rkywd^78ff(qi*#@&*^'

0 commit comments

Comments
 (0)