fix: scale replicas in response, K8s metrics client, quota precheck, auth tests
- Add GetMetrics method to MetricsClient interface and implement cluster metrics API - Add QuotaPrecheck service for validating resource quotas before deployment - Add auth DTO with role/permission models and auth handler tests - Add instance diagnostics: mounted NFS volumes, labels, annotations in pod diagnostics - Update workspace handler with GetWorkspace endpoint and shared-user list - Fix monitoring handler to use correct service method name - Add tail_lines fallback in instance handler for snake_case query params - Update nginx config for SSE log streaming support (no buffering) - Add comprehensive test coverage: auth_service_test, auth_handler_test, auth_dto_test, metrics_client_test, quota_precheck_test - Update error messages for quota validation and instance operations - ModifyModal: fix YAML lineWidth:0, modified keys summary, delta-only submit - InstanceCard: correctly disable scale-minus when replicas <= 0 - SidebarLayout: add hover transition for sidebar items - Update todo.md and lessons.md with latest fixes
This commit is contained in:
@ -1,19 +1,47 @@
|
||||
package dto
|
||||
|
||||
import "strings"
|
||||
|
||||
// RegisterRequest 用户注册请求
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
Role string `json:"role,omitempty"`
|
||||
WorkspaceID string `json:"workspaceId,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
DefaultClusterID string `json:"defaultClusterId,omitempty"`
|
||||
QuotaCPU string `json:"quotaCpu,omitempty"`
|
||||
QuotaMemory string `json:"quotaMemory,omitempty"`
|
||||
QuotaGPU string `json:"quotaGpu,omitempty"`
|
||||
QuotaGPUMem string `json:"quotaGpuMemory,omitempty"`
|
||||
IsActive *bool `json:"isActive,omitempty"`
|
||||
MustChangePassword *bool `json:"mustChangePassword,omitempty"`
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
Role string `json:"role,omitempty"`
|
||||
WorkspaceID string `json:"workspaceId,omitempty"`
|
||||
WorkspaceIDSnake string `json:"workspace_id,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
DefaultClusterID string `json:"defaultClusterId,omitempty"`
|
||||
DefaultClusterIDSnake string `json:"default_cluster_id,omitempty"`
|
||||
QuotaCPU string `json:"quotaCpu,omitempty"`
|
||||
QuotaCPUSnake string `json:"quota_cpu,omitempty"`
|
||||
QuotaMemory string `json:"quotaMemory,omitempty"`
|
||||
QuotaMemorySnake string `json:"quota_memory,omitempty"`
|
||||
QuotaGPU string `json:"quotaGpu,omitempty"`
|
||||
QuotaGPUSnake string `json:"quota_gpu,omitempty"`
|
||||
QuotaGPUMem string `json:"quotaGpuMemory,omitempty"`
|
||||
QuotaGPUMemSnake string `json:"quota_gpu_memory,omitempty"`
|
||||
IsActive *bool `json:"isActive,omitempty"`
|
||||
IsActiveSnake *bool `json:"is_active,omitempty"`
|
||||
MustChangePassword *bool `json:"mustChangePassword,omitempty"`
|
||||
MustChangePasswordSnake *bool `json:"must_change_password,omitempty"`
|
||||
}
|
||||
|
||||
func (r *RegisterRequest) Normalize() {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
r.WorkspaceID = firstNonBlank(r.WorkspaceID, r.WorkspaceIDSnake)
|
||||
r.DefaultClusterID = firstNonBlank(r.DefaultClusterID, r.DefaultClusterIDSnake)
|
||||
r.QuotaCPU = firstNonBlank(r.QuotaCPU, r.QuotaCPUSnake)
|
||||
r.QuotaMemory = firstNonBlank(r.QuotaMemory, r.QuotaMemorySnake)
|
||||
r.QuotaGPU = firstNonBlank(r.QuotaGPU, r.QuotaGPUSnake)
|
||||
r.QuotaGPUMem = firstNonBlank(r.QuotaGPUMem, r.QuotaGPUMemSnake)
|
||||
if r.IsActive == nil {
|
||||
r.IsActive = r.IsActiveSnake
|
||||
}
|
||||
if r.MustChangePassword == nil {
|
||||
r.MustChangePassword = r.MustChangePasswordSnake
|
||||
}
|
||||
}
|
||||
|
||||
// LoginRequest 用户登录请求
|
||||
@ -68,14 +96,47 @@ type UserResponse struct {
|
||||
|
||||
// UpdateUserRequest 管理员更新用户状态/角色请求
|
||||
type UpdateUserRequest struct {
|
||||
Role string `json:"role,omitempty"`
|
||||
WorkspaceID string `json:"workspaceId,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
DefaultClusterID string `json:"defaultClusterId,omitempty"`
|
||||
QuotaCPU string `json:"quotaCpu,omitempty"`
|
||||
QuotaMemory string `json:"quotaMemory,omitempty"`
|
||||
QuotaGPU string `json:"quotaGpu,omitempty"`
|
||||
QuotaGPUMem string `json:"quotaGpuMemory,omitempty"`
|
||||
IsActive *bool `json:"isActive,omitempty"`
|
||||
MustChangePassword *bool `json:"mustChangePassword,omitempty"`
|
||||
Role string `json:"role,omitempty"`
|
||||
WorkspaceID string `json:"workspaceId,omitempty"`
|
||||
WorkspaceIDSnake string `json:"workspace_id,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
DefaultClusterID string `json:"defaultClusterId,omitempty"`
|
||||
DefaultClusterIDSnake string `json:"default_cluster_id,omitempty"`
|
||||
QuotaCPU string `json:"quotaCpu,omitempty"`
|
||||
QuotaCPUSnake string `json:"quota_cpu,omitempty"`
|
||||
QuotaMemory string `json:"quotaMemory,omitempty"`
|
||||
QuotaMemorySnake string `json:"quota_memory,omitempty"`
|
||||
QuotaGPU string `json:"quotaGpu,omitempty"`
|
||||
QuotaGPUSnake string `json:"quota_gpu,omitempty"`
|
||||
QuotaGPUMem string `json:"quotaGpuMemory,omitempty"`
|
||||
QuotaGPUMemSnake string `json:"quota_gpu_memory,omitempty"`
|
||||
IsActive *bool `json:"isActive,omitempty"`
|
||||
IsActiveSnake *bool `json:"is_active,omitempty"`
|
||||
MustChangePassword *bool `json:"mustChangePassword,omitempty"`
|
||||
MustChangePasswordSnake *bool `json:"must_change_password,omitempty"`
|
||||
}
|
||||
|
||||
func (r *UpdateUserRequest) Normalize() {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
r.WorkspaceID = firstNonBlank(r.WorkspaceID, r.WorkspaceIDSnake)
|
||||
r.DefaultClusterID = firstNonBlank(r.DefaultClusterID, r.DefaultClusterIDSnake)
|
||||
r.QuotaCPU = firstNonBlank(r.QuotaCPU, r.QuotaCPUSnake)
|
||||
r.QuotaMemory = firstNonBlank(r.QuotaMemory, r.QuotaMemorySnake)
|
||||
r.QuotaGPU = firstNonBlank(r.QuotaGPU, r.QuotaGPUSnake)
|
||||
r.QuotaGPUMem = firstNonBlank(r.QuotaGPUMem, r.QuotaGPUMemSnake)
|
||||
if r.IsActive == nil {
|
||||
r.IsActive = r.IsActiveSnake
|
||||
}
|
||||
if r.MustChangePassword == nil {
|
||||
r.MustChangePassword = r.MustChangePasswordSnake
|
||||
}
|
||||
}
|
||||
|
||||
func firstNonBlank(primary, alternate string) string {
|
||||
if strings.TrimSpace(primary) != "" {
|
||||
return primary
|
||||
}
|
||||
return alternate
|
||||
}
|
||||
|
||||
51
backend/internal/adapter/input/http/dto/auth_dto_test.go
Normal file
51
backend/internal/adapter/input/http/dto/auth_dto_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package dto
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRegisterRequestNormalizeUsesSnakeCaseAlternates(t *testing.T) {
|
||||
active := false
|
||||
mustChange := true
|
||||
req := RegisterRequest{
|
||||
WorkspaceIDSnake: "workspace-1",
|
||||
DefaultClusterIDSnake: "cluster-1",
|
||||
QuotaCPUSnake: "2",
|
||||
QuotaMemorySnake: "4Gi",
|
||||
QuotaGPUSnake: "1",
|
||||
QuotaGPUMemSnake: "10000",
|
||||
IsActiveSnake: &active,
|
||||
MustChangePasswordSnake: &mustChange,
|
||||
}
|
||||
|
||||
req.Normalize()
|
||||
|
||||
if req.WorkspaceID != "workspace-1" || req.DefaultClusterID != "cluster-1" {
|
||||
t.Fatalf("expected snake case workspace/cluster fields to normalize, got %#v", req)
|
||||
}
|
||||
if req.QuotaCPU != "2" || req.QuotaMemory != "4Gi" || req.QuotaGPU != "1" || req.QuotaGPUMem != "10000" {
|
||||
t.Fatalf("expected snake case quota fields to normalize, got %#v", req)
|
||||
}
|
||||
if req.IsActive == nil || *req.IsActive {
|
||||
t.Fatalf("expected is_active=false to normalize, got %#v", req.IsActive)
|
||||
}
|
||||
if req.MustChangePassword == nil || !*req.MustChangePassword {
|
||||
t.Fatalf("expected must_change_password=true to normalize, got %#v", req.MustChangePassword)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateUserRequestNormalizeKeepsCamelCasePrimary(t *testing.T) {
|
||||
req := UpdateUserRequest{
|
||||
DefaultClusterID: "camel-cluster",
|
||||
DefaultClusterIDSnake: "snake-cluster",
|
||||
QuotaCPU: "3",
|
||||
QuotaCPUSnake: "4",
|
||||
}
|
||||
|
||||
req.Normalize()
|
||||
|
||||
if req.DefaultClusterID != "camel-cluster" {
|
||||
t.Fatalf("expected camelCase defaultClusterId to win, got %q", req.DefaultClusterID)
|
||||
}
|
||||
if req.QuotaCPU != "3" {
|
||||
t.Fatalf("expected camelCase quotaCpu to win, got %q", req.QuotaCPU)
|
||||
}
|
||||
}
|
||||
@ -2,25 +2,25 @@ package dto
|
||||
|
||||
// CreateInstanceRequest 创建实例请求
|
||||
type CreateInstanceRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Namespace string `json:"namespace" binding:"required"`
|
||||
RegistryID string `json:"registryId" binding:"required"`
|
||||
RegistryIDAlt string `json:"registry_id"`
|
||||
Repository string `json:"repository" binding:"required"`
|
||||
Tag string `json:"tag" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Values map[string]interface{} `json:"values"`
|
||||
ValuesYAML string `json:"valuesYaml"`
|
||||
ValuesYAMLAlt string `json:"values_yaml"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Namespace string `json:"namespace" binding:"required"`
|
||||
RegistryID string `json:"registryId" binding:"required"`
|
||||
RegistryIDAlt string `json:"registry_id"`
|
||||
Repository string `json:"repository" binding:"required"`
|
||||
Tag string `json:"tag" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Values map[string]interface{} `json:"values"`
|
||||
ValuesYAML string `json:"valuesYaml"`
|
||||
ValuesYAMLAlt string `json:"values_yaml"`
|
||||
}
|
||||
|
||||
// UpdateInstanceRequest 更新实例请求
|
||||
type UpdateInstanceRequest struct {
|
||||
Version string `json:"version"`
|
||||
Description string `json:"description"`
|
||||
Values map[string]interface{} `json:"values"`
|
||||
ValuesYAML string `json:"valuesYaml"`
|
||||
ValuesYAMLAlt string `json:"values_yaml"`
|
||||
Version string `json:"version"`
|
||||
Description string `json:"description"`
|
||||
Values map[string]interface{} `json:"values"`
|
||||
ValuesYAML string `json:"valuesYaml"`
|
||||
ValuesYAMLAlt string `json:"values_yaml"`
|
||||
}
|
||||
|
||||
// Normalize 将多种命名风格的字段合并到统一字段
|
||||
@ -67,6 +67,7 @@ type InstanceResponse struct {
|
||||
Status string `json:"status"`
|
||||
WorkspaceID string `json:"workspaceId"`
|
||||
OwnerID string `json:"ownerId"`
|
||||
OwnerUsername string `json:"ownerUsername,omitempty"`
|
||||
AllowedActions []string `json:"allowedActions,omitempty"`
|
||||
StatusReason string `json:"statusReason,omitempty"`
|
||||
LastOperation string `json:"lastOperation,omitempty"`
|
||||
|
||||
@ -8,29 +8,56 @@ import (
|
||||
|
||||
// ClusterMetricsResponse 集群监控响应
|
||||
type ClusterMetricsResponse struct {
|
||||
ClusterID string `json:"clusterId"`
|
||||
ClusterName string `json:"clusterName"`
|
||||
Status string `json:"status"`
|
||||
Uptime string `json:"uptime"`
|
||||
NodeCount int `json:"nodeCount"`
|
||||
PodCount int `json:"podCount"`
|
||||
LastCheck time.Time `json:"lastCheck"`
|
||||
TotalCPU string `json:"totalCpu"`
|
||||
TotalMemory string `json:"totalMemory"`
|
||||
TotalGPU int `json:"totalGpu"`
|
||||
UsedCPU string `json:"usedCpu"`
|
||||
UsedMemory string `json:"usedMemory"`
|
||||
UsedGPU int `json:"usedGpu"`
|
||||
CPUUsage float64 `json:"cpuUsage"`
|
||||
MemoryUsage float64 `json:"memoryUsage"`
|
||||
GPUUsage float64 `json:"gpuUsage"`
|
||||
MaxNodeCPU string `json:"maxNodeCpu"`
|
||||
MaxNodeMemory string `json:"maxNodeMemory"`
|
||||
MaxNodeGPU int `json:"maxNodeGpu"`
|
||||
MaxNodeCPUUsage float64 `json:"maxNodeCpuUsage"`
|
||||
MaxNodeMemUsage float64 `json:"maxNodeMemUsage"`
|
||||
MaxNodeGPUUsage float64 `json:"maxNodeGpuUsage"`
|
||||
Nodes []NodeMetricsResponse `json:"nodes,omitempty"`
|
||||
ClusterID string `json:"clusterId"`
|
||||
ClusterName string `json:"clusterName"`
|
||||
Status string `json:"status"`
|
||||
Uptime string `json:"uptime"`
|
||||
NodeCount int `json:"nodeCount"`
|
||||
PodCount int `json:"podCount"`
|
||||
LastCheck time.Time `json:"lastCheck"`
|
||||
TotalCPU string `json:"totalCpu"`
|
||||
TotalMemory string `json:"totalMemory"`
|
||||
TotalGPU int `json:"totalGpu"`
|
||||
UsedCPU string `json:"usedCpu"`
|
||||
UsedMemory string `json:"usedMemory"`
|
||||
UsedGPU int `json:"usedGpu"`
|
||||
CPUUsage float64 `json:"cpuUsage"`
|
||||
MemoryUsage float64 `json:"memoryUsage"`
|
||||
GPUUsage float64 `json:"gpuUsage"`
|
||||
CPURequests string `json:"cpuRequests,omitempty"`
|
||||
CPULimits string `json:"cpuLimits,omitempty"`
|
||||
MemoryRequests string `json:"memoryRequests,omitempty"`
|
||||
MemoryLimits string `json:"memoryLimits,omitempty"`
|
||||
GPURequests int64 `json:"gpuRequests,omitempty"`
|
||||
GPULimits int64 `json:"gpuLimits,omitempty"`
|
||||
GPUMemoryRequestsMB int64 `json:"gpuMemoryRequestsMb,omitempty"`
|
||||
GPUMemoryLimitsMB int64 `json:"gpuMemoryLimitsMb,omitempty"`
|
||||
AllocatedGPU int64 `json:"allocatedGpu,omitempty"`
|
||||
AllocatedGPUMemoryMB int64 `json:"allocatedGpuMemoryMb,omitempty"`
|
||||
ResourceUsageByUser []UserResourceUsageResponse `json:"resourceUsageByUser,omitempty"`
|
||||
MaxNodeCPU string `json:"maxNodeCpu"`
|
||||
MaxNodeMemory string `json:"maxNodeMemory"`
|
||||
MaxNodeGPU int `json:"maxNodeGpu"`
|
||||
MaxNodeCPUUsage float64 `json:"maxNodeCpuUsage"`
|
||||
MaxNodeMemUsage float64 `json:"maxNodeMemUsage"`
|
||||
MaxNodeGPUUsage float64 `json:"maxNodeGpuUsage"`
|
||||
Nodes []NodeMetricsResponse `json:"nodes,omitempty"`
|
||||
}
|
||||
|
||||
type UserResourceUsageResponse struct {
|
||||
UserID string `json:"userId"`
|
||||
Username string `json:"username"`
|
||||
WorkspaceID string `json:"workspaceId"`
|
||||
InstanceCount int `json:"instanceCount"`
|
||||
PodCount int `json:"podCount"`
|
||||
CPURequests string `json:"cpuRequests"`
|
||||
CPULimits string `json:"cpuLimits"`
|
||||
MemoryRequests string `json:"memoryRequests"`
|
||||
MemoryLimits string `json:"memoryLimits"`
|
||||
GPURequests int64 `json:"gpuRequests"`
|
||||
GPULimits int64 `json:"gpuLimits"`
|
||||
GPUMemoryRequestsMB int64 `json:"gpuMemoryRequestsMb"`
|
||||
GPUMemoryLimitsMB int64 `json:"gpuMemoryLimitsMb"`
|
||||
}
|
||||
|
||||
// NodeMetricsResponse 节点监控响应
|
||||
@ -72,28 +99,59 @@ type MonitoringSummaryResponse struct {
|
||||
// ToClusterMetricsResponse 转换为响应
|
||||
func ToClusterMetricsResponse(m *entity.ClusterMetrics) *ClusterMetricsResponse {
|
||||
resp := &ClusterMetricsResponse{
|
||||
ClusterID: m.ClusterID,
|
||||
ClusterName: m.ClusterName,
|
||||
Status: m.Status,
|
||||
Uptime: m.Uptime,
|
||||
NodeCount: m.NodeCount,
|
||||
PodCount: m.PodCount,
|
||||
LastCheck: m.LastCheck,
|
||||
TotalCPU: m.TotalCPU,
|
||||
TotalMemory: m.TotalMemory,
|
||||
TotalGPU: m.TotalGPU,
|
||||
UsedCPU: m.UsedCPU,
|
||||
UsedMemory: m.UsedMemory,
|
||||
UsedGPU: m.UsedGPU,
|
||||
CPUUsage: m.CPUUsage,
|
||||
MemoryUsage: m.MemoryUsage,
|
||||
GPUUsage: m.GPUUsage,
|
||||
MaxNodeCPU: m.MaxNodeCPU,
|
||||
MaxNodeMemory: m.MaxNodeMemory,
|
||||
MaxNodeGPU: m.MaxNodeGPU,
|
||||
MaxNodeCPUUsage: m.MaxNodeCPUUsage,
|
||||
MaxNodeMemUsage: m.MaxNodeMemUsage,
|
||||
MaxNodeGPUUsage: m.MaxNodeGPUUsage,
|
||||
ClusterID: m.ClusterID,
|
||||
ClusterName: m.ClusterName,
|
||||
Status: m.Status,
|
||||
Uptime: m.Uptime,
|
||||
NodeCount: m.NodeCount,
|
||||
PodCount: m.PodCount,
|
||||
LastCheck: m.LastCheck,
|
||||
TotalCPU: m.TotalCPU,
|
||||
TotalMemory: m.TotalMemory,
|
||||
TotalGPU: m.TotalGPU,
|
||||
UsedCPU: m.UsedCPU,
|
||||
UsedMemory: m.UsedMemory,
|
||||
UsedGPU: m.UsedGPU,
|
||||
CPUUsage: m.CPUUsage,
|
||||
MemoryUsage: m.MemoryUsage,
|
||||
GPUUsage: m.GPUUsage,
|
||||
CPURequests: m.CPURequests,
|
||||
CPULimits: m.CPULimits,
|
||||
MemoryRequests: m.MemoryRequests,
|
||||
MemoryLimits: m.MemoryLimits,
|
||||
GPURequests: m.GPURequests,
|
||||
GPULimits: m.GPULimits,
|
||||
GPUMemoryRequestsMB: m.GPUMemoryRequestsMB,
|
||||
GPUMemoryLimitsMB: m.GPUMemoryLimitsMB,
|
||||
AllocatedGPU: m.AllocatedGPU,
|
||||
AllocatedGPUMemoryMB: m.AllocatedGPUMemoryMB,
|
||||
MaxNodeCPU: m.MaxNodeCPU,
|
||||
MaxNodeMemory: m.MaxNodeMemory,
|
||||
MaxNodeGPU: m.MaxNodeGPU,
|
||||
MaxNodeCPUUsage: m.MaxNodeCPUUsage,
|
||||
MaxNodeMemUsage: m.MaxNodeMemUsage,
|
||||
MaxNodeGPUUsage: m.MaxNodeGPUUsage,
|
||||
}
|
||||
|
||||
if len(m.ResourceUsageByUser) > 0 {
|
||||
resp.ResourceUsageByUser = make([]UserResourceUsageResponse, len(m.ResourceUsageByUser))
|
||||
for i, usage := range m.ResourceUsageByUser {
|
||||
resp.ResourceUsageByUser[i] = UserResourceUsageResponse{
|
||||
UserID: usage.UserID,
|
||||
Username: usage.Username,
|
||||
WorkspaceID: usage.WorkspaceID,
|
||||
InstanceCount: usage.InstanceCount,
|
||||
PodCount: usage.PodCount,
|
||||
CPURequests: usage.CPURequests,
|
||||
CPULimits: usage.CPULimits,
|
||||
MemoryRequests: usage.MemoryRequests,
|
||||
MemoryLimits: usage.MemoryLimits,
|
||||
GPURequests: usage.GPURequests,
|
||||
GPULimits: usage.GPULimits,
|
||||
GPUMemoryRequestsMB: usage.GPUMemoryRequestsMB,
|
||||
GPUMemoryLimitsMB: usage.GPUMemoryLimitsMB,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(m.Nodes) > 0 {
|
||||
|
||||
Reference in New Issue
Block a user