-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ardika Bagus <[email protected]>
- Loading branch information
Showing
13 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.