Skip to content

Commit

Permalink
feat: add resource-leaker image
Browse files Browse the repository at this point in the history
Signed-off-by: Ardika Bagus <[email protected]>
  • Loading branch information
ardikabs committed Dec 4, 2024
1 parent 7b19b46 commit d5d72fd
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dockerfiles/resource-leaker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.10-bullseye
RUN pip install flask
COPY leaker.py /app/leaker.py
WORKDIR /app
CMD ["python", "leaker.py"]
79 changes: 79 additions & 0 deletions dockerfiles/resource-leaker/leaker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import multiprocessing
import threading
import time

from flask import Flask, jsonify, request

app = Flask(__name__)
memory_hog = []
cpu_hog_processes = []
leak_running = False
cpu_running = False

def memory_leak_simulator():
global memory_hog, leak_running
while leak_running:
memory_hog.append(" " * 10**6) # Allocate 1MB
time.sleep(1) # Gradual allocation

def cpu_stress_simulator():
while True:
_ = 1 + 1 # Simple CPU loop

@app.route('/start', methods=['POST'])
def start():
global leak_running, cpu_running, cpu_hog_processes

resource = request.args.get('resource', 'memory').lower()
if resource == "memory":
if not leak_running:
leak_running = True
thread = threading.Thread(target=memory_leak_simulator)
thread.start()
return jsonify({"status": "Memory leak started"}), 200
return jsonify({"status": "Memory leak already running"}), 400
elif resource == "cpu":
if not cpu_running:
cpu_running = True
cpu_hog_processes = [
multiprocessing.Process(target=cpu_stress_simulator)
for _ in range(multiprocessing.cpu_count())
]
for process in cpu_hog_processes:
process.start()
return jsonify({"status": "CPU stress started"}), 200
return jsonify({"status": "CPU stress already running"}), 400
return jsonify({"error": "Invalid resource type"}), 400

@app.route('/stop', methods=['POST'])
def stop():
global leak_running, cpu_running, memory_hog, cpu_hog_processes

resource = request.args.get('resource', 'memory').lower()
if resource == "memory":
if leak_running:
leak_running = False
memory_hog = [] # Clear memory
return jsonify({"status": "Memory leak stopped"}), 200
return jsonify({"status": "No memory leak running"}), 400
elif resource == "cpu":
if cpu_running:
cpu_running = False
for process in cpu_hog_processes:
process.terminate()
cpu_hog_processes = []
return jsonify({"status": "CPU stress stopped"}), 200
return jsonify({"status": "No CPU stress running"}), 400
return jsonify({"error": "Invalid resource type"}), 400

@app.route('/status', methods=['GET'])
def status():
return jsonify({
"memory_leak_running": leak_running,
"cpu_stress_running": cpu_running,
"memory_allocated_mb": len(memory_hog),
"cpu_cores_stressed": len(cpu_hog_processes)
})

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
40 changes: 40 additions & 0 deletions kubernetes/services/leaker/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: resource-leaker
labels:
app.kubernetes.io/name: resource-leaker
spec:
minReadySeconds: 3
revisionHistoryLimit: 5
progressDeadlineSeconds: 60
strategy:
rollingUpdate:
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app.kubernetes.io/name: resource-leaker
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9797"
labels:
app.kubernetes.io/name: resource-leaker
spec:
containers:
- name: leaker
image: ghcr.io/ardikabs/etc/resource-leaker:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 5000
protocol: TCP
resources:
limits:
cpu: 100m
memory: 64Mi
requests:
cpu: 100m
memory: 64Mi
15 changes: 15 additions & 0 deletions kubernetes/services/leaker/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: resource-leaker
name: resource-leaker
spec:
type: ClusterIP
selector:
app.kubernetes.io/name: resource-leaker
ports:
- name: http
port: 5000
protocol: TCP
targetPort: http
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d5d72fd

Please sign in to comment.