Skip to content

Commit

Permalink
fix programming mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky-sideburn committed Aug 13, 2024
1 parent fcbbbda commit 26bd209
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 121 deletions.
77 changes: 37 additions & 40 deletions html5/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

<div class="sidebar" id="sidebar">
<a id="gameModeButton" onclick="startGameMode()">Game Mode</a>
<a id="programmingModeButton" onclick="startProgrammingMode()">Programming Mode (alpha)</a>
<a id="programmingModeButton" onclick="startProgrammingMode()">Programming Mode</a>
<a href="#" onclick="showSpecialKeys()">Show Special Keys</a>
<a href="#" onclick="showCurrentChaosContainer()">Show Current Chaos Container for nodes</a>
<a href="#" onclick="showSetCurrentChaosContainer()">Set Custom Chaos Container for nodes</a>
Expand Down Expand Up @@ -424,13 +424,13 @@ <h2 class="accordion-header" id="specialkeys-headingThree">
<!-- START CHAOS PROGRAMMING MODE SCREEN -->
<div id="chaos-program-screen" style="display: none;">
<div class="">
<div class="row" style="margin-top: 2%;">
<font size="2" class="text-kinv" id="loadTestingPresetsText">Load Testing Presets & Chaos Programs</font>
</div>
<!-- <div class="row" style="margin-top: 2%;"> -->
<!-- <font size="2" class="text-kinv" id="loadTestingPresetsText">Load Testing Presets & Chaos Programs</font>
</div> -->
<div class="row">
<div class="btn-group" id="loadButtonGroup" role="group" aria-label="Basic example" style="overflow-x: scroll; width: 100%; padding-bottom: 2%; padding-top: 2%; scrollbar-color: dark;">
<button type="button" id="loadDefault" class="btn btn-light btn-md" onclick="loadPreset('default', 'k-inv')">Default</button>
<button type="button" id="loadCassandra" class="btn btn-light btn-md" onclick="loadPreset('cassandra', 'python')">Cassandra</button>
<!-- <button type="button" id="loadDefault" class="btn btn-light btn-md" onclick="loadPreset('default', 'k-inv')">Default</button> -->
<!-- <button type="button" id="loadCassandra" class="btn btn-light btn-md" onclick="loadPreset('cassandra', 'python')">Cassandra</button>
<button type="button" id="loadConsul" class="btn btn-light btn-md" onclick="loadPreset('consul', 'python')">Consul</button>
<button type="button" id="loadElasticsearch" class="btn btn-light btn-md" onclick="loadPreset('elasticsearch', 'python')">Elasticsearch</button>
<button type="button" id="loadEtcd3" class="btn btn-light btn-md" onclick="loadPreset('etcd3', 'python')">Etcd3</button>
Expand All @@ -444,17 +444,17 @@ <h2 class="accordion-header" id="specialkeys-headingThree">
<button type="button" id="loadNomad" class="btn btn-light btn-md" onclick="loadPreset('nomad', 'python')">Nomad</button>
<button type="button" id="loadPostgresql" class="btn btn-light btn-md" onclick="loadPreset('postgresql', 'python')">Postgresql</button>
<button type="button" id="loadPrometheus" class="btn btn-light btn-md" onclick="loadPreset('prometheus', 'python')">Prometheus</button>
<button type="button" id="loadRabbit" class="btn btn-light btn-md" onclick="loadPreset('rabbit', 'python')">Rabbit</button>
<button type="button" id="loadSSH" class="btn btn-light btn-md" onclick="loadPreset('SSH', 'python')">SSH</button>
<button type="button" id="loadVault" class="btn btn-light btn-md" onclick="loadPreset('vault', 'python')">Vault</button>
<button type="button" id="loadRabbit" class="btn btn-light btn-md" onclick="loadPreset('rabbit', 'python')">Rabbit</button> -->
<!-- <button type="button" id="loadSSH" class="btn btn-light btn-md" onclick="loadPreset('SSH', 'python')">SSH</button> -->
<!-- <button type="button" id="loadVault" class="btn btn-light btn-md" onclick="loadPreset('vault', 'python')">Vault</button> -->
</div>
</div>
<div class="row">
<div class="btn-group" id="loadChaosProgramButtonGroup" role="group" aria-label="Basic example" style="overflow-x: scroll; width: 100%; padding-bottom: 2.0%; padding-top: 0.5%; scrollbar-color: dark;">
</div>
</div>
</div>
<div class="row" style="margin-top: 1%;">
<div class="row" style="margin-top: 0,5%;">
<div id="alert_placeholder_programming_mode" style="margin-top: 1%; margin-bottom: 1%;"></div>
<div class="col">
<div class="row" width="10px">
Expand All @@ -469,36 +469,33 @@ <h2 class="accordion-header" id="specialkeys-headingThree">
<div class="form-group">
<label for="chaosProgramTextArea"></label>
<textarea class="form-control chaos-prog-area" id="chaosProgramTextArea" rows="50" style="min-width: 100%; font-family: Courier, monospace;">
chaos-codename: CODENAME_PLACEHOLDER
jobs:
cpu-attack-job:
additional-labels:
chaos-controller: kubeinvaders
chaos-type: stress-ng
chaos-codename: CODENAME_PLACEHOLDER
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --version

