Skip to content

Commit

Permalink
feat(gazer): Integrated with prometheus (#4)
Browse files Browse the repository at this point in the history
* Setup filters

* feat(gazer): Intergrated with prometheus

* Clean up
  • Loading branch information
isala404 authored Feb 9, 2022
1 parent 4259f88 commit 656c660
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

## Python
**/__pycache__/**
**/venv/**
6 changes: 3 additions & 3 deletions gazer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM alpine:edge
FROM alpine:3.15

RUN apk add bcc-tools py3-pip py3-numpy py3-pandas
RUN apk add bcc-tools py3-pip py3-numpy py3-pandas py3-bcc

RUN pip install termplotlib
RUN pip install termplotlib pyinotify pyyaml prometheus_client

WORKDIR /gazer

Expand Down
31 changes: 31 additions & 0 deletions gazer/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import threading
import pyinotify
import yaml
import os

NODE_NAME = os.getenv("NODE_NAME")


class ConfigWatcher(pyinotify.ProcessEvent):
config = None

def __init__(self, **kargs):
super().__init__(**kargs)
self.read_config()

def read_config(self):
with open(r'config/config.yaml') as file:
self.config = yaml.load(file, Loader=yaml.FullLoader)
self.config = dict(filter(lambda elem: elem[1]['node'] == NODE_NAME, self.config.items()))

def process_IN_CLOSE_WRITE(self, evt):
self.read_config()


config_watcher = ConfigWatcher()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, config_watcher)
wdd = wm.add_watch("config/config.yaml", pyinotify.IN_CLOSE_WRITE)
config_watcher_thread = threading.Thread(target=notifier.loop, args=())
config_watcher_thread.daemon = True
config_watcher_thread.start()
12 changes: 12 additions & 0 deletions gazer/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
10.244.1.9:
name: service-1
namespace: default
node: minikube-0
10.244.1.10:
name: service-2
namespace: default
node: minikube-0
10.244.11:
name: service-1
namespace: default
node: minikube-1
76 changes: 76 additions & 0 deletions gazer/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import curses
import time
from gazer import Gazer


def draw_menu(stdscr: curses.window):
k = 0
cursor_x = 0
cursor_y = 0

# Clear and refresh the screen for a blank canvas
stdscr.clear()
stdscr.refresh()
stdscr.nodelay(True)
# curses.delay_output(100)

# Start colors in curses
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)

# Loop where k is the last character pressed
while k != ord('q'):
try:
# Initialization
stdscr.clear()
height, width = stdscr.getmaxyx()

if k == curses.KEY_DOWN:
cursor_y = cursor_y + 1
elif k == curses.KEY_UP:
cursor_y = cursor_y - 1
elif k == curses.KEY_RIGHT:
cursor_x = cursor_x + 1
elif k == curses.KEY_LEFT:
cursor_x = cursor_x - 1

cursor_x = max(0, cursor_x)
cursor_x = min(width - 1, cursor_x)

cursor_y = max(0, cursor_y)
cursor_y = min(height - 1, cursor_y)

statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {}, {}".format(cursor_x, cursor_y)

stdscr.addstr(0, 0, "Requests", curses.color_pair(1))
stdscr.addstr(2, 0, gazer.request_log_text(), curses.color_pair(1))
stdscr.addstr(15, 0, gazer.syn_backlog_text(), curses.color_pair(1))

# Render status bar
stdscr.attron(curses.color_pair(3))
stdscr.addstr(height - 1, 0, statusbarstr)
stdscr.addstr(height - 1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
stdscr.attroff(curses.color_pair(3))

stdscr.move(cursor_y, cursor_x)

# Refresh the screen
stdscr.refresh()

# Wait for next input
k = stdscr.getch()
time.sleep(1 / 30)
except:
# Refresh the screen
stdscr.refresh()

# Wait for next input
k = stdscr.getch()


gazer = Gazer(console=True)
gazer.poll_data_in_bg()

curses.wrapper(draw_menu)
170 changes: 168 additions & 2 deletions gazer/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
apiVersion: v1
kind: Namespace
metadata:
name: lazy-koala
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: gazer
namespace: lazy-koala
labels:
app: gazer
spec:
Expand All @@ -12,18 +18,28 @@ spec:
metadata:
labels:
name: gazer
annotations:
lazy-koala/scrape: 'true'
spec:
containers:
- name: gazer
# command: [ "sh", "-c", "sleep 60d" ]
image: ghcr.io/mrsupiri/lazy-koala/gazer:latest
ports:
- containerPort: 8000
name: metrics
imagePullPolicy: IfNotPresent
resources:
requests:
ephemeral-storage: "2Gi"
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: PYTHONUNBUFFERED
value: "1"
- name: PYTHONWARNINGS
value: "ignore:Unverified HTTPS request"
securityContext:
privileged: true
capabilities:
Expand Down Expand Up @@ -51,8 +67,12 @@ spec:
- name: debug
mountPath: /sys/kernel/debug
readOnly: false
- name: config
mountPath: "/gazer/config"
readOnly: true
hostNetwork: true
hostPID: true
serviceAccountName: lazy-koala
initContainers:
- name: init-headers
image: ghcr.io/mrsupiri/lazy-koala/gazer:init
Expand Down Expand Up @@ -101,4 +121,150 @@ spec:
path: /lib/modules
- name: debug
hostPath:
path: /sys/kernel/debug
path: /sys/kernel/debug
- name: config
configMap:
name: gazer-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: lazy-koala
spec:
selector:
matchLabels:
app: prometheus
replicas: 1
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: lazy-koala
containers:
- name: prometheus
image: prom/prometheus:v2.33.1
ports:
- containerPort: 9090
name: default
volumeMounts:
- name: config-volume
mountPath: /etc/prometheus
volumes:
- name: config-volume
configMap:
name: prometheus-config
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: lazy-koala
rules:
- apiGroups:
- ""
resources:
- nodes/metrics
verbs:
- get
- apiGroups: [ "" ]
resources:
- nodes
- services
- endpoints
- pods
- namespaces
- configmaps
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- ingresses
verbs:
- get
- list
- watch
- apiGroups:
- "metrics.k8s.io"
resources:
- podmetrics
- pods
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: lazy-koala
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: lazy-koala
subjects:
- kind: ServiceAccount
name: lazy-koala
namespace: lazy-koala
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: lazy-koala
namespace: lazy-koala
---
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: lazy-koala
data:
prometheus.yml: |
global:
scrape_interval: 10s
scrape_configs:
- job_name: 'lazy-koala'
scrape_interval: 1s
metrics_path: /
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotationpresent_lazy_koala_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_container_port_name]
regex: metrics
action: keep
- source_labels: [__meta_kubernetes_pod_container_name]
target_label: gazer
---
apiVersion: v1
kind: ConfigMap
metadata:
name: gazer-config
namespace: lazy-koala
data:
config.yaml: |
10.244.1.15:
name: service-1-0de9ab8e-65bc59564d-f7rhp
serviceName: service-1
namespace: default
node: minikube-m02
10.244.1.14:
name: service-2-0de9ab8e-6fff4f8956-r9qlt
serviceName: service-2
namespace: default
node: minikube-m02
10.244.1.16:
name: service-3-0de9ab8e-75cc987cdc-ltcvr
serviceName: service-3
namespace: default
node: minikube-m02
10.244.1.17:
name: service-4-0de9ab8e-79d69d9c99-vlpdv
serviceName: service-4
namespace: default
node: minikube-m02
Loading

0 comments on commit 656c660

Please sign in to comment.