Skip to content

Commit

Permalink
feat: 整合dispatch-docker, dispatch-kubernetes模块到dispatch #9548
Browse files Browse the repository at this point in the history
  • Loading branch information
sawyersong2 committed Nov 9, 2023
1 parent 7a84121 commit d81cbe3
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ import com.tencent.devops.dispatch.kubernetes.pojo.BK_MACHINE_INTERFACE_TIMEOUT
import com.tencent.devops.dispatch.kubernetes.pojo.BK_TROUBLE_SHOOTING
import com.tencent.devops.dispatch.kubernetes.pojo.base.DispatchBuildImageReq
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import java.net.SocketTimeoutException
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.net.SocketTimeoutException

@Component
class BcsBuilderClient @Autowired constructor(
private val objectMapper: ObjectMapper,
private val clientCommon: BcsClientCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import okhttp3.Headers.Companion.toHeaders
import okhttp3.Request
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
class BcsClientCommon @Autowired constructor(
private val commonService: CommonService
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class BcsJobClient @Autowired constructor(
private val objectMapper: ObjectMapper,
private val clientCommon: BcsClientCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ import com.tencent.devops.dispatch.kubernetes.pojo.BK_GET_BCS_TASK_EXECUTION_TIM
import com.tencent.devops.dispatch.kubernetes.pojo.BK_GET_BCS_TASK_STATUS_ERROR
import com.tencent.devops.dispatch.kubernetes.pojo.BK_GET_BCS_TASK_STATUS_TIMEOUT
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import java.net.SocketTimeoutException
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.net.SocketTimeoutException

@Component
class BcsTaskClient @Autowired constructor(
private val objectMapper: ObjectMapper,
private val clientCommon: BcsClientCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ import com.tencent.devops.dispatch.kubernetes.utils.DispatchKubernetesCommonUtil
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service

@Service("bcsContainerService")
class BcsContainerService @Autowired constructor(
private val bcsBuilderClient: BcsBuilderClient,
private val logsPrinter: LogsPrinter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@

package com.tencent.devops.dispatch.kubernetes.bcs.service

import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsBuilderClient
import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsJobClient
import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsTaskClient
import com.tencent.devops.dispatch.kubernetes.bcs.pojo.BcsJob
import com.tencent.devops.dispatch.kubernetes.bcs.pojo.BcsJobStatusEnum
import com.tencent.devops.dispatch.kubernetes.bcs.pojo.NfsConfig
Expand All @@ -43,22 +41,13 @@ import com.tencent.devops.dispatch.kubernetes.pojo.base.DispatchBuildStatusResp
import com.tencent.devops.dispatch.kubernetes.pojo.base.DispatchJobLogResp
import com.tencent.devops.dispatch.kubernetes.pojo.base.DispatchJobReq
import com.tencent.devops.dispatch.kubernetes.pojo.base.DispatchTaskResp
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service

@Service("bcsJobService")
class BcsJobService @Autowired constructor(
private val bcsJobClient: BcsJobClient,
private val bcsTaskClient: BcsTaskClient,
private val bcsBuilderClient: BcsBuilderClient
private val bcsJobClient: BcsJobClient
) : JobService {

companion object {
private val logger = LoggerFactory.getLogger(BcsJobService::class.java)
}

@Value("\${bcs.resources.job.cpu}")
var cpu: Double = 32.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.tencent.devops.common.api.util.OkhttpUtils
import com.tencent.devops.common.dispatch.sdk.BuildFailureException
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import com.tencent.devops.dispatch.kubernetes.pojo.Builder
import com.tencent.devops.dispatch.kubernetes.pojo.DeleteBuilderParams
import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesBuilderStatus
Expand All @@ -42,16 +41,15 @@ import com.tencent.devops.dispatch.kubernetes.pojo.OperateBuilderParams
import com.tencent.devops.dispatch.kubernetes.pojo.StartBuilderParams
import com.tencent.devops.dispatch.kubernetes.pojo.StopBuilderParams
import com.tencent.devops.dispatch.kubernetes.pojo.TaskResp
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import com.tencent.devops.dispatch.kubernetes.pojo.getCodeMessage
import com.tencent.devops.dispatch.kubernetes.pojo.isRunning
import java.net.SocketTimeoutException
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.net.SocketTimeoutException

@Component
class KubernetesBuilderClient @Autowired constructor(
private val objectMapper: ObjectMapper,
private val clientCommon: KubernetesClientCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import okhttp3.Headers.Companion.toHeaders
import okhttp3.Request
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

@Component
class KubernetesClientCommon @Autowired constructor(
private val commonService: CommonService
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.OkhttpUtils
import com.tencent.devops.common.dispatch.sdk.BuildFailureException
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import com.tencent.devops.dispatch.kubernetes.pojo.BK_BUILD_AND_PUSH_INTERFACE_EXCEPTION
import com.tencent.devops.dispatch.kubernetes.pojo.BuildAndPushImage
import com.tencent.devops.dispatch.kubernetes.pojo.Job
import com.tencent.devops.dispatch.kubernetes.pojo.JobStatus
import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult
import com.tencent.devops.dispatch.kubernetes.pojo.TaskResp
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class KubernetesJobClient @Autowired constructor(
private val objectMapper: ObjectMapper,
private val clientCommon: KubernetesClientCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,21 @@ import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.OkhttpUtils
import com.tencent.devops.common.dispatch.sdk.BuildFailureException
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import com.tencent.devops.dispatch.kubernetes.pojo.BK_KUBERNETES_TASK_EXECUTE_TIMEOUT
import com.tencent.devops.dispatch.kubernetes.pojo.BK_KUBERNETES_TASK_STATUS_API_EXCEPTION
import com.tencent.devops.dispatch.kubernetes.pojo.BK_KUBERNETES_TASK_STATUS_API_TIMEOUT
import com.tencent.devops.dispatch.kubernetes.pojo.KubernetesResult
import com.tencent.devops.dispatch.kubernetes.pojo.TaskStatusEnum
import com.tencent.devops.dispatch.kubernetes.pojo.TaskStatusResp
import com.tencent.devops.dispatch.kubernetes.pojo.common.ErrorCodeEnum
import com.tencent.devops.dispatch.kubernetes.pojo.getCodeMessage
import com.tencent.devops.dispatch.kubernetes.pojo.isFailed
import com.tencent.devops.dispatch.kubernetes.pojo.isRunning
import com.tencent.devops.dispatch.kubernetes.pojo.isSuccess
import java.net.SocketTimeoutException
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.net.SocketTimeoutException

@Component
class KubernetesTaskClient @Autowired constructor(
private val objectMapper: ObjectMapper,
private val clientCommon: KubernetesClientCommon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@

package com.tencent.devops.dispatch.kubernetes.common

const val BUILD_NODE_ENVIRONMENT = "devops.build.node.environment"

const val ENV_KEY_PROJECT_ID = "devops_project_id"
const val ENV_KEY_AGENT_ID = "devops_agent_id"
const val ENV_KEY_AGENT_SECRET_KEY = "devops_agent_secret_key"
const val ENV_KEY_GATEWAY = "devops_gateway"

const val ENV_JOB_BUILD_TYPE = "JOB_POOL"
const val ENV_DEFAULT_LOCALE_LANGUAGE = "BK_CI_LOCALE_LANGUAGE"

const val CONFIG_VOLUME_NAME = "config-volume"
const val DATA_VOLUME_NAME = "data-volume"
const val DATA_VOLUME_MOUNT_PATH = "/data"
const val NFS_VOLUME_NAME_PREFIX = "nfs-volume"
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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.devops.dispatch.kubernetes.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.tencent.devops.common.service.config.CommonConfig
import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsBuilderClient
import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsClientCommon
import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsJobClient
import com.tencent.devops.dispatch.kubernetes.bcs.client.BcsTaskClient
import com.tencent.devops.dispatch.kubernetes.bcs.service.BcsContainerService
import com.tencent.devops.dispatch.kubernetes.bcs.service.BcsJobService
import com.tencent.devops.dispatch.kubernetes.client.KubernetesBuilderClient
import com.tencent.devops.dispatch.kubernetes.client.KubernetesClientCommon
import com.tencent.devops.dispatch.kubernetes.client.KubernetesJobClient
import com.tencent.devops.dispatch.kubernetes.client.KubernetesTaskClient
import com.tencent.devops.dispatch.kubernetes.components.LogsPrinter
import com.tencent.devops.dispatch.kubernetes.interfaces.CommonService
import com.tencent.devops.dispatch.kubernetes.service.KubernetesContainerService
import com.tencent.devops.dispatch.kubernetes.service.KubernetesJobService
import org.springframework.boot.autoconfigure.AutoConfigureOrder
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.core.Ordered

@Suppress("ALL")
@Configuration
@ConditionalOnWebApplication
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
class KubernetesBeanConfiguration {

@Bean
@Primary
@ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true")
fun kubernetesClientCommon(
commonService: CommonService
) = KubernetesClientCommon(commonService)

@Bean
@Primary
@ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true")
fun kubernetesBuilderClient(
objectMapper: ObjectMapper,
clientCommon: KubernetesClientCommon
) = KubernetesBuilderClient(objectMapper, clientCommon)

@Bean
@Primary
@ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true")
fun kubernetesJobClient(
objectMapper: ObjectMapper,
clientCommon: KubernetesClientCommon
) = KubernetesJobClient(objectMapper, clientCommon)

@Bean
@Primary
@ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true")
fun kubernetesTaskClient(
objectMapper: ObjectMapper,
clientCommon: KubernetesClientCommon
) = KubernetesTaskClient(objectMapper, clientCommon)

@Bean
@Primary
@ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true")
fun kubernetesContainerService(
logsPrinter: LogsPrinter,
commonConfig: CommonConfig,
kubernetesTaskClient: KubernetesTaskClient,
kubernetesBuilderClient: KubernetesBuilderClient,
kubernetesJobClient: KubernetesJobClient
) = KubernetesContainerService(
logsPrinter = logsPrinter,
commonConfig = commonConfig,
kubernetesTaskClient = kubernetesTaskClient,
kubernetesBuilderClient = kubernetesBuilderClient,
kubernetesJobClient = kubernetesJobClient
)

@Bean
@Primary
@ConditionalOnProperty(prefix = "kubernetes", name = ["enable"], havingValue = "true")
fun kubernetesJobService(
kubernetesJobClient: KubernetesJobClient
) = KubernetesJobService(kubernetesJobClient)

@Bean
@Primary
@ConditionalOnProperty(prefix = "bcs", name = ["enable"], havingValue = "true")
fun bcsClientCommon(
commonService: CommonService
) = BcsClientCommon(commonService)

@Bean
@Primary
@ConditionalOnProperty(prefix = "bcs", name = ["enable"], havingValue = "true")
fun bcsBuilderClient(
objectMapper: ObjectMapper,
clientCommon: BcsClientCommon
) = BcsBuilderClient(objectMapper, clientCommon)

@Bean
@Primary
@ConditionalOnProperty(prefix = "bcs", name = ["enable"], havingValue = "true")
fun bcsJobClient(
objectMapper: ObjectMapper,
clientCommon: BcsClientCommon
) = BcsJobClient(objectMapper, clientCommon)

@Bean
@Primary
@ConditionalOnProperty(prefix = "bcs", name = ["enable"], havingValue = "true")
fun bcsTaskClient(
objectMapper: ObjectMapper,
clientCommon: BcsClientCommon
) = BcsTaskClient(objectMapper, clientCommon)

@Bean
@Primary
@ConditionalOnProperty(prefix = "bcs", name = ["enable"], havingValue = "true")
fun bcsContainerService(
bcsBuilderClient: BcsBuilderClient,
logsPrinter: LogsPrinter,
bcsTaskClient: BcsTaskClient,
commonConfig: CommonConfig
) = BcsContainerService(bcsBuilderClient, logsPrinter, bcsTaskClient, commonConfig)

@Bean
@Primary
@ConditionalOnProperty(prefix = "bcs", name = ["enable"], havingValue = "true")
fun bcsJobService(
bcsJobClient: BcsJobClient
) = BcsJobService(bcsJobClient)
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.tencent.devops.dispatch.kubernetes.common.ENV_KEY_GATEWAY
import com.tencent.devops.dispatch.kubernetes.common.ENV_KEY_PROJECT_ID
import com.tencent.devops.dispatch.kubernetes.common.SLAVE_ENVIRONMENT
import com.tencent.devops.dispatch.kubernetes.components.LogsPrinter
import com.tencent.devops.dispatch.kubernetes.dao.DispatchKubernetesBuildDao
import com.tencent.devops.dispatch.kubernetes.interfaces.ContainerService
import com.tencent.devops.dispatch.kubernetes.pojo.BK_CONTAINER_BUILD_ERROR
import com.tencent.devops.dispatch.kubernetes.pojo.BK_READY_CREATE_KUBERNETES_BUILD_MACHINE
Expand Down Expand Up @@ -85,23 +84,18 @@ import com.tencent.devops.dispatch.kubernetes.pojo.isSuccess
import com.tencent.devops.dispatch.kubernetes.pojo.readyToStart
import com.tencent.devops.dispatch.kubernetes.utils.DispatchKubernetesCommonUtils
import org.apache.commons.lang3.RandomStringUtils
import org.jooq.DSLContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.Locale
import java.util.*
import java.util.stream.Collectors

@Service("kubernetesContainerService")
class KubernetesContainerService @Autowired constructor(
private val logsPrinter: LogsPrinter,
private val dslContext: DSLContext,
private val commonConfig: CommonConfig,
private val kubernetesTaskClient: KubernetesTaskClient,
private val kubernetesBuilderClient: KubernetesBuilderClient,
private val kubernetesJobClient: KubernetesJobClient,
private val dispatchKubernetesBuildDao: DispatchKubernetesBuildDao
private val kubernetesJobClient: KubernetesJobClient
) : ContainerService {

companion object {
Expand Down
Loading

0 comments on commit d81cbe3

Please sign in to comment.