Skip to content

Commit

Permalink
Merge pull request #5288 from sawyersong2/issue5245_github_branch
Browse files Browse the repository at this point in the history
feat: 完善dispatch docker调度鉴权参数 #5245
  • Loading branch information
irwinsun authored Oct 8, 2021
2 parents 3a3d283 + f765f3f commit 0c766da
Show file tree
Hide file tree
Showing 17 changed files with 586 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ data class DockerDispatchType(
// 商店镜像名称
override var imageName: String? = "",
// docker资源配置ID
var performanceConfigId: Int = 0
var performanceConfigId: Int = 0,
var imageRepositoryUserName: String = "",
var imageRepositoryPassword: String = ""
) : StoreDispatchType(dockerBuildVersion = if (dockerBuildVersion.isNullOrBlank()) {
imageCode
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@

package com.tencent.devops.dispatch.docker.api.user

import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_USER_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_USER_ID_DEFAULT_VALUE
import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID_DEFAULT_VALUE
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.dispatch.docker.pojo.ContainerInfo
import com.tencent.devops.dispatch.docker.pojo.DebugStartParam
import com.tencent.devops.dispatch.docker.pojo.DockerHostLoad
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
Expand Down Expand Up @@ -144,4 +147,13 @@ interface UserDockerHostResource {
@PathParam("vmSeqId")
vmSeqId: String
): Result<Boolean>?

@GET
@Path("/dockerhost-load")
@ApiOperation("获取dockerhost集群平均负载信息")
fun getDockerHostLoad(
@ApiParam("用户ID", required = true, defaultValue = AUTH_HEADER_DEVOPS_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_DEVOPS_USER_ID)
userId: String
): Result<DockerHostLoad>
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ data class DebugStartParam(
@ApiModelProperty("vmSeqId", required = true)
val vmSeqId: String,
@ApiModelProperty("imageType为BKSTORE时的镜像编码", required = false)
val imageCode: String?,
val imageCode: String? = null,
@ApiModelProperty("imageType为BKSTORE时的镜像版本", required = false)
val imageVersion: String?,
val imageVersion: String? = null,
@ApiModelProperty("镜像名称", required = false)
val imageName: String?,
val imageName: String? = null,
@ApiModelProperty("环境变量", required = true)
val buildEnv: Map<String, String>?,
@ApiModelProperty("镜像类型(BKDEVOPS或THIRD或BKSTORE)", required = true)
val imageType: String?,
@ApiModelProperty("镜像仓库凭证ID", required = true)
val credentialId: String?
@ApiModelProperty("镜像类型(BKDEVOPS或THIRD或BKSTORE)", required = false)
val imageType: String? = null,
@ApiModelProperty("镜像仓库凭证ID", required = false)
val credentialId: String? = null,
@ApiModelProperty("启动命令", required = false)
val cmd: String? = "/bin/sh",
@ApiModelProperty("镜像信息", required = false)
val containerPool: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.dispatch.docker.pojo

import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@ApiModel("DockerHostLoad")
data class DockerHostLoad(
val clusterLoad: Map<String, Load>
)

@ApiModel("DockerHostLoad")
data class Load(
@ApiModelProperty("构建机已使用量")
val usedNum: Int,
@ApiModelProperty("构建机CPU负载")
val averageCpuLoad: Int,
@ApiModelProperty("构建机内存负载")
val averageMemLoad: Int,
@ApiModelProperty("构建机硬盘负载")
val averageDiskLoad: Int,
@ApiModelProperty("构建机硬盘IO负载")
val averageDiskIOLoad: Int,
@ApiModelProperty("集群节点数量")
val totalNode: Int,
@ApiModelProperty("集群可用节点数量")
val enableNode: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tencent.devops.dispatch.docker.pojo

data class Pool(
val container: String?,
val credential: Credential?,
val env: Map<String, String>?,
val buildType: BuildType? = BuildType.DEVCLOUD
)

enum class BuildType {
DOCKER_VM,
DEVCLOUD
}

data class Credential(
val user: String,
val password: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ enum class DockerHostClusterType {
/**
* 无编译环境构建机群
*/
AGENT_LESS
AGENT_LESS,

/**
* mac构建机群
*/
MACOS
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class DockerHostClient @Autowired constructor(
LOG.info("${dispatchMessage.buildId}|startBuild|${dispatchMessage.id}|$dockerImage" +
"|${dispatchType.imageCode}|${dispatchType.imageVersion}|${dispatchType.credentialId}" +
"|${dispatchType.credentialProject}")
var userName: String? = null
var password: String? = null
var userName = dispatchType.imageRepositoryUserName
var password = dispatchType.imageRepositoryPassword
if (dispatchType.imageType == ImageType.THIRD) {
if (!dispatchType.credentialId.isNullOrBlank()) {
val projectId = if (dispatchType.credentialProject.isNullOrBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.tencent.devops.common.auth.api.AuthPermission
import com.tencent.devops.common.auth.api.AuthPermissionApi
import com.tencent.devops.common.auth.api.AuthResourceType
import com.tencent.devops.common.auth.code.PipelineAuthServiceCode
import com.tencent.devops.common.pipeline.type.docker.ImageType
import com.tencent.devops.common.service.utils.MessageCodeUtil
import com.tencent.devops.common.web.RestResource
import com.tencent.devops.dispatch.docker.api.user.UserDockerHostResource
Expand All @@ -48,11 +47,13 @@ import com.tencent.devops.dispatch.docker.service.DockerHostDebugService
import com.tencent.devops.dispatch.docker.utils.DockerHostUtils
import com.tencent.devops.dispatch.docker.pojo.ContainerInfo
import com.tencent.devops.dispatch.docker.pojo.DebugStartParam
import com.tencent.devops.dispatch.docker.pojo.DockerHostLoad
import com.tencent.devops.dispatch.pojo.enums.PipelineTaskStatus
import com.tencent.devops.process.constant.ProcessMessageCode
import org.jooq.DSLContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import javax.ws.rs.core.Response

@RestResource
Expand All @@ -72,6 +73,9 @@ class UserDockerHostResourceImpl @Autowired constructor(
private val logger = LoggerFactory.getLogger(UserDockerHostResourceImpl::class.java)
}

@Value("\${spring.cloud.consul.discovery.tags:prod}")
private val consulTag: String = "prod"

override fun startDebug(userId: String, debugStartParam: DebugStartParam): Result<Boolean>? {
checkPermission(userId, debugStartParam.projectId, debugStartParam.pipelineId, debugStartParam.vmSeqId)

Expand Down Expand Up @@ -162,22 +166,12 @@ class UserDockerHostResourceImpl @Autowired constructor(
poolNo = 1
}

with(debugStartParam) {
dockerHostDebugService.startDebug(
dockerIp = dockerIp,
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
vmSeqId = vmSeqId,
poolNo = poolNo,
imageCode = imageCode,
imageVersion = imageVersion,
imageName = imageName,
buildEnv = buildEnv,
imageType = ImageType.getType(imageType),
credentialId = credentialId
)
}
dockerHostDebugService.startDebug(
dockerIp = dockerIp,
userId = userId,
poolNo = poolNo,
debugStartParam = debugStartParam
)

return Result(true)
}
Expand Down Expand Up @@ -232,6 +226,10 @@ class UserDockerHostResourceImpl @Autowired constructor(
return dockerHostDebugService.cleanIp(projectId, pipelineId, vmSeqId)
}

override fun getDockerHostLoad(userId: String): Result<DockerHostLoad> {
return Result(dockerHostBuildService.getDockerHostLoad(userId))
}

fun checkParam(userId: String, projectId: String, pipelineId: String, vmSeqId: String) {
if (userId.isBlank()) {
throw ParamBlankException("Invalid userId")
Expand All @@ -250,14 +248,16 @@ class UserDockerHostResourceImpl @Autowired constructor(
private fun checkPermission(userId: String, projectId: String, pipelineId: String, vmSeqId: String) {
checkParam(userId, projectId, pipelineId, vmSeqId)

validPipelinePermission(
userId = userId,
authResourceType = AuthResourceType.PIPELINE_DEFAULT,
projectId = projectId,
pipelineId = pipelineId,
permission = AuthPermission.EDIT,
message = "用户($userId)无权限在工程($projectId)下编辑流水线($pipelineId)"
)
if (!consulTag.contains("stream") && !consulTag.contains("gitci")) {
validPipelinePermission(
userId = userId,
authResourceType = AuthResourceType.PIPELINE_DEFAULT,
projectId = projectId,
pipelineId = pipelineId,
permission = AuthPermission.EDIT,
message = "用户($userId)无权限在工程($projectId)下编辑流水线($pipelineId)"
)
}
}

private fun validPipelinePermission(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,22 @@ class PipelineDockerIPInfoDao {
fun getDockerIpList(
dslContext: DSLContext,
enable: Boolean,
grayEnv: Boolean
grayEnv: Boolean,
clusterName: DockerHostClusterType? = null
): Result<TDispatchPipelineDockerIpInfoRecord> {
with(TDispatchPipelineDockerIpInfo.T_DISPATCH_PIPELINE_DOCKER_IP_INFO) {
val conditions =
mutableListOf<Condition>(
ENABLE.eq(enable),
GRAY_ENV.eq(grayEnv)
)

if (clusterName != null) {
conditions.add(CLUSTER_NAME.eq(clusterName.name))
}

return dslContext.selectFrom(this)
.where(ENABLE.eq(enable))
.and(GRAY_ENV.eq(grayEnv))
.where(conditions)
.fetch()
}
}
Expand All @@ -277,6 +287,20 @@ class PipelineDockerIPInfoDao {
}
}

fun getAllDockerIpCount(
dslContext: DSLContext,
grayEnv: Boolean,
clusterName: DockerHostClusterType
): Long? {
with(TDispatchPipelineDockerIpInfo.T_DISPATCH_PIPELINE_DOCKER_IP_INFO) {
return dslContext.selectCount()
.from(this)
.where(GRAY_ENV.eq(grayEnv))
.and(CLUSTER_NAME.eq(clusterName.name))
.fetchOne(0, Long::class.java)
}
}

fun delete(
dslContext: DSLContext,
dockerIp: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import com.tencent.devops.dispatch.docker.pojo.DockerHostInfo
import com.tencent.devops.dispatch.docker.utils.DockerHostUtils
import com.tencent.devops.dispatch.docker.utils.RedisUtils
import com.tencent.devops.dispatch.docker.pojo.ContainerInfo
import com.tencent.devops.dispatch.docker.pojo.DockerHostLoad
import com.tencent.devops.dispatch.docker.pojo.Load
import com.tencent.devops.dispatch.docker.pojo.enums.DockerHostClusterType
import com.tencent.devops.dispatch.pojo.enums.PipelineTaskStatus
import com.tencent.devops.model.dispatch.tables.records.TDispatchPipelineDockerBuildRecord
import com.tencent.devops.process.pojo.mq.PipelineAgentShutdownEvent
Expand Down Expand Up @@ -87,6 +90,74 @@ class DockerHostBuildService @Autowired constructor(
fun enable(pipelineId: String, vmSeqId: Int?, enable: Boolean) =
pipelineDockerEnableDao.enable(dslContext, pipelineId, vmSeqId, enable)

fun getDockerHostLoad(userId: String): DockerHostLoad {
try {
return DockerHostLoad(
// 页面展示的key和文档对齐
clusterLoad = mapOf(
"docker" to getLoad(DockerHostClusterType.COMMON),
"agentless" to getLoad(DockerHostClusterType.AGENT_LESS),
"macos-10.15" to getLoad(DockerHostClusterType.MACOS)
)
)
} catch (e: Exception) {
LOG.error("$userId getDockerHostLoad error.", e)
throw RuntimeException("getDockerHostLoad error.")
}
}

private fun getLoad(clusterType: DockerHostClusterType): Load {
val dockerIpList = pipelineDockerIPInfoDao.getDockerIpList(
dslContext = dslContext,
enable = true,
grayEnv = false,
clusterName = clusterType
)

if (dockerIpList.isEmpty()) {
return Load(
averageCpuLoad = 0,
averageMemLoad = 0,
averageDiskIOLoad = 0,
averageDiskLoad = 0,
usedNum = 0,
enableNode = dockerIpList.size,
totalNode = pipelineDockerIPInfoDao.getAllDockerIpCount(
dslContext = dslContext,
grayEnv = false,
clusterName = clusterType
)?.toInt() ?: 0
)
}

var totalCpu = 0
var totalMem = 0
var totalDiskIO = 0
var totalDisk = 0
var totalUsed = 0
dockerIpList.forEach {
totalCpu += it.cpuLoad
totalMem += it.memLoad
totalDiskIO += it.diskIoLoad
totalDisk += it.diskLoad
totalUsed += it.usedNum
}

return Load(
averageCpuLoad = totalCpu / dockerIpList.size,
averageMemLoad = totalMem / dockerIpList.size,
averageDiskIOLoad = totalDiskIO / dockerIpList.size,
averageDiskLoad = totalDisk / dockerIpList.size,
usedNum = totalUsed / dockerIpList.size,
enableNode = dockerIpList.size,
totalNode = pipelineDockerIPInfoDao.getAllDockerIpCount(
dslContext = dslContext,
grayEnv = false,
clusterName = clusterType
)?.toInt() ?: 0
)
}

/* fun dockerHostBuild(event: PipelineAgentStartupEvent) {
val dispatchType = event.dispatchType as DockerDispatchType
dslContext.transaction { configuration ->
Expand Down
Loading

0 comments on commit 0c766da

Please sign in to comment.