-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
131 lines (110 loc) · 4.61 KB
/
Copy pathfrontend.py
File metadata and controls
131 lines (110 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import streamlit as st
import subprocess
import sys
import requests
st.set_page_config(page_title="Kubernetes-like System Dashboard", layout="wide")
st.title("Kubernetes-like System Dashboard")
API_URL = "http://localhost:5000"
# Helper function to run CLI commands
def run_command(command):
try:
script_path = "cli.py"
full_command = [sys.executable, script_path] + command
result = subprocess.run(full_command, capture_output=True, text=True, check=True)
return result.stdout
except subprocess.CalledProcessError as e:
return f"Error: {e.stderr}"
# Sidebar for navigation
st.sidebar.title("Operations")
operation = st.sidebar.radio(
"Select Operation",
["Add Node", "Launch Pod", "List Nodes", "Add Policy", "List Policies", "Pod Usage", "Simulate Communication", "Update Network Policy"]
)
# Main content area
if operation == "Add Node":
st.header("Add Node")
st.write("Specify the resources for the new node")
cpu_cores = st.number_input("CPU Cores", min_value=1, max_value=128, value=4)
if st.button("Add Node"):
result = run_command(["add_node", "--cpu_cores", str(cpu_cores)])
if not result.startswith("Error:"):
st.success("Node added successfully!")
else:
st.error("Failed to add node")
st.code(result)
elif operation == "Launch Pod":
st.header("Launch Pod")
st.write("Specify the resources needed for the new pod")
cpu_cores = st.number_input("CPU Cores Required", min_value=1, max_value=64, value=2)
if st.button("Launch Pod"):
result = run_command(["launch_pod", "--cpu_cores", str(cpu_cores)])
if not result.startswith("Error:"):
st.success("Pod launched successfully!")
else:
st.error("Failed to launch pod")
st.code(result)
elif operation == "List Nodes":
st.header("Cluster Nodes")
# Create a placeholder for the node list
node_list_placeholder = st.empty()
# Show initial node list
result = run_command(["list_nodes"])
node_list_placeholder.subheader("Available Nodes:")
node_list_placeholder.code(result)
# Refresh button to update the node list
if st.button("Refresh Node List"):
result = run_command(["list_nodes"])
# Update the same placeholder instead of creating a new one
node_list_placeholder.empty()
node_list_placeholder.subheader("Available Nodes:")
node_list_placeholder.code(result)
elif operation == "Add Policy":
st.header("Add Network Policy")
source_pod = st.text_input("Source Pod")
target_pod = st.text_input("Target Pod")
action = st.selectbox("Action", ["allow", "deny"])
if st.button("Add Policy"):
result = run_command(["add_policy", "--source_pod", source_pod, "--target_pod", target_pod, "--action", action])
if not result.startswith("Error:"):
st.success("Policy added successfully!")
else:
st.error("Failed to add policy")
st.code(result)
elif operation == "List Policies":
st.header("List Network Policies")
result = run_command(["list_policies"])
st.code(result)
elif operation == "Pod Usage":
st.header("Pod Resource Usage")
result = run_command(["pod_usage"])
st.code(result)
elif operation == "Simulate Communication":
st.header("Simulate Pod Communication")
source_pod = st.text_input("Source Pod")
target_pod = st.text_input("Target Pod")
if st.button("Simulate"):
response = requests.post(f"{API_URL}/communicate", json={"source_pod": source_pod, "target_pod": target_pod})
res = response.json()
if 'message' in res:
st.write(res['message'])
else:
st.error(res['error'])
elif operation == "Update Network Policy":
st.header("Update Network Policy")
source_pod = st.text_input("Source Pod")
target_pod = st.text_input("Target Pod")
new_action = st.selectbox("New Action", ["allow", "deny"])
if st.button("Update Policy"):
result = run_command(["update_policy", "--source_pod", source_pod, "--target_pod", target_pod, "--new_action", new_action])
if not result.startswith("Error:"):
st.success("Policy updated successfully!")
else:
st.error("Failed to update policy")
st.code(result)
# # Add some metrics/stats in the sidebar (placeholder)
# st.sidebar.markdown("---")
# st.sidebar.subheader("Cluster Statistics")
# # These can be dynamically fetched in the future
# st.sidebar.metric(label="Total Nodes", value="3")
# st.sidebar.metric(label="Total Pods", value="7")
# st.sidebar.metric(label="Available CPU", value="24 cores")