Skip to content

Commit

Permalink
Merge pull request #8847 from sawyersong2/issue8505_github_branch
Browse files Browse the repository at this point in the history
主集群和子集群间构建制品网关与流量网关的配置优化 #8505
  • Loading branch information
bkci-bot committed May 24, 2023
2 parents 7b6eee1 + 84006e2 commit faf19c8
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ class BuildLessConfig {

@Value("\${gatewayHeaderTag:#{null}}")
var gatewayHeaderTag: String? = null

/**
* 蓝盾网关配置
*/
@Value("\${devopsGateway.idc:#{null}}")
var idcGateway: String? = null

/**
* bkrepo网关配置
*/
@Value("\${devopsGateway.fileIdcGateway:#{null}}")
var fileIdcGateway: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ import com.tencent.devops.buildless.utils.ENV_BK_CI_DOCKER_HOST_IP
import com.tencent.devops.buildless.utils.ENV_BK_CI_DOCKER_HOST_WORKSPACE
import com.tencent.devops.buildless.utils.ENV_CONTAINER_NAME
import com.tencent.devops.buildless.utils.ENV_DEFAULT_LOCALE_LANGUAGE
import com.tencent.devops.buildless.utils.ENV_DEVOPS_FILE_GATEWAY
import com.tencent.devops.buildless.utils.ENV_DEVOPS_GATEWAY
import com.tencent.devops.buildless.utils.ENV_DOCKER_HOST_IP
import com.tencent.devops.buildless.utils.ENV_DOCKER_HOST_PORT
import com.tencent.devops.buildless.utils.ENV_JOB_BUILD_TYPE
import com.tencent.devops.buildless.utils.ENV_KEY_BK_TAG
import com.tencent.devops.buildless.utils.ENV_KEY_GATEWAY
import com.tencent.devops.buildless.utils.ENV_KEY_PROJECT_ID
import com.tencent.devops.buildless.utils.RandomUtil
import com.tencent.devops.buildless.utils.RedisUtils
import com.tencent.devops.common.api.util.ShaUtils
import com.tencent.devops.common.service.BkTag
import com.tencent.devops.common.service.config.CommonConfig
import com.tencent.devops.common.service.gray.Gray
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.io.File
Expand All @@ -73,7 +75,7 @@ import kotlin.streams.toList

@Service
class BuildLessContainerService(
private val gray: Gray,
private val bkTag: BkTag,
private val redisUtils: RedisUtils,
private val commonConfig: CommonConfig,
private val buildLessConfig: BuildLessConfig
Expand Down Expand Up @@ -122,27 +124,7 @@ class BuildLessContainerService(
.withName(containerName)
.withLabels(mapOf(BUILDLESS_POOL_PREFIX to ""))
.withCmd("/bin/sh", ENTRY_POINT_CMD)
.withEnv(
listOf(
"$ENV_KEY_GATEWAY=${buildLessConfig.gateway}",
"TERM=xterm-256color",
"$ENV_KEY_PROJECT_ID=${
if (gray.isGray()) {
"grayproject"
} else {
""
}
}",
"$ENV_DOCKER_HOST_IP=${CommonUtils.getHostIp()}",
"$ENV_DOCKER_HOST_PORT=${commonConfig.serverPort}",
"$BK_DISTCC_LOCAL_IP=${CommonUtils.getHostIp()}",
"$ENV_BK_CI_DOCKER_HOST_IP=${CommonUtils.getHostIp()}",
"$ENV_JOB_BUILD_TYPE=BUILD_LESS",
"$ENV_CONTAINER_NAME=$containerName",
"$ENV_BK_CI_DOCKER_HOST_WORKSPACE=$linkPath",
"$ENV_DEFAULT_LOCALE_LANGUAGE=${commonConfig.devopsDefaultLocaleLanguage}"
)
)
.withEnv(generateEnv(containerName, linkPath))
.withHostConfig(
// CPU and memory Limit
HostConfig()
Expand Down Expand Up @@ -259,7 +241,33 @@ class BuildLessContainerService(
return timeoutContainerList
}

fun createSymbolicLink(hostWorkspace: String): String {
private fun generateEnv(containerName: String, linkPath: String): List<String> {
val envList = mutableListOf<String>()
envList.addAll(listOf(
"$ENV_KEY_GATEWAY=${buildLessConfig.gateway}",
"TERM=xterm-256color",
"$ENV_KEY_BK_TAG=${bkTag.getFinalTag()}",
"$ENV_DOCKER_HOST_IP=${CommonUtils.getHostIp()}",
"$ENV_DOCKER_HOST_PORT=${commonConfig.serverPort}",
"$BK_DISTCC_LOCAL_IP=${CommonUtils.getHostIp()}",
"$ENV_BK_CI_DOCKER_HOST_IP=${CommonUtils.getHostIp()}",
"$ENV_JOB_BUILD_TYPE=BUILD_LESS",
"$ENV_CONTAINER_NAME=$containerName",
"$ENV_BK_CI_DOCKER_HOST_WORKSPACE=$linkPath",
"$ENV_DEFAULT_LOCALE_LANGUAGE=${commonConfig.devopsDefaultLocaleLanguage}"
))

buildLessConfig.idcGateway?.let {
envList.add("$ENV_DEVOPS_GATEWAY=$it")
}
buildLessConfig.fileIdcGateway?.let {
envList.add("$ENV_DEVOPS_FILE_GATEWAY=$it")
}

return envList
}

private fun createSymbolicLink(hostWorkspace: String): String {
val hostWorkspaceFile = File(hostWorkspace)
if (!hostWorkspaceFile.exists()) {
hostWorkspaceFile.mkdirs() // 新建的流水线的工作空间路径为空则新建目录
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const val SLAVE_BUILD_TYPE = "build.type"
const val WORKSPACE_ENV = "WORKSPACE"

const val ENV_KEY_DISTCC = "DISTCC_HOSTS"
const val ENV_KEY_PROJECT_ID = "devops_project_id"
const val ENV_KEY_BK_TAG = "devops_bk_tag"
const val ENV_KEY_BUILD_ID = "devops_build_id"
const val ENV_KEY_AGENT_ID = "devops_agent_id"
const val ENV_KEY_AGENT_SECRET_KEY = "devops_agent_secret_key"
Expand All @@ -70,3 +70,13 @@ const val ENV_BK_CI_DOCKER_HOST_WORKSPACE = "BK_CI_DOCKER_HOST_WORKSPACE" // doc
const val ENV_DEFAULT_LOCALE_LANGUAGE = "BK_CI_LOCALE_LANGUAGE"

const val ENTRY_POINT_CMD = "/data/init.sh"

/**
* bkrepo网关配置
*/
const val ENV_DEVOPS_FILE_GATEWAY = "DEVOPS_FILE_GATEWAY"

/**
* 蓝盾网关配置
*/
const val ENV_DEVOPS_GATEWAY = "DEVOPS_GATEWAY"
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.tencent.devops.common.dispatch.sdk.service.DockerRoutingSdkService
import com.tencent.devops.common.log.utils.BuildLogPrinter
import com.tencent.devops.common.dispatch.sdk.service.JobQuotaService
import com.tencent.devops.common.dispatch.sdk.utils.ChannelUtils
import com.tencent.devops.common.service.config.CommonConfig

@Configuration
class SDKConfiguration {
Expand All @@ -55,10 +56,19 @@ class SDKConfiguration {
@Autowired objectMapper: ObjectMapper,
@Autowired client: Client,
@Autowired channelUtils: ChannelUtils,
@Autowired buildLogPrinter: BuildLogPrinter
@Autowired buildLogPrinter: BuildLogPrinter,
@Autowired commonConfig: CommonConfig
) =
DispatchService(redisOperation, objectMapper, pipelineEventDispatcher, gateway,
client, channelUtils, buildLogPrinter)
DispatchService(
redisOperation = redisOperation,
objectMapper = objectMapper,
pipelineEventDispatcher = pipelineEventDispatcher,
gateway = gateway,
client = client,
channelUtils = channelUtils,
buildLogPrinter = buildLogPrinter,
commonConfig = commonConfig
)

@Bean
fun jobQuotaService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ object DockerConstants {
const val BK_JOB_REACHED_MAX_QUOTA_AND_ALREADY_DELAYED = "bkJobReachedMaxQuotaAndAlreadyDelayed"
// 当前项目下正在执行的【{0}】JOB数量已经达到配额最大值,将延迟 {1}s 后在降级队列重试,当前重试次数:{2}
const val BK_JOB_REACHED_MAX_QUOTA_AND_SOON_DELAYED = "bkJobReachedMaxQuotaAndSoonDelayed"

/**
* bkrepo网关配置
*/
const val ENV_DEVOPS_FILE_GATEWAY = "DEVOPS_FILE_GATEWAY"

/**
* 蓝盾网关配置
*/
const val ENV_DEVOPS_GATEWAY = "DEVOPS_GATEWAY"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import com.tencent.devops.common.dispatch.sdk.BuildFailureException
import com.tencent.devops.common.dispatch.sdk.pojo.DispatchMessage
import com.tencent.devops.common.dispatch.sdk.pojo.RedisBuild
import com.tencent.devops.common.dispatch.sdk.pojo.SecretInfo
import com.tencent.devops.common.dispatch.sdk.pojo.docker.DockerConstants.ENV_DEVOPS_FILE_GATEWAY
import com.tencent.devops.common.dispatch.sdk.pojo.docker.DockerConstants.ENV_DEVOPS_GATEWAY
import com.tencent.devops.common.dispatch.sdk.pojo.docker.DockerConstants.ENV_KEY_AGENT_ID
import com.tencent.devops.common.dispatch.sdk.pojo.docker.DockerConstants.ENV_KEY_AGENT_SECRET_KEY
import com.tencent.devops.common.dispatch.sdk.pojo.docker.DockerConstants.ENV_KEY_BUILD_ID
Expand All @@ -49,6 +51,7 @@ import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatch
import com.tencent.devops.common.log.utils.BuildLogPrinter
import com.tencent.devops.common.pipeline.enums.BuildStatus
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.service.config.CommonConfig
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.monitoring.api.service.DispatchReportResource
import com.tencent.devops.monitoring.pojo.DispatchStatus
Expand All @@ -68,7 +71,8 @@ class DispatchService constructor(
private val gateway: String?,
private val client: Client,
private val channelUtils: ChannelUtils,
private val buildLogPrinter: BuildLogPrinter
private val buildLogPrinter: BuildLogPrinter,
private val commonConfig: CommonConfig
) {

fun log(buildId: String, containerHashId: String?, vmSeqId: String, message: String, executeCount: Int?) {
Expand Down Expand Up @@ -100,6 +104,12 @@ class DispatchService constructor(
customBuildEnv[ENV_KEY_PROJECT_ID] = event.projectId
customBuildEnv[ENV_KEY_AGENT_ID] = secretInfo.hashId
customBuildEnv[ENV_KEY_AGENT_SECRET_KEY] = secretInfo.secretKey
commonConfig.fileDevnetGateway?.let {
customBuildEnv[ENV_DEVOPS_FILE_GATEWAY] = it
}
commonConfig.devopsDevnetProxyGateway?.let {
customBuildEnv[ENV_DEVOPS_GATEWAY] = it
}

return DispatchMessage(
id = secretInfo.hashId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ class CommonConfig {
@Value("\${sharding.routing.cacheSize:50000}")
val shardingRoutingCacheSize: Long = 50000

/**
* bkrepo DevNet区域网关配置
*/
@Value("\${devopsGateway.fileDevnetGateway:#{null}}")
val fileDevnetGateway: String? = null

/**
* bkrepo Idc区域网关配置
*/
@Value("\${devopsGateway.fileIdcGateway:#{null}}")
val fileIdcGateway: String? = null

/**
* 蓝盾默认语言
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_BUILD_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_BUILD_TYPE
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_PROJECT_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_VM_SEQ_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_GATEWAY_TAG
import com.tencent.devops.common.api.constant.HTTP_404
import com.tencent.devops.common.api.exception.ClientException
import com.tencent.devops.common.api.exception.RemoteServiceException
Expand Down Expand Up @@ -285,6 +286,10 @@ abstract class AbstractBuildResourceApi : WorkerRestApiSDK {
val buildType = BuildEnv.getBuildType()
val map = mutableMapOf<String, String>()

AgentEnv.getBkTag()?.let {
map[AUTH_HEADER_GATEWAY_TAG] = it
}

map[AUTH_HEADER_DEVOPS_BUILD_TYPE] = buildType.name
when (buildType) {
BuildType.AGENT -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ object AgentEnv {
private const val AGENT_ENV = "landun.env"
private const val AGENT_LOG_SAVE_MODE = "devops_log_save_mode"
private const val AGENT_PROPERTIES_FILE_NAME = ".agent.properties"
private const val BK_TAG = "devops_bk_tag"

private var projectId: String? = null
private var agentId: String? = null
Expand All @@ -68,6 +69,7 @@ object AgentEnv {
private var os: OSType? = null
private var env: Env? = null
private var logStorageMode: LogStorageMode? = null
private var bkTag: String? = null

private var property: Properties? = null

Expand Down Expand Up @@ -202,6 +204,20 @@ object AgentEnv {
return os!!
}

fun getBkTag(): String? {

if (bkTag.isNullOrBlank()) {
synchronized(this) {
if (bkTag.isNullOrBlank()) {
bkTag = getProperty(BK_TAG)
logger.info("Get the bkTag($bkTag)")
return bkTag
}
}
}
return bkTag!!
}

@Suppress("UNUSED")
fun is32BitSystem() = System.getProperty("sun.arch.data.model") == "32"

Expand Down
2 changes: 2 additions & 0 deletions support-files/templates/#etc#ci#common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ devopsGateway:
devnet: __BK_CI_PRIVATE_URL__ # not used yet. keep it same as `host' property.
oss: __BK_CI_PRIVATE_URL__ # not used yet. keep it same as `host' property.
ossProxy: __BK_CI_PRIVATE_URL__ # not used yet. keep it same as `host' property.
fileDevnetGateway: __BK_CI_PRIVATE_URL__ # not used yet. keep it same as `host' property.
fileIdcGateway: __BK_CI_PRIVATE_URL__ # not used yet. keep it same as `host' property.

# certificate server 配置
certificate:
Expand Down

0 comments on commit faf19c8

Please sign in to comment.