Files
devstar/modules/setting/devcontainer.go
2025-05-20 12:06:35 +08:00

206 lines
6.2 KiB
Go

package setting
import (
"code.gitea.io/gitea/modules/log"
)
const (
KUBERNETES = "kubernetes" // 支持 "k8s" 和 "kubernetes"
DOCKER = "docker"
)
// 检查用户输入的 DevContainer Agent 是否有效
func isValidAgent(agent string) bool {
return agent == "k8s" || agent == KUBERNETES || agent == DOCKER
}
const (
CLOUD_PROVIDER_TENCENT = "tencent"
DEVCONTAINER_CLOUD_NAT_RULE_DESCRIPTION_PREFIX = "DevContainer: "
)
// validCloudProviderSet 私有 Set 结构,标识目前系统所有支持的 Cloud Provider 类型
var validCloudProviderSet = map[string]struct{}{
CLOUD_PROVIDER_TENCENT: {},
}
type DevcontainerType struct {
Enabled bool
Host string
Agent string
Namespace string
TimeoutSeconds int64
DefaultGitBranchName string
DefaultDevcontainerImageName string
DockerHost string
}
type SSHKeyPairType struct {
KeySize int
}
type CloudType struct {
Enabled bool
Provider string
Tencent CloudProviderTencentType `ini:"devcontainer.cloud.tencent"`
}
type CloudProviderTencentType struct {
Endpoint string
Region string
NatGatewayId string
PublicIpAddress string
PrivateIpAddress string
IpProtocol string
SecretId string
SecretKey string
}
var Devcontainer = DevcontainerType{
Enabled: false,
Namespace: "default",
TimeoutSeconds: 900, // Max wait time for DevContainer to be ready (blocking), default is 15 minutes, can be overridden by app.ini
DefaultGitBranchName: "main", // Default branch name for .devcontainer/devcontainer.json
DefaultDevcontainerImageName: "devstar.cn/public/base-ssh-devcontainer:ubuntu-20.04-20241014", // Default image if not specified
}
var SSHKeypair = SSHKeyPairType{
KeySize: 2048, // Size of the SSH key
}
var Cloud = CloudType{
Enabled: false, // Cloud feature toggle
}
// validateDevcontainerSettings 检查从 ini 配置文件中读取 DevStar DevContainer 配置信息,若数据无效,则自动禁用 DevContainer
func validateDevcontainerSettings() {
// 检查 Host 是否为空,若为空,则自动将 DevContainer 设置为禁用
if len(Devcontainer.Host) == 0 {
log.Warn("INVALID config 'host' for DevStar DevContainer")
Devcontainer.Enabled = false
}
// 检查用户输入的 DevContainer Agent 是否存在支持列表,若不支持,则将 DevContainer 设置为禁用
if !isValidAgent(Devcontainer.Agent) {
log.Warn("Invalid config 'agent' for DevStar DevContainer")
Devcontainer.Enabled = false
}
// 检查默认分支名称设置
if len(Devcontainer.DefaultGitBranchName) == 0 {
log.Warn("INVALID config 'DefaultGitBranchName' for DevStar DevContainer")
Devcontainer.Enabled = false
}
// 检查默认 DevContainer Image
if len(Devcontainer.DefaultDevcontainerImageName) == 0 {
log.Warn("INVALID config 'DefaultGitBranchNameDefaultDevcontainerImageName' for DevStar DevContainer")
Devcontainer.Enabled = false
}
if Devcontainer.Enabled == false {
log.Warn("DevStar DevContainer Service Disabled")
} else {
log.Info("DevStar DevContainer Service Enabled")
}
}
// validateSSHKeyPairSettings 检查从 ini 配置文件中读取 DevStar SSH Key Pair 配置信息
func validateSSHKeyPairSettings() {
if SSHKeypair.KeySize < 1024 {
SSHKeypair.KeySize = 1024
}
}
// validateDevcontainerCloudSettings 检查从 ini 配置文件中读取 DevStar Cloud 配置信息
func validateDevcontainerCloudSettings() {
switch Cloud.Provider {
case CLOUD_PROVIDER_TENCENT:
// 腾讯云配置检查
if len(Cloud.Tencent.NatGatewayId) < 4 {
log.Warn("INVALID NAT Gateway ID '%v' for DevStar Cloud Provider Tencent", Cloud.Tencent.NatGatewayId)
Cloud.Enabled = false
}
if Cloud.Tencent.IpProtocol != "TCP" && Cloud.Tencent.IpProtocol != "UDP" {
log.Warn("INVALID IP Protocol '%v' for DevStar Cloud Provider Tencent", Cloud.Tencent.IpProtocol)
Cloud.Enabled = false
}
if len(Cloud.Tencent.Region) < 3 || len(Cloud.Tencent.Endpoint) == 0 {
log.Warn("INVALID (Region, Endpoint) pair ('%v', '%v') for DevStar Cloud Provider Tencent",
Cloud.Tencent.Region, Cloud.Tencent.Endpoint)
Cloud.Enabled = false
}
if len(Cloud.Tencent.PrivateIpAddress) == 0 || len(Cloud.Tencent.PublicIpAddress) == 0 {
log.Warn("INVALID (PublicIpAddress, PrivateIpAddress) pair ('%v', '%v') for DevStar Cloud Provider Tencent",
Cloud.Tencent.PublicIpAddress, Cloud.Tencent.PrivateIpAddress)
Cloud.Enabled = false
}
if len(Cloud.Tencent.SecretId) == 0 || len(Cloud.Tencent.SecretKey) == 0 {
log.Warn("INVALID (SecretId, SecretKey) pair for DevStar Cloud Provider Tencent")
Cloud.Enabled = false
}
default:
// 无效 Cloud Provider 名称
log.Warn("INVALID config '%v' for DevStar Cloud", Cloud.Provider)
Cloud.Enabled = false
}
if Cloud.Enabled == false {
log.Warn("DevStar Cloud Provider Service Disabled")
} else {
log.Info("DevStar Cloud Provider '%v' Enabled", Cloud.Provider)
}
}
// 修改 loadDevcontainerFrom 函数以更好地处理不同配置节点
func loadDevcontainerFrom(rootCfg ConfigProvider) {
// 检查是否存在新的配置节
hasNewConfig := true
if _, err := rootCfg.GetSection("devcontainer"); err != nil {
hasNewConfig = false
}
// 检查是否存在旧的配置节
hasOldConfig := true
if _, err := rootCfg.GetSection("devstar.devcontainer"); err != nil {
hasOldConfig = false
}
// 根据存在的配置节处理
if hasNewConfig {
// 新配置节存在,直接使用
mustMapSetting(rootCfg, "devcontainer", &Devcontainer)
log.Info("从 [devcontainer] 节加载配置")
} else if hasOldConfig {
// 只有旧配置节存在,直接从旧配置节加载
mustMapSetting(rootCfg, "devstar.devcontainer", &Devcontainer)
log.Info("从 [devstar.devcontainer] 节加载配置")
}
// 进行配置验证
validateDevcontainerSettings()
// 加载其他配置
mustMapSetting(rootCfg, "ssh_key_pair", &SSHKeypair)
validateSSHKeyPairSettings()
if Devcontainer.Agent == "k8s" || Devcontainer.Agent == KUBERNETES {
mustMapSetting(rootCfg, "devcontainer.cloud", &Cloud)
validateDevcontainerCloudSettings()
}
// 打印最终使用的命名空间
log.Info("DevContainer 将在命名空间 '%s' 中创建", Devcontainer.Namespace)
}