Skip to content

Commit

Permalink
add k8s configuration generator
Browse files Browse the repository at this point in the history
  • Loading branch information
OriHoch committed Oct 19, 2024
1 parent 4e6e3f9 commit 21783b4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 29 deletions.
18 changes: 17 additions & 1 deletion pages_src/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ def get_server_config_templates():
return res


def get_server_config_templates_k8s():
res = {}
for config_name, config_extension in {
'default': 'yaml',
}.items():
with open(f'pages_src/templates/config_k8s_{config_name}.{config_extension}') as f:
res[config_name] = encode_js_template(f.read())
return res


def generate(calculator_js_php_hash):
env = jinja2.Environment(
loader=jinja2.PackageLoader("pages_src", "templates"),
autoescape=jinja2.select_autoescape()
)
generate_template(env, "index.html")
serverconfiggen_hash = generate_template(env, "serverconfiggen.js", render_kwargs={
"config_templates_json": json.dumps(get_server_config_templates())
"config_templates_json": json.dumps(get_server_config_templates()),
"config_templates_json_k8s": json.dumps(get_server_config_templates_k8s())
}, generate_hash=True)
generate_template(env, "serverconfiggen.html", "serverconfiggen.html", {
"calculator_js_php_url": f"calculator.js.php?h={calculator_js_php_hash}",
Expand All @@ -60,6 +71,11 @@ def generate(calculator_js_php_hash):
"calculator_js_php_url": CALCULATOR_JS_PHP_URL,
"serverconfiggen_hash": serverconfiggen_hash,
})
generate_template(env, "serverconfiggen.html", "serverconfiggen_k8s.html", {
"calculator_js_php_url": f"calculator.js.php?h={calculator_js_php_hash}",
"serverconfiggen_hash": serverconfiggen_hash,
"k8s": True,
})
print('OK')


Expand Down
33 changes: 33 additions & 0 deletions pages_src/templates/config_k8s_default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cluster:
name: <CLUSTER_NAME> # required, used as a prefix for all resources
datacenter: "${c.datacenter}" # required, will be the same for all nodes in the cluster
ssh-key: # required, fill in the private and public keys for the cluster, will be used for ssh access to all nodes
private: |
-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----
public: |
ssh-...
private-network:
name: lan-... # required, lan network name, must be created in advance with enough ips for all the nodes in the cluster
default-node-config: # default values for all nodes in the cluster
cpu: "${c.cpuCores}${c.cpuType}"
ram: "${c.ram}"
disk: "${c.diskSizes.join(",")}"
billingcycle: "${c.billing}"
monthlypackage: "${c.billing === "monthly" ? c.netpack : ""}"
# dailybackup: "yes"
# managed: "yes"
# server: https://1.2.3.4:9345 # optional, in case you want to use a specific server as the main cluster server
# token: # optional, to use as the cluster join token, if not provided will try to get it from controlplane-1 node
# controlplane-node-name: # optional, if you want to specify the controlplane node name otherwise it will look for the first controlplane node

