Skip to content

Commit

Permalink
feat: 构建并推送镜像支持插件接入 TencentBlueKing#5032
Browse files Browse the repository at this point in the history
Signed-off-by: sawyersong <sawyersong@tencent.com>
  • Loading branch information
sawyersong2 committed Sep 2, 2021
1 parent 66e3b72 commit 996e69d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ interface BuildDockerHostResource {
@ApiParam(value = "elementId", required = true)
@QueryParam("elementId")
elementId: String,
@ApiParam(value = "是否同步构建", required = false)
@QueryParam("syncFlag")
syncFlag: Boolean?,
@ApiParam("镜像名称", required = true)
@ApiParam(value = "是否开启镜像扫描", required = false)
@QueryParam("scanFlag")
scanFlag: Boolean?,
@ApiParam("镜像构建参数", required = true)
dockerBuildParam: DockerBuildParam
): Result<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ data class DockerBuildParam(
@ApiModelProperty("并发构建池序号", required = false)
val poolNo: String? = "0",
@ApiModelProperty("镜像tag列表", required = false)
val imageTagList: List<String> = emptyList()
val imageTagList: List<String> = emptyList(),
@ApiModelProperty("pipelineTaskId", required = false)
val pipelineTaskId: String = ""
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BuildDockerHostResourceImpl @Autowired constructor(private val dockerServi
vmSeqId: String,
buildId: String,
elementId: String,
syncFlag: Boolean?,
scanFlag: Boolean?,
dockerBuildParam: DockerBuildParam
): Result<Boolean> {
logger.info("Enter ServiceDockerHostResourceImpl.dockerBuild...")
Expand All @@ -57,7 +57,7 @@ class BuildDockerHostResourceImpl @Autowired constructor(private val dockerServi
elementId = elementId,
dockerBuildParam = dockerBuildParam,
outer = true,
syncFlag = syncFlag
scanFlag = scanFlag
))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.github.dockerjava.transport.DockerHttpClient
import com.tencent.devops.dockerhost.config.DockerHostConfig
import com.tencent.devops.dockerhost.pojo.DockerBuildParam
import com.tencent.devops.dockerhost.services.image.ImageBuildHandler
import com.tencent.devops.dockerhost.services.image.ImageDeleteHandler
import com.tencent.devops.dockerhost.services.image.ImageHandlerContext
import com.tencent.devops.dockerhost.services.image.ImagePushHandler
import com.tencent.devops.dockerhost.services.image.ImageScanHandler
Expand All @@ -49,7 +50,8 @@ class DockerHostImageService(
private val dockerHostImageScanService: DockerHostImageScanService,*/
private val imageBuildHandler: ImageBuildHandler,
private val imageScanHandler: ImageScanHandler,
private val imagePushHandler: ImagePushHandler
private val imagePushHandler: ImagePushHandler,
private val imageDeleteHandler: ImageDeleteHandler
) {

companion object {
Expand All @@ -63,7 +65,8 @@ class DockerHostImageService(
dockerBuildParam: DockerBuildParam,
buildId: String,
elementId: String?,
outer: Boolean
outer: Boolean,
scanFlag: Boolean
): Pair<Boolean, String?> {
lateinit var dockerClient: DockerClient
try {
Expand All @@ -87,18 +90,24 @@ class DockerHostImageService(

dockerClient = DockerClientBuilder.getInstance(config).withDockerHttpClient(longHttpClient).build()

imageBuildHandler.setNextHandler(imageScanHandler.setNextHandler(imagePushHandler))
.handlerRequest(
ImageHandlerContext(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
vmSeqId = vmSeqId,
dockerBuildParam = dockerBuildParam,
dockerClient = dockerClient,
pipelineTaskId = elementId,
outer = outer
))
val imageHandlerContext = ImageHandlerContext(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
vmSeqId = vmSeqId,
dockerBuildParam = dockerBuildParam,
dockerClient = dockerClient,
pipelineTaskId = elementId,
outer = outer,
scanFlag = scanFlag
)

// 同步扫描
imageBuildHandler.setNextHandler(
imageScanHandler.setNextHandler(
imagePushHandler.setNextHandler(imageDeleteHandler)
)
).handlerRequest(imageHandlerContext)

return Pair(true, null)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,23 @@ class DockerService @Autowired constructor(
elementId: String?,
dockerBuildParam: DockerBuildParam,
outer: Boolean = false,
syncFlag: Boolean? = false
scanFlag: Boolean? = false
): Boolean {
logger.info("[$buildId]|projectId=$projectId|pipelineId=$pipelineId|vmSeqId=$vmSeqId|param=$dockerBuildParam")
if (null != syncFlag && syncFlag) {
return dockerHostImageService.dockerBuildAndPushImage(
projectId = projectId,
pipelineId = pipelineId,
vmSeqId = vmSeqId,
dockerBuildParam = dockerBuildParam,
buildId = buildId,
elementId = elementId,
outer = outer
).first
} else {
val future = executor.submit(Callable<Pair<Boolean, String?>> {
dockerHostImageService.dockerBuildAndPushImage(
projectId = projectId,
pipelineId = pipelineId,
vmSeqId = vmSeqId,
dockerBuildParam = dockerBuildParam,
buildId = buildId,
elementId = elementId,
outer = outer
)
})
buildTask[getKey(vmSeqId, buildId)] = future
return true
}
val future = executor.submit(Callable<Pair<Boolean, String?>> {
dockerHostImageService.dockerBuildAndPushImage(
projectId = projectId,
pipelineId = pipelineId,
vmSeqId = vmSeqId,
dockerBuildParam = dockerBuildParam,
buildId = buildId,
elementId = elementId,
outer = outer,
scanFlag = scanFlag ?: false
)
})
buildTask[getKey(vmSeqId, buildId)] = future
return true
}

fun getBuildResult(vmSeqId: String, buildId: String): Pair<Status, String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.tencent.devops.dockerhost.services.image

import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class ImageDeleteHandler : Handler<ImageHandlerContext>() {
override fun handlerRequest(handlerContext: ImageHandlerContext) {
with(handlerContext) {
if (!scanFlag) {
imageTagSet.parallelStream().forEach {
logger.info("[$buildId]|[$vmSeqId] Push image success, now remove local image, image name and tag: $it")
try {
dockerClient.removeImageCmd(it).exec()
logger.info("[$buildId]|[$vmSeqId] Remove local image success")
} catch (e: Throwable) {
logger.error("[$buildId]|[$vmSeqId] Docker rmi failed, msg: ${e.message}")
}
}
}
}
}

companion object {
private val logger = LoggerFactory.getLogger(ImageDeleteHandler::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tencent.devops.dockerhost.pojo.DockerBuildParam

data class ImageHandlerContext(
val outer: Boolean, // 是否为外部请求创建镜像
val scanFlag: Boolean, // 镜像扫描开关
val dockerClient: DockerClient,
val pipelineTaskId: String?,
val dockerBuildParam: DockerBuildParam,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ class ImagePushHandler(
.withAuthConfig(authConfig)
.exec(MyPushImageResultCallback(buildId, pipelineTaskId, dockerHostBuildApi))
.awaitCompletion()

logger.info("[$buildId]|[$vmSeqId] Push image success, now remove local image, image name and tag: $it")
try {
dockerClient.removeImageCmd(it).exec()
logger.info("[$buildId]|[$vmSeqId] Remove local image success")
} catch (e: Throwable) {
logger.error("[$buildId]|[$vmSeqId] Docker rmi failed, msg: ${e.message}")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ class ImageScanHandler(
) : Handler<ImageHandlerContext>() {
override fun handlerRequest(handlerContext: ImageHandlerContext) {
with(handlerContext) {
dockerHostImageScanService.scanningDocker(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
vmSeqId = vmSeqId,
imageTagSet = imageTagSet,
dockerClient = dockerClient
)
if (scanFlag) {
dockerHostImageScanService.scanningDocker(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
vmSeqId = vmSeqId,
imageTagSet = imageTagSet,
dockerClient = dockerClient
)
}

nextHandler?.handlerRequest(this)
}
Expand Down

0 comments on commit 996e69d

Please sign in to comment.