Skip to content

Commit

Permalink
Merge pull request #11076 from tangruotian/issue-10962
Browse files Browse the repository at this point in the history
第三方构建机DockerUi界面支持 #10962
  • Loading branch information
bkci-bot authored Oct 18, 2024
2 parents a76f4bb + e9eb85e commit 748da0e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.tencent.devops.common.pipeline.pojo.transfer.PreStep
import com.tencent.devops.common.pipeline.pojo.transfer.Resources
import com.tencent.devops.common.pipeline.type.BuildType
import com.tencent.devops.common.pipeline.type.StoreDispatchType
import com.tencent.devops.common.pipeline.type.agent.ThirdPartyAgentDispatch
import com.tencent.devops.common.pipeline.type.docker.ImageType
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.process.pojo.BuildTemplateAcrossInfo
Expand Down Expand Up @@ -121,12 +122,24 @@ class ContainerTransfer @Autowired(required = false) constructor(
)
}
}.getOrThrow()
// 获取imageName展示用
if (dispatchType is StoreDispatchType && dispatchType.imageType == ImageType.BKSTORE) {
val imageName = transferCache.getStoreImageDetail(
userId, dispatchType.imageCode!!, dispatchType.imageVersion
)?.name
dispatchType.imageName = imageName
}
if (dispatchType is ThirdPartyAgentDispatch &&
dispatchType.dockerInfo?.imageType == ImageType.BKSTORE &&
dispatchType.dockerInfo?.storeImage != null
) {
val imageName = transferCache.getStoreImageDetail(
userId = userId,
imageCode = dispatchType.dockerInfo!!.storeImage!!.imageCode,
imageVersion = dispatchType.dockerInfo!!.storeImage!!.imageVersion
)?.name
dispatchType.dockerInfo!!.storeImage!!.imageName = imageName
}
val vmContainer = VMBuildContainer(
jobId = job.id,
name = job.name ?: "Job-${jobIndex + 1}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ import com.tencent.devops.common.pipeline.enums.VMBaseOS
import com.tencent.devops.common.pipeline.type.DispatchType
import com.tencent.devops.common.pipeline.type.agent.Credential
import com.tencent.devops.common.pipeline.type.agent.ThirdPartyAgentDockerInfo
import com.tencent.devops.common.pipeline.type.agent.ThirdPartyAgentDockerInfoStoreImage
import com.tencent.devops.common.pipeline.type.agent.ThirdPartyAgentEnvDispatchType
import com.tencent.devops.common.pipeline.type.agent.ThirdPartyAgentIDDispatchType
import com.tencent.devops.common.pipeline.type.docker.ImageType
import com.tencent.devops.process.pojo.BuildTemplateAcrossInfo
import com.tencent.devops.process.yaml.transfer.VariableDefault.DEFAULT_JOB_PREPARE_TIMEOUT
import com.tencent.devops.process.yaml.transfer.VariableDefault.nullIfDefault
Expand Down Expand Up @@ -190,10 +192,21 @@ class DispatchTransfer @Autowired(required = false) constructor(
password = info.password,
credentialId = info.credId,
acrossTemplateId = info.acrossTemplateId,
jobId = job.id
jobId = job.id,
credentialProjectId = null
),
options = info.options,
imagePullPolicy = info.imagePullPolicy
imagePullPolicy = info.imagePullPolicy,
imageType = info.imageType,
storeImage = if (info.imageType == ImageType.BKSTORE && info.imageCode != null) {
ThirdPartyAgentDockerInfoStoreImage(
imageName = null,
imageCode = info.imageCode,
imageVersion = info.imageVersion
)
} else {
null
}
)
} else null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ object StreamDispatchUtils {
password = info.password,
credentialId = info.credId,
acrossTemplateId = info.acrossTemplateId,
jobId = job.id
jobId = job.id,
credentialProjectId = null
),
options = info.options,
imagePullPolicy = info.imagePullPolicy
imagePullPolicy = info.imagePullPolicy,
storeImage = null
)

