package models import ( "time" "lokideploy/pkg/crypto" ) type Cluster struct { ID uint `json:"id"` Name string `json:"name"` Description string `json:"description"` ConfigType string `json:"configType"` // kubeconfig | token APIServer string `json:"apiServer"` // 展示用 KubeConfig string `json:"kubeconfig"` // 统一存储(加密存储时带ENC:前缀) KubeConfigPath string `json:"kubeconfigPath"` Context string `json:"context"` Version string `json:"version"` NodesCount int `json:"nodesCount"` Status string `json:"status"` LastConnected time.Time `json:"lastConnected"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` } func (c *Cluster) Sanitized() map[string]interface{} { return map[string]interface{}{ "id": c.ID, "name": c.Name, "description": c.Description, "configType": c.ConfigType, "apiServer": c.APIServer, "context": c.Context, "version": c.Version, "nodesCount": c.NodesCount, "status": c.Status, "hasKubeconfig": c.KubeConfig != "", "lastConnected": c.LastConnected, "createdAt": c.CreatedAt, "updatedAt": c.UpdatedAt, } } // DecryptKubeconfig 解密集群的kubeconfig配置 // kubeconfig字段可能以ENC:前缀存储加密数据 func (c *Cluster) DecryptKubeconfig() string { return crypto.DecryptField(c.KubeConfig) } // IsKubeconfigEncrypted 检查kubeconfig是否已加密存储 func (c *Cluster) IsKubeconfigEncrypted() bool { return crypto.IsEncryptedField(c.KubeConfig) } // EncryptAndSetKubeconfig 加密并设置kubeconfig func (c *Cluster) EncryptAndSetKubeconfig(kubeconfig string) error { enc, err := crypto.EncryptField(kubeconfig) if err != nil { return err } c.KubeConfig = enc return nil }