From b84d105890edb44e2cba79f1192177aac296980b Mon Sep 17 00:00:00 2001 From: Eugenio Marzo Date: Sat, 7 Jan 2023 16:00:48 +0100 Subject: [PATCH] code review --- html5/kubeinvaders.js | 39 +++------------------------------ scripts/logs_loop/start.py | 41 ++++++++++++++--------------------- scripts/metrics_loop/start.py | 1 - scripts/node.lua | 2 +- scripts/pod.lua | 2 +- 5 files changed, 21 insertions(+), 64 deletions(-) diff --git a/html5/kubeinvaders.js b/html5/kubeinvaders.js index 6e7d0f4..b2c2076 100644 --- a/html5/kubeinvaders.js +++ b/html5/kubeinvaders.js @@ -240,10 +240,6 @@ function drawChaosProgramFlow() { var chaosProgram = ""; chaosProgram = $('#chaosProgramTextArea').val(); - // var chaosProgramWithCodename = chaosProgram.replace('CODENAME_PLACEHOLDER', codename); - // console.log(chaosProgramWithCodename); - // $('#chaosProgramTextArea').val(chaosProgramWithCodename); - var oReq = new XMLHttpRequest(); oReq.open("POST", k8s_url + "/chaos/programs/json-flow", true); @@ -313,7 +309,6 @@ function contains(a, obj) { function getMetrics() { var oReq = new XMLHttpRequest(); oReq.onload = function () { - //console.log(this.responseText); var lines = this.responseText.split('\n'); for (var i = 0;i < lines.length;i++){ metric = lines[i].split(' '); @@ -336,9 +331,6 @@ function getMetrics() { else if (metric[0].match(chaos_job_regex)) { metrics_split = metric[0].split(":"); chaos_jobs_status.set(metrics_split[1] + ":" + metrics_split[2] + ":" + metrics_split[3], metric[1]); - // for (let [key, value] of chaos_jobs_status) { - // console.log(key + " = " + value); - // } } else if (metric[0] == "current_chaos_job_pod") { $('#current_chaos_job_pod').text(metric[1]); @@ -413,7 +405,6 @@ function getEndpoint() { function getCurrentChaosContainer() { var oReq = new XMLHttpRequest(); oReq.onload = function () { - //console.log(this.responseText); job_parsed = JSON.stringify(JSON.parse(this.responseText), null, 4); $('#currentChaosContainerYaml').text(job_parsed); $('#currentChaosContainerJsonTextArea').val(job_parsed); @@ -472,7 +463,6 @@ function setChaosContainer() { oReq.onreadystatechange = function () { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { - //console.log(this.responseText); $('#alert_placeholder2').text('New container definition has been saved.'); } };; @@ -494,7 +484,6 @@ function runChaosProgram() { oReq.open("POST", k8s_url + "/kube/chaos/programming_mode?id=" + random_code, true); oReq.onreadystatechange = function () { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { - //console.log(this.responseText); now = new Date().toLocaleString().replace(',','') $('#alert_placeholder4').replaceWith(alert_div + 'Chaos Program completed at ' + now + ' '); } @@ -506,9 +495,9 @@ function runChaosProgram() { function startChaosNode(node_name) { var oReq = new XMLHttpRequest(); oReq.onload = function () { - //console.log(JSON.parse(this.responseText)) + $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Launched chaos job against ' + node_name + ''); };; - $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Start Chaos Job on ' + node_name + ''); + $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Start chaos job against ' + node_name + ''); oReq.open("GET", k8s_url + "/kube/chaos/nodes?nodename=" + node_name + "&namespace=" + namespace); oReq.send(); } @@ -516,9 +505,8 @@ function startChaosNode(node_name) { function deletePods(pod_name) { var oReq = new XMLHttpRequest(); oReq.onload = function () { - //console.log(JSON.parse(this.responseText)) + $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Kill ' + pod_name + ''); };; - $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Kill ' + pod_name + ''); oReq.open("GET", k8s_url + "/kube/pods?action=delete&pod_name=" + pod_name + "&namespace=" + namespace); oReq.send(); } @@ -573,42 +561,27 @@ function keyDownHandler(e) { e.preventDefault(); if(e.key == "Right" || e.key == "ArrowRight") { rightPressed = true; - //console.log("Go right"); - //console.log("Spaceship Y:" + spaceshipY); - //console.log("Spaceship X: " + spaceshipX); } else if(e.key == "Left" || e.key == "ArrowLeft") { leftPressed = true; - //console.log("Go left"); - //console.log("Spaceship Y:" + spaceshipY); - //console.log("Spaceship X: " + spaceshipX); } if(e.key == "Up" || e.key == "ArrowUp") { upPressed = true; - //console.log("Go up"); - //console.log("Spaceship Y:" + spaceshipY); - //console.log("Spaceship X: " + spaceshipX); } else if(e.key == "Down" || e.key == "ArrowDown") { downPressed = true; - //console.log("Go down"); - //console.log("Spaceship Y: " + spaceshipY); - //console.log("Spaceship X: " + spaceshipX); } else if(e.keyCode == 83) { if (shuffle) { shuffle = false; $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Disable shuffle'); - //console.log("Deactivate shuffle"); } else { shuffle = true - //console.log("Activate shuffle"); $('#alert_placeholder').replaceWith(alert_div + 'Latest action: Enable shuffle'); } } else if(e.keyCode == 32) { - //console.log("Shot"); shot = true } else if(e.keyCode == 78) { @@ -696,7 +669,6 @@ function drawAlien(alienX, alienY, name) { function checkRocketAlienCollision() { if (contains(aliensY, rocketY)) { - //console.log("The y of rocket is the same of an alien. rocketY=" + rocketY + " List of aliensY:" + aliensY); var i; for (i=aliens.length - 1; i >= 0; i--) { if (aliens[i]["active"] && (rocketY - aliens[i]["y"] < 5)) { @@ -708,7 +680,6 @@ function checkRocketAlienCollision() { } if(contains(rangeX, rocketX)) { - //console.log("collision detected"); collisionDetected = true; aliens[i]["active"] = false; if (contains(nodes, aliens[i]["name"])) { @@ -746,7 +717,6 @@ function drawRocket() { if(shot && rocketLaunched) { if (rocketY < 0) { - //console.log("Rocket arrived to the end of canvas"); shot = false; rocketLaunched = false; } @@ -946,7 +916,6 @@ window.setInterval(function setAliens() { } aliens = []; - //console.log("Length of aliensY array: " + aliensY.length); if (pods.length > 0) { for (k=10; k>0; k--) { if (!contains(aliensY, k)) { @@ -977,7 +946,6 @@ window.setInterval(function setAliens() { cnt =+ 1; } if (aliens.length % 12 == 0) { - //console.log("we need another line of aliens for Y="+aliensIncrementY); x = 10; y += aliensIncrementY; for (k=y+10; k>=y; k--) { @@ -985,7 +953,6 @@ window.setInterval(function setAliens() { aliensY.push(k); } } - //console.log("aliensY contains new Y for detecting eventual collisions. aliensY="+aliensY); } else { x += 60; diff --git a/scripts/logs_loop/start.py b/scripts/logs_loop/start.py index 014f087..8d8afe9 100644 --- a/scripts/logs_loop/start.py +++ b/scripts/logs_loop/start.py @@ -20,10 +20,8 @@ import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) def compute_line(api_response_line, api_instance, container): - #api_response_line = api_response_line.encode('utf-8', 'xmlcharrefreplace') - logging.info(f"[logid:{logid}] API Response: ||{api_response_line}||") + logging.debug(f"[logid:{logid}] API Response: ||{api_response_line}||") logrow = f"
-----------------------
Namespace: {pod.metadata.namespace}
Pod: {pod.metadata.name}
Container: {container}
>>>{api_response_line}
" - #store = False sha256log = sha256(logrow.encode('utf-8')).hexdigest() if not r.exists(f"log:{logid}:{pod.metadata.name}:{sha256log}"): @@ -71,7 +69,6 @@ def line_prepender(filename, line, logid): # create logger logging.basicConfig(level=logging.INFO) -#logging.basicConfig(filename='/tmp/example.log', encoding='utf-8', level=os.environ.get("LOGLEVEL", "INFO")) logging.info('Starting script for KubeInvaders taking logs from pods...') @@ -192,44 +189,43 @@ while True: if cached_regex_match == "maching": webtail_pods.append(pod) regex_match_info = f"[logid:{logid}] Taking logs of {pod.metadata.name}. Redis has cached that {log_pod_regex} is good for {pod.metadata.name}" - #logging.info(regex_match_info) r.set(f"log_status:{logid}", regex_match_info) else: regex_match_info = f"[logid:{logid}] SKIPPING logs of {pod.metadata.name}. Redis has cached that {log_pod_regex} is not good for {pod.metadata.name}" - #logging.info(regex_match_info) + logging.debug(regex_match_info) r.set(f"log_status:{logid}", regex_match_info) else: - # logging.info(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}|") - # logging.info(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}|") - # logging.info(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}|") - # logging.info(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}|") + logging.debug(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}|") + logging.debug(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}|") + logging.debug(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}|") + logging.debug(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}|") if re.search(f"{pod_re}", pod.metadata.name) or re.search(r"{pod_re}", pod.metadata.name): - #logging.info(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}| RESULT: OK") + logging.debug(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}| RESULT: OK") regex_key_name = f"regex_cmp:{regexsha}:{regexsha}:{logid}:{pod.metadata.namespace}:{pod.metadata.name}" if re.search(f"{namespace_re}", pod.metadata.namespace) or re.search(r"{namespace_re}", pod.metadata.namespace): - #logging.info(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}| RESULT: OK") + logging.debug(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}| RESULT: OK") if re.search(f"{labels_re}", str(pod.metadata.labels)) or re.search(r"{labels_re}", str(pod.metadata.labels)): - #logging.info(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}| RESULT: OK") + logging.debug(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}| RESULT: OK") if re.search(f"{annotations_re}", str(pod.metadata.annotations)) or re.search(r"{annotations_re}", str(pod.metadata.annotations)): - #logging.info(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}| RESULT: OK") + logging.debug(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}| RESULT: OK") webtail_pods.append(pod) regex_match_info = f"[logid:{logid}] Taking logs from {pod.metadata.name}. It is compliant with the Regex {log_pod_regex}" r.set(regex_key_name, "maching") - #logging.info(regex_match_info) + logging.debug(regex_match_info) r.set(f"log_status:{logid}", regex_match_info) else: - #logging.info(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}| RESULT FAILED!") + logging.debug(f"[logid:{logid}] Regex comparison |{annotations_re}| |{str(pod.metadata.annotations)}| RESULT FAILED!") r.set(regex_key_name, "not_maching") else: - #logging.info(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}| RESULT: FAILED!") + logging.debug(f"[logid:{logid}] Regex comparison |{labels_re}| |{str(pod.metadata.labels)}| RESULT: FAILED!") r.set(regex_key_name, "not_maching") else: - #logging.info(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}| RESULT: FAILED!") + logging.debug(f"[logid:{logid}] Regex comparison |{namespace_re}| |{pod.metadata.namespace}| RESULT: FAILED!") r.set(regex_key_name, "not_maching") else: - #logging.info(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}| RESULT FAILED!") + logging.debug(f"[logid:{logid}] Regex comparison |{pod_re}| |{pod.metadata.name}| RESULT FAILED!") r.set(regex_key_name, "not_maching") try: @@ -256,19 +252,14 @@ while True: for container in container_list: if webtail_switch or (pod.metadata.labels.get('approle') != None and pod.metadata.labels['approle'] == 'chaosnode' and pod.status.phase != "Pending"): try: - latest_log_tail = r.get(f"log_time:{pod.metadata.name}") - #logging.info(f"[logid:{logid}] Reading logs of {pod.metadata.name} on {pod.metadata.namespace}") - + latest_log_tail = r.get(f"log_time:{pod.metadata.name}") if r.exists(f"log_time:{logid}:{pod.metadata.name}"): latest_log_tail_time = r.get(f"log_time:{logid}:{pod.metadata.name}") else: latest_log_tail_time = time.time() - #logging.info(f"[logid:{logid}] Latest latest_log_tail for {pod.metadata.name} is {latest_log_tail_time}. Current Unix Time is {time.time()}") since = int(time.time() - float(latest_log_tail_time)) + 1 - #logging.info(f"[logid:{logid}] Diff from time.time() and latest_log_tail_time for {pod.metadata.name} is {since}") - if since == 0: since = 1 diff --git a/scripts/metrics_loop/start.py b/scripts/metrics_loop/start.py index e4260b2..d6c2e0b 100644 --- a/scripts/metrics_loop/start.py +++ b/scripts/metrics_loop/start.py @@ -75,7 +75,6 @@ namespace = "kubeinvaders" while True: try: api_response = api_instance.list_namespaced_pod(namespace="kubeinvaders") - #logging.info(api_response) except ApiException as e: logging.info(e) diff --git a/scripts/node.lua b/scripts/node.lua index 079f7f6..b376163 100644 --- a/scripts/node.lua +++ b/scripts/node.lua @@ -3,7 +3,7 @@ local ltn12 = require "ltn12" local json = require 'lunajson' local redis = require "resty.redis" -k8s_url = "" +local k8s_url = "" if os.getenv("KUBERNETES_SERVICE_HOST") then k8s_url = "https://" .. os.getenv("KUBERNETES_SERVICE_HOST") .. ":" .. os.getenv("KUBERNETES_SERVICE_PORT_HTTPS") diff --git a/scripts/pod.lua b/scripts/pod.lua index ef0493a..5024f91 100644 --- a/scripts/pod.lua +++ b/scripts/pod.lua @@ -4,7 +4,7 @@ local json = require 'lunajson' local redis = require "resty.redis" local incr = 0 -k8s_url = "" +local k8s_url = "" if os.getenv("KUBERNETES_SERVICE_HOST") then k8s_url = "https://" .. os.getenv("KUBERNETES_SERVICE_HOST") .. ":" .. os.getenv("KUBERNETES_SERVICE_PORT_HTTPS")