Added support for customizing chaos container for nodes

This commit is contained in:
Eugenio Marzo
2021-07-04 17:20:26 +02:00
parent a83193cb84
commit fda9d47b4f
4 changed files with 171 additions and 98 deletions
+53 -9
View File
@@ -21,22 +21,22 @@
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Chaos Menu
Chaos Containers
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink">
<li><a class="dropdown-item" href="#" onclick="showCurrentChaosJob()">Show Current Chaos Job</a></li>
</ul>
<li><a class="dropdown-item" href="#" onclick="showCurrentChaosContainer()">Show Current Chaos Container for nodes</a></li>
<li><a class="dropdown-item" href="#" onclick="showSetCurrentChaosContainer()">Set Custom Chaos Container for nodes</a></li>
</li>
</ul>
</div>
</div>
</nav>
<!-- Modal -->
<div class="modal fade" id="currentChaosJobModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal fade" id="currentChaosContainerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<pre id="currentChaosJobYaml"></pre>
<pre id="currentChaosContainrYaml"></pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="closeCurrentChaosJobModal()">Close</button>
@@ -44,6 +44,29 @@
</div>
</div>
</div>
<div class="modal fade" id="setChaosContainerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<form>
<div class="form-group">
<label for="exampleFormControlTextarea1" style="margin-bottom: 3%;" onte>Chaos Container Definition</label>
<textarea class="form-control" id="currentChaosContainerJsonTextArea" rows="20"></textarea>
</div>
<div class="form-group">
<p id="alert_placeholder2" style="color: black; font-family: 'Courier New', Courier, monospace; margin-top: 3%;"></p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" onclick="setChaosContainer()">Save</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="closeSetChaosContainerModal()">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container" style="width: 50%;">
<div class="starter-template">
<div class="row">
@@ -78,13 +101,34 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script>
function showCurrentChaosJob() {
getCurrentChaosJob();
$('#currentChaosJobModal').modal('show');
function showCurrentChaosContainer() {
getCurrentChaosContainer();
$('#currentChaosContainerModal').modal('show');
modal_opened = true;
}
function closeCurrentChaosJobModal() {
$('#currentChaosJobModal').modal('hide');
$('#currentChaosContainerModal').modal('hide');
modal_opened = false;
}
function showSetCurrentChaosContainer() {
$('#alert_placeholder2').text('');
getCurrentChaosContainer();
$('#setChaosContainerModal').modal('show');
modal_opened = true;
}
function closeSetChaosContainerModal() {
$('#setChaosContainerModal').modal('hide');
modal_opened = false;
}
$('textarea').on('input', function() {
$('#alert_placeholder2').text('');
});
</script>
</body>
</html>
+107 -75
View File
@@ -14,6 +14,7 @@ var namespaces = [];
var namespaces_index = 0;
var namespace = namespaces[namespaces_index];
var endpoint = "";
var modal_opened = false;
// pods list from kubernetes
var pods = [];
@@ -73,6 +74,15 @@ var help = false;
var chaos_nodes = true;
var chaos_pods = true;
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
@@ -103,18 +113,38 @@ function getEndpoint() {
oReq.send();
}
function getCurrentChaosJob() {
function getCurrentChaosContainer() {
var oReq = new XMLHttpRequest();
oReq.onload = function () {
console.log(this.responseText);
job_parsed = JSON.stringify(JSON.parse(this.responseText), null, 4);
console.log(job_parsed);
$('#currentChaosJobYaml').text(job_parsed);
$('#currentChaosContainrYaml').text(job_parsed);
$('#currentChaosContainerJsonTextArea').val(job_parsed);
};;
oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/chaos/containers?action=default");
oReq.open("GET", "https://ENDPOINT_PLACEHOLDER/kube/chaos/containers?action=container_definition");
oReq.send();
}
function setChaosContainer() {
if (!IsJsonString($('#currentChaosContainerJsonTextArea').val())) {
$('#alert_placeholder2').text('JSON syntax not valid.');
}
else {
var oReq = new XMLHttpRequest();
oReq.open("POST", "https://ENDPOINT_PLACEHOLDER/kube/chaos/containers?action=set", true);
oReq.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log(this.responseText);
$('#alert_placeholder2').text('New container definition has been saved.');
}
};;
oReq.setRequestHeader("Content-Type", "application/json");
oReq.send($('#currentChaosContainerJsonTextArea').val());
}
}
function startChaosNode(node_name) {
var oReq = new XMLHttpRequest();
oReq.onload = function () {
@@ -179,86 +209,88 @@ window.setInterval(function getKubeItems() {
}, 1000)
function keyDownHandler(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
rightPressed = true;
//console.log("Go right");
//console.log("Spaceship Y:" + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
else if(e.key == "Left" || e.key == "ArrowLeft") {
leftPressed = true;
//console.log("Go left");
//console.log("Spaceship Y:" + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
if(e.key == "Up" || e.key == "ArrowUp") {
upPressed = true;
//console.log("Go up");
//console.log("Spaceship Y:" + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
else if(e.key == "Down" || e.key == "ArrowDown") {
downPressed = true;
//console.log("Go down");
//console.log("Spaceship Y: " + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
else if(e.keyCode == 83) {
if (shuffle) {
shuffle = false;
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Disable shuffle</div>');
//console.log("Deactivate shuffle");
if (!modal_opened) {
if(e.key == "Right" || e.key == "ArrowRight") {
rightPressed = true;
//console.log("Go right");
//console.log("Spaceship Y:" + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
else {
shuffle = true
//console.log("Activate shuffle");
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Enable shuffle</div>');
else if(e.key == "Left" || e.key == "ArrowLeft") {
leftPressed = true;
//console.log("Go left");
//console.log("Spaceship Y:" + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
}
else if(e.keyCode == 32) {
//console.log("Shot");
shot = true
}
else if(e.keyCode == 78) {
//console.log("Change Namespace");
if (namespaces_index < namespaces.length-1) {
namespaces_index +=1 ;
if(e.key == "Up" || e.key == "ArrowUp") {
upPressed = true;
//console.log("Go up");
//console.log("Spaceship Y:" + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
else {
namespaces_index = 0;
else if(e.key == "Down" || e.key == "ArrowDown") {
downPressed = true;
//console.log("Go down");
//console.log("Spaceship Y: " + spaceshipY);
//console.log("Spaceship X: " + spaceshipX);
}
namespace = namespaces[namespaces_index];
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Change target namespace to ' + namespace + '</div>');
aliens = [];
pods = [];
}
else if(e.keyCode == 72) {
if (help) {
help = false;
else if(e.keyCode == 83) {
if (shuffle) {
shuffle = false;
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Disable shuffle</div>');
//console.log("Deactivate shuffle");
}
else {
shuffle = true
//console.log("Activate shuffle");
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Enable shuffle</div>');
}
}
else {
help = true
else if(e.keyCode == 32) {
//console.log("Shot");
shot = true
}
}
else if(e.keyCode == 67) {
if (chaos_nodes) {
chaos_nodes = false;
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Show nodes</div>');
else if(e.keyCode == 78) {
//console.log("Change Namespace");
if (namespaces_index < namespaces.length-1) {
namespaces_index +=1 ;
}
else {
namespaces_index = 0;
}
namespace = namespaces[namespaces_index];
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Change target namespace to ' + namespace + '</div>');
aliens = [];
pods = [];
}
else if(e.keyCode == 72) {
if (help) {
help = false;
}
else {
help = true
}
}
else if(e.keyCode == 67) {
if (chaos_nodes) {
chaos_nodes = false;
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Show nodes</div>');
}
else {
chaos_nodes = true
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Hide nodes</div>');
}
}
else {
chaos_nodes = true
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Hide nodes</div>');
}
}
else if(e.keyCode == 80) {
if (chaos_pods) {
chaos_pods = false;
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Show pods</div>');
}
else {
chaos_pods = true
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Hide pods</div>');
else if(e.keyCode == 80) {
if (chaos_pods) {
chaos_pods = false;
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Show pods</div>');
}
else {
chaos_pods = true
$('#alert_placeholder').replaceWith('<div id="alert_placeholder" class="alert alert-info" role="alert">Latest action: Hide pods</div>');
}
}
}
}
+1
View File
@@ -98,6 +98,7 @@ server {
}
location /kube/chaos/containers {
lua_need_request_body 'on';
access_by_lua_file "/usr/local/openresty/nginx/conf/kubeinvaders/chaos-containers.lua";
}
}
+10 -14
View File
@@ -17,24 +17,20 @@ 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'
if ngx.var.request_method == "GET" and action == 'default' then
ngx.say(config['default_chaos_container'])
elseif ngx.var.request_method == "GET" and action == 'chaos_container' then
if ngx.var.request_method == "GET" and action == 'container_definition' then
ngx.log(ngx.ERR, "Received request for getting chaos container definition")
local res, err = red:get("chaos_container")
if res == ngx.null then
ngx.say('A custom chaos container has not yet been configured')
ngx.log(ngx.ERR, "There is no chaos_container key set in Redis. Taking default chaos container definition")
ngx.say(config['default_chaos_container'])
else
ngx.log(ngx.ERR, "There is chaos_container key set in Redis. Taking custom chaos container definition")
ngx.say(res)
end
elseif ngx.var.request_method == "POST" and action == 'set' then
ngx.say('New chaos container has been configured')
if arg['chaos_container'] == nil then
ngx.say("Please set the parameter 'chaos_container'")
else
red:set("chaos_container", arg['chaos_container'])
ngx.say("chaos_container has been configured in Redis")
end
local body_data = ngx.req.get_body_data()
ngx.log(ngx.ERR, "Received new yaml definition for chaos container: " .. body_data)
red:set("chaos_container", body_data)
ngx.say("New chaos container definition has been configured in Redis")
return ngx.exit(ngx.status)
end