uplifted the pod concept

added a use_dns flag at container creation time
This commit is contained in:
2025-05-05 16:29:48 +09:30
parent 840e691da0
commit dd36416a5e
5 changed files with 327 additions and 131 deletions
+74 -87
View File
@@ -2,35 +2,28 @@ import json
import streamlit as st
from streamlit_server.utils.helpers import format_timestamp
def render_list():
"""
Renders a list of all container workloads.
Renders a list of container pods, each with its containers.
"""
st.header("Container Workload Management")
# Initialize session state variables for delete all confirmation
if 'container_delete_all_confirmation_shown' not in st.session_state:
st.session_state.container_delete_all_confirmation_shown = False
if 'container_delete_all_confirmed' not in st.session_state:
st.session_state.container_delete_all_confirmed = False
# Initialize session state variables for delete confirmation
if 'container_delete_confirmation_shown' not in st.session_state:
st.session_state.container_delete_confirmation_shown = False
if 'container_delete_confirmed' not in st.session_state:
st.session_state.container_delete_confirmed = False
# Initialize session state flags for pod deletion
if 'pod_delete_all_confirmation_shown' not in st.session_state:
st.session_state.pod_delete_all_confirmation_shown = False
if 'pod_delete_all_confirmed' not in st.session_state:
st.session_state.pod_delete_all_confirmed = False
if 'pod_delete_confirmation_shown' not in st.session_state:
st.session_state.pod_delete_confirmation_shown = False
if 'pod_delete_confirmed' not in st.session_state:
st.session_state.pod_delete_confirmed = False
# Fetch all VDCs for the dropdown
# Fetch VDCs
vdcs = st.session_state.client.get_virtual_data_centers()
vdc_options = {vdc['name']: vdc['id'] for vdc in vdcs} if vdcs else {}
# Add a dropdown to select VDC
selected_vdc_name = st.selectbox("Filter by Virtual Data Center", list(vdc_options.keys()))
# Fetch all Workloads_hosts for the dropdown
workload_hosts = st.session_state.client.get_workload_hosts()
workload_host_options = {workload_host['hostname']: workload_host['id'] for workload_host in workload_hosts} if workload_hosts else {}
# Create Container Workload Form
with st.expander("Create New Container Workload"):
with st.form("create_container"):
@@ -56,16 +49,18 @@ def render_list():
internal_port = st.number_input("Internal Port", min_value=1, max_value=65535, step=1)
external_port = st.number_input("External Port", min_value=1, max_value=65535, step=1)
# New: Use DNS (Cloudflare) Checkbox
use_dns = st.checkbox("Use DNS (Cloudflare)", value=True)
submit = st.form_submit_button("Create Container Workload")
if submit and selected_vdc_name_create and container_name:
# Use the selected VDC's ID
vdc_id = vdc_options[selected_vdc_name_create]
container_payload = {
"docker_image": docker_image,
"container_name": container_name,
"networks": "bridge" # Default network
"networks": "bridge", # Default network
}
# Only add ports if both fields are non-zero
@@ -74,6 +69,7 @@ def render_list():
payload = {
"vdc": vdc_id,
"use_dns": use_dns,
"containers": [container_payload]
}
@@ -82,87 +78,78 @@ def render_list():
st.success("Container workload created successfully!")
# List Container Pods
st.subheader("Existing Container Pods")
pods = st.session_state.client.get_container_pods()
# List Container Workloads
st.subheader("Existing Container Workloads")
containers = st.session_state.client.get_containers()
if pods:
# Filter by selected VDC
selected_vdc_id = vdc_options[selected_vdc_name]
filtered_pods = [pod for pod in pods if pod['vdc_id'] == selected_vdc_id]
if containers:
# Filter containers based on the selected VDC
if selected_vdc_name:
selected_vdc_id = vdc_options[selected_vdc_name]
filtered_containers = [container for container in containers if container['vdc_id'] == selected_vdc_id]
else:
filtered_containers = containers
# Delete All functionality
if not st.session_state.container_delete_all_confirmation_shown:
if st.button("Delete All Containers", key="delete_all_containers", type="primary", help="This will delete all visible containers"):
st.session_state.container_delete_all_confirmation_shown = True
# Delete All Pods
if not st.session_state.pod_delete_all_confirmation_shown:
if st.button("Delete All Pods", key="delete_all_pods", type="primary", help="This will delete all visible pods"):
st.session_state.pod_delete_all_confirmation_shown = True
st.rerun()
# Show confirmation if delete was clicked
if st.session_state.container_delete_all_confirmation_shown and not st.session_state.container_delete_all_confirmed:
st.warning("Are you sure you want to delete all containers? This action cannot be undone.")
if st.session_state.pod_delete_all_confirmation_shown and not st.session_state.pod_delete_all_confirmed:
st.warning("Are you sure you want to delete all pods? This action cannot be undone.")
confirm_col1, confirm_col2 = st.columns([1, 1])
with confirm_col1:
if st.button("Yes, Delete All", key="confirm_delete_all_containers"):
st.session_state.container_delete_all_confirmed = True
if st.button("Yes, Delete All Pods", key="confirm_delete_all_pods"):
st.session_state.pod_delete_all_confirmed = True
st.rerun()
with confirm_col2:
if st.button("Cancel", key="cancel_delete_all_containers"):
st.session_state.container_delete_all_confirmation_shown = False
if st.button("Cancel", key="cancel_delete_all_pods"):
st.session_state.pod_delete_all_confirmation_shown = False
st.rerun()
# Perform deletion if confirmed
if st.session_state.container_delete_all_confirmed:
if st.session_state.pod_delete_all_confirmed:
deletion_count = 0
for container in filtered_containers:
# Call the delete API for each container
result = st.session_state.client.delete_container(container['id'])
for pod in filtered_pods:
result = st.session_state.client.delete_container_pod(pod['pod_id'])
if result:
deletion_count += 1
st.success(f"Successfully deleted {deletion_count} containers.")
# Reset the confirmation flags
st.session_state.container_delete_all_confirmation_shown = False
st.session_state.container_delete_all_confirmed = False
# Display the containers
for container in filtered_containers:
with st.container():
col1, col2, col3 = st.columns([3, 1, 1])
with col1:
# Safely handle empty or None launch_params
launch_params = container.get('launch_params') # Use .get() to avoid KeyError if 'launch_params' doesn't exist
if launch_params: # Check if launch_params is not empty or None
try:
container_name = json.loads(launch_params).get('docker_image') # Use .get() to avoid KeyError if 'docker_image' doesn't exist
except json.JSONDecodeError:
print("LP not vald json")
# Handle the case where launch_params is not valid JSON
container_name = None
else:
print("lp empty")
# Handle the case where launch_params is empty or None
container_name = None
st.markdown(f"### {container['name']}")
st.text(f"Status: {container['status']}")
st.text(f"VDC ID: {container['vdc_id']}")
st.text(f"Image: {container_name}")
with col2:
st.text(f"ID: {container['id']}")
with col3:
if st.button("View Details", key=f"view_{container['id']}"):
st.session_state.selected_resource = container
st.session_state.view_type = 'detail'
st.session_state.selected_resource_type = 'container'
st.success(f"Successfully deleted {deletion_count} pods.")
st.session_state.pod_delete_all_confirmation_shown = False
st.session_state.pod_delete_all_confirmed = False
st.rerun()
# Render Pods nicely
for pod in filtered_pods:
with st.expander(f"🛰️ Pod: {pod['pod_name']} (ID: {pod['pod_id']})", expanded=False):
st.markdown(f"**Virtual Data Center ID:** `{pod['vdc_id']}`")
st.markdown(f"**Region ID:** `{pod.get('region_id', 'N/A')}`")
st.markdown(f"**NSController ID:** `{pod['nscontroller_workload_id']}`")
st.markdown("---")
# Container List
for container in pod['containers']:
with st.container():
cols = st.columns([3, 2, 1])
with cols[0]:
st.markdown(f"**Container Name:** {container['container_name']}")
st.text(f"Status: {container['status']}")
with cols[1]:
st.text(f"Container ID: {container['container_id']}")
with cols[2]:
if st.button("View Details", key=f"view_{container['container_id']}"):
st.session_state.selected_resource = container
st.session_state.view_type = 'detail'
st.session_state.selected_resource_type = 'container'
st.rerun()
st.markdown("---")
# Delete pod button
if st.button(f"🗑️ Delete Pod {pod['pod_name']}", key=f"delete_pod_{pod['pod_id']}"):
result = st.session_state.client.delete_container_pod(pod['pod_id'])
if result:
st.success(f"Deleted Pod {pod['pod_name']}")
st.rerun()
# st.switch_page("streamlit_server/views/containers.py&container_id=d", query_params={"team": "streamlit"})
else:
st.info("No container workloads found.")
st.info("No pods found in the selected VDC.")
def render_detail():