Skip to content

Commit

Permalink
feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435
Browse files Browse the repository at this point in the history
* feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435

* feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435

* feat: fs客户端管理支持ip、版本的模糊查询 TencentBlueKing#2435
  • Loading branch information
yaoxuwan authored Aug 1, 2024
1 parent 3a9b478 commit 301b08c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/backend/fs/boot-fs-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ dependencies {
implementation("io.jsonwebtoken:jjwt-api")
runtimeOnly("io.jsonwebtoken:jjwt-impl")
runtimeOnly("io.jsonwebtoken:jjwt-jackson")
implementation("com.tencent.devops:devops-boot-starter-service")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.tencent.devops:devops-boot-starter-service-reactive")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
implementation("com.playtika.reactivefeign:feign-reactor-spring-cloud-starter")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tencent.bkrepo.fs.server.config.feign

import feign.Contract
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor
import org.springframework.cloud.openfeign.FeignClientProperties
import org.springframework.cloud.openfeign.FeignFormatterRegistrar
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.convert.ConversionService
import org.springframework.format.support.DefaultFormattingConversionService
import org.springframework.format.support.FormattingConversionService

/**
* 同步feign的全局配置
* */
@Configuration
class FeignGlobalConfiguration {
@Bean
fun feignContract(
parameterProcessors: List<AnnotatedParameterProcessor>,
feignClientProperties: FeignClientProperties?,
feignConversionService: ConversionService,
): Contract {
val decodeSlash = feignClientProperties?.isDecodeSlash ?: true
return OldSpringMvcContract(parameterProcessors, feignConversionService, decodeSlash)
}

@Bean
fun feignConversionService(feignFormatterRegistrars: List<FeignFormatterRegistrar>): FormattingConversionService {
val conversionService: FormattingConversionService = DefaultFormattingConversionService()
for (feignFormatterRegistrar in feignFormatterRegistrars) {
feignFormatterRegistrar.registerFormatters(conversionService)
}
return conversionService
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.tencent.bkrepo.fs.server.config.feign

import feign.MethodMetadata
import feign.Util.emptyToNull
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor
import org.springframework.cloud.openfeign.support.SpringMvcContract
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation
import org.springframework.core.convert.ConversionService
import org.springframework.core.io.DefaultResourceLoader
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping

/**
* 新版本的Spring Cloud的@FeignClient出于安全考虑(不是十分理解),不允许使用@RequestMapping,
* 但这对现有的使用方式有很大的影响,需要修改所有的FeignClient,所以这里我们恢复之前的逻辑,允许在
* FeignClient上使用RequestMapping定义公共的路由前缀。
* */
class OldSpringMvcContract(
annotatedParameterProcessors: List<AnnotatedParameterProcessor>,
conversionService: ConversionService,
private val decodeSlash: Boolean,
) : SpringMvcContract(annotatedParameterProcessors, conversionService, decodeSlash) {
private val resourceLoader = DefaultResourceLoader()

/**
* 旧版本的SpringMvcContract处理逻辑
* */
override fun processAnnotationOnClass(data: MethodMetadata, clz: Class<*>) {
if (clz.interfaces.isNotEmpty()) {
return
}
val classAnnotation = findMergedAnnotation(clz, RequestMapping::class.java) ?: return
// Prepend path from class annotation if specified
if (classAnnotation.value.isNotEmpty()) {
var pathValue = emptyToNull(classAnnotation.value[0])
pathValue = resolve(pathValue)
if (!pathValue.startsWith("/")) {
pathValue = "/$pathValue"
}
data.template().uri(pathValue)
if (data.template().decodeSlash() != decodeSlash) {
data.template().decodeSlash(decodeSlash)
}
}
}
private fun resolve(value: String): String {
return if (StringUtils.hasText(value) && resourceLoader is ConfigurableApplicationContext) {
(resourceLoader as ConfigurableApplicationContext).environment.resolvePlaceholders(value)
} else {
value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ package com.tencent.bkrepo.fs.server.service
import com.tencent.bkrepo.common.api.exception.ErrorCodeException
import com.tencent.bkrepo.common.api.message.CommonMessageCode
import com.tencent.bkrepo.common.api.pojo.Page
import com.tencent.bkrepo.common.api.util.EscapeUtils
import com.tencent.bkrepo.common.metrics.push.custom.CustomMetricsExporter
import com.tencent.bkrepo.common.metrics.push.custom.base.MetricsItem
import com.tencent.bkrepo.common.metrics.push.custom.enums.DataModel
Expand Down Expand Up @@ -113,11 +114,11 @@ class ClientService(
suspend fun listClients(request: ClientListRequest): Page<ClientDetail> {
val pageRequest = Pages.ofRequest(request.pageNumber, request.pageSize)
val criteria = Criteria()
request.projectId?.let { criteria.and(TClient::projectId.name).isEqualTo(request.projectId) }
request.repoName?.let { criteria.and(TClient::repoName.name).isEqualTo(request.repoName) }
request.online?.let { criteria.and(TClient::online.name).isEqualTo(request.online) }
request.ip?.let { criteria.and(TClient::ip.name).isEqualTo(request.ip) }
request.version?.let { criteria.and(TClient::version.name).isEqualTo(request.version) }
request.projectId?.let { criteria.and(TClient::projectId.name).isEqualTo(it) }
request.repoName?.let { criteria.and(TClient::repoName.name).isEqualTo(it) }
request.online?.let { criteria.and(TClient::online.name).isEqualTo(it) }
request.ip?.let { criteria.and(TClient::ip.name).regex(convertToRegex(it)) }
request.version?.let { criteria.and(TClient::version.name).regex(convertToRegex(it)) }
val query = Query(criteria)
val count = clientRepository.count(query)
val data = clientRepository.find(query.with(pageRequest))
Expand Down Expand Up @@ -279,6 +280,10 @@ class ClientService(
)
}

private fun convertToRegex(value: String): String {
return EscapeUtils.escapeRegexExceptWildcard(value).replace("*", ".*")
}

private fun TDailyClient.convert(): DailyClientDetail {
return DailyClientDetail(
id = id!!,
Expand Down

0 comments on commit 301b08c

Please sign in to comment.