mem-attack-job:
additional-labels:
chaos-controller: kubeinvaders
chaos-type: stress-ng
chaos-codename: CODENAME_PLACEHOLDER
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --version

experiments:
- name: cpu-attack-exp
job: cpu-attack-job
loop: 5

- name: mem-attack-exp
job: mem-attack-job
loop: 5
chaos-codename: CODENAME_PLACEHOLDER
k8s_jobs:
cpu-attack-job:
additional-labels:
chaos-controller: kubeinvaders
chaos-type: stress-ng
chaos-codename: CODENAME_PLACEHOLDER
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --version
mem-attack-job:
additional-labels:
chaos-controller: kubeinvaders
chaos-type: stress-ng
chaos-codename: CODENAME_PLACEHOLDER
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --version
experiments:
- name: cpu_attack_exp
k8s_job: cpu-attack-job
loop: 5
- name: mem_attack_exp
k8s_job: mem-attack-job
loop: 5
</textarea>
</div>
</form>
Expand Down
11 changes: 8 additions & 3 deletions html5/programming_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ function runChaosProgram() {
$('#chaosProgramTextArea').val(chaosProgramWithCodename);
codename_configured = true;

var now = new Date().toLocaleString().replace(',','')
$('#alert_placeholder_programming_mode').replaceWith(alert_div + 'Chaos Program launched at ' + now + ' </div>');
//var now = new Date().toLocaleString().replace(',','')
//$('#alert_placeholder_programming_mode').replaceWith(alert_div + 'Chaos Program launched at ' + now + ' </div>');

var oReq = new XMLHttpRequest();
oReq.open("POST", k8s_url + "/kube/chaos/programming_mode?id=" + random_code, true);
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
now = new Date().toLocaleString().replace(',','')
$('#alert_placeholder_programming_mode').replaceWith(alert_div + 'Chaos Program completed at ' + now + ' </div>');
if (this.responseText.includes("Invalid")) {
$('#alert_placeholder_programming_mode').replaceWith(alert_div + this.responseText + ' </div>');
}
else {
$('#alert_placeholder_programming_mode').replaceWith(alert_div + this.responseText + 'Chaos Program loaded at ' + now + ' </div>');
}
}
};;
oReq.setRequestHeader("Content-Type", "application/json");
Expand Down
16 changes: 12 additions & 4 deletions nginx/KubeInvaders.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ server {
end
}

set_by_lua_block $platform_engineering_demo_mode {
if (os.getenv("PLATFORM_ENGINEERING_STATS") == nil) then
return "false"
else
return os.getenv("PLATFORM_ENGINEERING_STATS")
end
}

