feat: K8s集群监控数据展示 — 修复详情标签数据加载 + 补充Pod/Deployment/Service/Ingress种子数据 + deployment_count字段
This commit is contained in:
1 parent
1244a7cc6e
commit
8d47f8d86c
4 files changed
+340
-25
No files matched your search
+140
-4
@@ -160,10 +160,10 @@ func generateDomains(db *gorm.DB) error {
|
||||
|
||||
func generateK8sClusters(db *gorm.DB) error {
|
||||
clusters := []models.K8sCluster{
|
||||
{Name: "prod-cluster", Code: "prod", Version: "v1.28.0", Status: "healthy", NodeCount: 10, PodCount: 150, ServiceCount: 30, APIEndpoint: "https://prod-api.ezops.local:6443", CPUUsage: 65.5, MemoryUsage: 72.3, Description: "生产环境 Kubernetes 集群"},
|
||||
{Name: "staging-cluster", Code: "staging", Version: "v1.28.0", Status: "healthy", NodeCount: 5, PodCount: 80, ServiceCount: 15, APIEndpoint: "https://staging-api.ezops.local:6443", CPUUsage: 45.2, MemoryUsage: 55.0, Description: "预发环境 Kubernetes 集群"},
|
||||
{Name: "dev-cluster", Code: "dev", Version: "v1.27.0", Status: "warning", NodeCount: 3, PodCount: 45, ServiceCount: 8, APIEndpoint: "https://dev-api.ezops.local:6443", CPUUsage: 80.1, MemoryUsage: 85.0, Description: "开发环境 Kubernetes 集群"},
|
||||
{Name: "test-cluster", Code: "test", Version: "v1.27.0", Status: "error", NodeCount: 2, PodCount: 20, ServiceCount: 5, APIEndpoint: "https://test-api.ezops.local:6443", CPUUsage: 30.0, MemoryUsage: 40.0, Description: "测试环境 Kubernetes 集群"},
|
||||
{Name: "prod-cluster", Code: "prod", Version: "v1.28.0", Status: "healthy", NodeCount: 10, PodCount: 150, ServiceCount: 30, DeploymentCount: 15, APIEndpoint: "https://prod-api.ezops.local:6443", CPUUsage: 65.5, MemoryUsage: 72.3, Description: "生产环境 Kubernetes 集群"},
|
||||
{Name: "staging-cluster", Code: "staging", Version: "v1.28.0", Status: "healthy", NodeCount: 5, PodCount: 80, ServiceCount: 15, DeploymentCount: 10, APIEndpoint: "https://staging-api.ezops.local:6443", CPUUsage: 45.2, MemoryUsage: 55.0, Description: "预发环境 Kubernetes 集群"},
|
||||
{Name: "dev-cluster", Code: "dev", Version: "v1.27.0", Status: "warning", NodeCount: 3, PodCount: 45, ServiceCount: 8, DeploymentCount: 8, APIEndpoint: "https://dev-api.ezops.local:6443", CPUUsage: 80.1, MemoryUsage: 85.0, Description: "开发环境 Kubernetes 集群"},
|
||||
{Name: "test-cluster", Code: "test", Version: "v1.27.0", Status: "error", NodeCount: 2, PodCount: 20, ServiceCount: 5, DeploymentCount: 5, APIEndpoint: "https://test-api.ezops.local:6443", CPUUsage: 30.0, MemoryUsage: 40.0, Description: "测试环境 Kubernetes 集群"},
|
||||
}
|
||||
for i := range clusters {
|
||||
if err := db.Create(&clusters[i]).Error; err != nil {
|
||||
@@ -171,6 +171,7 @@ func generateK8sClusters(db *gorm.DB) error {
|
||||
}
|
||||
}
|
||||
for _, cluster := range clusters {
|
||||
// 生成节点
|
||||
for j := 0; j < cluster.NodeCount; j++ {
|
||||
node := models.K8sNode{
|
||||
ClusterID: cluster.ID,
|
||||
@@ -193,6 +194,141 @@ func generateK8sClusters(db *gorm.DB) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 Deployments
|
||||
depImages := []string{
|
||||
"nginx:1.25.3", "redis:7.2-alpine", "mysql:8.0.35",
|
||||
"kafka:3.6.0", "elasticsearch:8.11.0", "grafana/grafana:10.2.0",
|
||||
"prom/prometheus:v2.48.0", "busybox:1.36", "easzlab/kubeasz:3.6.1",
|
||||
}
|
||||
depNames := []string{
|
||||
"nginx-deploy", "redis-cluster", "mysql-master", "mysql-slave",
|
||||
"kafka-broker", "es-cluster", "grafana", "prometheus",
|
||||
"filebeat", "logstash", "jaeger", "zipkin",
|
||||
"frontend", "backend-api", "gateway",
|
||||
}
|
||||
namespaces := []string{"default", "kube-system", "monitoring", "ingress-nginx", "production"}
|
||||
numDeps := cluster.DeploymentCount
|
||||
if numDeps < 5 {
|
||||
numDeps = 5 + rand.Intn(10)
|
||||
}
|
||||
for d := 0; d < numDeps && d < len(depNames); d++ {
|
||||
replicas := []int{1, 2, 3, 5}[rand.Intn(4)]
|
||||
available := replicas
|
||||
if rand.Intn(4) == 0 {
|
||||
available = replicas - 1
|
||||
}
|
||||
if available < 0 {
|
||||
available = 0
|
||||
}
|
||||
dep := models.K8sDeployment{
|
||||
ClusterID: cluster.ID,
|
||||
Namespace: namespaces[rand.Intn(len(namespaces))],
|
||||
Name: depNames[d],
|
||||
Replicas: replicas,
|
||||
AvailableReplicas: available,
|
||||
ReadyReplicas: available,
|
||||
Image: depImages[rand.Intn(len(depImages))],
|
||||
Strategy: "RollingUpdate",
|
||||
}
|
||||
if err := db.Create(&dep).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 Pods
|
||||
podStatuses := []string{"Running", "Running", "Running", "Running", "Pending", "CrashLoopBackOff"}
|
||||
numPods := cluster.PodCount
|
||||
if numPods > 30 {
|
||||
numPods = 30
|
||||
}
|
||||
if numPods < 10 {
|
||||
numPods = 10 + rand.Intn(15)
|
||||
}
|
||||
for p := 0; p < numPods; p++ {
|
||||
status := podStatuses[rand.Intn(len(podStatuses))]
|
||||
pod := models.K8sPod{
|
||||
ClusterID: cluster.ID,
|
||||
Namespace: namespaces[rand.Intn(len(namespaces))],
|
||||
Name: fmt.Sprintf("%s-pod-%d", cluster.Code, p+1),
|
||||
Status: status,
|
||||
NodeName: fmt.Sprintf("%s-node-%d", cluster.Code, rand.Intn(cluster.NodeCount)+1),
|
||||
IP: fmt.Sprintf("10.%d.%d.%d", rand.Intn(256), rand.Intn(256), rand.Intn(256)),
|
||||
RestartCount: rand.Intn(10),
|
||||
Image: depImages[rand.Intn(len(depImages))],
|
||||
CPUUsage: rand.Float64() * 500,
|
||||
MemoryUsage: rand.Float64() * 2048,
|
||||
}
|
||||
if err := db.Create(&pod).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 Services
|
||||
svcTypes := []string{"ClusterIP", "NodePort", "LoadBalancer"}
|
||||
svcNames := []string{
|
||||
"nginx-svc", "redis-svc", "mysql-svc", "kafka-svc",
|
||||
"es-svc", "grafana-svc", "prometheus-svc",
|
||||
"frontend-svc", "backend-svc", "gateway-svc",
|
||||
}
|
||||
numSvcs := cluster.ServiceCount
|
||||
if numSvcs > 10 {
|
||||
numSvcs = 10
|
||||
}
|
||||
if numSvcs < 5 {
|
||||
numSvcs = 5 + rand.Intn(5)
|
||||
}
|
||||
for s := 0; s < numSvcs && s < len(svcNames); s++ {
|
||||
svcType := svcTypes[rand.Intn(len(svcTypes))]
|
||||
ports := "[]"
|
||||
switch rand.Intn(4) {
|
||||
case 0:
|
||||
ports = `[{"port":80,"protocol":"TCP","targetPort":80}]`
|
||||
case 1:
|
||||
ports = `[{"port":443,"protocol":"TCP","targetPort":8443}]`
|
||||
case 2:
|
||||
ports = `[{"port":8080,"protocol":"TCP","targetPort":8080},{"port":9090,"protocol":"TCP","targetPort":9090}]`
|
||||
default:
|
||||
ports = `[{"port":3306,"protocol":"TCP","targetPort":3306}]`
|
||||
}
|
||||
svc := models.K8sService{
|
||||
ClusterID: cluster.ID,
|
||||
Namespace: namespaces[rand.Intn(len(namespaces))],
|
||||
Name: svcNames[s],
|
||||
Type: svcType,
|
||||
ClusterIP: fmt.Sprintf("10.96.%d.%d", rand.Intn(256), rand.Intn(256)),
|
||||
ExternalIP: "",
|
||||
Ports: ports,
|
||||
Selector: fmt.Sprintf(`{"app":"%s"}`, svcNames[s]),
|
||||
}
|
||||
if svcType == "LoadBalancer" {
|
||||
svc.ExternalIP = fmt.Sprintf("203.0.113.%d", rand.Intn(256))
|
||||
}
|
||||
if svcType == "NodePort" {
|
||||
svc.ExternalIP = fmt.Sprintf("%d-%d", 30000+rand.Intn(1000), 32767)
|
||||
}
|
||||
if err := db.Create(&svc).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 Ingresses
|
||||
ingNames := []string{"main-ingress", "api-ingress", "grafana-ingress", "canary-ingress"}
|
||||
numIngs := 2 + rand.Intn(3)
|
||||
for ig := 0; ig < numIngs && ig < len(ingNames); ig++ {
|
||||
hosts := fmt.Sprintf(`["%s.ezops.local"]`, ingNames[ig])
|
||||
ing := models.K8sIngress{
|
||||
ClusterID: cluster.ID,
|
||||
Namespace: namespaces[rand.Intn(len(namespaces))],
|
||||
Name: ingNames[ig],
|
||||
Hosts: hosts,
|
||||
Backend: fmt.Sprintf("%s-svc:80", ingNames[ig]),
|
||||
TLS: rand.Intn(2) == 0,
|
||||
}
|
||||
if err := db.Create(&ing).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Generated K8s clusters:", len(clusters))
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user