try new feature...

This commit is contained in:
Eugenio Marzo
2022-10-20 23:39:53 +02:00
parent bc4794dc35
commit 791c5792d4
8 changed files with 180 additions and 98 deletions
+1
View File
@@ -48,6 +48,7 @@ COPY scripts/node.lua /usr/local/openresty/nginx/conf/kubeinvaders/node.lua
COPY scripts/kube-linter.lua /usr/local/openresty/nginx/conf/kubeinvaders/kube-linter.lua
COPY scripts/chaos-node.lua /usr/local/openresty/nginx/conf/kubeinvaders/chaos-node.lua
COPY scripts/chaos-containers.lua /usr/local/openresty/nginx/conf/kubeinvaders/chaos-containers.lua
COPY scripts/programming_mode.lua /usr/local/openresty/nginx/conf/kubeinvaders/programming_mode.lua
COPY scripts/config_kubeinv.lua /usr/local/openresty/lualib/config_kubeinv.lua
COPY nginx/KubeInvaders.conf /etc/nginx/conf.d/KubeInvaders.conf
RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx /var/www/html /etc/nginx/conf.d
+75 -32
View File
@@ -118,16 +118,27 @@
<div class="row" style="margin-bottom: 1%;">
<div class="col text-center">
<button type="button" id="controlAutoPilotButton" class="btn btn-light" onclick="startGameMode()">Game Mode</button>
<button type="button" id="gameModeButton" class="btn btn-light" onclick="startGameMode()">Game Mode</button>
</div>
<div class="col text-center">
<p>KubeInvaders</p>
<div id="programming-mode-buttons" style="display: none;">
<button type="button" id="runProgrammingModeButton" class="btn btn-dark" onclick="runChaosProgram()">RUN</button>
</div>
</div>
<div class="col text-center">
<button type="button" id="controlAutoPilotButton" class="btn btn-light" onclick="startProgrammingMode()">Programming Mode</button>
<button type="button" id="programmingModeButton" class="btn btn-light" onclick="startProgrammingMode()">Programming Mode</button>
</div>
</div>
<div id="programming-mode-buttons" style="display: none;">
<div class="row">
<div class="col text-center">
</div>
<div class="col text-center">
</div>
<div class="col text-center">
</div>
</div>
</div>
<div id="game-buttons" style="display: none;">
<div class="row">
<div class="col text-center">
@@ -164,33 +175,45 @@
</div>
</div>
<div class="row" style="margin-top: 1%;">
<form>
<div class="form-group">
<label for="exampleFormControlTextarea1"></label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="20" style="font-family: Courier, monospace;">
<div id="chaos-program-screen" style="display: none;">
<div class="row" style="margin-top: 1%;">
<form>
<div id="alert_placeholder" style="margin-top: 3%;"></div>
<div class="form-group">
<label for="chaosProgramTextArea"></label>
<textarea class="form-control" id="chaosProgramTextArea" rows="20" style="font-family: Courier, monospace;">
jobs:
cpu_attack:
from: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: ["stress-ng", "--cpu", "4", "--io", "2", "--vm", "1", "--vm-bytes", "1G", "--timeout", "10s", "--metrics-brief"]
mem_attack:
from: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: ["stress-ng", "--vm 2", "-vm-bytes", "1G"]
cpu-attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help
mem-attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help
experiments:
- name: cpu_attack
- name: cpu-attack-exp
job: cpu-attack
loop: 5
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
- name: mem_attack
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
- name: mem-attack-exp
job: mem-attack
loop: 5
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
</textarea>
</div>
</form>
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
</textarea>
</div>
</form>
</div>
</div>
<div id="game-screen" style="display: none;">
@@ -210,9 +233,8 @@ experiments:
</div>
</div>
</div>
<div id="alert_placeholder" style="margin-top: 3%;"></div>
</div>
<div id="alert_placeholder" style="margin-top: 3%;"></div>
</div>
<div id="footer" style="margin-top: 5%;">
<div class="container text-center">
@@ -259,11 +281,8 @@ experiments:
}
}
/* TODO: Improve this function... */
function startGameMode() {
var game_buttons = document.getElementById("game-buttons");
var game_screen = document.getElementById("game-screen");
if (game_mode_switch) {
game_mode_switch = false;
} else {
@@ -275,11 +294,35 @@ experiments:
} else {
game_buttons.style.display = "none";
}
if (game_screen.style.display === "none") {
game_screen.style.display = "block";
} else {
game_screen.style.display = "none";
}
chaos_program_screen.style.display = "none";
programming_mode_buttons.style.display = "none";
}
function startProgrammingMode() {
if (chaos_program_screen.style.display === "none") {
chaos_program_screen.style.display = "block";
} else {
chaos_program_screen.style.display = "none";
}
if (programming_mode_buttons.style.display === "none") {
programming_mode_buttons.style.display = "block";
} else {
programming_mode_buttons.style.display = "none";
}
game_buttons.style.display = "none";
game_screen.style.display = "none";
}
function showSpecialKeys() {
+18
View File
@@ -23,6 +23,11 @@ var randomFactor = 10;
var pods = [];
var game_mode_switch = false;
var game_buttons = document.getElementById("game-buttons");
var game_screen = document.getElementById("game-screen");
var chaos_program_screen = document.getElementById("chaos-program-screen");
var programming_mode_buttons = document.getElementById("programming-mode-buttons");
// nodes list from kubernetes
var nodes = [];
@@ -195,6 +200,19 @@ function setChaosContainer() {
}
}
function runChaosProgram() {
var oReq = new XMLHttpRequest();
oReq.open("POST", "https://" + clu_endpoint + "/kube/chaos/programming_mode", true);
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
//console.log(this.responseText);
$('#alert_placeholder2').text('New Chaos Program has been started.');
}
};;
oReq.setRequestHeader("Content-Type", "application/json");
oReq.send($('#chaosProgramTextArea').val());
}
function startChaosNode(node_name) {
var oReq = new XMLHttpRequest();
oReq.onload = function () {
+1 -1
View File
@@ -20,7 +20,7 @@ server {
sub_filter endpoint_placeholder $endpoint;
}
location /kube/programming_mode {
location /kube/chaos/programming_mode {
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/programming_mode.lua";
}
+6
View File
@@ -11,3 +11,9 @@ end
local token = os.getenv("TOKEN")
local arg = ngx.req.get_uri_args()
local body_data = ngx.req.get_body_data()
file = io.open("/tmp/experiments.yaml", "w")
io.output(file)
io.write(body_data)
io.close(file)
-40
View File
@@ -1,40 +0,0 @@
jobs:
cpu_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- "--cpu"
- "4"
- "--io"
- "2"
- "--vm"
- "1"
- "--vm-bytes"
- "1G"
- "--timeout"
- "10s"
- "--metrics-brief"
mem_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- "--vm 2"
- "-vm-bytes"
- "1G"
experiments:
- name: cpu_attack_exp
job: cpu_attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
- name: mem_attack_exp
job: mem_attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
+53
View File
@@ -0,0 +1,53 @@
jobs:
# cpu-attack:
# image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
# command: "stress-ng"
# args:
# - --cpu
# - 4
# - --io
# - 2
# - --vm
# - 1
# - --vm-bytes
# - 1G
# - --timeout
# - 10s
# - --metrics-brief
# mem-attack:
# image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
# command: "stress-ng"
# args:
# - --vm
# - 2
# - --vm-bytes
# - 1G
cpu-attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help
mem-attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help
experiments:
- name: cpu-attack-exp
job: cpu-attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
- name: mem-attack-exp
job: mem-attack
loop: 5
after:
check_url: https://google.it
check_payload: ''
check_tls_ignore: true
+26 -25
View File
@@ -6,7 +6,8 @@ from kubernetes import client, config
from kubernetes.client.rest import ApiException
import requests
from string import Template
import string
import random
def create_container(image, name, command, args):
container = client.V1Container(
@@ -27,13 +28,13 @@ def create_container(image, name, command, args):
def create_pod_template(pod_name, container):
pod_template = client.V1PodTemplateSpec(
spec=client.V1PodSpec(restart_policy="Never", containers=[container]),
metadata=client.V1ObjectMeta(name=pod_name, labels={"pod_name": pod_name}),
metadata=client.V1ObjectMeta(name=pod_name, labels={"pod_name": pod_name, "approle": "chaosnode"}),
)
return pod_template
def create_job(job_name, pod_template):
metadata = client.V1ObjectMeta(name=job_name, labels={"job_name": job_name})
metadata = client.V1ObjectMeta(name=job_name, labels={"job_name": job_name, "approle": "chaosnode"})
job = client.V1Job(
api_version="batch/v1",
@@ -41,7 +42,7 @@ def create_job(job_name, pod_template):
metadata=metadata,
spec=client.V1JobSpec(backoff_limit=0, template=pod_template),
)
logger.info(job)
#logger.info(job)
return job
@@ -49,7 +50,7 @@ def create_job(job_name, pod_template):
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
logging.info('Starting script for KubeInvaders programming mode')
with open("./example.yaml", 'r') as stream:
with open("./experiments.yaml", 'r') as stream:
try:
parsed_yaml=yaml.safe_load(stream)
logging.info(f"Parsed yaml => {parsed_yaml}")
@@ -57,8 +58,9 @@ with open("./example.yaml", 'r') as stream:
print(exc)
configuration = client.Configuration()
configuration.api_key = {"authorization": "Bearer ...."}
configuration.host = 'https://localhost:6443'
token = os.environ["TOKEN"]
configuration.api_key = {"authorization": f"Bearer {token}"}
configuration.host = os.environ["ENDPOINT"]
configuration.insecure_skip_tls_verify = True
configuration.verify_ssl = False
@@ -75,25 +77,24 @@ for job in parsed_yaml["jobs"]:
for exp in parsed_yaml["experiments"]:
logging.info(f"Processing the experiment {exp}")
job_attrs = parsed_yaml["jobs"][exp["job"]]
logging.info(f"args= {job_attrs['args']}")
args = []
for arg in job_attrs['args']:
args.append(str(arg))
logging.info(f"args = {args}")
logging.info(f"command = {job_attrs['command']}")
container = create_container(
image = job_attrs["image"],
name = exp["name"],
command = job_attrs["command"],
args = job_attrs["args"]
image = job_attrs['image'],
name = exp['name'],
command = [job_attrs['command']],
args = args
)
letters = string.ascii_lowercase
rand_suffix = ''.join(random.choice(letters) for i in range(5))
job_name = f"{exp['name']}-{rand_suffix}"
pod_template = create_pod_template(exp["name"], container)
logging.info(f"Creating job {exp['name']}")
job_def = create_job(exp["name"], pod_template)
batch_api.create_namespaced_job('kubeinvaders', job_def)
# try:
# api_response = api_instance.list_namespaced_pod_template(namespace, pretty=True, label_selector='foo=bar')
# logging.info(api_response)
# except ApiException as e:
# logging.info(e)
# if len(api_response.items) == 0:
# logging.info("Black box exporter is not installed. Installing it...")
logging.info(f"Creating job {job_name}")
job_def = create_job(job_name, pod_template)
batch_api.create_namespaced_job('kubeinvaders', job_def)