diff --git a/html5/index.html b/html5/index.html index 32abba133..21ad0daeb 100644 --- a/html5/index.html +++ b/html5/index.html @@ -101,7 +101,7 @@ '); + } } };; oReq.setRequestHeader("Content-Type", "application/json"); diff --git a/nginx/KubeInvaders.conf b/nginx/KubeInvaders.conf index b68f7f6ec..6a5913877 100644 --- a/nginx/KubeInvaders.conf +++ b/nginx/KubeInvaders.conf @@ -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'; @@ -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) @@ -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." @@ -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) @@ -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) diff --git a/scripts/metrics_loop/experiments.yaml b/scripts/metrics_loop/experiments.yaml index 599e76d44..726863f49 100644 --- a/scripts/metrics_loop/experiments.yaml +++ b/scripts/metrics_loop/experiments.yaml @@ -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 diff --git a/scripts/metrics_loop/start.py b/scripts/metrics_loop/start.py index 26dac34bc..48b19eae8 100644 --- a/scripts/metrics_loop/start.py +++ b/scripts/metrics_loop/start.py @@ -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) diff --git a/scripts/programming_mode.lua b/scripts/programming_mode.lua index 06b64f365..9b3ba110c 100644 --- a/scripts/programming_mode.lua +++ b/scripts/programming_mode.lua @@ -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) diff --git a/scripts/programming_mode/experiments.yaml b/scripts/programming_mode/experiments.yaml index a5006a298..4865e128b 100644 --- a/scripts/programming_mode/experiments.yaml +++ b/scripts/programming_mode/experiments.yaml @@ -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 diff --git a/scripts/programming_mode/start.py b/scripts/programming_mode/start.py index fcea7503f..83854e689 100644 --- a/scripts/programming_mode/start.py +++ b/scripts/programming_mode/start.py @@ -10,6 +10,7 @@ import string import random import redis +import re def create_container(image, name, command, args): container = client.V1Container( @@ -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') @@ -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) @@ -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])