diff --git a/nginx/metrics.lua b/nginx/metrics.lua deleted file mode 100644 index d524060..0000000 --- a/nginx/metrics.lua +++ /dev/null @@ -1,77 +0,0 @@ -local redis = require "resty.redis" -local arg = ngx.req.get_uri_args() -local incr = 0 -if ngx.var.request_method == "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 - - -elseif 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 - -elseif ngx.var.request_method == "GET" and ngx.var.request_uri == "/metrics" then - for i, res in ipairs(red:keys("*")) do - ngx.log(ngx.ERR, res) - ngx.say(res .. " " .. red:get(res)) - end -end diff --git a/nginx/node.lua b/nginx/node.lua deleted file mode 100644 index 2ab8d90..0000000 --- a/nginx/node.lua +++ /dev/null @@ -1,98 +0,0 @@ -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") -else - k8s_url = os.getenv("ENDPOINT") -end - -local token = os.getenv("TOKEN") -local arg = ngx.req.get_uri_args() -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' -ngx.header['Access-Control-Expose-Headers'] = 'Content-Length,Content-Range'; - ---ngx.log(ngx.ERR, "token: " .. token) -ngx.log(ngx.ERR, "url: " .. url) - -local headers = { - ["Accept"] = "application/json", - ["Content-Type"] = "application/json", - ["Authorization"] = "Bearer " .. token, -} - -local resp = {} - -local ok, statusCode, headers, statusText = https.request{ - url = url, - headers = headers, - method = "GET", - sink = ltn12.sink.table(resp) -} - -ngx.log(ngx.ERR, "REQUEST LOGS...") -ngx.log(ngx.ERR, ok) -ngx.log(ngx.ERR, statusCode) -ngx.log(ngx.ERR, statusText) - -local i = 0 -nodes["items"] = {} - -for k,v in ipairs(resp) do - decoded = json.decode(v) - if decoded["kind"] == "NodeList" then - for k2,v2 in ipairs(decoded["items"]) do - -- TODO: masters should be included? - -- if not v2["metadata"]["labels"]["node-role.kubernetes.io/master"] then - ngx.log(ngx.ERR, "found node " .. v2["metadata"]["name"]) - nodes["items"][i] = v2["metadata"]["name"] - i = i + 1 - --end - end - end -end - -ngx.say(json.encode(nodes)) diff --git a/nginx/pod.lua b/nginx/pod.lua deleted file mode 100644 index 37862cb..0000000 --- a/nginx/pod.lua +++ /dev/null @@ -1,125 +0,0 @@ -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") -else - k8s_url = os.getenv("ENDPOINT") -end - -local token = os.getenv("TOKEN") -local arg = ngx.req.get_uri_args() -local namespace = arg['namespace'] -local decoded = nil -local action = arg['action'] -local url = '' -local pods = {} -local method = 'GET' -local pods_not_found = true; - -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' -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 - - -- 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" - -elseif action == "delete" then - local pod_name = arg['pod_name'] - url = k8s_url.. "/api/v1/namespaces/" .. namespace .. "/pods/" .. pod_name - method = "DELETE" - -else - ngx.say("Please set the parameter 'action'") - ngx.exit(ngx.OK) -end - ---ngx.log(ngx.ERR, "token: " .. token) -ngx.log(ngx.ERR, "url: " .. url) -ngx.log(ngx.ERR, "namespace: " .. namespace) - -local headers = { - ["Accept"] = "application/json", - ["Content-Type"] = "application/json", - ["Authorization"] = "Bearer " .. token, -} - -local resp = {} - -local ok, statusCode, headers, statusText = https.request{ - url = url, - headers = headers, - method = method, - sink = ltn12.sink.table(resp) -} - -ngx.log(ngx.ERR, "REQUEST LOGS...") -ngx.log(ngx.ERR, ok) -ngx.log(ngx.ERR, statusCode) -ngx.log(ngx.ERR, statusText) - -if action == "list" then - local i = 1 - pods["items"] = {} - for k,v in ipairs(resp) do - decoded = json.decode(v) - if decoded["kind"] == "PodList" then - for k2,v2 in ipairs(decoded["items"]) do - if v2["status"]["phase"] == "Running" and v2["metadata"]["labels"]["approle"] ~= "chaosnode" then - ngx.log(ngx.ERR, "found pod " .. v2["metadata"]["name"]) - pods["items"][i] = v2["metadata"]["name"] - i = i + 1 - pods_not_found = false; - end - end - end - end - - if pods_not_found then - ngx.log(ngx.ERR, "No pods found into the namespace " .. namespace) - ngx.say("{\"items\": []}") - else - ngx.say(json.encode(pods)) - end - -elseif action == "delete" then - ngx.say(table.concat(resp)) -end