feat: multi-cluster management with pool-based load balancing

- Add ClusterPool entity for grouping clusters into named pools
- Support 3 deployment modes: manual cluster, pool load-balanced, default fallback
- Pool strategies: least-apps (fewest deployed apps) and round-robin
- Add pool CRUD API endpoints (admin) and public pool listing
- Frontend deploy page: 3-mode cluster selector (Default/Manual/Pool)
- Frontend app detail: shows assigned cluster and pool info
- Admin pools management page with cluster selection and strategy picker
- Application entity extended with poolId field
This commit is contained in:
keyhan
2026-04-05 17:37:05 +03:30
parent 1b1ccfc18f
commit 2621dc0cc6
14 changed files with 976 additions and 14 deletions
+187 -2
View File
@@ -2,10 +2,10 @@
import { useState, useRef, useCallback } from 'react';
import { useRouter } from 'next/navigation';
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQuery } from '@tanstack/react-query';
import api from '@/lib/api';
import toast from 'react-hot-toast';
import type { CreateApplicationDto } from '@/types';
import type { CreateApplicationDto, ClusterPublic, ClusterPoolPublic } from '@/types';
const steps = ['Basic Info', 'Runtime & Database', 'Resources', 'Review'];
@@ -34,8 +34,19 @@ export default function DeployPage() {
const [zipFile, setZipFile] = useState<File | null>(null);
const [uploadProgress, setUploadProgress] = useState(0);
const [isDragging, setIsDragging] = useState(false);
const [clusterMode, setClusterMode] = useState<'default' | 'manual' | 'pool'>('default');
const fileInputRef = useRef<HTMLInputElement>(null);
const { data: clusters = [] } = useQuery<ClusterPublic[]>({
queryKey: ['clusters-public'],
queryFn: () => api.get('/clusters/public').then((r) => r.data),
});
const { data: pools = [] } = useQuery<ClusterPoolPublic[]>({
queryKey: ['pools-public'],
queryFn: () => api.get('/clusters/pools/public').then((r) => r.data),
});
const createMutation = useMutation({
mutationFn: async (data: CreateApplicationDto) => {
const res = await api.post('/applications', data);
@@ -367,6 +378,170 @@ export default function DeployPage() {
{step === 2 && (
<div className="space-y-6">
<h2 className="text-lg font-semibold">Resources & Configuration</h2>
{/* Cluster Assignment Mode */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">Cluster Assignment</label>
<div className="grid grid-cols-3 gap-3 mb-4">
<button
type="button"
onClick={() => {
setClusterMode('default');
setForm({ ...form, clusterId: undefined, poolId: undefined });
}}
className={`p-3 rounded-xl border-2 text-center transition-colors ${
clusterMode === 'default'
? 'border-primary-500 bg-primary-50'
: 'border-gray-200 hover:border-gray-300'
}`}
>
<span className="text-xl">🏠</span>
<p className="mt-1 font-semibold text-sm text-gray-900">Default</p>
<p className="text-xs text-gray-500">Use default cluster</p>
</button>
<button
type="button"
onClick={() => {
setClusterMode('manual');
setForm({ ...form, poolId: undefined });
}}
className={`p-3 rounded-xl border-2 text-center transition-colors ${
clusterMode === 'manual'
? 'border-primary-500 bg-primary-50'
: 'border-gray-200 hover:border-gray-300'
}`}
>
<span className="text-xl">🎯</span>
<p className="mt-1 font-semibold text-sm text-gray-900">Manual</p>
<p className="text-xs text-gray-500">Pick a specific cluster</p>
</button>
<button
type="button"
onClick={() => {
setClusterMode('pool');
setForm({ ...form, clusterId: undefined });
}}
className={`p-3 rounded-xl border-2 text-center transition-colors ${
clusterMode === 'pool'
? 'border-primary-500 bg-primary-50'
: 'border-gray-200 hover:border-gray-300'
}`}
>
<span className="text-xl"></span>
<p className="mt-1 font-semibold text-sm text-gray-900">Load Balanced</p>
<p className="text-xs text-gray-500">Pick a cluster pool</p>
</button>
</div>
{/* Manual: show cluster list */}
{clusterMode === 'manual' && (
<div className="space-y-2">
{clusters.length === 0 ? (
<p className="text-sm text-gray-400 text-center py-4">No clusters available</p>
) : (
clusters.map((cluster) => (
<button
key={cluster.id}
type="button"
onClick={() => setForm({ ...form, clusterId: cluster.id })}
className={`w-full p-3 rounded-xl border-2 text-left transition-colors ${
form.clusterId === cluster.id
? 'border-primary-500 bg-primary-50'
: 'border-gray-200 hover:border-gray-300'
}`}
>
<div className="flex items-center justify-between">
<div className="flex items-center space-x-3">
<span className="text-lg">🖥</span>
<div>
<p className="font-semibold text-sm text-gray-900">
{cluster.name}
{cluster.isDefault && (
<span className="ml-2 text-xs bg-blue-100 text-blue-700 px-1.5 py-0.5 rounded">Default</span>
)}
</p>
<p className="text-xs text-gray-500">
{[cluster.provider, cluster.region].filter(Boolean).join(' · ') || 'No region info'}
</p>
</div>
</div>
<span className={`px-2 py-0.5 rounded-full text-xs font-medium ${
cluster.status === 'active' ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-500'
}`}>
{cluster.status}
</span>
</div>
</button>
))
)}
</div>
)}
{/* Pool: show pool list */}
{clusterMode === 'pool' && (
<div className="space-y-2">
{pools.length === 0 ? (
<div className="text-center py-4">
<p className="text-sm text-gray-400">No cluster pools configured</p>
<p className="text-xs text-gray-400 mt-1">Ask your admin to create a cluster pool</p>
</div>
) : (
pools.map((pool) => (
<button
key={pool.id}
type="button"
onClick={() => setForm({ ...form, poolId: pool.id })}
className={`w-full p-3 rounded-xl border-2 text-left transition-colors ${
form.poolId === pool.id
? 'border-primary-500 bg-primary-50'
: 'border-gray-200 hover:border-gray-300'
}`}
>
<div className="flex items-center justify-between">
<div className="flex items-center space-x-3">
<span className="text-lg"></span>
<div>
<p className="font-semibold text-sm text-gray-900">{pool.name}</p>
{pool.description && (
<p className="text-xs text-gray-500">{pool.description}</p>
)}
<div className="flex items-center space-x-2 mt-1">
<span className="text-xs bg-purple-100 text-purple-700 px-1.5 py-0.5 rounded">
{pool.strategy === 'least-apps' ? '📊 Least Apps' : '🔄 Round Robin'}
</span>
<span className="text-xs text-gray-400">
{pool.clusters.length} cluster{pool.clusters.length !== 1 ? 's' : ''}:
{' '}{pool.clusters.map((c) => c.name).join(', ')}
</span>
</div>
</div>
</div>
</div>
</button>
))
)}
</div>
)}
{/* Default: info text */}
{clusterMode === 'default' && (
<div className="p-3 bg-gray-50 rounded-xl border border-gray-200">
<div className="flex items-center space-x-3">
<span className="text-lg">🏠</span>
<div>
<p className="text-sm font-medium text-gray-700">Default cluster will be used</p>
<p className="text-xs text-gray-500">
Your app will be deployed to the platform&apos;s default cluster
{clusters.find((c) => c.isDefault) && (
<> <strong>{clusters.find((c) => c.isDefault)?.name}</strong></>
)}
</p>
</div>
</div>
</div>
)}
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">CPU Request</label>
@@ -497,6 +672,16 @@ export default function DeployPage() {
<span className="text-sm font-medium text-green-600">🔑 Token provided</span>
</div>
)}
<div className="flex justify-between">
<span className="text-sm text-gray-500">Cluster</span>
<span className="text-sm font-medium">
{clusterMode === 'manual' && form.clusterId
? `🎯 ${clusters.find((c) => c.id === form.clusterId)?.name || form.clusterId}`
: clusterMode === 'pool' && form.poolId
? `⚖️ ${pools.find((p) => p.id === form.poolId)?.name || 'Pool'} (Load Balanced)`
: '🏠 Default Cluster'}
</span>
</div>
<div className="flex justify-between">
<span className="text-sm text-gray-500">CPU</span>
<span className="text-sm font-medium">{form.cpuRequest} / {form.cpuLimit}</span>