Files
kubeinvaders/main/spaceship.script
T
2019-04-22 11:36:03 +02:00

354 lines
10 KiB
Plaintext

current_pods = {}
check_current_pods = false
endpoint = ""
token = ""
namespace = ""
automatic = false
local timer = require ("main.timer")
function reverse(tbl)
for i=1, math.floor(#tbl / 2) do
local tmp = tbl[i]
tbl[i] = tbl[#tbl - i + 1]
tbl[#tbl - i + 1] = tmp
end
end
function swap_pod(items)
current_pod_not_running = 0
kube_pod_not_running = 0
kube_pod_running = 0
pod_items_size = table.getn(items)
current_kubeinvaders_pod_size = table.getn(current_pods)
for i,value in ipairs(items) do
if value['status']['phase'] ~= "Running" or value['metadata']['deletionTimestamp'] ~= nil then
kube_pod_not_running = kube_pod_not_running + 1
else
kube_pod_running = kube_pod_running + 1
end
end
msg.post("ui#gui", "hello_gui",{ pod_running = kube_pod_running })
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
end
end
--print ("[swap_pod]: current pods on Kubernetes: " .. pod_items_size)
--print ("[swap_pod]: current pods on Kubeinvaders: " .. current_kubeinvaders_pod_size)
--print ("[swap_pod]: not running pods Kubernetes: " .. kube_pod_not_running)
--print ("[swap_pod]: not running pods in KubeInvaders: " .. current_pod_not_running)
if kube_pod_running < current_kubeinvaders_pod_size then
print ("[swap_pod]: there are less pod running in kubernetes")
i = current_kubeinvaders_pod_size - 1
deleted_pods = 0
print ("[swap_pod] clean red pods. Current KubveInvaders pod:" .. current_kubeinvaders_pod_size)
while ( i > 0) do
if current_pods[i] ~= nil and current_pods[i]["color"] == "red" then
go.delete(current_pods[i]["id"])
table.remove(current_pods,i)
deleted_pods = deleted_pods + 1
end
i = i - 1
end
msg.post("ui#gui", "error",{ errormessage = "Synchronizing alien ships with Kubernetes pods.." })
return
end
if kube_pod_running > current_kubeinvaders_pod_size then
for i,value in ipairs(current_pods) do
go.delete(value["id"])
end
current_pods = {}
set_pods()
end
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)
pod_running = {}
if response.status == 0 then
msg.post("ui#gui", "error",{ errormessage = "Error connecting to " .. conf['endpoint'] })
end
pods = json.decode(response.response)
pod_items_size = table.getn(pods["items"])
pod_items = pods["items"]
i = 0
while i <= pod_items_size do
if pod_items[i] ~= nil then
if pod_items[i]['status']['phase'] == "Running" and pod_items[i]['metadata']['deletionTimestamp'] == nil then
table.insert(pod_running,pod_items[i])
end
end
i = i + 1
end
swap_pod(pod_running)
end
function ai()
msg.post("ui#gui", "error",{ errormessage = "" })
if automatic == false then
return
end
for i=1,1 do
rand_pod = math.random(100, 1000)
end
pos = go.get_position()
pos.y = 220
go.set_position(pos)
pos.x = rand_pod
go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_INQUAD, 1,0, function()
pos = go.get_position()
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 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)
if string.match(response.status, '4.*') or string.match(response.status, '5.*') then
msg.post("ui#gui", "error",{ errormessage = "Status code: " .. response.status .. "\n" .. response.response })
end
end
function http_pod_delete_request_result(self, _, response)
pods = {}
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" and pod_items[a]['metadata']['deletionTimestamp'] == nil then
pod_to_delete = pod_items[a]['metadata']['name']
break
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)
if string.match(response.status, '4.*') or string.match(response.status, '5.*') then
msg.post("ui#gui", "error",{ errormessage = "Status code: " .. response.status .. "\n" .. response.response })
end
pods = json.decode(response.response)
pod_items_size = table.getn(pods["items"])
pod_items = pods["items"]
check_above = false
a = 0
distance_between_pods = 1000 / pod_items_size
y_pos = 600
space_between_factor = 0
if pod_items_size <= 15 then
space_between_factor = pod_items_size
else
space_between_factor = 15
end
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()
if a <= 15 then
pos.x = 100 + ( a * (1000 / space_between_factor) )
end
if (a % 15 == 0) then
y_pos = y_pos - (( a / 15 ) * 50)
check_above = true
end
if check_above == true then
pos_of_pod_above = go.get_position(current_pods[a - 14]['id'])
pos.x = pos_of_pod_above.x
end
pos.y = y_pos
if phase == "Running" and this_pod['metadata']['deletionTimestamp'] == nil 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)
msg.post("ui#gui", "mode",{ mode = "You are using the manual mode. Press \"a\" to use switch to automatic"})
conf = {}
local path = os.getenv("HOME") .. "/.KubeInv.json"
local contents = ""
conf = {}
local file = io.open( path, "r" )
if file then
local contents = file:read( "*a" )
conf = json.decode(contents);
io.close(file)
else
conf["token"] = os.getenv("TOKEN")
conf["endpoint"] = os.getenv("ENDPOINT")
conf["namespace"] = os.getenv("NAMESPACE")
end
if conf["token"] == nil or conf["endpoint"] == nil or conf["namespace"] == nil then
msg.post("ui#gui", "howto",{ howto_message = "Please create .KubeInv.json in " .. os.getenv("HOME") .. "\nThe file .KubeInv.json should be like this \n{\"endpoint\": \"http://ocmaster39\",\"token\": \"xxxx\",\"namespace\": \"foobar\"}"})
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"] .. "\n\n" .. "Namespace: " .. conf["namespace"]
msg.post("ui#gui", "info",{ infomessage = info })
msg.post(".", "acquire_input_focus")
headers = {
["Accept"] = "application/json",
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. token
}
timer.repeat_seconds(0.3, function()
update_pod()
end)
timer.repeat_seconds(2, function()
ai()
end)
set_pods()
end
end
function update(self, dt)
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("automatic") then
automatic = true
msg.post("ui#gui", "mode",{ mode = "You are using the automatic mode. Press \"m\" to use switch to manual"})
elseif action_id == hash("manual") then
automatic = false
msg.post("ui#gui", "mode",{ mode = "You are using the manual mode. Press \"a\" to use switch to automatic"})
elseif action_id == hash("space") and action.pressed then
msg.post("ui#gui", "error",{ errormessage = "" })
if automatic == false 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
end
function on_message(self, message_id, message, sender)
end