return ThirdPartyAgentEnvDispatchType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ enum class PoolType {

private fun makeContainer(dockerInfo: ThirdPartyAgentDockerInfo?): Container3? {
if (dockerInfo == null) return null
// 使用研发商店镜像的情况
if (dockerInfo.storeImage != null) {
return Container3(
imageVersion = dockerInfo.storeImage?.imageVersion,
imageCode = dockerInfo.storeImage?.imageCode,
credentials = with(dockerInfo.credential) {
when {
this == null -> null
credentialId != null -> dockerInfo.credential?.credentialId?.ifBlank { null }
user != null && password != null -> Credentials(user!!, password!!)
else -> null
}
},
options = dockerInfo.options,
imagePullPolicy = dockerInfo.imagePullPolicy
)
}
return Container3(
image = dockerInfo.image,
credentials = with(dockerInfo.credential) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package com.tencent.devops.common.pipeline.type.agent
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude
import com.tencent.devops.common.api.util.EnvUtils
import com.tencent.devops.common.pipeline.type.docker.ImageType

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
data class ThirdPartyAgentDockerInfo(
var image: String,
var credential: Credential?,
var options: DockerOptions?,
var imagePullPolicy: String?
var imagePullPolicy: String?,
// 仅仅前端展示使用,不参与后端计算
val imageType: ImageType? = ImageType.THIRD,
val storeImage: ThirdPartyAgentDockerInfoStoreImage?
)

fun ThirdPartyAgentDockerInfo.replaceField(variables: Map<String, String>) {
Expand Down Expand Up @@ -51,7 +55,8 @@ data class Credential(
var credentialId: String?,
// 跨项目使用凭据相关信息
val acrossTemplateId: String?,
val jobId: String?
val jobId: String?,
var credentialProjectId: String?
)

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -63,6 +68,15 @@ data class DockerOptions(
var privileged: Boolean?
)

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
data class ThirdPartyAgentDockerInfoStoreImage(
// 仅仅前端展示使用,不参与后端计算
var imageName: String?,
val imageCode: String,
val imageVersion: String?
)

// 第三方构建机docker类型,调度使用,会带有调度相关信息
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object ThirdPartyAgentUtils {
val tickets = try {
DispatchCommonUtils.getCredential(
client = client,
projectId = projectId,
projectId = credInfo.credentialProjectId ?: projectId,
credentialId = credInfo.credentialId!!,
type = CredentialType.USERNAME_PASSWORD
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import com.tencent.devops.common.pipeline.matrix.DispatchInfo
import com.tencent.devops.common.pipeline.matrix.SampleDispatchInfo
import com.tencent.devops.common.pipeline.type.DispatchType
import com.tencent.devops.common.pipeline.type.StoreDispatchType
import com.tencent.devops.common.pipeline.type.agent.Credential
import com.tencent.devops.common.pipeline.type.agent.ThirdPartyAgentDispatch
import com.tencent.devops.common.pipeline.type.docker.ImageType
import com.tencent.devops.common.pipeline.type.kubernetes.KubernetesDispatchType
import com.tencent.devops.process.engine.service.store.StoreImageHelper
Expand All @@ -41,6 +43,7 @@ import com.tencent.devops.process.service.BuildVariableService
import com.tencent.devops.process.service.PipelineBuildTemplateAcrossInfoService
import com.tencent.devops.process.yaml.pojo.StreamDispatchInfo
import com.tencent.devops.process.yaml.v2.utils.StreamDispatchUtils
import com.tencent.devops.store.pojo.image.response.ImageRepoInfo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

Expand All @@ -64,6 +67,37 @@ class DispatchTypeParserImpl @Autowired constructor(
buildId: String,
dispatchType: DispatchType
) {
// 针对第三方构建机docker场景处理
if (dispatchType is ThirdPartyAgentDispatch && dispatchType.dockerInfo?.storeImage != null) {
val dockerInfo = dispatchType.dockerInfo!!
// 从商店获取镜像真实信息
val imageRepoInfo = storeImageHelper.getImageRepoInfo(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
imageCode = dockerInfo.storeImage!!.imageCode,
imageVersion = dockerInfo.storeImage!!.imageVersion,
defaultPrefix = ""
)
val completeImageName = getCompleteImageName(imageRepoInfo)
// 镜像来源替换为原始来源
dockerInfo.image = completeImageName
if (dockerInfo.credential == null) {
Credential(
credentialId = imageRepoInfo.ticketId,
credentialProjectId = imageRepoInfo.ticketProject,
jobId = null,
acrossTemplateId = null,
user = null,
password = null
).also { dockerInfo.credential = it }
} else {
dockerInfo.credential!!.credentialId = imageRepoInfo.ticketId
dockerInfo.credential!!.credentialProjectId = imageRepoInfo.ticketProject
}
}

if (dispatchType !is StoreDispatchType) {
return
}
Expand All @@ -81,18 +115,7 @@ class DispatchTypeParserImpl @Autowired constructor(
defaultPrefix = ""
)

val completeImageName = if (ImageType.BKDEVOPS == imageRepoInfo.sourceType) {
// 蓝盾项目源镜像
imageRepoInfo.repoName
} else {
// 第三方源镜像
// dockerhub镜像名称不带斜杠前缀
if (imageRepoInfo.repoUrl.isBlank()) {
imageRepoInfo.repoName
} else {
"${imageRepoInfo.repoUrl}/${imageRepoInfo.repoName}"
}
} + ":" + imageRepoInfo.repoTag
val completeImageName = getCompleteImageName(imageRepoInfo)
// 镜像来源替换为原始来源
dispatchType.imageType = imageRepoInfo.sourceType
dispatchType.value = completeImageName
Expand All @@ -110,6 +133,20 @@ class DispatchTypeParserImpl @Autowired constructor(
}
}

private fun getCompleteImageName(imageRepoInfo: ImageRepoInfo) =
if (ImageType.BKDEVOPS == imageRepoInfo.sourceType) {
// 蓝盾项目源镜像
imageRepoInfo.repoName
} else {
// 第三方源镜像
// dockerhub镜像名称不带斜杠前缀
if (imageRepoInfo.repoUrl.isBlank()) {
imageRepoInfo.repoName
} else {
"${imageRepoInfo.repoUrl}/${imageRepoInfo.repoName}"
}
} + ":" + imageRepoInfo.repoTag

override fun parseInfo(
projectId: String,
pipelineId: String,
Expand Down

0 comments on commit 748da0e

Please sign in to comment.