Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Flask, send_file, jsonify
from virtcluster.cluster import create_vcluster, switch_context, get_kubeconfig
import virtcluster.CreateService as CreateService
import virtcluster.GetServices as GetServices
from virtcluster import service
from virtcluster.utils import get_available_node_port

import subprocess
import os
Expand All @@ -17,9 +17,9 @@ def create_vcluster_endpoint(cluster_name):
namespace = f"vcluster-{cluster_name}".lower()
cluster_name = cluster_name.lower()
print("Getting free nodeport")
nodePort = GetServices.main()
nodePort = get_available_node_port()
print(f"Creating service with exposed port {nodePort}")
CreateService.main(cluster_name, namespace, nodePort)
service.create(cluster_name, namespace, nodePort)
print("Service created")

create_vcluster(cluster_name)
Expand Down
14 changes: 8 additions & 6 deletions virtcluster/CreateService.py → virtcluster/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,30 @@ def run_command(command):
return result.stdout.decode().strip()


def main(clusterName, namespace, nodePort):
def create(clsuter_name, namespace, node_port):
data = {
"clusterName": clusterName,
"clusterName": clsuter_name,
"namespace": namespace,
"nodePort": nodePort
"nodePort": node_port
}

template = Template(template_str)

rendered_str = template.render(data)

with open(f"nodeport-service-{clusterName}.yaml", "w") as f:
with open(f"nodeport-service-{clsuter_name}.yaml", "w") as f:
f.write(rendered_str)

print(f"Template rendered and saved to nodeport-service-{clusterName}.yaml")
print(f"Template rendered and saved to nodeport-service-{clsuter_name}.yaml")
print("Applying kubernetes manifest for nodeport service")

try:
print(run_command(f"kubectl create namespace {namespace}"))
#time.sleep(2)
run_command(f"kubectl apply -f nodeport-service-{clusterName}.yaml")
run_command(f"kubectl apply -f nodeport-service-{clsuter_name}.yaml")
except:
raise KeyError("the namespace is probably already in use, aborting")


if __name__ == "__main__":
pass
13 changes: 8 additions & 5 deletions virtcluster/GetServices.py → virtcluster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import random
# Configs can be set in Configuration class directly or using helper utility

def get_list_nodes_used():
def _get_list_nodes_used():
config.load_kube_config()

v1 = client.CoreV1Api()
Expand All @@ -16,14 +16,17 @@ def get_list_nodes_used():
nodes_used = [svc.spec.ports[0].node_port for svc in ret.items]
return nodes_used

def main():

nodes_used = get_list_nodes_used()
def get_available_node_port():
nodes_used = _get_list_nodes_used()
while True:
new_node_port = random.randint(30005, 32767)
if new_node_port not in nodes_used:
return new_node_port


if __name__ == "__main__":
main()
nodes_used = get_list_nodes_used()
while True:
new_node_port = random.randint(30005, 32767)
if new_node_port not in nodes_used:
print(new_node_port)