From 840429b6df5064ddc3898963beb5c87fe8972b9c Mon Sep 17 00:00:00 2001 From: felixncheng Date: Mon, 11 Sep 2023 16:13:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=84=E7=90=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E7=9A=84=E9=97=AE=E9=A2=98=20#1086?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/TemporaryScanTokenServiceImpl.kt | 2 +- .../tencent/bkrepo/analyst/utils/K8SHelper.kt | 4 ++-- .../pojo/scanner/utils/DockerUtils.kt | 22 +++++++++++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/service/impl/TemporaryScanTokenServiceImpl.kt b/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/service/impl/TemporaryScanTokenServiceImpl.kt index 5c4261afc1..24d9d24b8e 100644 --- a/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/service/impl/TemporaryScanTokenServiceImpl.kt +++ b/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/service/impl/TemporaryScanTokenServiceImpl.kt @@ -64,7 +64,7 @@ import org.springframework.data.redis.connection.RedisStringCommands.SetOption.U import org.springframework.data.redis.core.RedisTemplate import org.springframework.data.redis.core.types.Expiration import org.springframework.stereotype.Service -import java.util.* +import java.util.Base64 import java.util.concurrent.TimeUnit import kotlin.collections.HashMap import kotlin.collections.LinkedHashMap diff --git a/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/utils/K8SHelper.kt b/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/utils/K8SHelper.kt index 2ab3321849..250daab9e4 100644 --- a/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/utils/K8SHelper.kt +++ b/src/backend/analyst/biz-analyst/src/main/kotlin/com/tencent/bkrepo/analyst/utils/K8SHelper.kt @@ -36,7 +36,7 @@ import io.kubernetes.client.openapi.ApiException import io.kubernetes.client.openapi.apis.CoreV1Api import io.kubernetes.client.openapi.models.V1Secret import org.slf4j.LoggerFactory -import java.util.* +import java.util.Base64 class K8SHelper(k8sProp: KubernetesExecutionClusterProperties) { @@ -48,7 +48,7 @@ class K8SHelper(k8sProp: KubernetesExecutionClusterProperties) { secretName: String, dockerServer: String, dockerUserName: String, - dockerPassword: String + dockerPassword: String, ): V1Secret { val dockerAuthBytes = "$dockerUserName:$dockerPassword".toByteArray() val dockerAuth = Base64.getEncoder().encodeToString(dockerAuthBytes) diff --git a/src/backend/common/common-analysis/src/main/kotlin/com/tencent/bkrepo/common/analysis/pojo/scanner/utils/DockerUtils.kt b/src/backend/common/common-analysis/src/main/kotlin/com/tencent/bkrepo/common/analysis/pojo/scanner/utils/DockerUtils.kt index b0aaff2fd2..b424947e50 100644 --- a/src/backend/common/common-analysis/src/main/kotlin/com/tencent/bkrepo/common/analysis/pojo/scanner/utils/DockerUtils.kt +++ b/src/backend/common/common-analysis/src/main/kotlin/com/tencent/bkrepo/common/analysis/pojo/scanner/utils/DockerUtils.kt @@ -28,6 +28,7 @@ package com.tencent.bkrepo.common.analysis.pojo.scanner.utils import com.github.dockerjava.api.DockerClient +import com.github.dockerjava.api.command.PullImageCmd import com.github.dockerjava.api.command.PullImageResultCallback import com.github.dockerjava.api.command.WaitContainerResultCallback import com.github.dockerjava.api.model.AuthConfig @@ -73,11 +74,7 @@ object DockerUtils { logger.info("pulling image: $tag") val elapsedTime = measureTimeMillis { val result = pullImageCmd(tag) - .withAuthConfig( - AuthConfig() - .withUsername(userName) - .withPassword(password) - ) + .withAuthConfigIfNeed(userName, password) .exec(PullImageResultCallback()) .awaitCompletion(DEFAULT_PULL_IMAGE_DURATION, TimeUnit.MILLISECONDS) if (!result) { @@ -92,7 +89,7 @@ object DockerUtils { userName: String?, password: String?, hostConfig: HostConfig? = null, - cmd: List? = null + cmd: List? = null, ): String { // 拉取镜像 pullImage(image, userName, password) @@ -122,7 +119,7 @@ object DockerUtils { binds: Binds, maxSize: Long, mem: Long, - withPrivileged: Boolean = false + withPrivileged: Boolean = false, ): HostConfig { return HostConfig().apply { withBinds(binds) @@ -149,4 +146,15 @@ object DockerUtils { } } } + + private fun PullImageCmd.withAuthConfigIfNeed(userName: String?, password: String?): PullImageCmd { + if (userName != null && password != null) { + withAuthConfig( + AuthConfig() + .withUsername(userName) + .withPassword(password), + ) + } + return this + } }