- 品牌: Go 模块/数据文件/二进制/构建部署资产全部 opsx→lokistack;前端 localStorage 命名空间迁移(读旧写新,含按用户 id 前缀迁移);修复 nginx upstream 与 Makefile 引用已改名服务的断裂 - i18n: react-i18next 落地,common + 11 个页面域命名空间全站中英(菜单/顶栏/登录/列表页/表单/确认弹窗/状态徽章);设置页语言即时切换;修复词典自注册导致的循环导入白屏 - 用户管理: 批量删除(POST /users/batch-delete,不删自己/最后管理员,部分失败不阻断)+ 行复选/全选/批量工具条 - 主题: 浅色侧栏(中性灰激活态)、四态模式(浅色/深色/跟随系统/定时切换,按时区判定)、顶栏太阳月亮二态、设置页紧凑分段选择器 - UI: 登录记住用户名、CardActionButton 统一五页卡片操作钮、shadcn Badge 替换手写徽章、语义色修正 - 清理: 移除主机 Web 终端(前后端)、k8s-helm/Helm 遗留代码与字段、README 重写精简 - 修复: 侧栏与日志告警 order 重复、路由标题 i18n key 未翻译、词典双逗号等
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"
|
|
"lokistack/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
|
|
}
|