location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
Expand Down Expand Up @@ -558,7 +566,7 @@ server {
ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range';
ngx.req.read_body()
local data = ngx.req.get_body_data()
-- -- ngx.log(ngx.INFO, "[PROGRAMMING-MODE-DIAGRAM] data sent from web interface => " .. data)
-- ngx.log(ngx.INFO, "[PROGRAMMING-MODE-DIAGRAM] data sent from web interface => " .. data)

math.randomseed(os.clock()*100000000000)
local rand = math.random(999, 9999)
Expand All @@ -574,7 +582,7 @@ server {
local result = handle:read("*a")
local rc = handle:close()

-- ngx.log(ngx.INFO, "[programming_mode_diagram] Result of yaml syntax check =>||" .. result .. "||")
-- ngx.log(ngx.INFO, "[PROGRAMMING-MODE-DIAGRAM] Result of yaml syntax check =>||" .. result .. "||")

if data == nil then
error = "[PROGRAMMING-MODE-DIAGRAM] No chaos program already loaded."
Expand All @@ -589,7 +597,7 @@ server {

local yaml_data = lyaml.load(data)

if not key_exists(yaml_data, "jobs") then
if not key_exists(yaml_data, "k8s_jobs") then
error = "[PROGRAMMING-MODE-DIAGRAM] Chaos program does not contain 'jobs' key. Please fix yaml definition."
-- ngx.log(ngx.INFO, error)
ngx.say(error)
Expand All @@ -599,7 +607,7 @@ server {
-- ngx.log(ngx.INFO, error)
ngx.say(error)

elseif is_key_empty(yaml_data, "jobs") then
elseif is_key_empty(yaml_data, "k8s_jobs") then
error = "[PROGRAMMING-MODE-DIAGRAM] Chaos program does not contain valid 'jobs' key. Please fix yaml definition, add jobs"
-- ngx.log(ngx.INFO, error)
ngx.say(error)
Expand Down
31 changes: 2 additions & 29 deletions scripts/metrics_loop/experiments.yaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
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:
cpu_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help

mem-attack:
mem_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help

experiments:
- name: cpu-attack-exp
job: cpu-attack
Expand Down
2 changes: 1 addition & 1 deletion scripts/metrics_loop/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def create_job(job_name, pod_template):
logging.debug(e)
if pod.metadata.labels.get('chaos-codename') != None:
codename = pod.metadata.labels.get('chaos-codename')
job_name = pod.metadata.labels.get('job-name')
job_name = pod.metadata.labels.get('job-name').replace("-","_")
exp_name = pod.metadata.labels.get('experiment-name')
if pod.status.phase in ["Pending", "Running", "Succeeded"]:
r.set(f"chaos_jobs_status:{codename}:{exp_name}:{job_name}", 1.0)
Expand Down
2 changes: 1 addition & 1 deletion scripts/programming_mode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ red:expire("logs_enabled:" .. arg['id'], "10")
local handle = io.popen("python3 /opt/programming_mode/start.py " .. file_name .. " " .. k8s_url)
local result = handle:read("*a")

ngx.say("Chaos program has been started...")
ngx.say(result)
39 changes: 6 additions & 33 deletions scripts/programming_mode/experiments.yaml
Original file line number Diff line number Diff line change
@@ -1,53 +1,26 @@
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:
cpu_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
args:
- --help

mem-attack:
mem_attack:
image: docker.io/luckysideburn/kubeinvaders-stress-ng:latest
command: "stress-ng"
additional-labels:
test-label: "test"
args:
- --help

experiments:
- name: cpu-attack-exp
job: cpu-attack
- 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
- name: mem_attack_exp
job: mem_attack
loop: 5
after:
check_url: https://google.it
Expand Down
37 changes: 27 additions & 10 deletions scripts/programming_mode/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import string
import random
import redis
import re

def create_container(image, name, command, args):
container = client.V1Container(
Expand Down Expand Up @@ -59,7 +60,7 @@ def create_job(job_name, pod_template):
parsed_yaml=yaml.safe_load(stream)
logging.info(f"Parsed yaml => {parsed_yaml}")
except yaml.YAMLError as exc:
print(exc)
print(exc + " ")

r = redis.Redis(unix_socket_path='/tmp/redis.sock')

Expand All @@ -76,37 +77,53 @@ def create_job(job_name, pod_template):
api_instance = client.CoreV1Api()
batch_api = client.BatchV1Api()
namespace = "kubeinvaders"
k8s_regex = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
prom_regex = "[a-zA-Z_:][a-zA-Z0-9_:]*"

for job in parsed_yaml["jobs"]:
for job in parsed_yaml["k8s_jobs"]:
logging.info(f"Found job {job}")

if not re.fullmatch(k8s_regex, job):
ret = f"Invalid name for k8s_jobs: {job}, please match Kubernetes name format '[a-z0-9]([-a-z0-9]*[a-z0-9])?'"
logging.info(ret)
print(ret)
quit()

for exp in parsed_yaml["experiments"]:

for _ in range(exp["loop"]):
logging.info(f"Processing the experiment {exp}")
job_attrs = parsed_yaml["jobs"][exp["job"]]
job_attrs = parsed_yaml["k8s_jobs"][exp["k8s_job"]]
args = []
for arg in job_attrs['args']:
args.append(str(arg))

logging.info(f"args = {args}")
logging.info(f"command = {job_attrs['command']}")
logging.info(f"image = {job_attrs['image']}")
logging.info(f"k8s_job = {exp['k8s_job']}")

if not re.fullmatch(prom_regex, exp["name"]):
ret = f"Invalid name for experiment: {exp['name']}, please match Prometheus metric name format '[a-zA-Z_:][a-zA-Z0-9_:]*'"
logging.info(ret)
print(ret)
quit()

container = create_container(
image = job_attrs['image'],
name = exp['name'],
name = exp['k8s_job'],
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['job']}-{rand_suffix}"
job_name = f"{exp['k8s_job']}-{rand_suffix}"

if 'additional-labels' in job_attrs:
logging.info(f"additional-labels = {job_attrs['additional-labels']}")
pod_template = create_pod_template(f"{exp['name']}-exec", job_attrs['additional-labels'], container, exp["name"])
pod_template = create_pod_template(f"{exp['k8s_job']}_exec", job_attrs['additional-labels'], container, exp['name'])
else:
pod_template = create_pod_template(f"{exp['name']}-exec", {}, container, exp["name"])
pod_template = create_pod_template(f"{exp['k8s_job']}_exec", {}, container, exp['k8s_job'])

logging.info(f"Creating job {job_name}")
job_def = create_job(job_name, pod_template)
Expand All @@ -119,11 +136,11 @@ def create_job(job_name, pod_template):

if 'additional-labels' in job_attrs and 'chaos-codename' in job_attrs['additional-labels']:
codename = job_attrs['additional-labels']['chaos-codename']
r.set(f"chaos_jobs_status:{codename}:{exp['name']}:{exp['job']}", 0.0)
metric_job_name = job_name.replace("-","_");
r.set(f"chaos_jobs_status:{codename}:{exp['name']}:{job_name}", 0.0)

if r.exists('chaos_node_jobs_total') == 1:
r.incr('chaos_node_jobs_total')
else:
r.set("chaos_node_jobs_total", 1)

os.remove(sys.argv[1])

0 comments on commit 26bd209

Please sign in to comment.