mirror of
https://github.com/lucky-sideburn/kubeinvaders.git
synced 2026-05-18 22:56:40 +00:00
update doc
This commit is contained in:
@@ -332,27 +332,45 @@ server {
|
||||
}
|
||||
|
||||
location /metrics {
|
||||
default_type text/html;
|
||||
default_type text/plain;
|
||||
content_by_lua_block {
|
||||
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'
|
||||
|
||||
local redis = require "resty.redis"
|
||||
local red = redis:new()
|
||||
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
|
||||
|
||||
for i, res in ipairs(red:keys("*total*")) do
|
||||
if string.find(res, "chaos_node_jobs_total_on") then
|
||||
local node = string.gsub(res, "chaos_node_jobs_total_on_", "")
|
||||
local metric = "chaos_jobs_node_count{node=\"".. node .."\"}"
|
||||
ngx.say(metric .. " " .. red:get(res))
|
||||
if not okredis then
|
||||
ngx.log(ngx.ERR, "[metrics] Redis connection failed: " .. tostring(errredis))
|
||||
ngx.status = 500
|
||||
ngx.say("Redis connection failed")
|
||||
return
|
||||
end
|
||||
|
||||
elseif string.find(res, "deleted_pods_total_on") then
|
||||
local namespace = string.gsub(res, "deleted_pods_total_on_", "")
|
||||
local metric = "deleted_namespace_pods_count{namespace=\"".. namespace .."\"}"
|
||||
ngx.say(metric .. " " .. red:get(res))
|
||||
local total_keys = red:keys("*total*")
|
||||
if total_keys and type(total_keys) == "table" then
|
||||
for i, res in ipairs(total_keys) do
|
||||
if string.find(res, "chaos_node_jobs_total_on") then
|
||||
local node = string.gsub(res, "chaos_node_jobs_total_on_", "")
|
||||
local metric = "chaos_jobs_node_count{node=\"".. node .."\"}"
|
||||
ngx.say(metric .. " " .. red:get(res))
|
||||
|
||||
elseif string.find(res, "deleted_pods_total_on") then
|
||||
local namespace = string.gsub(res, "deleted_pods_total_on_", "")
|
||||
local metric = "deleted_namespace_pods_count{namespace=\"".. namespace .."\"}"
|
||||
ngx.say(metric .. " " .. red:get(res))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for i, res in ipairs(red:keys("pods_match_regex:*")) do
|
||||
ngx.say(res .. " " .. red:get(res))
|
||||
local regex_keys = red:keys("pods_match_regex:*")
|
||||
if regex_keys and type(regex_keys) == "table" then
|
||||
for i, res in ipairs(regex_keys) do
|
||||
ngx.say(res .. " " .. red:get(res))
|
||||
end
|
||||
end
|
||||
|
||||
local metrics = {
|
||||
@@ -377,20 +395,39 @@ server {
|
||||
end
|
||||
end
|
||||
|
||||
for i, res in ipairs(red:keys("chaos_jobs_status*")) do
|
||||
ngx.say(res .. " " .. red:get(res))
|
||||
local status_keys = red:keys("chaos_jobs_status*")
|
||||
if status_keys and type(status_keys) == "table" then
|
||||
for i, res in ipairs(status_keys) do
|
||||
ngx.say(res .. " " .. red:get(res))
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
location /chaos_jobs_pod_phase {
|
||||
default_type text/html;
|
||||
default_type text/plain;
|
||||
content_by_lua_block {
|
||||
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'
|
||||
|
||||
local redis = require "resty.redis"
|
||||
local red = redis:new()
|
||||
local okredis, errredis = red:connect("unix:/tmp/redis.sock")
|
||||
for i, res in ipairs(red:keys("chaos_jobs_pod_phase*")) do
|
||||
ngx.say(res .. " " .. red:get(res))
|
||||
|
||||
if not okredis then
|
||||
ngx.log(ngx.ERR, "[chaos_jobs_pod_phase] Redis connection failed: " .. tostring(errredis))
|
||||
ngx.status = 500
|
||||
ngx.say("Redis connection failed")
|
||||
return
|
||||
end
|
||||
|
||||
local phase_keys = red:keys("chaos_jobs_pod_phase*")
|
||||
if phase_keys and type(phase_keys) == "table" then
|
||||
for i, res in ipairs(phase_keys) do
|
||||
ngx.say(res .. " " .. red:get(res))
|
||||
end
|
||||
end
|
||||
}
|
||||
}
|
||||
@@ -740,10 +777,6 @@ server {
|
||||
|
||||
local lyaml = require "lyaml"
|
||||
local json = require 'lunajson'
|
||||
math.randomseed(os.clock()*100000000000)
|
||||
local rand = math.random(999, 9999)
|
||||
local file_name = "/tmp/chaosprogram" .. rand
|
||||
|
||||
|
||||
ngx.header['Access-Control-Allow-Origin'] = '*'
|
||||
ngx.header['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
|
||||
@@ -751,29 +784,8 @@ 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] Chaos program payload sent from client: " .. data)
|
||||
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Write temp file: " .. file_name)
|
||||
|
||||
local yamlfile = io.open(file_name, "w")
|
||||
yamlfile:write(data)
|
||||
yamlfile:close()
|
||||
|
||||
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Checking if " .. file_name .. " is a valid YAML file")
|
||||
local handle = io.popen("python3 -c 'import yaml, sys; print(yaml.safe_load(sys.stdin))' < " .. file_name .. " ; echo $? > " .. file_name .. ".check")
|
||||
local result = handle:read("*a")
|
||||
|
||||
handle = io.popen("cat " .. file_name .. ".check")
|
||||
result = handle:read("*a")
|
||||
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Exit code for checking YAML syntax of " .. file_name .. " is " .. result)
|
||||
|
||||
if result == "0\n" then
|
||||
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] YAML Syntax is OK")
|
||||
else
|
||||
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] YAML Syntax is NOT OK")
|
||||
handle = io.popen("rm -f " .. file_name .. ".check")
|
||||
handle:read("*a")
|
||||
ngx.status = 400
|
||||
ngx.say("Invalid YAML Chaos Program")
|
||||
if data then
|
||||
ngx.log(ngx.INFO, "[PROGRAMMING_MODE] Chaos program payload sent from client: " .. data)
|
||||
end
|
||||
|
||||
if data == nil then
|
||||
@@ -781,8 +793,12 @@ server {
|
||||
ngx.status = 400
|
||||
ngx.say(error)
|
||||
else
|
||||
|
||||
local yaml_data = lyaml.load(data)
|
||||
local parse_ok, yaml_data = pcall(lyaml.load, data)
|
||||
if not parse_ok or type(yaml_data) ~= "table" then
|
||||
ngx.status = 400
|
||||
ngx.say("Invalid YAML Chaos Program")
|
||||
return
|
||||
end
|
||||
|
||||
if not key_exists(yaml_data, "k8s_jobs") then
|
||||
error = "[PROGRAMMING_MODE] Chaos program does not contain 'jobs' key."
|
||||
@@ -805,7 +821,6 @@ server {
|
||||
ngx.say(error)
|
||||
|
||||
else
|
||||
os.remove(file_name)
|
||||
local response = json.encode(yaml_data)
|
||||
ngx.log(ngx.INFO, response)
|
||||
ngx.status = 200
|
||||
|
||||
Reference in New Issue
Block a user