- kubernetes/: 30Gi PVC、单副本 Recreate、Secret/Service/NodePort/Ingress 清单与部署文档 - 数据安全: kubeconfig/token 统一加密存储,启动迁移历史明文,DB 0600 - Token 接入: 命名空间级最小权限提示、长期 Token、自签证书 TLS 兼容 - 部署: 命名空间级 Token 下跳过 Namespace patch/枚举,节点信息容错 - 日志页: namespace → app → pod 选择顺序与作用域收窄 - 设置页: 拆分为通用/告警/LDAP/镜像源四个 tab - 弹框: 统一 max-h 滚动与长命令换行,修复内容溢出
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"time"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"opsxloki/models"
|
|
)
|
|
|
|
type ClusterDeploymentOption struct {
|
|
Name string `json:"name"`
|
|
Replicas int `json:"replicas"`
|
|
ReadyReplicas int `json:"readyReplicas"`
|
|
}
|
|
|
|
var kubernetesNamespacePattern = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
|
|
|
|
func ListClusterNamespaces(ctx context.Context, cluster *models.Cluster) ([]string, error) {
|
|
client, err := clusterClient(cluster, 10*time.Second)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
if apierrors.IsForbidden(err) {
|
|
// 命名空间级 Token 无权枚举命名空间:返回空列表,由用户手填
|
|
return []string{}, nil
|
|
}
|
|
return nil, fmt.Errorf("读取集群命名空间失败: %w", err)
|
|
}
|
|
result := make([]string, 0, len(items.Items))
|
|
for _, item := range items.Items {
|
|
result = append(result, item.Name)
|
|
}
|
|
sort.Strings(result)
|
|
return result, nil
|
|
}
|
|
|
|
func ListClusterDeployments(ctx context.Context, cluster *models.Cluster, namespace string) ([]ClusterDeploymentOption, error) {
|
|
if len(namespace) > 63 || !kubernetesNamespacePattern.MatchString(namespace) {
|
|
return nil, fmt.Errorf("命名空间名称无效")
|
|
}
|
|
client, err := clusterClient(cluster, 10*time.Second)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items, err := client.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("读取集群 Deployment 失败: %w", err)
|
|
}
|
|
result := make([]ClusterDeploymentOption, 0, len(items.Items))
|
|
for _, item := range items.Items {
|
|
replicas := int32(1)
|
|
if item.Spec.Replicas != nil {
|
|
replicas = *item.Spec.Replicas
|
|
}
|
|
result = append(result, ClusterDeploymentOption{
|
|
Name: item.Name, Replicas: int(replicas), ReadyReplicas: int(item.Status.ReadyReplicas),
|
|
})
|
|
}
|
|
sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name })
|
|
return result, nil
|
|
}
|