Skip to content

Commit

Permalink
feat: Turbo编译加速对接权限中心RBAC TencentBlueKing#316
Browse files Browse the repository at this point in the history
  • Loading branch information
eazence committed Nov 20, 2024
1 parent 3a374ec commit 90135e6
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/backend/turbo/api-turbo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
api("com.tencent.bk.devops.ci.common:common-api") {
isTransitive = false
}
api("com.tencent.bk.sdk:iam-java-sdk")
}

plugins {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tencent.devops.turbo.api

import com.tencent.bk.sdk.iam.dto.callback.request.CallbackRequestDTO
import com.tencent.devops.api.pojo.Response
import com.tencent.devops.common.util.constants.AUTH_HEADER_DEVOPS_PROJECT_ID
import io.swagger.annotations.Api
Expand All @@ -9,6 +10,7 @@ import org.springframework.cloud.openfeign.FeignClient
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestMapping

Expand All @@ -33,4 +35,11 @@ interface IServiceTurboPlanController {
@PathVariable("pipelineElementId")
pipelineElementId: String
): Response<String?>

@ApiOperation("特定资源列表,注册存量加速方案")
@PostMapping("/instances/list")
fun resourceList(
@ApiParam(value = "回调信息", required = true)
callBackInfo: CallbackRequestDTO
): Response<String>
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package com.tencent.devops.turbo.controller

import com.tencent.bk.sdk.iam.dto.callback.request.CallbackRequestDTO
import com.tencent.devops.api.pojo.Response
import com.tencent.devops.turbo.api.IServiceTurboPlanController
import com.tencent.devops.turbo.service.TurboPlanService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RestController

@Suppress("MaxLineLength")
@RestController
class ServiceTurboPlanController @Autowired constructor(
private val turboPlanService: TurboPlanService
) : IServiceTurboPlanController {

override fun findTurboPlanIdByProjectIdAndPipelineInfo(projectId: String, pipelineId: String, pipelineElementId: String): Response<String?> {
return Response.success(turboPlanService.findMigratedTurboPlanByPipelineInfo(projectId, pipelineId, pipelineElementId)?.taskId)
override fun findTurboPlanIdByProjectIdAndPipelineInfo(
projectId: String,
pipelineId: String,
pipelineElementId: String
): Response<String?> {
return Response.success(
turboPlanService.findMigratedTurboPlanByPipelineInfo(
projectId,
pipelineId,
pipelineElementId
)?.taskId
)
}

override fun resourceList(callBackInfo: CallbackRequestDTO): Response<String> {
return Response.success(turboPlanService.getInstanceByResource(callBackInfo))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ class TurboPlanDao @Autowired constructor(
/**
* 根据项目id和创建时间获取加速方案列表
*/
fun getTurboPlanByProjectIdAndCreatedDate(projectId: String, startTime: LocalDate?, endTime: LocalDate?, pageable: Pageable): Page<TTurboPlanEntity> {
fun getTurboPlanByProjectIdAndCreatedDate(
projectId: String,
startTime: LocalDate?,
endTime: LocalDate?,
pageable: Pageable
): Page<TTurboPlanEntity> {
val query = turboPlanParameter(projectId, startTime, endTime)
//先算总数
val totalCount = mongoTemplate.count(query, TTurboPlanEntity::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package com.tencent.devops.turbo.service

import com.tencent.bk.sdk.iam.constants.CallbackMethodEnum
import com.tencent.bk.sdk.iam.dto.callback.request.CallbackRequestDTO
import com.tencent.bk.sdk.iam.dto.callback.response.CallbackBaseResponseDTO
import com.tencent.bk.sdk.iam.dto.callback.response.FetchInstanceInfoResponseDTO
import com.tencent.bk.sdk.iam.dto.callback.response.InstanceInfoDTO
import com.tencent.bk.sdk.iam.dto.callback.response.ListInstanceResponseDTO
import com.tencent.devops.common.api.exception.TurboException
import com.tencent.devops.common.api.exception.code.TURBO_NO_DATA_FOUND
import com.tencent.devops.common.api.exception.code.TURBO_PARAM_INVALID
import com.tencent.devops.common.api.exception.code.TURBO_THIRDPARTY_SYSTEM_FAIL
import com.tencent.devops.common.api.pojo.Page
import com.tencent.devops.common.api.util.OkhttpUtil
import com.tencent.devops.common.auth.api.AuthRegisterApi
import com.tencent.devops.common.auth.callback.FetchInstanceInfo
import com.tencent.devops.common.auth.callback.ListInstanceInfo
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.db.PageUtils
import com.tencent.devops.common.service.prometheus.BkTimed
import com.tencent.devops.common.util.JsonUtil
import com.tencent.devops.common.util.MathUtil
import com.tencent.devops.common.util.constants.COMMON_NUM_0L
import com.tencent.devops.common.util.constants.COMMON_NUM_10L
import com.tencent.devops.common.util.constants.COMMON_NUM_1L
import com.tencent.devops.common.util.constants.SYSTEM_ADMIN
import com.tencent.devops.project.api.service.ServiceProjectResource
import com.tencent.devops.turbo.dao.mongotemplate.TurboPlanDao
Expand All @@ -33,6 +44,7 @@ import com.tencent.devops.turbo.vo.TurboPlanStatusBatchUpdateReqVO
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Sort
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import java.time.LocalDate
Expand Down Expand Up @@ -750,4 +762,82 @@ class TurboPlanService @Autowired constructor(
"all project id turbo plan status updated successfully!"
}
}

/**
* iam(蓝盾)回调实现方法
*/
fun getInstanceByResource(callBackInfo: CallbackRequestDTO): String {
logger.info("CallbackRequestDTO: {}", callBackInfo)
val result: CallbackBaseResponseDTO = when (callBackInfo.method) {
CallbackMethodEnum.LIST_INSTANCE -> handleListInstance(callBackInfo)
CallbackMethodEnum.FETCH_INSTANCE_INFO -> handleFetchInstanceInfo(callBackInfo)
else -> CallbackBaseResponseDTO()
}
return JsonUtil.toJson(result)
}

/**
* 按项目id分页获取方案清单
*/
private fun handleListInstance(callBackInfo: CallbackRequestDTO): CallbackBaseResponseDTO {
return callBackInfo.filter.parent?.id?.let { parentId ->
listInstance(parentId, callBackInfo.page.offset, callBackInfo.page.limit)
} ?: ListInstanceInfo().buildListInstanceFailResult()
}

/**
* 按方案id批量获取方案清单
*/
private fun handleFetchInstanceInfo(callBackInfo: CallbackRequestDTO): CallbackBaseResponseDTO {
val idList = callBackInfo.filter.idList
return if (idList.isNullOrEmpty()) {
FetchInstanceInfo().buildFetchInstanceFailResult()
} else {
val turboPlanIdSet = idList.mapNotNull { it.toString() }.toSet()
fetchInstance(turboPlanIdSet)
}
}

/**
* 按项目id
*/
private fun listInstance(projectId: String, offset: Long?, limit: Long?): ListInstanceResponseDTO {
val safeOffset = (offset ?: COMMON_NUM_0L).coerceAtLeast(COMMON_NUM_0L)
val safeLimit = (limit ?: COMMON_NUM_10L).coerceAtLeast(COMMON_NUM_1L)
val pageNum = safeOffset / safeLimit + COMMON_NUM_1L
val pageable =
PageUtils.convertPageSizeToPageable(pageNum.toInt(), safeLimit.toInt(), "_id", Sort.Direction.ASC.name)

val turboPlanEntityList = turboPlanDao.getAllTurboPlanList(null, null, projectId, pageable).records
val instanceInfoList = turboPlanEntityList.map { it.toDTO() }
val result = ListInstanceInfo()
return if (instanceInfoList.isEmpty()) result.buildListInstanceFailResult() else result.buildListInstanceResult(
instanceInfoList,
instanceInfoList.size.toLong()
)
}

/**
* 按方案id
*/
private fun fetchInstance(turboPlanIdSet: Set<String>): FetchInstanceInfoResponseDTO {
val turboPlanEntityList = turboPlanRepository.findByIdIn(turboPlanIdSet.toList())
val instanceInfos = turboPlanEntityList.map { it.toDTO() }
val result = FetchInstanceInfo()
return if (instanceInfos.isEmpty()) result.buildFetchInstanceFailResult() else result.buildFetchInstanceResult(
instanceInfos
)
}

/**
* entity转DTO
*/
private fun TTurboPlanEntity.toDTO(): InstanceInfoDTO {
return InstanceInfoDTO().apply {
id = this@toDTO.id
displayName = this@toDTO.planName
iamApprover =
listOf(if (this@toDTO.createdBy.isNullOrBlank()) this@toDTO.createdBy else this@toDTO.updatedBy)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
dependencies {
api(project(":common-turbo:common-turbo-client"))
api("com.tencent.bk.sdk:iam-java-sdk") {
isTransitive = false
}
api("com.tencent.bk.devops.ci.auth:api-auth") {
isTransitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ const val BASE_EXCLUDED_PROJECT_ID_LIST = "EXCLUDED_PROJECT_ID_LIST"
* 内部测试的方案,服务计费时需要过滤掉
*/
const val BASE_EXCLUDED_COMMON_PLAN_ID = "EXCLUDED_COMMON_PLAN_ID_LIST"

/**
* 常用整数
*/
const val COMMON_NUM_0L = 0L
const val COMMON_NUM_1L = 1L
const val COMMON_NUM_10L = 10L

0 comments on commit 90135e6

Please sign in to comment.