From 2cd94faba2308ba30ad3a8b6a5e3339133d95f9b Mon Sep 17 00:00:00 2001 From: felixncheng Date: Thu, 18 Jul 2024 15:48:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81k8s=20shell=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=87=BA=E9=94=99=E6=97=B6=EF=BC=8C=E4=B8=8A=E6=8A=A5?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E6=97=A5=E5=BF=97=20#210=20#208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../schedule/handler/K8sShellHandler.kt | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/devops-boot-project/devops-boot-core/devops-schedule/devops-schedule-worker/src/main/kotlin/com/tencent/devops/schedule/handler/K8sShellHandler.kt b/devops-boot-project/devops-boot-core/devops-schedule/devops-schedule-worker/src/main/kotlin/com/tencent/devops/schedule/handler/K8sShellHandler.kt index 85c598a..60da9b5 100644 --- a/devops-boot-project/devops-boot-core/devops-schedule/devops-schedule-worker/src/main/kotlin/com/tencent/devops/schedule/handler/K8sShellHandler.kt +++ b/devops-boot-project/devops-boot-core/devops-schedule/devops-schedule-worker/src/main/kotlin/com/tencent/devops/schedule/handler/K8sShellHandler.kt @@ -57,8 +57,8 @@ class K8sShellHandler( } data = mapOf(CMD to source) } - val configMap = api.createNamespacedConfigMap(namespace, configMapBody, null, null, null) - logger.info("Created configmap $configMap") + api.createNamespacedConfigMap(namespace, configMapBody, null, null, null) + logger.info("Created configmap $configMapName") } val podBody = V1Pod { metadata { @@ -73,7 +73,8 @@ class K8sShellHandler( setEnv(context) volumeMounts { name = "shell-$logId" - mountPath = WORK_SPACE + mountPath = "$WORK_SPACE/$CMD" + subPath = CMD readOnly = true } resources { @@ -116,21 +117,37 @@ class K8sShellHandler( status = pod?.status?.phase.orEmpty() } logger.info("Pod status: $status") - val log = api.readNamespacedPodLog( - podName, - namespace, - logId, - true, - null, - null, - null, - null, - null, - null, - null, - ) - logger.info("Pod log: $log") - check(pod?.status?.phase == "Succeeded") + if (logger.isDebugEnabled) { + val log = api.readNamespacedPodLog( + podName, + namespace, + logId, + false, + null, + null, + null, + null, + null, + null, + null, + ) + logger.debug("Pod log: $log") + } + check(pod?.status?.phase == "Succeeded") { + api.readNamespacedPodLog( + podName, + namespace, + logId, + false, + null, + null, + null, + null, + null, + LOG_TAIL_LINES, + null, + ) + } } catch (e: ApiException) { logger.error(e.buildMessage()) throw e @@ -179,5 +196,6 @@ class K8sShellHandler( private val logger = LoggerFactory.getLogger(K8sShellHandler::class.java) private const val CMD = "run.sh" private const val WORK_SPACE = "/data/workspace" + private const val LOG_TAIL_LINES = 20 } }