mirror of
https://github.com/lucky-sideburn/kubeinvaders.git
synced 2026-08-23 21:46:21 +00:00
improves automatic pilot
This commit is contained in:
+3
-2
@@ -8,7 +8,7 @@ RUN mv ./kubectl /usr/local/bin/kubectl
|
||||
|
||||
# Install Openresty
|
||||
RUN apt-get update
|
||||
RUN apt-get -y install --no-install-recommends wget gnupg ca-certificates jq openssl
|
||||
RUN apt-get -y install --no-install-recommends wget gnupg ca-certificates jq openssl task-spooler at
|
||||
RUN wget -O - https://openresty.org/package/pubkey.gpg | apt-key add -
|
||||
RUN codename=`grep -Po 'VERSION="[0-9]+ \(\K[^)]+' /etc/os-release` && echo "deb http://openresty.org/package/debian $codename openresty" | tee /etc/apt/sources.list.d/openresty.list
|
||||
RUN apt-get update
|
||||
@@ -31,9 +31,10 @@ RUN chmod +x /opt/chaos-node.sh
|
||||
COPY ./js-web/KubeInvaders /var/www/html
|
||||
|
||||
# Configure Nginx
|
||||
RUN sed -i.bak 's/^user/#user/' /etc/nginx/nginx.conf
|
||||
#RUN sed -i.bak 's/^user/#user/' /etc/nginx/nginx.conf
|
||||
RUN sed -i.bak 's/listen\(.*\)80;/listen 8081;/' /etc/nginx/conf.d/default.conf
|
||||
|
||||
COPY nginx/nginx.conf /etc/nginx/nginx.conf
|
||||
COPY nginx/KubeInvaders.templ /etc/nginx/conf.d/KubeInvaders.templ
|
||||
COPY nginx/KubeInvaders_dev.templ /etc/nginx/conf.d/KubeInvaders_dev.templ
|
||||
|
||||
|
||||
+21
-10
@@ -6,7 +6,7 @@ NODE_NAME=$3
|
||||
NAMESPACE=$4
|
||||
RAND=$(openssl rand -hex 4)
|
||||
YAML_FILE=/tmp/kubeinvaders-chaos-node-$RAND.yaml
|
||||
|
||||
CLEANER_SCRIPT=/tmp/$RAND-cleaner.sh
|
||||
# Very temporary work-around
|
||||
KUBE=$(cat /etc/nginx/conf.d/KubeInvaders.conf | grep proxy_pass | head -n1 | awk '{ print $2 }' | sed 's/;//g')
|
||||
|
||||
@@ -38,16 +38,27 @@ spec:
|
||||
backoffLimit: 1
|
||||
EOF
|
||||
|
||||
cat <<EOF > $CLEANER_SCRIPT
|
||||
#!/bin/sh
|
||||
for i in $(kubectl get jobs -l app=kubeinvaders -l approle=chaosnode -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}' --token=${TOKEN} --server=${KUBE} -n ${NAMESPACE} --insecure-skip-tls-verify=true)
|
||||
do
|
||||
kubectl delete job \$i --token=${TOKEN} --server=${KUBE} --insecure-skip-tls-verify=true -n $NAMESPACE >> /tmp/kubectl.log 2>&1
|
||||
done
|
||||
|
||||
for i in $(kubectl get pods -l app=kubeinvaders -l approle=chaosnode --field-selector=status.phase==Succeeded --token=${TOKEN} --server=${KUBE} -n ${NAMESPACE} --insecure-skip-tls-verify=true | grep -v NAME | awk '{ print $1 }')
|
||||
do
|
||||
kubectl delete pod \$i --token=${TOKEN} --server=${KUBE} --insecure-skip-tls-verify=true -n $NAMESPACE >> /tmp/kubectl.log 2>&1
|
||||
done
|
||||
|
||||
rm -f $YAML_FILE
|
||||
rm -f $CLEANER_SCRIPT
|
||||
EOF
|
||||
|
||||
chmod +x $CLEANER_SCRIPT
|
||||
|
||||
sed -i "s/node_name_placeholder/$NODE_NAME/g" $YAML_FILE
|
||||
sed -i "s/random_suffix/$RAND/g" $YAML_FILE
|
||||
|
||||
kubectl create -f $YAML_FILE --token=${TOKEN} --server=${KUBE} -n ${NAMESPACE} --insecure-skip-tls-verify=true >> /tmp/kubectl.log 2>&1
|
||||
tsp bash -c "kubectl create -f $YAML_FILE --token=${TOKEN} --server=${KUBE} -n ${NAMESPACE} --insecure-skip-tls-verify=true >> /tmp/kubectl.log 2>&1"
|
||||
tsp bash -c "./$CLEANER_SCRIPT"
|
||||
|
||||
for i in $(kubectl get jobs -l app=kubeinvaders -l approle=chaosnode -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}' --token=${TOKEN} --server=${KUBE} -n ${NAMESPACE} --insecure-skip-tls-verify=true)
|
||||
kubectl delete job $i --token=${TOKEN} --server=${KUBE} --insecure-skip-tls-verify=true -n $NAMESPACE >> /tmp/kubectl.log 2>&1
|
||||
done
|
||||
|
||||
for i in $(kubectl get pods -l app=kubeinvaders -l approle=chaosnode --field-selector=status.phase==Succeeded --token=${TOKEN} --server=${KUBE} --insecure-skip-tls-verify=true | grep -v NAME | awk '{ print $1 }')
|
||||
kubectl delete pod $i --token=${TOKEN} --server=${KUBE} --insecure-skip-tls-verify=true -n $NAMESPACE >> /tmp/kubectl.log 2>&1
|
||||
done
|
||||
rm -f $YAML_FILE
|
||||
|
||||
+2
-5
@@ -15,11 +15,9 @@ function init(self)
|
||||
end
|
||||
|
||||
function on_message(self, message_id, message, sender)
|
||||
print "[on_message] bullet object received a message"
|
||||
if message_id == COLLISION_RESPONSE then
|
||||
print "[on_message] collision detected"
|
||||
|
||||
-- check if it is a k8s_node
|
||||
if message_id == COLLISION_RESPONSE then
|
||||
|
||||
print("check if the bullet collides with a k8s_node")
|
||||
for i,value in ipairs(kubernetes_nodes) do
|
||||
if message["other_id"] == value["id"] then
|
||||
@@ -57,7 +55,6 @@ function on_message(self, message_id, message, sender)
|
||||
msg.post(message["other_id"], "play_animation", {id = hash("chaos")})
|
||||
print("launch deploy of chaos_node to " .. node_name)
|
||||
k8s_node.deploy_chaos_node(node_name)
|
||||
-- msg.post(message["other_id"], "play_animation", {id = hash("led")})
|
||||
bullet_collision_action = "delete_pod"
|
||||
for k,v in ipairs(kubernetes_nodes) do
|
||||
if v["id"] == message["other_id"] then
|
||||
|
||||
+22
-3
@@ -22,7 +22,26 @@ function M.http_node_metrics_result(self, _, response)
|
||||
end
|
||||
|
||||
function M.get_nodes()
|
||||
http.request(endpoint .. "/api/v1/nodes", "GET", M.http_get_nodes_result, headers)
|
||||
current_time = os.clock()
|
||||
|
||||
worker_diff_time = current_time - latest_kubernetes_nodes_request_show
|
||||
|
||||
if worker_diff_time > 0.05 then
|
||||
kubernetes_nodes_size = table.getn(kubernetes_nodes)
|
||||
if kubernetes_nodes_size == 0 and not kubernetes_nodes_request_show then
|
||||
print("Show kubernetes nodes...")
|
||||
http.request(endpoint .. "/api/v1/nodes", "GET", M.http_get_nodes_result, headers)
|
||||
kubernetes_nodes_request_show = true
|
||||
else
|
||||
print("Kubernetes nodes already present...")
|
||||
for k,v in ipairs(kubernetes_nodes) do
|
||||
go.delete(v["id"])
|
||||
end
|
||||
kubernetes_nodes_request_show = false
|
||||
kubernetes_nodes = {}
|
||||
end
|
||||
latest_kubernetes_nodes_request_show = os.clock()
|
||||
end
|
||||
end
|
||||
|
||||
function M.http_get_nodes_result(self, _, response)
|
||||
@@ -31,7 +50,7 @@ function M.http_get_nodes_result(self, _, response)
|
||||
local nodes = json.decode(response.response)
|
||||
local node_items = nodes["items"]
|
||||
local node_items_size = table.getn(node_items)
|
||||
local pos_x = 80
|
||||
local pos_x = 300
|
||||
local pos_y = 500
|
||||
local nodes_cnt = 1
|
||||
|
||||
@@ -69,7 +88,7 @@ function M.http_get_nodes_result(self, _, response)
|
||||
kubernetes_nodes[k1] = { name = v2["name"], id = new_node }
|
||||
if ((nodes_cnt % 15 ) == 0) then
|
||||
pos_y = pos_y + 200
|
||||
pos_x = 80
|
||||
pos_x = 300
|
||||
else
|
||||
pos_x = pos_x + 80
|
||||
end
|
||||
|
||||
@@ -16,10 +16,7 @@ function M.print_pod_name()
|
||||
if pod_name:match("[%a%d%p_]%s[%a%d%p_]") ~= nil then
|
||||
pod_name = pod_name:gsub(" ", "\n")
|
||||
end
|
||||
|
||||
msg.post("ui#gui","pod_name",{ pod_name = pod_name, position = last_pod_position })
|
||||
--msg.post("ui#gui","pod_name",{ pod_name = pod_name })
|
||||
|
||||
msg.post("ui#gui","pod_name",{ pod_name = pod_name, position = last_pod_position })
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -6,33 +6,23 @@
|
||||
local M = {}
|
||||
|
||||
function M.auto()
|
||||
launch_bullet = false
|
||||
msg.post("ui#gui", "error",{ errormessage = "" })
|
||||
if automatic == false then
|
||||
return
|
||||
end
|
||||
for i=1,1 do
|
||||
rand_pos = math.random(300, 1500)
|
||||
end
|
||||
msg.post("ui#gui", "error",{ errormessage = "" })
|
||||
math.randomseed(os.clock()*100000000000)
|
||||
rand_pos = math.random(300, 1500)
|
||||
pos = go.get_position()
|
||||
pos.y = 300
|
||||
go.set_position(pos)
|
||||
|
||||
pos.x = rand_pos
|
||||
|
||||
launch_bullet = true
|
||||
|
||||
|
||||
if launch_bullet == true then
|
||||
go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_LINEAR, 0.5, 0, function()
|
||||
pos = go.get_position()
|
||||
bullet = factory.create("/bullet#bulletfactory", pos)
|
||||
go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_LINEAR, 0.5, 0, function()
|
||||
pos = go.get_position()
|
||||
bullet = factory.create("/bullet#bulletfactory", pos)
|
||||
go.animate(bullet, "position.y", go.PLAYBACK_ONCE_FORWARD, 1200, go.EASING_LINEAR, 1, 0, function()
|
||||
go.delete(bullet)
|
||||
end)
|
||||
end)
|
||||
else
|
||||
go.animate(go.get_id(), "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x, go.EASING_INQUAD, 1,0)
|
||||
end
|
||||
go.delete(bullet)
|
||||
end)
|
||||
end)
|
||||
|
||||
end
|
||||
|
||||
return M
|
||||
+11
-28
@@ -98,8 +98,6 @@ latest_kubernetes_nodes_request_show = 0
|
||||
-- max number of nodes to show
|
||||
max_nodes = 15
|
||||
|
||||
|
||||
|
||||
local COLLISION_RESPONSE = hash("collision_response")
|
||||
local timer = require "main.mymodules.timer"
|
||||
local spaceship = require "main.mymodules.spaceship"
|
||||
@@ -207,8 +205,14 @@ function start_chaos()
|
||||
msg.post("ui#gui", "error1",{ errormessage = "" })
|
||||
end
|
||||
end)
|
||||
timer.repeat_seconds(0.5, function()
|
||||
spaceship.auto()
|
||||
timer.repeat_seconds(1, function()
|
||||
if automatic then
|
||||
spaceship.auto()
|
||||
math.randomseed(os.clock()*100000000000)
|
||||
if math.random(1, 100) % 3 == 0 then
|
||||
k8s_node.get_nodes()
|
||||
end
|
||||
end
|
||||
end)
|
||||
pod_api.set_pods()
|
||||
end
|
||||
@@ -283,29 +287,7 @@ function on_input(self, action_id, action)
|
||||
msg.post("ui#gui", "mode",{ mode = "Press \"a\" to use automatic pilot"})
|
||||
|
||||
elseif action_id == hash("worker") then
|
||||
|
||||
current_time = os.clock()
|
||||
|
||||
worker_diff_time = current_time - latest_kubernetes_nodes_request_show
|
||||
|
||||
if worker_diff_time > 0.05 then
|
||||
kubernetes_nodes_size = table.getn(kubernetes_nodes)
|
||||
if kubernetes_nodes_size == 0 and not kubernetes_nodes_request_show then
|
||||
print("Show kubernetes nodes...")
|
||||
k8s_node.get_nodes()
|
||||
kubernetes_nodes_request_show = true
|
||||
else
|
||||
print("Kubernetes nodes already present...")
|
||||
for k,v in ipairs(kubernetes_nodes) do
|
||||
go.delete(v["id"])
|
||||
end
|
||||
kubernetes_nodes_request_show = false
|
||||
kubernetes_nodes = {}
|
||||
end
|
||||
latest_kubernetes_nodes_request_show = os.clock()
|
||||
end
|
||||
|
||||
|
||||
k8s_node.get_nodes()
|
||||
|
||||
elseif action_id == hash("namespace") then
|
||||
print("[change_namespace] change namespaces requested by user")
|
||||
@@ -415,8 +397,9 @@ function on_message(self, message_id, message, sender)
|
||||
end
|
||||
end
|
||||
|
||||
if message_id == COLLISION_RESPONSE and started_chaos and last_pod_log then
|
||||
if message_id == COLLISION_RESPONSE and started_chaos and not last_pod_log then
|
||||
|
||||
print("log1!")
|
||||
msg.post("ui#gui","pod_log")
|
||||
|
||||
for i,value in ipairs(current_pods) do
|
||||
|
||||
@@ -21,9 +21,8 @@ server {
|
||||
local arg = ngx.req.get_uri_args()
|
||||
ngx.req.read_body()
|
||||
local request_body = ngx.req.get_body_data()
|
||||
local handle = io.popen("/opt/chaos-node.sh " .. arg['k8s_url'] .. " " .. arg['token'] .. " " .. arg['node_name'] .. " " .. arg['namespace'])
|
||||
local result = handle:read("*a")
|
||||
ngx.say(result)
|
||||
os.execute("/opt/chaos-node.sh " .. arg['k8s_url'] .. " " .. arg['token'] .. " " .. arg['node_name'] .. " " .. arg['namespace'] .. " &")
|
||||
ngx.say('chaos_node started!')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +21,8 @@ server {
|
||||
local arg = ngx.req.get_uri_args()
|
||||
ngx.req.read_body()
|
||||
local request_body = ngx.req.get_body_data()
|
||||
local handle = io.popen("/opt/chaos-node.sh " .. arg['k8s_url'] .. " " .. arg['token'] .. " " .. arg['node_name'] .. " " .. arg['namespace'])
|
||||
local result = handle:read("*a")
|
||||
ngx.say(result)
|
||||
os.execute("/opt/chaos-node.sh " .. arg['k8s_url'] .. " " .. arg['token'] .. " " .. arg['node_name'] .. " " .. arg['namespace'] .. " &")
|
||||
ngx.say('chaos_node started!')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
worker_processes 2;
|
||||
error_log /var/log/nginx/error.log warn;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
sendfile on;
|
||||
keepalive_timeout 30;
|
||||
include /etc/nginx/conf.d/*.conf;
|
||||
}
|
||||
Reference in New Issue
Block a user