- Add Workspace domain (entity, repository, service, handler, DTO) - Add multi-tenant K8s client with tenant binding and quota management - Add K8s diagnostics client (instance diagnostics) - Add authorization middleware (authz package) - Restructure frontend to feature-based architecture (features/) - Add User Management page in configuration - Add AccessDenied page and route guards - Refactor shared components (form inputs, layout, UI) - Update Tailwind config for new design system - Add comprehensive documentation (docs/, tasks/, plans) - Improve cluster service with better kubeconfig handling - Add tests for crypto, config, helm client, tenant binding
111 lines
2.5 KiB
Go
111 lines
2.5 KiB
Go
package entity
|
||
|
||
import (
|
||
"time"
|
||
)
|
||
|
||
// Cluster Kubernetes 集群领域实体
|
||
type Cluster struct {
|
||
ID string
|
||
WorkspaceID string
|
||
OwnerID string
|
||
Visibility string
|
||
Name string
|
||
Host string // Kubernetes API Server URL
|
||
CAData string // Base64 encoded CA certificate
|
||
CertData string // Base64 encoded client certificate
|
||
KeyData string // Base64 encoded client key
|
||
Token string // Bearer token (alternative to cert auth)
|
||
Description string
|
||
DefaultNamespace string
|
||
CreatedAt time.Time
|
||
UpdatedAt time.Time
|
||
}
|
||
|
||
// NewCluster 创建新集群
|
||
func NewCluster(name, host string) *Cluster {
|
||
now := time.Now()
|
||
return &Cluster{
|
||
Name: name,
|
||
Host: host,
|
||
Visibility: "private",
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
}
|
||
|
||
// Update 更新集群信息
|
||
func (c *Cluster) Update(name, host, description string) {
|
||
if name != "" {
|
||
c.Name = name
|
||
}
|
||
if host != "" {
|
||
c.Host = host
|
||
}
|
||
c.Description = description
|
||
c.UpdatedAt = time.Now()
|
||
}
|
||
|
||
// SetCertAuth 设置证书认证
|
||
func (c *Cluster) SetCertAuth(caData, certData, keyData string) {
|
||
c.CAData = caData
|
||
c.CertData = certData
|
||
c.KeyData = keyData
|
||
c.UpdatedAt = time.Now()
|
||
}
|
||
|
||
// SetTokenAuth 设置 Token 认证
|
||
func (c *Cluster) SetTokenAuth(token string) {
|
||
c.Token = token
|
||
c.UpdatedAt = time.Now()
|
||
}
|
||
|
||
// Validate 验证集群配置
|
||
func (c *Cluster) Validate() error {
|
||
if c.Name == "" {
|
||
return ErrInvalidClusterName
|
||
}
|
||
if c.Host == "" {
|
||
return ErrInvalidClusterHost
|
||
}
|
||
if c.Visibility == "" {
|
||
c.Visibility = "private"
|
||
}
|
||
// 必须有认证方式:证书或 Token
|
||
if (c.CertData == "" || c.KeyData == "") && c.Token == "" {
|
||
return ErrInvalidClusterAuth
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// GetKubeConfig 生成 kubeconfig 内容
|
||
func (c *Cluster) GetKubeConfig() string {
|
||
// 如果 CAData 已经包含完整的 kubeconfig,直接返回
|
||
if len(c.CAData) > 100 && (c.CAData[:11] == "apiVersion:" || c.CAData[:5] == "kind:") {
|
||
return c.CAData
|
||
}
|
||
|
||
// 否则从证书数据生成 kubeconfig
|
||
kubeconfig := `apiVersion: v1
|
||
kind: Config
|
||
clusters:
|
||
- cluster:
|
||
certificate-authority-data: ` + c.CAData + `
|
||
server: ` + c.Host + `
|
||
name: ` + c.Name + `
|
||
contexts:
|
||
- context:
|
||
cluster: ` + c.Name + `
|
||
user: ` + c.Name + `
|
||
name: ` + c.Name + `
|
||
current-context: ` + c.Name + `
|
||
users:
|
||
- name: ` + c.Name + `
|
||
user:
|
||
client-certificate-data: ` + c.CertData + `
|
||
client-key-data: ` + c.KeyData + `
|
||
`
|
||
|
||
return kubeconfig
|
||
}
|