forked from TencentBlueKing/bk-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 构建并推送镜像支持插件接入 TencentBlueKing#5032
Signed-off-by: sawyersong <sawyersong@tencent.com>
- Loading branch information
1 parent
c1e0812
commit 4ad66a3
Showing
8 changed files
with
340 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...st/biz-dockerhost/src/main/kotlin/com/tencent/devops/dockerhost/services/image/Handler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.tencent.devops.dockerhost.services.image | ||
|
||
abstract class Handler<T: HandlerContext> { | ||
protected var nextHandler: Handler<T>? = null | ||
|
||
fun setNextHandler(handler: Handler<T>): Handler<T> { | ||
this.nextHandler = handler | ||
return this | ||
} | ||
abstract fun handlerRequest(handlerContext: T) | ||
} |
8 changes: 8 additions & 0 deletions
8
...dockerhost/src/main/kotlin/com/tencent/devops/dockerhost/services/image/HandlerContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.tencent.devops.dockerhost.services.image | ||
|
||
open class HandlerContext( | ||
open val projectId: String, | ||
open val pipelineId: String, | ||
open val buildId: String, | ||
open val vmSeqId: String | ||
) |
129 changes: 129 additions & 0 deletions
129
...kerhost/src/main/kotlin/com/tencent/devops/dockerhost/services/image/ImageBuildHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.tencent.devops.dockerhost.services.image | ||
|
||
import com.github.dockerjava.api.command.BuildImageResultCallback | ||
import com.github.dockerjava.api.model.AuthConfig | ||
import com.github.dockerjava.api.model.AuthConfigurations | ||
import com.github.dockerjava.api.model.BuildResponseItem | ||
import com.tencent.devops.dockerhost.config.DockerHostConfig | ||
import com.tencent.devops.dockerhost.dispatch.DockerHostBuildResourceApi | ||
import org.apache.commons.lang3.StringUtils | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Service | ||
import java.io.File | ||
import java.nio.file.Paths | ||
|
||
@Service | ||
class ImageBuildHandler( | ||
private val dockerHostConfig: DockerHostConfig, | ||
private val dockerHostBuildApi: DockerHostBuildResourceApi | ||
) : Handler<ImageHandlerContext>() { | ||
override fun handlerRequest(handlerContext: ImageHandlerContext) { | ||
with(handlerContext) { | ||
val authConfigurations = AuthConfigurations() | ||
val ticket = dockerBuildParam.ticket | ||
val args = dockerBuildParam.args | ||
ticket.forEach { | ||
val baseConfig = AuthConfig() | ||
.withUsername(it.second) | ||
.withPassword(it.third) | ||
.withRegistryAddress(it.first) | ||
authConfigurations.addConfig(baseConfig) | ||
} | ||
|
||
val workspace = getWorkspace(pipelineId, vmSeqId.toInt(), dockerBuildParam.poolNo ?: "0") | ||
val buildDir = Paths.get(workspace + dockerBuildParam.buildDir).normalize().toString() | ||
val dockerfilePath = Paths.get(workspace + dockerBuildParam.dockerFile).normalize().toString() | ||
val baseDirectory = File(buildDir) | ||
val dockerfile = File(dockerfilePath) | ||
|
||
val imageNameTagSet = mutableSetOf<String>() | ||
if (dockerBuildParam.imageTagList.isNotEmpty()) { | ||
dockerBuildParam.imageTagList.forEach { | ||
imageNameTagSet.add(getImageNameWithTag( | ||
repoAddr = dockerBuildParam.repoAddr, | ||
projectId = projectId, | ||
imageName = dockerBuildParam.imageName, | ||
imageTag = it, | ||
outer = outer | ||
)) | ||
} | ||
} else { | ||
imageNameTagSet.add(getImageNameWithTag( | ||
repoAddr = dockerBuildParam.repoAddr, | ||
projectId = projectId, | ||
imageName = dockerBuildParam.imageName, | ||
imageTag = dockerBuildParam.imageTag, | ||
outer = outer | ||
)) | ||
} | ||
|
||
this.imageTagSet = imageNameTagSet | ||
|
||
logger.info("[$buildId]|[$vmSeqId] Build docker image, workspace: $workspace, buildDir:$buildDir, dockerfile: $dockerfilePath") | ||
logger.info("[$buildId]|[$vmSeqId] Build docker image, imageNameTag: $imageNameTagSet") | ||
val step = dockerClient.buildImageCmd().withNoCache(true) | ||
.withPull(true) | ||
.withBuildAuthConfigs(authConfigurations) | ||
.withBaseDirectory(baseDirectory) | ||
.withDockerfile(dockerfile) | ||
.withTags(imageNameTagSet) | ||
args.map { it.trim().split("=") }.forEach { | ||
step.withBuildArg(it.first(), it.last()) | ||
} | ||
step.exec(MyBuildImageResultCallback(buildId, pipelineTaskId, dockerHostBuildApi)) | ||
.awaitImageId() | ||
|
||
nextHandler?.handlerRequest(this) | ||
} | ||
} | ||
|
||
private fun getWorkspace(pipelineId: String, vmSeqId: Int, poolNo: String): String { | ||
return "${dockerHostConfig.hostPathWorkspace}/$pipelineId/${getTailPath(vmSeqId, poolNo.toInt())}/" | ||
} | ||
|
||
private fun getTailPath(vmSeqId: Int, poolNo: Int): String { | ||
return if (poolNo > 1) { | ||
"$vmSeqId" + "_$poolNo" | ||
} else { | ||
vmSeqId.toString() | ||
} | ||
} | ||
|
||
private fun getImageNameWithTag( | ||
repoAddr: String, | ||
projectId: String, | ||
imageName: String, | ||
imageTag: String, | ||
outer: Boolean = false | ||
): String { | ||
return if (outer) { | ||
"$repoAddr/$imageName:$imageTag" | ||
} else { | ||
"$repoAddr/paas/$projectId/$imageName:$imageTag" | ||
} | ||
} | ||
|
||
inner class MyBuildImageResultCallback internal constructor( | ||
private val buildId: String, | ||
private val elementId: String?, | ||
private val dockerHostBuildApi: DockerHostBuildResourceApi | ||
) : BuildImageResultCallback() { | ||
override fun onNext(item: BuildResponseItem?) { | ||
val text = item?.stream | ||
if (null != text) { | ||
dockerHostBuildApi.postLog( | ||
buildId, | ||
false, | ||
StringUtils.removeEnd(text, "\n"), | ||
elementId | ||
) | ||
} | ||
|
||
super.onNext(item) | ||
} | ||
} | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(ImageHandlerContext::class.java) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...rhost/src/main/kotlin/com/tencent/devops/dockerhost/services/image/ImageHandlerContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.tencent.devops.dockerhost.services.image | ||
|
||
import com.github.dockerjava.api.DockerClient | ||
import com.tencent.devops.dockerhost.pojo.DockerBuildParam | ||
|
||
data class ImageHandlerContext( | ||
val outer: Boolean, // 是否为外部请求创建镜像 | ||
val dockerClient: DockerClient, | ||
val pipelineTaskId: String?, | ||
val dockerBuildParam: DockerBuildParam, | ||
var imageTagSet: MutableSet<String> = mutableSetOf(), | ||
override val projectId: String, | ||
override val pipelineId: String, | ||
override val buildId: String, | ||
override val vmSeqId: String | ||
) : HandlerContext( | ||
projectId = projectId, | ||
pipelineId = pipelineId, | ||
buildId = buildId, | ||
vmSeqId = vmSeqId | ||
) |
Oops, something went wrong.