diff --git a/main.py b/main.py index 53c40b0..8901cee 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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) diff --git a/virtcluster/CreateService.py b/virtcluster/service.py similarity index 75% rename from virtcluster/CreateService.py rename to virtcluster/service.py index 93ed0a6..9f7eda4 100644 --- a/virtcluster/CreateService.py +++ b/virtcluster/service.py @@ -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 \ No newline at end of file diff --git a/virtcluster/GetServices.py b/virtcluster/utils.py similarity index 74% rename from virtcluster/GetServices.py rename to virtcluster/utils.py index eac04bb..5184812 100644 --- a/virtcluster/GetServices.py +++ b/virtcluster/utils.py @@ -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() @@ -16,9 +16,8 @@ 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: @@ -26,4 +25,8 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + 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) \ No newline at end of file