mirror of
https://github.com/lucky-sideburn/kubeinvaders.git
synced 2026-05-10 18:56:40 +00:00
266 lines
7.6 KiB
Plaintext
266 lines
7.6 KiB
Plaintext
current_pods = {}
|
|
check_current_pods = false
|
|
endpoint = ""
|
|
token = ""
|
|
namespace = ""
|
|
local timer = require ("main.timer")
|
|
|
|
function swap_pod(items)
|
|
current_pod_not_running = 0
|
|
kube_pod_not_running = 0
|
|
kube_pod_running = 0
|
|
pod_items_size = table.getn(items)
|
|
|
|
for i,value in ipairs(current_pods) do
|
|
if value["collision"] ~= nil and value["color"] == "red" then
|
|
print("[swap_pod] creating " .. value["id"] .. " in red. Pos: " .. " " .. value["old_position"])
|
|
current_pods[i]["id"] = factory.create("/pod_not_running#podfactory", value["old_position"])
|
|
current_pods[i]["color"] = "red"
|
|
current_pods[i]["collision"] = nil
|
|
delete_request_pod()
|
|
return
|
|
end
|
|
end
|
|
|
|
for i,value in ipairs(current_pods) do
|
|
if value["id"] ~= "reset" and value["color"] == "red" then
|
|
current_pod_not_running = current_pod_not_running + 1
|
|
--print ("[swap_pod]:" .. value["id"] .. " is not running in KubeInvaders")
|
|
end
|
|
end
|
|
for i,value in ipairs(items) do
|
|
if value['status']['phase'] ~= "Running" then
|
|
kube_pod_not_running = kube_pod_not_running + 1
|
|
--print ("[swap_pod]:" .. value['metadata']['name'] .. " is not running on Kubernetes cluster")
|
|
else
|
|
kube_pod_running = kube_pod_running + 1
|
|
end
|
|
end
|
|
print ("[swap_pod]: current pods on Kubernetes: " .. pod_items_size)
|
|
print ("[swap_pod]: not running pods Kubernetes: " .. kube_pod_not_running)
|
|
print ("[swap_pod]: not running pods in KubeInvaders: " .. current_pod_not_running)
|
|
|
|
msg.post("ui#gui", "hello_gui",{ pod_running = kube_pod_running })
|
|
|
|
if (current_pod_not_running > 0) and (kube_pod_not_running > current_pod_not_running )then
|
|
diff = kube_pod_not_running - current_pod_not_running
|
|
a = 0
|
|
while ( a < diff ) do
|
|
for i,value in ipairs(current_pods) do
|
|
if value["color"] == "white" then
|
|
go.delete(value["id"])
|
|
local pod = factory.create("/pod_not_running#podfactory", go.get_position(value["id"]))
|
|
current_pods[i] = { id = pod , color = "red" }
|
|
end
|
|
end
|
|
a = a + 1
|
|
end
|
|
|
|
elseif current_pod_not_running > kube_pod_not_running then
|
|
diff = current_pod_not_running - kube_pod_not_running
|
|
a = 0
|
|
while ( a < diff ) do
|
|
for i,value in ipairs(current_pods) do
|
|
if value["color"] == "red" then
|
|
pos = go.get_position()
|
|
go.delete(value["id"])
|
|
local pod = factory.create("/pod#podfactory", go.get_position(value["id"]))
|
|
current_pods[i] = { id = pod , color = "white" }
|
|
end
|
|
end
|
|
a = a + 1
|
|
end
|
|
end
|
|
end
|
|
|
|
function http_update_pod_result(self, _, response)
|
|
pods = json.decode(response.response)
|
|
pod_items_size = table.getn(pods["items"])
|
|
pod_items = pods["items"]
|
|
swap_pod(pod_items)
|
|
end
|
|
|
|
function update_pod()
|
|
http.request(endpoint .. "/api/v1/namespaces/" .. namespace .. "/pods", "GET", http_update_pod_result,headers)
|
|
end
|
|
|
|
function delete_pod(pod_name)
|
|
print "[delete_pod] delete pods of Kubernetes"
|
|
http.request(endpoint .. "/api/v1/namespaces/" .. namespace .. "/pods/" .. pod_name, "DELETE", http_pod_delete_result,headers)
|
|
end
|
|
|
|
function delete_request_pod()
|
|
http.request(endpoint .. "/api/v1/namespaces/".. namespace .. "/pods", "GET", http_pod_delete_request_result,headers)
|
|
end
|
|
|
|
function http_pod_delete_result(self, _, response)
|
|
print "[http_pod_delete_result] Request for delete pod on kubernetes has been done"
|
|
--print(response.status)
|
|
--print(response.response)
|
|
--print(response.headers)
|
|
end
|
|
|
|
function http_pod_delete_request_result(self, _, response)
|
|
pods = {}
|
|
--print(response.status)
|
|
--print(response.response)
|
|
--print(response.headers)
|
|
pods = json.decode(response.response)
|
|
pod_items_size = table.getn(pods["items"])
|
|
a = 0
|
|
while a <= pod_items_size do
|
|
if pod_items[a] ~= nil then
|
|
if pod_items[a]['status']['phase'] == "Running" then
|
|
pod_to_delete = pod_items[a]['metadata']['name']
|
|
end
|
|
end
|
|
a = a + 1
|
|
end
|
|
delete_pod(pod_to_delete)
|
|
end
|
|
|
|
function http_pod_result(self, _, response)
|
|
--print(response.status)
|
|
--print(response.response)
|
|
--print(response.headers)
|
|
pods = json.decode(response.response)
|
|
pod_items_size = table.getn(pods["items"])
|
|
pod_items = pods["items"]
|
|
a = 0
|
|
distance_between_pods = 1000 / pod_items_size
|
|
|
|
while( a <= pod_items_size )
|
|
do
|
|
if pod_items[a] ~= nil then
|
|
this_pod = pod_items[a]
|
|
phase = this_pod["status"]["phase"]
|
|
local pos = go.get_position()
|
|
pos.x = 100 + ( a * (1000 / pod_items_size) )
|
|
pos.y = 600
|
|
if phase == "Running" then
|
|
local pod = factory.create("/pod#podfactory", pos)
|
|
table.insert(current_pods, { id = pod , color = "white" })
|
|
check_current_pods = true
|
|
else
|
|
local pod = factory.create("/pod_not_running#podfactory", pos)
|
|
table.insert(current_pods, { id = pod , color = "red" })
|
|
check_current_pods = true
|
|
end
|
|
end
|
|
a = a+1
|
|
end
|
|
end
|
|
|
|
function set_pods()
|
|
http.request(endpoint .. "/api/v1/namespaces/".. namespace .. "/pods", "GET", http_pod_result,headers)
|
|
end
|
|
|
|
function init(self)
|
|
|
|
conf = {}
|
|
|
|
fp = io.open( ".KubeInv.conf", "r" )
|
|
if fp ~= nil then
|
|
for line in fp:lines() do
|
|
line = line:match( "%s*(.+)" )
|
|
if line and line:sub( 1, 1 ) ~= "#" and line:sub( 1, 1 ) ~= ";" then
|
|
option = line:match( "%S+" ):lower()
|
|
value = line:match( "%S*%s*(.*)" )
|
|
|
|
if not value then
|
|
conf[option] = true
|
|
else
|
|
if not value:find( "," ) then
|
|
conf[option] = value
|
|
else
|
|
value = value .. ","
|
|
conf[option] = {}
|
|
for entry in value:gmatch( "%s*(.-)," ) do
|
|
conf[option][#conf[option]+1] = entry
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|
|
fp:close()
|
|
end
|
|
|
|
local handle = io.popen('pwd')
|
|
local result = handle:read("*a")
|
|
handle:close()
|
|
|
|
if conf["token"] == nil or conf["endpoint"] == nil or conf["namespace"] == nil then
|
|
msg.post("ui#gui", "howto",{ howto_message = "Please create .KubeInv.conf in " .. result .. "Example of KubeInv.conf:\n\nnamespace foobar\ntoken eyJhbGciOiJ43i423i94239i49\nendpoint https://ocmaster39:8443\n"})
|
|
else
|
|
print( "token: ", conf["token"] )
|
|
print( "endpoint: ", conf["endpoint"] )
|
|
print( "namespace: ", conf["namespace"] )
|
|
|
|
endpoint = conf["endpoint"]
|
|
namespace = conf["namespace"]
|
|
token = conf["token"]
|
|
|
|
info = "Endpoint: " .. conf["endpoint"] .. " " .. "Namespace: " .. conf["namespace"]
|
|
msg.post("ui#gui", "info",{ infomessage = info })
|
|
|
|
msg.post(".", "acquire_input_focus")
|
|
headers = {
|
|
["Authorization"] = "Bearer " .. token ,
|
|
["Accept"] = "application/json",
|
|
["Content-Type"] = "application/json"
|
|
}
|
|
timer.repeat_seconds(0.5, function()
|
|
update_pod()
|
|
end)
|
|
set_pods()
|
|
end
|
|
end
|
|
|
|
function update(self, dt)
|
|
--if rawequal(next(current_pods), nil) and check_current_pods then
|
|
-- for i,value in ipairs(current_pods) do
|
|
-- print ("delete " .. value["id"] )
|
|
-- if value["id"] ~= "reset" then
|
|
-- go.delete(id["id"])
|
|
-- end
|
|
-- end
|
|
--set_pods()
|
|
--current_pods = { id = "reset"}
|
|
--end
|
|
timer.update(dt)
|
|
end
|
|
|
|
function on_input(self, action_id, action)
|
|
print "start on_input"
|
|
if action_id == hash("up") then
|
|
local p = go.get_position()
|
|
p.y = p.y + 4
|
|
go.set_position(p)
|
|
elseif action_id == hash("down") then
|
|
local p = go.get_position()
|
|
p.y = p.y - 4
|
|
go.set_position(p)
|
|
elseif action_id == hash("left") then
|
|
local p = go.get_position()
|
|
p.x = p.x - 4
|
|
go.set_position(p)
|
|
elseif action_id == hash("right") then
|
|
local p = go.get_position()
|
|
p.x = p.x + 4
|
|
go.set_position(p)
|
|
elseif action_id == hash("click") and action.pressed then
|
|
print(action_id)
|
|
elseif action_id == hash("space") and action.pressed then
|
|
local pos = go.get_position()
|
|
local to = pos.y + 2 * 64
|
|
local bullet = factory.create("/bullet#bulletfactory", pos)
|
|
go.animate(bullet, "position.y", go.PLAYBACK_ONCE_FORWARD, 700, go.EASING_INQUAD, 1,0,function()
|
|
go.delete(bullet)
|
|
end)
|
|
end
|
|
end
|
|
|
|
function on_message(self, message_id, message, sender)
|
|
|
|
end |