Skip to content

Commit

Permalink
Merge pull request #25 from Yuanruitao/master
Browse files Browse the repository at this point in the history
feat:升级Atom Sdk, 新增Kubernetes接入 #23
  • Loading branch information
Yuanruitao authored Nov 19, 2022
2 parents 8070e82 + 0c91d8f commit f0e705a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 135 deletions.
13 changes: 8 additions & 5 deletions src/backend/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:5.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
classpath "org.jetbrains.kotlin:kotlin-allopen:1.3.21"
}

Expand All @@ -17,7 +17,10 @@ buildscript {
svnkitVersion = "1.9.3"
commonExecVersion = "1.3"
jacksonVersion = "2.9.2"
jacksonDatabindVersion = "2.13.4.2"
compressVersion = "1.15"
javaPluginSdkVersion = "1.1.5"
kotlinVersion = "1.5.30"
}
}

Expand Down Expand Up @@ -80,22 +83,22 @@ allprojects {
}

dependencies {
compile "com.tencent.devops.ci-plugins:java-plugin-sdk:1.1.5"
compile files("lib/java-plugin-sdk-1.0.7.jar")
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "com.tencent.devops.ci-plugins:java-plugin-sdk:${javaPluginSdkVersion}"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
compile "org.bouncycastle:bcprov-jdk16:$bouncyCastleVersion"
compile "org.apache.commons:commons-exec:$commonExecVersion"
compile "com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$jacksonVersion"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion"
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonDatabindVersion"
compile "org.apache.commons:commons-compress:$compressVersion"
compile "org.hashids:hashids:1.0.3"
compile group: 'commons-codec', name: 'commons-codec', version: '1.15'
compile("org.apache.commons:commons-compress:1.15")
compile "org.tukaani:xz:1.2"
testCompile group: 'junit', name: 'junit', version: '4.12'
}


compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Expand Down
149 changes: 19 additions & 130 deletions src/backend/core/src/main/kotlin/com/tencent/devops/docker/DockerRun.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package com.tencent.devops.docker

import com.tencent.bk.devops.atom.pojo.Result
import com.tencent.bk.devops.plugin.api.impl.KubernetesBuildApi
import com.tencent.bk.devops.plugin.docker.CommonExecutor
import com.tencent.bk.devops.plugin.docker.PcgDevCloudExecutor
import com.tencent.bk.devops.plugin.docker.DockerApi
import com.tencent.bk.devops.plugin.docker.KubernetesExecutor
import com.tencent.bk.devops.plugin.docker.ThirdPartExecutor
import com.tencent.bk.devops.plugin.docker.exception.DockerRunLogException
import com.tencent.bk.devops.plugin.docker.pojo.DockerRunLogRequest
import com.tencent.bk.devops.plugin.docker.pojo.DockerRunLogResponse
import com.tencent.bk.devops.plugin.docker.pojo.DockerRunRequest
import com.tencent.bk.devops.plugin.docker.pojo.common.DockerStatus
import com.tencent.bk.devops.plugin.docker.pojo.common.KubernetesPodStatus
import com.tencent.bk.devops.plugin.docker.pojo.job.response.JobStatusResp
import com.tencent.devops.docker.pojo.CommandParam
import com.tencent.devops.docker.pojo.ImageParam
import com.tencent.devops.docker.tools.LogUtils
import com.tencent.devops.docker.utils.CodeccConfig
import com.tencent.devops.pojo.exception.CodeccDependentException
import com.tencent.devops.pojo.exception.CodeccTaskExecException
import java.io.File

