diff --git a/assets/box.png b/assets/box.png index 3218b1f..23922d0 100644 Binary files a/assets/box.png and b/assets/box.png differ diff --git a/assets/box_bck.png b/assets/box_bck.png new file mode 100644 index 0000000..3218b1f Binary files /dev/null and b/assets/box_bck.png differ diff --git a/assets/k8s_node.atlas b/assets/k8s_node.atlas new file mode 100644 index 0000000..fe4a01f --- /dev/null +++ b/assets/k8s_node.atlas @@ -0,0 +1,34 @@ +images { + image: "/assets/k8s_node.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF +} +animations { + id: "led" + images { + image: "/assets/k8s_node1.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF + } + images { + image: "/assets/k8s_node2.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF + } + images { + image: "/assets/k8s_node3.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF + } + images { + image: "/assets/k8s_node4.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF + } + images { + image: "/assets/k8s_node5.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF + } + playback: PLAYBACK_LOOP_FORWARD + fps: 2 + flip_horizontal: 0 + flip_vertical: 0 +} +margin: 0 +extrude_borders: 2 +inner_padding: 0 diff --git a/assets/k8s_node.png b/assets/k8s_node.png new file mode 100644 index 0000000..0739c4a Binary files /dev/null and b/assets/k8s_node.png differ diff --git a/assets/k8s_node1.png b/assets/k8s_node1.png new file mode 100644 index 0000000..3cbd85f Binary files /dev/null and b/assets/k8s_node1.png differ diff --git a/assets/k8s_node2.png b/assets/k8s_node2.png new file mode 100644 index 0000000..455e59c Binary files /dev/null and b/assets/k8s_node2.png differ diff --git a/assets/k8s_node3.png b/assets/k8s_node3.png new file mode 100644 index 0000000..c85e4a7 Binary files /dev/null and b/assets/k8s_node3.png differ diff --git a/assets/k8s_node4.png b/assets/k8s_node4.png new file mode 100644 index 0000000..c547ae3 Binary files /dev/null and b/assets/k8s_node4.png differ diff --git a/assets/k8s_node5.png b/assets/k8s_node5.png new file mode 100644 index 0000000..0b4da48 Binary files /dev/null and b/assets/k8s_node5.png differ diff --git a/input/game.input_binding b/input/game.input_binding index 1f62a91..c95c23c 100644 --- a/input/game.input_binding +++ b/input/game.input_binding @@ -54,6 +54,10 @@ key_trigger { input: KEY_K action: "kubelinter" } +key_trigger { + input: KEY_W + action: "worker" +} mouse_trigger { input: MOUSE_BUTTON_1 action: "click" diff --git a/main/gui.gui b/main/gui.gui index 8c42d29..bf1936e 100644 --- a/main/gui.gui +++ b/main/gui.gui @@ -311,7 +311,7 @@ nodes { type: TYPE_TEXT blend_mode: BLEND_MODE_ALPHA text: "" - font: "kubefont" + font: "arcade" id: "pod_name" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE @@ -592,68 +592,6 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } -nodes { - position { - x: 0.0 - y: 0.0 - z: 0.0 - w: 1.0 - } - rotation { - x: 0.0 - y: 0.0 - z: 0.0 - w: 1.0 - } - scale { - x: 1.0 - y: 1.0 - z: 1.0 - w: 1.0 - } - size { - x: 1200.0 - y: 1000.0 - z: 0.0 - w: 1.0 - } - color { - x: 0.0 - y: 1.0 - z: 0.0 - w: 1.0 - } - type: TYPE_TEXT - blend_mode: BLEND_MODE_ALPHA - text: "" - font: "arcade" - id: "kubelinter" - xanchor: XANCHOR_NONE - yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER - outline { - x: 1.0 - y: 1.0 - z: 1.0 - w: 1.0 - } - shadow { - x: 1.0 - y: 1.0 - z: 1.0 - w: 1.0 - } - adjust_mode: ADJUST_MODE_FIT - line_break: true - layer: "" - inherit_alpha: true - alpha: 1.0 - outline_alpha: 1.0 - shadow_alpha: 1.0 - template_node_child: false - text_leading: 1.0 - text_tracking: 0.0 -} nodes { position { x: -893.0 @@ -708,6 +646,69 @@ nodes { template_node_child: false size_mode: SIZE_MODE_AUTO } +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 1200.0 + y: 1000.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.0 + y: 1.0 + z: 0.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "" + font: "arcade" + id: "kubelinter" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + parent: "kubelinter_box" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} nodes { position { x: -893.0 @@ -887,6 +888,68 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } +nodes { + position { + x: -330.0 + y: 78.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 2.0 + y: 2.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.0 + y: 1.0 + z: 0.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "" + font: "arcade" + id: "node_name" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} material: "/builtins/materials/gui.material" adjust_reference: ADJUST_REFERENCE_PARENT max_nodes: 512 diff --git a/main/k8s_node.go b/main/k8s_node.go new file mode 100644 index 0000000..0cb10e0 --- /dev/null +++ b/main/k8s_node.go @@ -0,0 +1,83 @@ +components { + id: "k8s_node1" + component: "/main/k8s_node.script" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } +} +embedded_components { + id: "k8s_node" + type: "sprite" + data: "tile_set: \"/assets/k8s_node.atlas\"\n" + "default_animation: \"led\"\n" + "material: \"/builtins/materials/sprite.material\"\n" + "blend_mode: BLEND_MODE_ALPHA\n" + "" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } +} +embedded_components { + id: "collisionobject" + type: "collisionobject" + data: "collision_shape: \"\"\n" + "type: COLLISION_OBJECT_TYPE_KINEMATIC\n" + "mass: 0.0\n" + "friction: 0.1\n" + "restitution: 0.5\n" + "group: \"k8s_node\"\n" + "mask: \"bullet\"\n" + "mask: \"spaceship\"\n" + "embedded_collision_shape {\n" + " shapes {\n" + " shape_type: TYPE_BOX\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + " index: 0\n" + " count: 3\n" + " }\n" + " data: 25.0\n" + " data: 40.0\n" + " data: 10.0\n" + "}\n" + "linear_damping: 0.0\n" + "angular_damping: 0.0\n" + "locked_rotation: false\n" + "" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } +} diff --git a/main/k8s_node.script b/main/k8s_node.script new file mode 100644 index 0000000..b47b572 --- /dev/null +++ b/main/k8s_node.script @@ -0,0 +1,41 @@ +local COLLISION_RESPONSE = hash("collision_response") + +function init(self) + -- Add initialization code here + -- Learn more: https://defold.com/manuals/script/ + -- Remove this function if not needed +end + +function final(self) + -- Add finalization code here + -- Learn more: https://defold.com/manuals/script/ + -- Remove this function if not needed +end + +function update(self, dt) + -- Add update code here + -- Learn more: https://defold.com/manuals/script/ + -- Remove this function if not needed +end + +function on_message(self, message_id, message, sender) + -- TO DO +end + +function on_input(self, action_id, action) + -- Add input-handling code here. The game object this script is attached to + -- must have acquired input focus: + -- + -- msg.post(".", "acquire_input_focus") + -- + -- All mapped input bindings will be received. Mouse and touch input will + -- be received regardless of where on the screen it happened. + -- Learn more: https://defold.com/manuals/input/ + -- Remove this function if not needed +end + +function on_reload(self) + -- Add reload-handling code here + -- Learn more: https://defold.com/manuals/hot-reload/ + -- Remove this function if not needed +end diff --git a/main/kubeinvaders.collection b/main/kubeinvaders.collection index 9a0aaae..0aed489 100644 --- a/main/kubeinvaders.collection +++ b/main/kubeinvaders.collection @@ -48,6 +48,7 @@ embedded_instances { "group: \\\"spaceship\\\"\\n" "mask: \\\"pod\\\"\\n" "mask: \\\"bullet\\\"\\n" + "mask: \\\"k8s_node\\\"\\n" "embedded_collision_shape {\\n" " shapes {\\n" " shape_type: TYPE_SPHERE\\n" @@ -65,7 +66,7 @@ embedded_instances { " index: 0\\n" " count: 1\\n" " }\\n" - " data: 50.0\\n" + " data: 40.0\\n" "}\\n" "linear_damping: 0.0\\n" "angular_damping: 0.0\\n" @@ -628,3 +629,88 @@ embedded_instances { z: 1.0 } } +embedded_instances { + id: "k8s_node" + data: "embedded_components {\n" + " id: \"k8s_node_factory\"\n" + " type: \"factory\"\n" + " data: \"prototype: \\\"/main/k8s_node.go\\\"\\n" + "load_dynamically: false\\n" + "\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "embedded_components {\n" + " id: \"collisionobject\"\n" + " type: \"collisionobject\"\n" + " data: \"collision_shape: \\\"\\\"\\n" + "type: COLLISION_OBJECT_TYPE_DYNAMIC\\n" + "mass: 1.0\\n" + "friction: 0.1\\n" + "restitution: 0.5\\n" + "group: \\\"k8s_node\\\"\\n" + "mask: \\\"spaceship\\\"\\n" + "embedded_collision_shape {\\n" + " shapes {\\n" + " shape_type: TYPE_BOX\\n" + " position {\\n" + " x: 0.0\\n" + " y: 0.0\\n" + " z: 0.0\\n" + " }\\n" + " rotation {\\n" + " x: 0.0\\n" + " y: 0.0\\n" + " z: 0.0\\n" + " w: 1.0\\n" + " }\\n" + " index: 0\\n" + " count: 3\\n" + " }\\n" + " data: 10.0\\n" + " data: 10.0\\n" + " data: 10.0\\n" + "}\\n" + "linear_damping: 0.0\\n" + "angular_damping: 0.0\\n" + "locked_rotation: false\\n" + "\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale3 { + x: 1.0 + y: 1.0 + z: 1.0 + } +} diff --git a/main/kubeinvaders.gui_script b/main/kubeinvaders.gui_script index 566e0d8..e4225c6 100644 --- a/main/kubeinvaders.gui_script +++ b/main/kubeinvaders.gui_script @@ -22,6 +22,29 @@ function on_message(self, message_id, message, sender) elseif message_id == hash("info") then gui.set_text(gui.get_node("info"), message.infomessage) + elseif message_id == hash("node_name") then + spaceship_x = message.spaceship_pos.x + + if spaceship_x <= 80 then + spaceship_x = spaceship_x + 100 + end + + spaceship_y = message.spaceship_pos.y + node_name_pos = vmath.vector3(spaceship_x, spaceship_y - 50, 0) + gui.set_text(gui.get_node("node_name"), message.node_name) + gui.set_position(gui.get_node("node_name"), node_name_pos) + k8s_nodes_show = true + + elseif message_id == hash("reset_node_name") and k8s_nodes_show then + node_name_pos = gui.get_position(gui.get_node("node_name")) + print("Reset node name text box") + if node_name_pos.x > 0 then + gui.set_text(gui.get_node("node_name"), '') + node_name_pos = vmath.vector3(-501, -501, 0) + print("Reset node_name text node") + k8s_nodes_show = false + end + elseif message_id == hash("pod_log") then --gui.set_text(gui.get_node("pod_log"), message.infomessage) if message.infomessage and message.infomessage ~= '' and not kubelinter_read then @@ -43,7 +66,7 @@ function on_message(self, message_id, message, sender) gui.set_text(gui.get_node("mode"), message.mode) elseif message_id == hash("open_kubelinter_box") then - gui.set_position(gui.get_node("kubelinter"), message.position) + --gui.set_position(gui.get_node("kubelinter"), message.position) gui.set_position(gui.get_node("kubelinter_box"), vmath.vector3(893, 539, 0)) elseif message_id == hash("close_kubelinter_box") then @@ -57,7 +80,7 @@ function on_message(self, message_id, message, sender) elseif message_id == hash("quit_help") then gui.set_position(gui.get_node("inputs"), message.position) - + elseif message_id == hash("pod_name") then if message.pod_name == false or message.pod_name == nil then fake_position = message.position diff --git a/main/mymodules/help.lua b/main/mymodules/help.lua index c444fa5..5de8998 100644 --- a/main/mymodules/help.lua +++ b/main/mymodules/help.lua @@ -1,11 +1,11 @@ local M = {} function M.get_help() - msg.post("ui#gui","help",{ position = vmath.vector3(5, 300, 0) }) + msg.post("ui#gui","help",{ position = vmath.vector3(5, 300, 0) }) end function M.quit_help() - msg.post("ui#gui","quit_help",{ position = vmath.vector3(-1000, -1000, 0) }) + msg.post("ui#gui","quit_help",{ position = vmath.vector3(-1000, -1000, 0) }) end return M \ No newline at end of file diff --git a/main/mymodules/node.lua b/main/mymodules/node.lua new file mode 100644 index 0000000..34dce5e --- /dev/null +++ b/main/mymodules/node.lua @@ -0,0 +1,47 @@ +local M = {} + +function M.node_metrics(pod_name) + http.request(endpoint .. "/apis/metrics.k8s.io/v1beta1/nodes", "GET", M.http_node_metrics_result, headers) +end + +function M.http_node_metrics_result(self, _, response) + print(response.response) +end + +function M.get_nodes() + http.request(endpoint .. "/api/v1/nodes", "GET", M.http_get_nodes_result, headers) +end + +function M.http_get_nodes_result(self, _, response) + kubernetes_nodes = {} + print("Get nodes output") + print('/api/v1/nodes response: ' .. response.status) + nodes = json.decode(response.response) + node_items = nodes["items"] + node_items_size = table.getn(node_items) + print("Node table size: " .. node_items_size) + pos_x = 80 + pos_y = 500 + nodes_cnt = 1 + for k, v in pairs(node_items) do + for k1, v2 in pairs(v) do + if v2["name"] then + print("Found k8s node: " .. v2["name"]) + pos = vmath.vector3(pos_x, pos_y, 0) + new_node = factory.create("/k8s_node#k8s_node_factory", pos) + table.insert(kubernetes_nodes,{ name = v2["name"], id = new_node}) + + if ((nodes_cnt % 15 ) == 0) then + pos_y = pos_y + 100 + pos_x = 80 + else + pos_x = pos_x + 80 + end + + nodes_cnt = nodes_cnt + 1 + end + end + end +end + +return M \ No newline at end of file diff --git a/main/mymodules/node_api.lua b/main/mymodules/node_api.lua deleted file mode 100644 index 086dec6..0000000 --- a/main/mymodules/node_api.lua +++ /dev/null @@ -1,11 +0,0 @@ -local M = {} - -function M.node_metrics(pod_name) - http.request(endpoint .. "/apis/metrics.k8s.io/v1beta1/nodes", "GET", M.http_node_metrics_result, headers) -end - -function M.http_node_metrics_result(self, _, response) - print(response.response) -end - -return M \ No newline at end of file diff --git a/main/mymodules/pod.lua b/main/mymodules/pod.lua index 6ce2f16..62034a5 100644 --- a/main/mymodules/pod.lua +++ b/main/mymodules/pod.lua @@ -65,8 +65,8 @@ function M.swap_pod(items) end end - print ("[swap_pod]: pods not running pods in Kubernetes: " .. kubernetes_pod_not_running) - print ("[swap_pod]: pods running pods in Kubernetes: " .. kubernetes_pod_running) + -- print ("[swap_pod]: pods not running pods in Kubernetes: " .. kubernetes_pod_not_running) + -- print ("[swap_pod]: pods running pods in Kubernetes: " .. kubernetes_pod_running) msg.post("ui#gui", "hello_gui",{ pod_running = kubernetes_pod_running }) for i,value in ipairs(current_pods) do @@ -77,8 +77,8 @@ function M.swap_pod(items) end end - print ("[swap_pod]: pods not running pods in KubeInvaders: " .. kubeinvaders_pod_not_running) - print ("[swap_pod]: pods running pods in KubeInvaders: " .. kubeinvaders_pod_running) + -- print ("[swap_pod]: pods not running pods in KubeInvaders: " .. kubeinvaders_pod_not_running) + -- print ("[swap_pod]: pods running pods in KubeInvaders: " .. kubeinvaders_pod_running) if kubernetes_pod_running < kubeinvaders_pod_running then print ("[swap_pod] there are less pod running in kubernetes than kubeinvaders") diff --git a/main/spaceship.script b/main/spaceship.script index 9bbbdef..f46d819 100644 --- a/main/spaceship.script +++ b/main/spaceship.script @@ -80,6 +80,13 @@ kubelinter_read = false -- Keetp track of the line showed of the kubelinter analysis content kubelinter_index = 0 +-- by default the k8s nodes are not shown. Set this var to true +k8s_nodes_show = false + +-- global table that contains k8s nodes +kubernetes_nodes = {} + + local COLLISION_RESPONSE = hash("collision_response") local timer = require "main.mymodules.timer" local spaceship = require "main.mymodules.spaceship" @@ -88,6 +95,7 @@ local pod_mod = require "main.mymodules.pod" local pod_api = require "main.mymodules.pod_api" local press_play = require "main.mymodules.press_play" local kubelinter = require "main.mymodules.kubelinter" +local k8s_node = require "main.mymodules.node" function start_chaos() msg.post("ui#gui", "mode",{ mode = "Press \"a\" to use automatic pilot" }) @@ -151,8 +159,9 @@ function start_chaos() pod_api.update_pod() pod_mod.swap_pod(kubernetes_pods) end) - timer.repeat_seconds(1, function() - msg.post("ui#gui","pod_name",{ pod_name = false, position = go.get_position()}) + + timer.repeat_seconds(1, function() + msg.post("ui#gui", "pod_name", { pod_name = false, position = go.get_position()}) if kubelinter_pod then kubelinter.run(pod_name) kubelinter_pod = false @@ -190,6 +199,9 @@ function update(self, dt) end function on_input(self, action_id, action) + + msg.post("ui#gui", "reset_node_name", {}) + p = go.get_position() if action_id == hash("up") then if p.y < 1000 then @@ -222,7 +234,6 @@ function on_input(self, action_id, action) last_pod_log = false elseif action_id == hash("help") then - print "help!" help.get_help() elseif action_id == hash("quit_help") then @@ -237,8 +248,12 @@ function on_input(self, action_id, action) automatic = false msg.post("ui#gui", "mode",{ mode = "Press \"a\" to use automatic pilot"}) + elseif action_id == hash("worker") then + print("Show kubernetes nodes...") + k8s_node.get_nodes() + elseif action_id == hash("namespace") then - print ("[change_namespace] change namespaces requested by user") + print("[change_namespace] change namespaces requested by user") msg.post("ui#gui", "error",{ errormessage = "changing namespace!" }) -- Returns an approximation of the amount in seconds of CPU time used by the program. @@ -246,7 +261,7 @@ function on_input(self, action_id, action) if last_namespace_change ~= false then diff_time = current_time - last_namespace_change - print ("[change_namespace] diff_time ".. diff_time) + print("[change_namespace] diff_time ".. diff_time) else diff_time = 99 end @@ -335,7 +350,18 @@ end function on_message(self, message_id, message, sender) current_time = os.clock() - print('DEBUG' .. message_id) + + if message_id == COLLISION_RESPONSE and not k8s_nodes_show then + for i,value in ipairs(kubernetes_nodes) do + if message["other_id"] == value["id"] then + print("Spaceship collides with " .. value["name"]) + spaceship_pos = go.get_position() + msg.post("ui#gui","node_name", { node_name = value["name"], spaceship_pos = spaceship_pos }) + end + end + return + end + if message_id == COLLISION_RESPONSE and started_chaos then msg.post("ui#gui","pod_log") @@ -358,6 +384,7 @@ function on_message(self, message_id, message, sender) print("pod_name " .. pod_name) last_pod_log_time = os.clock() end + elseif message_id == hash('start_chaos') then start_chaos() end