node-pools:
# controlplane: # optional, the controlplane node pool is created anyway, this is only if you want to change the controlplane node config
# nodes: 1 # must always be set to 1 - no support for high availability yet
# default-node-config: # optional, default values for all nodes in this pool, values here override the global default-node-config
worker1:
nodes: 3 # required, number of nodes in this node pool
# default-node-config: # optional, default values for all nodes in this pool, values here override the global default-node-config
worker2:
nodes: [5, 6] # nodes can also be specified like this to keep specific node numbers, each number is the node number in the pool
3 changes: 2 additions & 1 deletion pages_src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{% block body %}
<ul class="list-group">
<li class="list-group-item"><a href="serverconfiggen.html">Configuration Generator</a></li>
<li class="list-group-item"><a href="serverconfiggen_k8s.html">K8S Configuration Generator</a></li>
</ul>
{% endblock %}
{% endblock %}
17 changes: 13 additions & 4 deletions pages_src/templates/serverconfiggen.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
{{ super() }}
<script src="serverconfiggen.js?h={{ serverconfiggen_hash }}"></script>
<script>
serverconfiggeninit("{{ calculator_js_php_url }}")
serverconfiggeninit("{{ calculator_js_php_url }}", {{ "true" if k8s else "false" }})
</script>
{% endblock %}
{% block body %}
<div class="container"><div class="row"><div class="col">
<h1 class="mt-5">Kamatera Toolbox Server Configuration Generator</h1>
<p>Use this tool to generate server configurations for Kamatera drivers and tools.</p>
{% if k8s %}
<h1 class="mt-5">Kamatera Kubernetes Configuration Generator</h1>
<p>Use this tool to generate Kamatera Kubernetes (kconfig) configuration files.</p>
{% else %}
<h1 class="mt-5">Kamatera Toolbox Server Configuration Generator</h1>
<p>Use this tool to generate server configurations for Kamatera drivers and tools.</p>
{% endif %}
<div class="spinner-border" role="status" id="mainloader">
<span class="visually-hidden">Loading...</span>
</div>
Expand All @@ -23,6 +28,7 @@ <h1 class="mt-5">Kamatera Toolbox Server Configuration Generator</h1>
<option value="" selected>Choose a datacenter</option>
</select>
</div>
{% if not k8s %}
<div class="mb-3">
<label for="imagecategory" class="form-label">Image Category</label>
<select class="form-select" aria-label="Image Category" id="imagecategory">
Expand All @@ -36,6 +42,7 @@ <h1 class="mt-5">Kamatera Toolbox Server Configuration Generator</h1>
</select>
<div class="form-text" id="imageDescription"></div>
</div>
{% endif %}
<div class="mb-3">
<label for="cputype" class="form-label">CPU Type</label>
<select class="form-select" aria-label="CPU Type" id="cputype">
Expand Down Expand Up @@ -77,6 +84,7 @@ <h1 class="mt-5">Kamatera Toolbox Server Configuration Generator</h1>
<option value="" selected>Choose a datacenter to see available network traffic packages</option>
</select>
</div>
{% if not k8s %}
<div class="mb-3">
<label for="configformat" class="form-label">Configuration Format</label>
<div class="invisible form-text" id="forceConfigFormat"></div>
Expand All @@ -86,9 +94,10 @@ <h1 class="mt-5">Kamatera Toolbox Server Configuration Generator</h1>
</select>
</div>
</div>
{% endif %}
<div class="mb-3">
<div class="form-text text-danger" id="configurationError"></div>
<label for="configuration" class="form-label">Configuration</label>
<label for="configuration" class="form-label">{% if k8s %}kconfig.yaml{% else %}Configuration{% endif %}</label>
<textarea class="form-control" id="configuration" rows="15"></textarea>
</div>
</form>
Expand Down
56 changes: 34 additions & 22 deletions pages_src/templates/serverconfiggen.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
window.serverconfiggeninit = function (calculator_js_php_url) {
window.serverconfiggeninit = function (calculator_js_php_url, k8s) {
var datacenterNames = {
"AS": "Asia: China, Hong Kong",
"CA-TR": "North America: Canada, Toronto",
Expand All @@ -20,7 +20,11 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
D: "Dedicated",
T: "Burstable"
};
window.configTemplates = {{ config_templates_json }};
if (k8s) {
window.configTemplates = {{ config_templates_json_k8s }};
} else {
window.configTemplates = {{ config_templates_json }};
}
window.datacenters = {};
window.images = {};
window.imageCategories = {};
Expand Down Expand Up @@ -52,15 +56,17 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
var db = getImageDescription(b).toLowerCase();
return da < db ? -1 : (da === db ? 0 : 1)
}), function (i, image) {
window.images[image.id] = {
description: getImageDescription(image),
originalDescriptionHtml: image.description.replace(/\n/g, "<br/>"),
imageSizeGB: image.imageSizeGB,
minRamMB: image.minRamMB,
name: image.name
};
if (window.imageCategories[image.category] === undefined) window.imageCategories[image.category] = [];
window.imageCategories[image.category].push(image.id);
if (!k8s) {
window.images[image.id] = {
description: getImageDescription(image),
originalDescriptionHtml: image.description.replace(/\n/g, "<br/>"),
imageSizeGB: image.imageSizeGB,
minRamMB: image.minRamMB,
name: image.name
};
if (window.imageCategories[image.category] === undefined) window.imageCategories[image.category] = [];
window.imageCategories[image.category].push(image.id);
}
$.each(image.datacenters, function (i, dc) {
if (window.datacenters[dc] === undefined) window.datacenters[dc] = {
name: datacenterNames[dc] === undefined ? dc : datacenterNames[dc],
Expand Down Expand Up @@ -103,7 +109,7 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
})
};
var updateDatacenterUi = function () {
updateImagesUi();
if (!k8s) updateImagesUi();
updateNetPacksUi();
}
var updateImagesUi = function () {
Expand Down Expand Up @@ -265,9 +271,11 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
if (!datacenter) {
return setConfigurationError("Please select a datacenter");
}
var imageId = $("#image").val();
if (!imageId) {
return setConfigurationError("Please select an image");
if (!k8s) {
var imageId = $("#image").val();
if (!imageId) {
return setConfigurationError("Please select an image");
}
}
var cpuType = $("#cputype").val();
if (!cpuType) {
Expand Down Expand Up @@ -324,10 +332,10 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
}
var setFromLocalstorage = function () {
if (window.localStorage.getItem("datacenter")) $("#" + "datacenter").val(window.localStorage.getItem("datacenter") || "");
if (window.localStorage.getItem("imagecategory")) $("#" + "imagecategory").val(window.localStorage.getItem("imagecategory") || "");
if (!k8s && window.localStorage.getItem("imagecategory")) $("#" + "imagecategory").val(window.localStorage.getItem("imagecategory") || "");
if (window.localStorage.getItem("billing")) $("#" + "billing").val(window.localStorage.getItem("billing") || "");
updateDatacenterUi()
if (window.localStorage.getItem("image")) $("#" + "image").val(window.localStorage.getItem("image") || "");
if (!k8s && window.localStorage.getItem("image")) $("#" + "image").val(window.localStorage.getItem("image") || "");
if (window.localStorage.getItem("cputype")) $("#" + "cputype").val(window.localStorage.getItem("cputype") || "");
updateCpuTypeUi()
if (window.localStorage.getItem("cpucores")) $("#" + "cpucores").val(window.localStorage.getItem("cpucores") || "");
Expand All @@ -350,10 +358,12 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
$("#datacenter").append($("<option>").attr("value", datacenterId).text(datacenter.name));
})
$("#datacenter").change(updateDatacenterUi);
$.each(imageCategories, function (imageCategory) {
$("#imagecategory").append($("<option>").attr("value", imageCategory).text(imageCategory));
})
$("#imagecategory").change(updateImagesUi)
if (!k8s) {
$.each(imageCategories, function (imageCategory) {
$("#imagecategory").append($("<option>").attr("value", imageCategory).text(imageCategory));
})
$("#imagecategory").change(updateImagesUi)
}
$.each(cpuTypes, function (cpuTypeId, cpuType) {
$("#cputype").append($("<option>").attr("value", cpuTypeId).text(cpuType.name));
})
Expand All @@ -379,7 +389,9 @@ window.serverconfiggeninit = function (calculator_js_php_url) {
$("#configformat").append($("<option>").attr("value", configFormat).text(configFormat));
})
}
$("#image").change(onImageChange);
if (!k8s) {
$("#image").change(onImageChange);
}
$("select").change(onAnyChange);
$("#additionaldisk a").click(addAdditionalDisk);
$("#clearConfigurations a").click(clearLocalStorage);
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
requests==2.24.0
requests==2.32.3
ruamel.yaml==0.16.10
pytest==5.4.3
paramiko==2.7.1
pytest-xdist==1.32.0
pexpect==4.8.0
Jinja2==3.1.2
watchdog==2.1.9
urllib3==2.2.3

0 comments on commit 21783b4

Please sign in to comment.