-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from ci-plugins/issue_6725
feat: 提供统一的云原生调度接入层 #6725
- Loading branch information
Showing
11 changed files
with
420 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/kotlin/com/tencent/bk/devops/plugin/api/impl/KubernetesBuildApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.tencent.bk.devops.plugin.api.impl | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference | ||
import com.tencent.bk.devops.atom.api.BaseApi | ||
import com.tencent.bk.devops.atom.utils.http.SdkUtils | ||
import com.tencent.bk.devops.atom.utils.json.JsonUtil | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchBuildImageReq | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchBuildStatusResp | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchJobLogResp | ||
import com.tencent.bk.devops.plugin.docker.utils.EnvUtils | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchJobReq | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchTaskResp | ||
import com.tencent.bk.devops.plugin.pojo.Result | ||
import okhttp3.RequestBody | ||
import org.apache.commons.io.FileUtils | ||
import org.slf4j.LoggerFactory | ||
import java.io.File | ||
import java.io.IOException | ||
import java.nio.charset.Charset | ||
|
||
/** | ||
* BCS接口类 | ||
*/ | ||
class KubernetesBuildApi : BaseApi() { | ||
|
||
fun createJob(dispatchJobReq: DispatchJobReq): Result<DispatchTaskResp?> { | ||
val path = "/dispatch-kubernetes/api/build/job/create" | ||
dispatchJobReq.copy(podNameSelector = EnvUtils.getHostName()) | ||
val requestBody = RequestBody.create(JSON_CONTENT_TYPE, JsonUtil.toJson(dispatchJobReq)) | ||
|
||
val request = buildPost(path, requestBody, mutableMapOf("X-DEVOPS-UID" to getUserId())) | ||
val responseContent = request(request, "kubernetes job失败") | ||
logger.debug("create kubernetes job response: $responseContent") | ||
|
||
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchTaskResp?>>() {}) | ||
} | ||
|
||
fun getJobStatus(jobName: String): Result<DispatchBuildStatusResp> { | ||
val path = "/dispatch-kubernetes/api/build/job/" + jobName + "/status" | ||
val request = buildGet(path, mutableMapOf("X-DEVOPS-UID" to getUserId())) | ||
val responseContent = request(request, "获取job状态失败") | ||
logger.debug("get kubernetes job status response: $responseContent") | ||
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchBuildStatusResp>>() {}) | ||
} | ||
|
||
fun getJobLogs(jobName: String, sinceTime: Int): Result<DispatchJobLogResp> { | ||
val path = "/dispatch-kubernetes/api/build/job/" + jobName + "/logs?sinceTime=" + sinceTime | ||
val request = buildGet(path, mutableMapOf("X-DEVOPS-UID" to getUserId())) | ||
val responseContent = request(request, "获取job日志失败") | ||
logger.debug("get kubernetes job logs response: $responseContent") | ||
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchJobLogResp>>() {}) | ||
} | ||
|
||
fun getTask(taskId: String): Result<DispatchBuildStatusResp> { | ||
val path = "/dispatch-kubernetes/api/build/task/status?taskId=" + taskId | ||
val request = buildGet(path, mutableMapOf("X-DEVOPS-UID" to getUserId())) | ||
val responseContent = request(request, "获取task信息失败") | ||
logger.debug("get kubernetes task response: $responseContent") | ||
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchBuildStatusResp>>() {}) | ||
} | ||
|
||
fun dockerBuildAndPush(dispatchBuildImageReq: DispatchBuildImageReq): Result<DispatchTaskResp?> { | ||
val path = "/dispatch-kubernetes/api/build/image/buildPush" | ||
dispatchBuildImageReq.copy(podName = EnvUtils.getHostName()) | ||
val requestBody = RequestBody.create(JSON_CONTENT_TYPE, JsonUtil.toJson(dispatchBuildImageReq)) | ||
|
||
val request = buildPost(path, requestBody, mutableMapOf("X-DEVOPS-UID" to getUserId())) | ||
val responseContent = request(request, "kubernetes docker build失败") | ||
logger.debug("docker build response: $responseContent") | ||
|
||
return JsonUtil.fromJson(responseContent, object : TypeReference<Result<DispatchTaskResp?>>() {}) | ||
} | ||
|
||
private fun getUserId(): String { | ||
val inputJson: String? | ||
try { | ||
inputJson = FileUtils.readFileToString( | ||
File(SdkUtils.getDataDir() + "/" + SdkUtils.getInputFile()), | ||
Charset.defaultCharset() | ||
) | ||
} catch (e: IOException) { | ||
logger.error("parse inputJson throw Exception", e) | ||
return "" | ||
} | ||
|
||
val inputMap: Map<String, Any> = JsonUtil.fromJson(inputJson, | ||
object : TypeReference<MutableMap<String, Any>>() {}) | ||
return inputMap["pipeline.start.user.name"] as String | ||
} | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(KubernetesBuildApi::class.java) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
src/main/kotlin/com/tencent/bk/devops/plugin/docker/KubernetesExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package com.tencent.bk.devops.plugin.docker | ||
|
||
import com.tencent.bk.devops.plugin.api.impl.KubernetesBuildApi | ||
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.DockerRunResponse | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchBuildStatusResp | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchJobReq | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.DockerRegistry | ||
import com.tencent.bk.devops.plugin.pojo.kubernetes.JobParam | ||
import com.tencent.bk.devops.plugin.docker.pojo.common.DockerStatus | ||
import com.tencent.bk.devops.plugin.docker.utils.EnvUtils | ||
import org.apache.commons.lang3.RandomStringUtils | ||
import org.apache.tools.ant.types.Commandline | ||
import org.slf4j.LoggerFactory | ||
|
||
object KubernetesExecutor { | ||
private const val VOLUME_SERVER = "volume_server" | ||
private const val VOLUME_PATH = "volume_path" | ||
private const val VOLUME_MOUNT_PATH = "volume_mount_path" | ||
|
||
private val logger = LoggerFactory.getLogger(KubernetesExecutor::class.java) | ||
|
||
fun execute(request: DockerRunRequest): DockerRunResponse { | ||
val startTimeStamp = System.currentTimeMillis() / 1000 | ||
val jobRequest = getJobRequest(request) | ||
val task = KubernetesBuildApi().createJob(jobRequest).data | ||
|
||
val extraOptionMap = mapOf( | ||
"kubernetesTaskId" to task?.taskId.toString(), | ||
"bcsJobName" to jobRequest.alias, | ||
"startTimeStamp" to startTimeStamp.toString() | ||
) | ||
|
||
return DockerRunResponse( | ||
extraOptions = request.extraOptions?.plus(extraOptionMap) ?: extraOptionMap | ||
) | ||
} | ||
|
||
fun getLogs(param: DockerRunLogRequest): DockerRunLogResponse { | ||
val extraOptions = param.extraOptions.toMutableMap() | ||
|
||
// get task status | ||
val taskId = param.extraOptions["kubernetesTaskId"] ?: throw RuntimeException("kubernetesTaskId is null") | ||
val taskStatusFlag = param.extraOptions["taskStatusFlag"] | ||
if (taskStatusFlag.isNullOrBlank() || taskStatusFlag == DockerStatus.running) { | ||
val taskStatus = KubernetesBuildApi().getTask(taskId).data | ||
taskStatus.let { | ||
if (taskStatus!!.status == "failed") { | ||
return DockerRunLogResponse( | ||
status = DockerStatus.failure, | ||
message = "get task status fail", | ||
extraOptions = extraOptions | ||
) | ||
} | ||
if (taskStatus.status != "succeeded") { | ||
return DockerRunLogResponse( | ||
status = DockerStatus.running, | ||
message = "get task status...", | ||
extraOptions = extraOptions | ||
) | ||
} | ||
} | ||
} | ||
extraOptions["taskStatusFlag"] = DockerStatus.success | ||
|
||
// get job status | ||
val jobStatusFlag = param.extraOptions["jobStatusFlag"] | ||
val jobName = param.extraOptions["bcsJobName"] ?: throw RuntimeException("bcsJobName is null") | ||
var jobStatusResp: DispatchBuildStatusResp? = null | ||
if (jobStatusFlag.isNullOrBlank() || jobStatusFlag == DockerStatus.running) { | ||
jobStatusResp = KubernetesBuildApi().getJobStatus(jobName).data!! | ||
val jobStatus = jobStatusResp.status | ||
if ("failed" != jobStatus && "succeeded" != jobStatus && "running" != jobStatus) { | ||
return DockerRunLogResponse( | ||
status = DockerStatus.running, | ||
message = "get job status...", | ||
extraOptions = extraOptions | ||
) | ||
} | ||
} | ||
extraOptions["jobStatusFlag"] = DockerStatus.success | ||
|
||
// actual get log logic | ||
val startTimeStamp = extraOptions["startTimeStamp"]?.toInt() ?: (System.currentTimeMillis() / 1000).toInt() | ||
val logs = mutableListOf<String>() | ||
|
||
val logResult = KubernetesBuildApi().getJobLogs(jobName, startTimeStamp).data!! | ||
|
||
if ((logResult.log != null && logResult.log.isNotEmpty()) || !logResult.errorMsg.isNullOrBlank()) { | ||
extraOptions["startTimeStamp"] = (startTimeStamp + param.timeGap).toString() | ||
logResult.log.let { | ||
logs.addAll(logResult.log ?: emptyList()) | ||
} | ||
|
||
logResult.errorMsg?.let { | ||
logs.add(logResult.errorMsg) | ||
} | ||
} | ||
|
||
if (jobStatusResp == null) { | ||
jobStatusResp = KubernetesBuildApi().getJobStatus(jobName).data | ||
} | ||
val finalStatus = jobStatusResp | ||
|
||
if (finalStatus!!.status in listOf("failed", "succeeded")) { | ||
logger.info("final job status data: $jobStatusResp") | ||
Thread.sleep(6000) | ||
val finalLogs = KubernetesBuildApi().getJobLogs(jobName, startTimeStamp + 6).data!! | ||
if (finalStatus.status == "failed") { | ||
return DockerRunLogResponse( | ||
log = logs.plus(finalLogs.errorMsg ?: ""), | ||
status = DockerStatus.failure, | ||
message = "docker run fail...", | ||
extraOptions = extraOptions | ||
) | ||
} | ||
return DockerRunLogResponse( | ||
log = logs.plus(finalLogs?.log ?: emptyList()), | ||
status = DockerStatus.success, | ||
message = "docker run success...", | ||
extraOptions = extraOptions | ||
) | ||
} | ||
|
||
return DockerRunLogResponse( | ||
log = logs, | ||
status = DockerStatus.running, | ||
message = "get log...", | ||
extraOptions = extraOptions | ||
) | ||
} | ||
|
||
private fun getJobRequest(param: DockerRunRequest): DispatchJobReq { | ||
with(param) { | ||
// get job param | ||
val cmdTmp = mutableListOf<String>() | ||
command.forEach { | ||
cmdTmp.add(it.removePrefix("\"").removeSuffix("\"").removePrefix("\'") | ||
.removeSuffix("\'")) | ||
} | ||
val cmd = if (cmdTmp.size == 1) { | ||
Commandline.translateCommandline(cmdTmp.first()).toList() | ||
} else { | ||
cmdTmp | ||
} | ||
val jobParam = JobParam( | ||
env = envMap, | ||
command = cmd, | ||
labels = labels, | ||
ipEnabled = ipEnabled | ||
) | ||
|
||
if (jobParam.nfsVolume == null) { | ||
val volumeServer = System.getenv(VOLUME_SERVER) | ||
if (!volumeServer.isNullOrBlank()) { | ||
jobParam.nfsVolume = listOf( | ||
JobParam.NfsVolume( | ||
System.getenv(VOLUME_SERVER), | ||
System.getenv(VOLUME_PATH), | ||
System.getenv(VOLUME_MOUNT_PATH) | ||
) | ||
) | ||
} | ||
} | ||
|
||
// get docker image host & path | ||
val imagePair = getImagePair(param.imageName) | ||
|
||
// get user pass param | ||
val registry = DockerRegistry( | ||
host = imagePair.first, | ||
username = param.dockerLoginUsername, | ||
password = param.dockerLoginPassword | ||
) | ||
|
||
return DispatchJobReq( | ||
alias = "job-${System.currentTimeMillis()}-${RandomStringUtils.randomAlphabetic(8).toLowerCase()}", | ||
activeDeadlineSeconds = 86400, | ||
image = imageName, | ||
registry = registry, | ||
params = jobParam, | ||
podNameSelector = EnvUtils.getHostName() | ||
) | ||
} | ||
} | ||
|
||
private fun getImagePair(imageName: String): Pair<String, String> { | ||
val targetImageRepo = imageName.split("/").first() | ||
val targetImageName = imageName.removePrefix(targetImageRepo).removeSuffix("/") | ||
return Pair(targetImageRepo, targetImageName) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/tencent/bk/devops/plugin/pojo/kubernetes/DispatchBuildImageReq.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. | ||
* | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. | ||
* | ||
* A copy of the MIT License is included in this file. | ||
* | ||
* | ||
* Terms of the MIT License: | ||
* --------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of | ||
* the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.tencent.bk.devops.plugin.pojo.kubernetes | ||
|
||
data class DispatchBuildImageReq( | ||
val jobName: String, | ||
val imageName: List<String>, | ||
val registry: List<Registry>, | ||
val fromRegistry: List<Registry>, | ||
val buildArgs: Map<String, Any>, | ||
val workPath: String, | ||
val dockerFile: String, | ||
val podName: String | ||
) | ||
|
||
data class Registry( | ||
val host: String, | ||
val username: String, | ||
val password: String | ||
) |
Oops, something went wrong.