ocdp v1
This commit is contained in:
103
backend/internal/domain/entity/cluster.go
Normal file
103
backend/internal/domain/entity/cluster.go
Normal file
@ -0,0 +1,103 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cluster Kubernetes 集群领域实体
|
||||
type Cluster struct {
|
||||
ID 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
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// NewCluster 创建新集群
|
||||
func NewCluster(name, host string) *Cluster {
|
||||
now := time.Now()
|
||||
return &Cluster{
|
||||
Name: name,
|
||||
Host: host,
|
||||
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
|
||||
}
|
||||
// 必须有认证方式:证书或 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user