Skip to content

Commit

Permalink
feat: 专机集群挂载路径配置优化 TencentBlueKing#6226
Browse files Browse the repository at this point in the history
Signed-off-by: sawyersong <[email protected]>
  • Loading branch information
sawyersong2 committed Mar 1, 2022
1 parent 3be5142 commit d75026c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ data class DockerHostBuildInfo(
val status: Int? = 0,
val imageName: String,
val containerId: String,
@Deprecated("待废除")
val wsInHost: Boolean? = true,
// @Deprecated("待废除")
// val wsInHost: Boolean? = true,
val poolNo: Int,
val registryUser: String?,
val registryPwd: String?,
Expand All @@ -63,5 +63,6 @@ data class DockerHostBuildInfo(
blkioDeviceWriteBps = 125829120,
disk = 100,
description = ""
)
),
val specialProjectList: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ class DockerHostClient @Autowired constructor(
status = PipelineTaskStatus.RUNNING.status,
imageName = dockerImage!!,
containerId = "",
wsInHost = true,
poolNo = poolNo,
registryUser = userName ?: "",
registryPwd = password ?: "",
Expand All @@ -189,7 +188,8 @@ class DockerHostClient @Autowired constructor(
containerHashId = dispatchMessage.containerHashId,
customBuildEnv = dispatchMessage.customBuildEnv,
dockerResource = getDockerResource(dispatchType),
qpcUniquePath = getQpcUniquePath(dispatchMessage)
qpcUniquePath = getQpcUniquePath(dispatchMessage),
specialProjectList = getSpecialProjectList()
)

pipelineDockerTaskSimpleDao.createOrUpdate(
Expand Down Expand Up @@ -268,7 +268,6 @@ class DockerHostClient @Autowired constructor(
status = PipelineTaskStatus.RUNNING.status,
imageName = dockerImage,
containerId = "",
wsInHost = true,
poolNo = 0,
registryUser = defaultImageConfig.agentLessRegistryUserName ?: "",
registryPwd = defaultImageConfig.agentLessRegistryPassword ?: "",
Expand Down Expand Up @@ -347,7 +346,6 @@ class DockerHostClient @Autowired constructor(
status = 0,
imageName = "",
containerId = containerId,
wsInHost = true,
poolNo = 0,
registryUser = "",
registryPwd = "",
Expand Down Expand Up @@ -550,6 +548,10 @@ class DockerHostClient @Autowired constructor(
}
}

fun getSpecialProjectList(): String? {
return redisUtils.getSpecialProjectListKey()
}

private fun getDockerResource(dockerDispatchType: DockerDispatchType): DockerResourceOptionsVO {
if (dockerDispatchType.performanceConfigId != 0) {
val dockerResourceOptionRecord = dockerResourceOptionsDao.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class RedisUtils @Autowired constructor(
fun deleteHeartBeat(buildId: String, vmSeqId: String) =
redisOperation.delete(HeartBeatUtils.genHeartBeatKey(buildId, vmSeqId))

// 专机集群项目管理白名单
fun getSpecialProjectListKey(): String? {
return redisOperation.get("dispatchdocker:special_project_list_key")
}

private fun dockerBuildKey(id: Long, secretKey: String) =
"docker_build_key_${HashUtil.encodeLongId(id)}_$secretKey"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ class DockerService @Autowired constructor(
customBuildEnv = customBuildEnv,
buildType = buildType,
qpcUniquePath = qpcUniquePath,
dockerResource = dockerResource
dockerResource = dockerResource,
specialProjectList = specialProjectList,
)

containerPullImageHandler.setNextHandler(
Expand All @@ -231,14 +232,6 @@ class DockerService @Autowired constructor(

return containerHandlerContext.containerId!!
}
/* val containerId = dockerHostBuildService.createContainer(dockerHostBuildInfo)
dockerHostBuildService.log(
buildId = dockerHostBuildInfo.buildId,
message = "构建环境启动成功,等待Agent启动...",
tag = VMUtils.genStartVMTaskId(dockerHostBuildInfo.vmSeqId.toString()),
containerHashId = dockerHostBuildInfo.containerHashId
)
return containerId*/
}

fun getDockerHostLoad(): DockerHostLoad {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data class ContainerHandlerContext(
val qpcUniquePath: String? = null,
val dockerResource: DockerResourceOptionsVO? = null,
val dockerRunParam: DockerRunParam? = null,
val specialProjectList: String? = null,
var formatImageName: String? = "",
var containerId: String? = "",
var dockerRunResponse: DockerRunResponse? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,13 @@ import com.tencent.devops.dockerhost.services.generator.DockerBindGenerator
import com.tencent.devops.dockerhost.services.generator.annotation.BindGenerator
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock

@BindGenerator(description = "默认Docker Bind生成器")
@Component
class SystemDockerBindGenerator @Autowired constructor(private val dockerHostConfig: DockerHostConfig) :
DockerBindGenerator {

private val whiteList = mutableSetOf<String>()

private val whiteListLocker = ReentrantLock()

override fun generateBinds(handlerContext: ContainerHandlerContext): List<Bind> {
with(handlerContext) {

val binds = mutableListOf(
Bind(getMavenRepoPath(), Volume(dockerHostConfig.volumeMavenRepo)),
Bind(getNpmPrefixPath(), Volume(dockerHostConfig.volumeNpmPrefix)),
Expand All @@ -71,7 +63,7 @@ class SystemDockerBindGenerator @Autowired constructor(private val dockerHostCon
binds.add(Bind(getWorkspace(), Volume(dockerHostConfig.volumeWorkspace)))
}

if (enableProjectShare(projectId)) {
if (enableProjectShare(specialProjectList, projectId)) {
binds.add(Bind(getProjectShareDir(projectId), Volume(dockerHostConfig.volumeProjectShare)))
}

Expand Down Expand Up @@ -127,25 +119,22 @@ class SystemDockerBindGenerator @Autowired constructor(private val dockerHostCon
return "${dockerHostConfig.hostPathProjectShare}/$projectCode/"
}

private fun enableProjectShare(projectCode: String): Boolean {

if (dockerHostConfig.shareProjectCodeWhiteList.isNullOrBlank()) {
return false
private fun enableProjectShare(
specialProjectList: String?,
projectId: String
): Boolean {
if (!specialProjectList.isNullOrBlank()) {
return specialProjectList.split(",").map { it.trim() }.contains(projectId)
}

if (whiteList.isEmpty()) {
try {
whiteListLocker.tryLock(TimeUnit.SECONDS.toNanos(1), TimeUnit.NANOSECONDS)
if (whiteList.isEmpty()) {
whiteList.addAll(dockerHostConfig.shareProjectCodeWhiteList!!.split(",").map { it.trim() })
}
} catch (ignored: InterruptedException) {
} finally {
whiteListLocker.unlock()
}
if (dockerHostConfig.shareProjectCodeWhiteList.isNullOrBlank()) {
return dockerHostConfig.shareProjectCodeWhiteList!!
.split(",")
.map { it.trim() }
.contains(projectId)
}

return whiteList.contains(projectCode)
return false
}

private fun getTailPath(vmSeqId: Int, poolNo: Int): String {
Expand Down

0 comments on commit d75026c

Please sign in to comment.