object DockerRun {
val api = DockerApi()

// 64位containerId,预留docker后续升级拓展位
private val containerIdRegex = Regex("^[0-9a-z]{64,}$")

fun runImage(imageParam: ImageParam, commandParam: CommandParam, toolName: String) {
LogUtils.printLog("execute image params: $imageParam")

Expand All @@ -41,10 +37,7 @@ object DockerRun {
PcgDevCloudExecutor.PCG_TOKEN_SECRET_ID to (commandParam.extraPrams[PcgDevCloudExecutor.PCG_TOKEN_SECRET_ID] ?: ""),
PcgDevCloudExecutor.PCG_TOKEN_SECRET_KEY to (commandParam.extraPrams[PcgDevCloudExecutor.PCG_TOKEN_SECRET_KEY] ?: ""),
PcgDevCloudExecutor.PCG_REQUEST_HOST to (commandParam.extraPrams[PcgDevCloudExecutor.PCG_REQUEST_HOST] ?: "")
)),
// 不指定CPU
cpu = null,
memory = null
))
)
val dockerRunResponse = api.dockerRunCommand(
projectId = commandParam.landunParam.devopsProjectId,
Expand Down Expand Up @@ -96,8 +89,9 @@ object DockerRun {
}

private fun getRunLogResponse(api: DockerApi, commandParam: CommandParam, extraOptions: Map<String, String>, timeGap: Long): DockerRunLogResponse {
var response: DockerRunLogResponse? = null
try {
return dockerRunGetLog(
response = api.dockerRunGetLog(
projectId = commandParam.landunParam.devopsProjectId,
pipelineId = commandParam.landunParam.devopsPipelineId,
buildId = commandParam.landunParam.buildId,
Expand All @@ -108,132 +102,27 @@ object DockerRun {
extraOptions = extraOptions
)
).data!!
return response
} catch (e: Exception) {
LogUtils.printErrorLog("get docker run log response: $response")
LogUtils.printErrorLog("fail to get docker run log: ${commandParam.landunParam.buildId}, " +
"${commandParam.landunParam.devopsVmSeqId}, " +
extraOptions.filter { !it.key.contains("token", ignoreCase = true) }
"${commandParam.landunParam.devopsVmSeqId}, " +
extraOptions.filter { !it.key.contains("token", ignoreCase = true) }
)
e.printStackTrace()
}
return DockerRunLogResponse(
log = listOf(),
status = DockerStatus.running,
message = "",
extraOptions = extraOptions
)
}

private fun dockerRunGetLog(
projectId: String,
pipelineId: String,
buildId: String,
param: DockerRunLogRequest
): Result<DockerRunLogResponse> {
try {
val property = System.getenv("devops_slave_model")
val kubernetesEnv = System.getenv("devops.build.node.environment")

val response = when {
"docker" == property -> CommonExecutor.getLogs(projectId, pipelineId, buildId, param)
"Kubernetes" == kubernetesEnv -> getLogs(param)
else -> ThirdPartExecutor.getLogs(param)
}


return com.tencent.bk.devops.atom.pojo.Result(response)
} catch (e: Exception) {
throw DockerRunLogException(e.message ?: "")
}
}

fun getLogs(param: DockerRunLogRequest): DockerRunLogResponse {
val extraOptions = param.extraOptions.toMutableMap()

val api = KubernetesBuildApi()

LogUtils.printLog("[kubernetes]:getLog|param|$param")

// get job status
val jobStatusFlag = param.extraOptions["jobStatusFlag"]
val jobName = param.extraOptions["kubernetesJobName"] ?: throw RuntimeException("kubernetesJobName is null")
var jobStatusResp: JobStatusResp? = null
var jobIp = ""
if (jobStatusFlag.isNullOrBlank() || jobStatusFlag == DockerStatus.running) {
jobStatusResp = api.getJobStatus(jobName).data

LogUtils.printLog("[kubernetes]:getLog|jobStatusFlag|resp|$jobStatusResp")

jobIp = jobStatusResp?.pod_result!![0].ip ?: ""
val jobStatus = jobStatusResp.status
if (KubernetesPodStatus.failed != jobStatus &&
"succeeded" != jobStatus &&
KubernetesPodStatus.running != jobStatus
) {
return DockerRunLogResponse(
status = DockerStatus.running,
message = "get job status...",
extraOptions = extraOptions
)
}
}
extraOptions["jobIp"] = jobIp
extraOptions["jobStatusFlag"] = DockerStatus.success

// actual get log logic
val startTimeStamp = extraOptions["startTimeStamp"]?.apply {
if (trim().length == 13) {
toLong() / 1000
// container id不存在则认为失败
val containerId = extraOptions["dockerContainerId"]
if (containerId.isNullOrBlank() || !containerId.matches(containerIdRegex)) {
LogUtils.printErrorLog("get docker containerId fail : $containerId")
throw CodeccDependentException(e.message ?: "")
} else {
toLong()
e.printStackTrace()
}
}?.toLong() ?: (System.currentTimeMillis() / 1000)
val logs = mutableListOf<String>()

val logResult = api.getLog(jobName, startTimeStamp)

// only if not blank then add start time
val isNotBlank = logResult.isNullOrBlank()
if (!isNotBlank) extraOptions["startTimeStamp"] = (startTimeStamp + param.timeGap).toString()

// add logs
if (!isNotBlank) logs.add(logResult!!)

if (jobStatusResp == null) {
jobStatusResp = api.getJobStatus(jobName).data
}
val finalStatus = jobStatusResp
val podResults = finalStatus?.pod_result
podResults?.forEach { ps ->
ps.events?.forEach { event ->
// add logs
logs.add(event.message)
}
}
LogUtils.printLog("[kubernetes]:getLog|finalStatus|resp|$finalStatus")

if (finalStatus?.status in listOf(KubernetesPodStatus.failed, "succeeded")) {
Thread.sleep(6000)
val finalLogs = api.getLog(jobName, startTimeStamp + 6000)
if (finalStatus?.status == KubernetesPodStatus.failed) {
return DockerRunLogResponse(
log = logs.plus(finalLogs ?: ""),
status = DockerStatus.failure,
message = "docker run fail...",
extraOptions = extraOptions
)
}
return DockerRunLogResponse(
log = logs.plus(finalLogs ?: ""),
status = DockerStatus.success,
message = "docker run success...",
extraOptions = extraOptions
)
}

return DockerRunLogResponse(
log = logs,
log = listOf(),
status = DockerStatus.running,
message = "get log...",
message = "",
extraOptions = extraOptions
)
}
Expand Down

0 comments on commit f0e705a

Please sign in to comment.