import { useEffect, useState } from 'react';
import { EKSClient, DescribeClusterCommand } from '@aws-sdk/client-eks';
import axios from 'axios';
// Configure AWS SDK v3
const region = 'us-east-1'; // Change to your region
// Create an Axios instance
const axiosInstance = axios.create();
// Add a request interceptor
axiosInstance.interceptors.request.use(config => {
config.headers['Access-Control-Allow-Origin'] = '*';
return config;
}, error => {
return Promise.reject(error);
});
const eksClient = new EKSClient({
region,
credentials: {
accessKeyId: "AKIA3X4DCJJWHYF5M42F",
secretAccessKey: "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",
},
});
const Header = () => {
return (
);
};
const Card = ({ title, children, className = "" }) => {
return (
{title}
{children}
);
};
const StatusItem = ({ value, label }) => {
return (
);
};
const StatusTable = ({ headers, rows }) => {
return (
{headers.map((header, index) => (
| {header} |
))}
{rows.map((row, rowIndex) => (
{row.map((cell, cellIndex) => (
| = headers.length - 2 ? 'text-right' : 'text-left'} dark:text-white`}>
{cell}
|
))}
))}
);
};
const Dashboard = ({ clusterName }) => {
const [clusterOverview, setClusterOverview] = useState({});
const [nodes, setNodes] = useState([]);
const [pods, setPods] = useState([]);
useEffect(() => {
const fetchClusterData = async () => {
try {
const clusterData = await eksClient.send(new DescribeClusterCommand({ name: clusterName }));
const endpoint = clusterData.cluster.endpoint;
console.log(endpoint)
const nodeCount = await getNodeCount(endpoint);
const podCount = await getPodCount(endpoint);
const serviceCount = await getServiceCount(endpoint);
setClusterOverview({
nodes: nodeCount,
pods: podCount,
services: serviceCount,
});
const nodesData = await getNodeStatuses(endpoint);
setNodes(nodesData);
const podsData = await getPodStatuses(endpoint);
setPods(podsData);
} catch (error) {
console.error('Error fetching data from AWS:', error);
}
};
fetchClusterData();
}, [clusterName]);
const getNodeCount = async (endpoint) => {
const response = await axiosInstance.get(`${endpoint}/api/v1/nodes`);
return response.data.items.length;
};
const getPodCount = async (endpoint) => {
const response = await axiosInstance.get(`${endpoint}/api/v1/pods`);
return response.data.items.length;
};
const getServiceCount = async (endpoint) => {
const response = await axiosInstance.get(`${endpoint}/api/v1/services`);
return response.data.items.length;
};
const getNodeStatuses = async (endpoint) => {
const response = await axiosInstance.get(`${endpoint}/api/v1/nodes`);
return response.data.items.map(node => ({
name: node.metadata.name,
status: node.status.conditions.find(condition => condition.type === 'Ready').status === 'True' ? 'Running' : 'NotReady',
cpu: `${Math.round((node.status.allocatable.cpu / node.status.capacity.cpu) * 100)}%`,
memory: `${Math.round((node.status.allocatable.memory / node.status.capacity.memory) * 100)}%`,
}));
};
const getPodStatuses = async (endpoint) => {
const response = await axiosInstance.get(`${endpoint}/api/v1/pods`);
return response.data.items.map(pod => ({
name: pod.metadata.name,
status: pod.status.phase,
image: pod.spec.containers[0].image,
}));
};
return (
[node.name, node.status, node.cpu, node.memory])}
/>
[pod.name, pod.status, pod.image])}
/>
);
};
export default Dashboard;