|
| 1 | +package com.tencent.bk.devops.plugin.docker |
| 2 | + |
| 3 | +import com.tencent.bk.devops.plugin.api.impl.KubernetesBuildApi |
| 4 | +import com.tencent.bk.devops.plugin.docker.pojo.DockerRunLogRequest |
| 5 | +import com.tencent.bk.devops.plugin.docker.pojo.DockerRunLogResponse |
| 6 | +import com.tencent.bk.devops.plugin.docker.pojo.DockerRunRequest |
| 7 | +import com.tencent.bk.devops.plugin.docker.pojo.DockerRunResponse |
| 8 | +import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchBuildStatusResp |
| 9 | +import com.tencent.bk.devops.plugin.pojo.kubernetes.DispatchJobReq |
| 10 | +import com.tencent.bk.devops.plugin.pojo.kubernetes.DockerRegistry |
| 11 | +import com.tencent.bk.devops.plugin.pojo.kubernetes.JobParam |
| 12 | +import com.tencent.bk.devops.plugin.docker.pojo.common.DockerStatus |
| 13 | +import com.tencent.bk.devops.plugin.docker.utils.EnvUtils |
| 14 | +import org.apache.commons.lang3.RandomStringUtils |
| 15 | +import org.apache.tools.ant.types.Commandline |
| 16 | +import org.slf4j.LoggerFactory |
| 17 | + |
| 18 | +object KubernetesExecutor { |
| 19 | + private const val VOLUME_SERVER = "volume_server" |
| 20 | + private const val VOLUME_PATH = "volume_path" |
| 21 | + private const val VOLUME_MOUNT_PATH = "volume_mount_path" |
| 22 | + |
| 23 | + private val logger = LoggerFactory.getLogger(KubernetesExecutor::class.java) |
| 24 | + |
| 25 | + fun execute(request: DockerRunRequest): DockerRunResponse { |
| 26 | + val startTimeStamp = System.currentTimeMillis() / 1000 |
| 27 | + val jobRequest = getJobRequest(request) |
| 28 | + val task = KubernetesBuildApi().createJob(jobRequest).data |
| 29 | + |
| 30 | + val extraOptionMap = mapOf( |
| 31 | + "kubernetesTaskId" to task?.taskId.toString(), |
| 32 | + "bcsJobName" to jobRequest.alias, |
| 33 | + "startTimeStamp" to startTimeStamp.toString() |
| 34 | + ) |
| 35 | + |
| 36 | + return DockerRunResponse( |
| 37 | + extraOptions = request.extraOptions?.plus(extraOptionMap) ?: extraOptionMap |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + fun getLogs(param: DockerRunLogRequest): DockerRunLogResponse { |
| 42 | + val extraOptions = param.extraOptions.toMutableMap() |
| 43 | + |
| 44 | + // get task status |
| 45 | + val taskId = param.extraOptions["kubernetesTaskId"] ?: throw RuntimeException("kubernetesTaskId is null") |
| 46 | + val taskStatusFlag = param.extraOptions["taskStatusFlag"] |
| 47 | + if (taskStatusFlag.isNullOrBlank() || taskStatusFlag == DockerStatus.running) { |
| 48 | + val taskStatus = KubernetesBuildApi().getTask(taskId).data |
| 49 | + taskStatus.let { |
| 50 | + if (taskStatus!!.status == "failed") { |
| 51 | + return DockerRunLogResponse( |
| 52 | + status = DockerStatus.failure, |
| 53 | + message = "get task status fail", |
| 54 | + extraOptions = extraOptions |
| 55 | + ) |
| 56 | + } |
| 57 | + if (taskStatus.status != "succeeded") { |
| 58 | + return DockerRunLogResponse( |
| 59 | + status = DockerStatus.running, |
| 60 | + message = "get task status...", |
| 61 | + extraOptions = extraOptions |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + extraOptions["taskStatusFlag"] = DockerStatus.success |
| 67 | + |
| 68 | + // get job status |
| 69 | + val jobStatusFlag = param.extraOptions["jobStatusFlag"] |
| 70 | + val jobName = param.extraOptions["bcsJobName"] ?: throw RuntimeException("bcsJobName is null") |
| 71 | + var jobStatusResp: DispatchBuildStatusResp? = null |
| 72 | + if (jobStatusFlag.isNullOrBlank() || jobStatusFlag == DockerStatus.running) { |
| 73 | + jobStatusResp = KubernetesBuildApi().getJobStatus(jobName).data!! |
| 74 | + val jobStatus = jobStatusResp.status |
| 75 | + if ("failed" != jobStatus && "succeeded" != jobStatus && "running" != jobStatus) { |
| 76 | + return DockerRunLogResponse( |
| 77 | + status = DockerStatus.running, |
| 78 | + message = "get job status...", |
| 79 | + extraOptions = extraOptions |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + extraOptions["jobStatusFlag"] = DockerStatus.success |
| 84 | + |
| 85 | + // actual get log logic |
| 86 | + val startTimeStamp = extraOptions["startTimeStamp"]?.toInt() ?: (System.currentTimeMillis() / 1000).toInt() |
| 87 | + val logs = mutableListOf<String>() |
| 88 | + |
| 89 | + val logResult = KubernetesBuildApi().getJobLogs(jobName, startTimeStamp).data!! |
| 90 | + |
| 91 | + if ((logResult.log != null && logResult.log.isNotEmpty()) || !logResult.errorMsg.isNullOrBlank()) { |
| 92 | + extraOptions["startTimeStamp"] = (startTimeStamp + param.timeGap).toString() |
| 93 | + logResult.log.let { |
| 94 | + logs.addAll(logResult.log ?: emptyList()) |
| 95 | + } |
| 96 | + |
| 97 | + logResult.errorMsg?.let { |
| 98 | + logs.add(logResult.errorMsg) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (jobStatusResp == null) { |
| 103 | + jobStatusResp = KubernetesBuildApi().getJobStatus(jobName).data |
| 104 | + } |
| 105 | + val finalStatus = jobStatusResp |
| 106 | + |
| 107 | + if (finalStatus!!.status in listOf("failed", "succeeded")) { |
| 108 | + logger.info("final job status data: $jobStatusResp") |
| 109 | + Thread.sleep(6000) |
| 110 | + val finalLogs = KubernetesBuildApi().getJobLogs(jobName, startTimeStamp + 6).data!! |
| 111 | + if (finalStatus.status == "failed") { |
| 112 | + return DockerRunLogResponse( |
| 113 | + log = logs.plus(finalLogs.errorMsg ?: ""), |
| 114 | + status = DockerStatus.failure, |
| 115 | + message = "docker run fail...", |
| 116 | + extraOptions = extraOptions |
| 117 | + ) |
| 118 | + } |
| 119 | + return DockerRunLogResponse( |
| 120 | + log = logs.plus(finalLogs?.log ?: emptyList()), |
| 121 | + status = DockerStatus.success, |
| 122 | + message = "docker run success...", |
| 123 | + extraOptions = extraOptions |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + return DockerRunLogResponse( |
| 128 | + log = logs, |
| 129 | + status = DockerStatus.running, |
| 130 | + message = "get log...", |
| 131 | + extraOptions = extraOptions |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + private fun getJobRequest(param: DockerRunRequest): DispatchJobReq { |
| 136 | + with(param) { |
| 137 | + // get job param |
| 138 | + val cmdTmp = mutableListOf<String>() |
| 139 | + command.forEach { |
| 140 | + cmdTmp.add(it.removePrefix("\"").removeSuffix("\"").removePrefix("\'") |
| 141 | + .removeSuffix("\'")) |
| 142 | + } |
| 143 | + val cmd = if (cmdTmp.size == 1) { |
| 144 | + Commandline.translateCommandline(cmdTmp.first()).toList() |
| 145 | + } else { |
| 146 | + cmdTmp |
| 147 | + } |
| 148 | + val jobParam = JobParam( |
| 149 | + env = envMap, |
| 150 | + command = cmd, |
| 151 | + labels = labels, |
| 152 | + ipEnabled = ipEnabled |
| 153 | + ) |
| 154 | + |
| 155 | + if (jobParam.nfsVolume == null) { |
| 156 | + val volumeServer = System.getenv(VOLUME_SERVER) |
| 157 | + if (!volumeServer.isNullOrBlank()) { |
| 158 | + jobParam.nfsVolume = listOf( |
| 159 | + JobParam.NfsVolume( |
| 160 | + System.getenv(VOLUME_SERVER), |
| 161 | + System.getenv(VOLUME_PATH), |
| 162 | + System.getenv(VOLUME_MOUNT_PATH) |
| 163 | + ) |
| 164 | + ) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // get docker image host & path |
| 169 | + val imagePair = getImagePair(param.imageName) |
| 170 | + |
| 171 | + // get user pass param |
| 172 | + val registry = DockerRegistry( |
| 173 | + host = imagePair.first, |
| 174 | + username = param.dockerLoginUsername, |
| 175 | + password = param.dockerLoginPassword |
| 176 | + ) |
| 177 | + |
| 178 | + return DispatchJobReq( |
| 179 | + alias = "job-${System.currentTimeMillis()}-${RandomStringUtils.randomAlphabetic(8).toLowerCase()}", |
| 180 | + activeDeadlineSeconds = 86400, |
| 181 | + image = imageName, |
| 182 | + registry = registry, |
| 183 | + params = jobParam, |
| 184 | + podNameSelector = EnvUtils.getHostName() |
| 185 | + ) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private fun getImagePair(imageName: String): Pair<String, String> { |
| 190 | + val targetImageRepo = imageName.split("/").first() |
| 191 | + val targetImageName = imageName.removePrefix(targetImageRepo).removeSuffix("/") |
| 192 | + return Pair(targetImageRepo, targetImageName) |
| 193 | + } |
| 194 | +} |
0 commit comments