From 5f69a88254c312c4e3faa37955439e7e36532568 Mon Sep 17 00:00:00 2001 From: Eugenio Marzo Date: Thu, 11 Feb 2021 15:18:40 +0000 Subject: [PATCH] fix endpoint --- entrypoint.sh | 2 ++ html5/kubeinvaders.js | 12 ++++++------ nginx/node.lua | 38 ++++++++++++++++++++++++++++++++++++-- nginx/pod.lua | 43 ++++++++++++++++++++++++++++++++++++++++--- temporary_hack.sh | 19 ------------------- 5 files changed, 84 insertions(+), 30 deletions(-) delete mode 100644 temporary_hack.sh diff --git a/entrypoint.sh b/entrypoint.sh index 3d69f79..2cb485c 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,4 +2,6 @@ redis-server /etc/redis/redis.conf & +sed -i "s/ENDPOINT_PLACEHOLDER/$ENDPOINT/g" /var/www/html/kubeinvaders.js + nginx -c /etc/nginx/nginx.conf -g 'daemon off;' diff --git a/html5/kubeinvaders.js b/html5/kubeinvaders.js index 65b9d69..c25fad0 100644 --- a/html5/kubeinvaders.js +++ b/html5/kubeinvaders.js @@ -87,7 +87,7 @@ function getNamespaces(){ namespaces = namespaces.split(","); namespace = namespaces[namespaces_index]; };; - oReq.open("GET", "https://kubeinvaders.io/kube/namespaces"); + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/namespaces"); oReq.send(); } @@ -97,7 +97,7 @@ function getEndpoint(){ oReq.onload = function () { endpoint = this.responseText; };; - oReq.open("GET", "https://kubeinvaders.io/kube/endpoint"); + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/endpoint"); oReq.send(); } @@ -106,7 +106,7 @@ function startChaosNode(node_name){ oReq.onload = function () { console.log(JSON.parse(this.responseText)) };; - oReq.open("GET", "https://kubeinvaders.io/kube/chaos/nodes?nodename=node_name=" + node_name + "&namespace=" + namespace); + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/chaos/nodes?nodename=node_name=" + node_name + "&namespace=" + namespace); oReq.send(); } @@ -115,7 +115,7 @@ function deletePods(pod_name){ oReq.onload = function () { console.log(JSON.parse(this.responseText)) };; - oReq.open("GET", "https://kubeinvaders.io/kube/pods?action=delete&pod_name=" + pod_name + "&namespace=" + namespace); + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/pods?action=delete&pod_name=" + pod_name + "&namespace=" + namespace); oReq.send(); } @@ -125,7 +125,7 @@ function getPods(){ json_parsed = JSON.parse(this.responseText) pods = json_parsed["items"].concat(nodes); };; - oReq.open("GET", "https://kubeinvaders.io/kube/pods?action=list&namespace=" + namespace); + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/pods?action=list&namespace=" + namespace); oReq.send(); } @@ -135,7 +135,7 @@ function getNodes(){ json_parsed = JSON.parse(this.responseText) nodes = json_parsed["items"]; };; - oReq.open("GET", "https://kubeinvaders.io/kube/nodes"); + oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/nodes"); oReq.send(); } diff --git a/nginx/node.lua b/nginx/node.lua index 9922037..aaf7047 100644 --- a/nginx/node.lua +++ b/nginx/node.lua @@ -1,8 +1,7 @@ -loadfile("/tmp/metrics.lua") - local https = require "ssl.https" local ltn12 = require "ltn12" local json = require 'lunajson' +local redis = require "resty.redis" if os.getenv("KUBERNETES_SERVICE_HOST") then k8s_url = "https://" .. os.getenv("KUBERNETES_SERVICE_HOST") .. ":" .. os.getenv("KUBERNETES_SERVICE_PORT_HTTPS") @@ -16,6 +15,41 @@ local url = k8s_url.. "/api/v1/nodes" local decoded = nil local nodes = {} + +if ngx.var.request_method == "GET" and string.match(ngx.var.request_uri, "^.*/chaos[-]node.*$") then + local red = redis:new() + local okredis, errredis = red:connect("unix:/tmp/redis.sock") + + if okredis then + ngx.log(ngx.ERR, "Connection to Redis is ok") + else + ngx.log(ngx.ERR, "Connection to Redis is not ok") + ngx.log(ngx.ERR, errredis) + end + -- Count the total of chaos jobs launched against nodes + local chaos_node_res, err = red:get("chaos_node_jobs_total") + + if chaos_node_res == ngx.null then + ngx.say(err) + red:set("chaos_node_jobs_total", 0) + else + local incr = chaos_node_res + 1 + local res, err = red:set("chaos_node_jobs_total",incr) + end + + -- Count the total of chaos jobs launched against nodes per node + local node_name = arg['node_name'] + local chaos_node_res, err = red:get("chaos_node_jobs_total_on_" .. node_name) + if chaos_node_res == ngx.null then + ngx.say(err) + red:set("chaos_node_jobs_total_on_" .. node_name, 1) + else + local incr = chaos_node_res + 1 + local res, err = red:set("chaos_node_jobs_total_on_" .. node_name,incr) + end +end + + ngx.header['Access-Control-Allow-Origin'] = '*' ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' diff --git a/nginx/pod.lua b/nginx/pod.lua index 0619d99..48a8681 100644 --- a/nginx/pod.lua +++ b/nginx/pod.lua @@ -1,7 +1,8 @@ -loadfile("/tmp/metrics.lua") local https = require "ssl.https" local ltn12 = require "ltn12" local json = require 'lunajson' +local redis = require "resty.redis" +local incr = 0 if os.getenv("KUBERNETES_SERVICE_HOST") then k8s_url = "https://" .. os.getenv("KUBERNETES_SERVICE_HOST") .. ":" .. os.getenv("KUBERNETES_SERVICE_PORT_HTTPS") @@ -24,6 +25,44 @@ ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' ngx.header['Access-Control-Allow-Headers'] = 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range'; + +if action == "delete" then + local red = redis:new() + local okredis, errredis = red:connect("unix:/tmp/redis.sock") + + if okredis then + ngx.log(ngx.ERR, "Connection to Redis is ok") + else + ngx.log(ngx.ERR, "Connection to Redis is not ok") + ngx.log(ngx.ERR, errredis) + end + -- Count the total of deleted pods + local res, err = red:get("deleted_pods_total") + + if res == ngx.null then + ngx.say(err) + red:set("deleted_pods_total", 1) + else + incr = res + 1 + red:set("deleted_pods_total", incr) + end + + -- TODO: Make a better regular expression! + local namespace_pods = string.match(ngx.var.request_uri, '^.*/namespaces/(.*)/.*$') + local namespace = namespace_pods:gsub("/pods", "") + + -- Count the total of deleted pods for namespace + local res, err = red:get("deleted_pods_total_on_" .. namespace) + + if res == ngx.null then + red:set("deleted_pods_total_on_" .. namespace, 1) + else + incr = res + 1 + red:set("deleted_pods_total_on_" .. namespace, incr) + end +end + + if action == "list" then url = k8s_url.. "/api/v1/namespaces/" .. namespace .. "/pods" @@ -61,8 +100,6 @@ ngx.log(ngx.ERR, ok) ngx.log(ngx.ERR, statusCode) ngx.log(ngx.ERR, statusText) -ngx.log(ngx.ERR, table.concat(resp)) - if action == "list" then local i = 1 pods["items"] = {} diff --git a/temporary_hack.sh b/temporary_hack.sh deleted file mode 100644 index 6813041..0000000 --- a/temporary_hack.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -redis-server /etc/redis/redis.conf & - -if [ ! -z $DEVELOPMENT ]; -then - ENDPOINT_JS=$(echo "${ENDPOINT}" | sed "s/\//\\\\\//g") - envsubst '${ENDPOINT}' < "/etc/nginx/conf.d/KubeInvaders_dev.templ" > "/etc/nginx/conf.d/KubeInvaders.conf" - sed -i "s/var\ ENV={};/var\ ENV={};ENV[\"TOKEN\"]=\"$TOKEN\";ENV[\"ENDPOINT\"]=\"$ENDPOINT_JS\";ENV[\"NAMESPACE\"]=\"$NAMESPACE\";ENV[\"HITSLIMIT\"]=\"$HITSLIMIT\";ENV[\"ALIENPROXIMITY\"]=\"$ALIENPROXIMITY\";ENV[\"UPDATETIME\"]=\"$UPDATETIME\";/g" /var/www/html/KubeInvaders_wasm.js -else - ENDPOINT=$(echo "https://${ROUTE_HOST}" | sed "s/\//\\\\\//g") - envsubst '${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT_HTTPS}' < "/etc/nginx/conf.d/KubeInvaders.templ" > "/etc/nginx/conf.d/KubeInvaders.conf" - sed -i "s/var\ ENV={};/var\ ENV={};ENV[\"TOKEN\"]=\"$TOKEN\";ENV[\"ENDPOINT\"]=\"$ENDPOINT\/kube\";ENV[\"NAMESPACE\"]=\"$NAMESPACE\";ENV[\"HITSLIMIT\"]=\"$HITSLIMIT\";ENV[\"ALIENPROXIMITY\"]=\"$ALIENPROXIMITY\";ENV[\"UPDATETIME\"]=\"$UPDATETIME\";/g" /var/www/html/KubeInvaders_wasm.js -fi - -sed -i "s/TOTAL_ENV_SIZE=1024/TOTAL_ENV_SIZE=2048/g" /var/www/html/KubeInvaders_wasm.js -sed -i "s/TOTAL_ENV_SIZE=1024/TOTAL_ENV_SIZE=2048/g" /var/www/html/KubeInvaders_asmjs.js - -nginx -c /etc/nginx/nginx.conf -g 'daemon off;'