Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Image checkImageInspect接口优化 #9609 #9610

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,27 @@

package com.tencent.devops.image.service

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.dockerjava.api.model.PullResponseItem
import com.github.dockerjava.core.DefaultDockerClientConfig
import com.github.dockerjava.core.DockerClientBuilder
import com.github.dockerjava.core.command.PullImageResultCallback
import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.OkhttpUtils
import com.tencent.devops.image.config.DockerConfig
import com.tencent.devops.image.pojo.CheckDockerImageRequest
import com.tencent.devops.image.pojo.CheckDockerImageResponse
import com.tencent.devops.image.utils.CommonUtils
import okhttp3.Headers.Companion.toHeaders
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service

@Service
Expand All @@ -55,11 +66,19 @@ class InspectImageService @Autowired constructor(

private val dockerCli = DockerClientBuilder.getInstance(config).build()

@Value("\${image.checkImageUrl:}")
var checkImageUrl: String? = null

fun checkDockerImage(
userId: String,
checkDockerImageRequestList: List<CheckDockerImageRequest>
): List<CheckDockerImageResponse> {
logger.info("checkImage userId: $userId, checkDockerImageRequestList: $checkDockerImageRequestList")

if (!checkImageUrl.isNullOrBlank()) {
return checkRemoteDockerImage(userId, checkDockerImageRequestList)
}

val imageInspectList = mutableListOf<CheckDockerImageResponse>()
checkDockerImageRequestList.parallelStream().forEach {
// 判断用户录入的镜像信息是否能正常拉取到镜像
Expand Down Expand Up @@ -129,6 +148,40 @@ class InspectImageService @Autowired constructor(
return imageInspectList
}

fun checkRemoteDockerImage(
userId: String,
checkDockerImageRequestList: List<CheckDockerImageRequest>
): List<CheckDockerImageResponse> {
try {
val url = "$checkImageUrl/api/service/docker-image/checkDockerImage"
val request = Request
.Builder()
.url(url)
.headers(mutableMapOf(AUTH_HEADER_USER_ID to userId).toHeaders())
.post(
JsonUtil.toJson(checkDockerImageRequestList)
.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
)
.build()

OkhttpUtils.doHttp(request).use { response ->
val responseContent = response.body!!.string()
logger.info("$userId check remoteImage: $responseContent")
if (!response.isSuccessful) {
logger.error("Check remoteImage fail. $responseContent")
throw RuntimeException("Check remoteImage fail. $responseContent")
}

val responseMap: Map<String, Any> = jacksonObjectMapper().readValue(responseContent)
return JsonUtil.to(JsonUtil.toJson(responseMap["data"] ?: ""),
object : TypeReference<List<CheckDockerImageResponse>>() {})
}
} catch (e: Exception) {
logger.error("Check remoteImage error: ${e.message}")
throw RuntimeException("Check remoteImage error: ${e.message}")
}
}

inner class MyPullImageResultCallback internal constructor(
private val userId: String
) : PullImageResultCallback() {
Expand Down