Files
aiops/backend/internal/models/k8s.go
T
attacker 00d5510366 feat: 完善告警规则配置页面 — 增删改查弹窗 + 修复白屏bug + UpdateRule notify_channels序列化修复
- AlertManager: 新建/编辑告警规则弹窗(规则名称/描述/类型/数据源/表达式/级别/持续时间/通知渠道Switch)
- AlertManager: 修复白屏bug — getRules返回{list,total}未解包导致rules.map崩溃(Frontend Pitfall #1)
- AlertManager: 修复规则列表字段显示 — 原引用不存在的rule.metric/operator/threshold字段,改为rule.source/expression
- AlertManager: 新增规则删除按钮(带确认)
- alert.go: 修复UpdateRule notify_channels序列化bug — 原直接赋值[]string到text列导致500,改用toJSON()
- 同步提交前序未提交改动(WebShell/K8s/Server/Login等组件改进)
2026-06-14 03:07:44 +08:00

195 lines
7.5 KiB
Go

package models
import (
"time"
"gorm.io/gorm"
)
// K8sCluster K8s集群模型
type K8sCluster struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"size:128;not null" json:"name"`
Code string `gorm:"size:64;uniqueIndex;not null" json:"code"`
Version string `gorm:"size:32" json:"version"`
Status string `gorm:"size:32;default:healthy" json:"status"` // healthy, warning, error
ConfigType int `gorm:"default:1" json:"config_type"` // 0=token, 1=kubeconfig
APIServer string `gorm:"size:255" json:"api_server"`
APIEndpoint string `gorm:"size:255" json:"api_endpoint"`
Token string `gorm:"type:text" json:"-"`
KubeConfig string `gorm:"type:text" json:"-"`
NodeCount int `gorm:"default:0" json:"node_count"`
PodCount int `gorm:"default:0" json:"pod_count"`
ServiceCount int `gorm:"default:0" json:"service_count"`
CPUUsage float64 `json:"cpu_usage"`
MemoryUsage float64 `json:"memory_usage"`
Description string `gorm:"size:500" json:"description"`
Labels string `gorm:"type:text" json:"labels"` // JSON
LastSyncAt *time.Time `json:"last_sync_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
// TableName 表名
func (K8sCluster) TableName() string {
return "k8s_clusters"
}
// K8sNode K8s节点模型
type K8sNode struct {
ID uint `gorm:"primarykey" json:"id"`
ClusterID uint `gorm:"not null" json:"cluster_id"`
Name string `gorm:"size:128;not null" json:"name"`
IP string `gorm:"size:64" json:"ip"`
Role string `gorm:"size:32" json:"role"` // master, worker
Status string `gorm:"size:32" json:"status"`
OS string `gorm:"size:64" json:"os"`
KubeletVer string `gorm:"size:32" json:"kubelet_version"`
CPU int `json:"cpu"`
Memory int64 `json:"memory"`
Disk int64 `json:"disk"`
CPUUsage float64 `json:"cpu_usage"`
MemoryUsage float64 `json:"memory_usage"`
DiskUsage float64 `json:"disk_usage"`
PodCount int `json:"pod_count"`
MaxPods int `json:"max_pods"`
Labels string `gorm:"type:text" json:"labels"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// 关联
Cluster *K8sCluster `json:"cluster,omitempty"`
}
// TableName 表名
func (K8sNode) TableName() string {
return "k8s_nodes"
}
// K8sPod K8s Pod模型
type K8sPod struct {
ID uint `gorm:"primarykey" json:"id"`
ClusterID uint `gorm:"not null" json:"cluster_id"`
Namespace string `gorm:"size:64;not null" json:"namespace"`
Name string `gorm:"size:128;not null" json:"name"`
Status string `gorm:"size:32" json:"status"` // Running, Pending, Succeeded, Failed
NodeName string `gorm:"size:128" json:"node_name"`
IP string `gorm:"size:64" json:"ip"`
RestartCount int `json:"restart_count"`
Image string `gorm:"size:255" json:"image"`
CPUUsage float64 `json:"cpu_usage"`
MemoryUsage float64 `json:"memory_usage"`
Labels string `gorm:"type:text" json:"labels"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// 关联
Cluster *K8sCluster `json:"cluster,omitempty"`
}
// TableName 表名
func (K8sPod) TableName() string {
return "k8s_pods"
}
// K8sDeployment K8s Deployment模型
type K8sDeployment struct {
ID uint `gorm:"primarykey" json:"id"`
ClusterID uint `gorm:"not null" json:"cluster_id"`
Namespace string `gorm:"size:64;not null" json:"namespace"`
Name string `gorm:"size:128;not null" json:"name"`
Replicas int `json:"replicas"`
AvailableReplicas int `json:"available_replicas"`
ReadyReplicas int `json:"ready_replicas"`
Image string `gorm:"size:255" json:"image"`
Strategy string `gorm:"size:32" json:"strategy"`
Labels string `gorm:"type:text" json:"labels"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// 关联
Cluster *K8sCluster `json:"cluster,omitempty"`
}
// TableName 表名
func (K8sDeployment) TableName() string {
return "k8s_deployments"
}
// K8sService K8s Service模型
type K8sService struct {
ID uint `gorm:"primarykey" json:"id"`
ClusterID uint `gorm:"not null" json:"cluster_id"`
Namespace string `gorm:"size:64;not null" json:"namespace"`
Name string `gorm:"size:128;not null" json:"name"`
Type string `gorm:"size:32" json:"type"` // ClusterIP, NodePort, LoadBalancer
ClusterIP string `gorm:"size:64" json:"cluster_ip"`
ExternalIP string `gorm:"size:255" json:"external_ip"`
Ports string `gorm:"type:text" json:"ports"` // JSON
Selector string `gorm:"type:text" json:"selector"`
Labels string `gorm:"type:text" json:"labels"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// 关联
Cluster *K8sCluster `json:"cluster,omitempty"`
}
// TableName 表名
func (K8sService) TableName() string {
return "k8s_services"
}
// K8sIngress K8s Ingress模型
type K8sIngress struct {
ID uint `gorm:"primarykey" json:"id"`
ClusterID uint `gorm:"not null" json:"cluster_id"`
Namespace string `gorm:"size:64;not null" json:"namespace"`
Name string `gorm:"size:128;not null" json:"name"`
Hosts string `gorm:"type:text" json:"hosts"` // JSON
Backend string `gorm:"size:255" json:"backend"`
TLS bool `json:"tls"`
TLSHosts string `gorm:"type:text" json:"tls_hosts"`
Labels string `gorm:"type:text" json:"labels"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// 关联
Cluster *K8sCluster `json:"cluster,omitempty"`
}
// TableName 表名
func (K8sIngress) TableName() string {
return "k8s_ingresses"
}
// CreateK8sClusterRequest 创建K8s集群请求
type CreateK8sClusterRequest struct {
Name string `json:"name" binding:"required"`
Code string `json:"code" binding:"required"`
Version string `json:"version"`
ConfigType int `json:"config_type"` // 0=token, 1=kubeconfig
APIServer string `json:"api_server"`
APIEndpoint string `json:"api_endpoint"`
Token string `json:"token"`
KubeConfig string `json:"kube_config"`
Description string `json:"description"`
}
// K8sClusterStats K8s集群统计
type K8sClusterStats struct {
Total int64 `json:"total"`
Healthy int64 `json:"healthy"`
Warning int64 `json:"warning"`
Error int64 `json:"error"`
TotalNodes int64 `json:"total_nodes"`
TotalPods int64 `json:"total_pods"`
TotalServices int64 `json:"total_services"`
}