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) } } func loadDevcontainerFrom(rootCfg ConfigProvider) { mustMapSetting(rootCfg, "devcontainer", &Devcontainer) validateDevcontainerSettings() mustMapSetting(rootCfg, "ssh_key_pair", &SSHKeypair) validateSSHKeyPairSettings() if Devcontainer.Agent == "k8s" || Devcontainer.Agent == KUBERNETES { mustMapSetting(rootCfg, "devcontainer.cloud", &Cloud) validateDevcontainerCloudSettings() } }