diff --git a/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/util/StorageCredentialHelper.kt b/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/util/StorageCredentialHelper.kt index 9f4cfbd7f3..6fd7c50c04 100644 --- a/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/util/StorageCredentialHelper.kt +++ b/src/backend/common/common-metadata/metadata-service/src/main/kotlin/com/tencent/bkrepo/common/metadata/util/StorageCredentialHelper.kt @@ -83,7 +83,10 @@ class StorageCredentialHelper( } fun convert(credentials: TStorageCredentials): StorageCredentials { - return credentials.credentials.readJsonString().apply { this.key = credentials.id } + return credentials.credentials.readJsonString().apply { + key = credentials.id + storageRegion = credentials.region.toString() + } } fun buildStorageCredential( diff --git a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/FileSystemCredentials.kt b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/FileSystemCredentials.kt index 31fc886af8..c8d25e38cb 100644 --- a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/FileSystemCredentials.kt +++ b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/FileSystemCredentials.kt @@ -46,6 +46,7 @@ data class FileSystemCredentials( override var upload: UploadProperties = UploadProperties(), override var encrypt: EncryptProperties = EncryptProperties(), override var compress: CompressProperties = CompressProperties(), + override var storageRegion: String? = null, ) : StorageCredentials(key, cache, upload, encrypt, compress) { companion object { diff --git a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/HDFSCredentials.kt b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/HDFSCredentials.kt index 4c3b11b48c..f89912e979 100644 --- a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/HDFSCredentials.kt +++ b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/HDFSCredentials.kt @@ -51,6 +51,7 @@ data class HDFSCredentials( override var upload: UploadProperties = UploadProperties(), override var encrypt: EncryptProperties = EncryptProperties(), override var compress: CompressProperties = CompressProperties(), + override var storageRegion: String? = null, ) : StorageCredentials(key, cache, upload, encrypt, compress) { companion object { const val type = "hdfs" diff --git a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/InnerCosCredentials.kt b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/InnerCosCredentials.kt index 3c395c7c3b..e2e9266904 100644 --- a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/InnerCosCredentials.kt +++ b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/InnerCosCredentials.kt @@ -60,6 +60,7 @@ data class InnerCosCredentials( override var upload: UploadProperties = UploadProperties(), override var encrypt: EncryptProperties = EncryptProperties(), override var compress: CompressProperties = CompressProperties(), + override var storageRegion: String? = null, ) : StorageCredentials(key, cache, upload, encrypt, compress) { companion object { diff --git a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/StorageCredentials.kt b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/StorageCredentials.kt index aba40fcc46..2b4a49c0de 100644 --- a/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/StorageCredentials.kt +++ b/src/backend/common/common-storage/storage-api/src/main/kotlin/com/tencent/bkrepo/common/storage/credentials/StorageCredentials.kt @@ -53,4 +53,5 @@ open class StorageCredentials( open var upload: UploadProperties, open var encrypt: EncryptProperties, open var compress: CompressProperties, + open var storageRegion: String? = null, ) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/InternalFlowRequest.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/InternalFlowRequest.kt new file mode 100644 index 0000000000..819b889a8c --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/InternalFlowRequest.kt @@ -0,0 +1,59 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 Tencent. 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.bkrepo.opdata.pojo + +import com.tencent.bkrepo.opdata.pojo.enums.ForwardType +import com.tencent.bkrepo.opdata.pojo.enums.LevelType +import io.swagger.v3.oas.annotations.media.Schema + +/** + * 内部流转请求 + */ +@Schema(title = "内部流转请求") +data class InternalFlowRequest( + @get:Schema(title = "级别类型", required = true) + val level: LevelType, + + @get:Schema(title = "名称", required = true) + val name: String, + + @get:Schema(title = "标签", required = true) + val tag: String, + + @get:Schema(title = "区域", required = true) + val region: String, + + @get:Schema(title = "下一个节点标签") + val next: String? = null, + + @get:Schema(title = "流转方向") + val forward: ForwardType? = null, + + @get:Schema(title = "流转提示") + val forwardTip: String? = null +) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/NameWithTag.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/NameWithTag.kt new file mode 100644 index 0000000000..d3f23014ce --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/NameWithTag.kt @@ -0,0 +1,35 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 Tencent. 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.bkrepo.opdata.pojo + + +data class NameWithTag( + val name: String, + val tag: String?, + val region: String +) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/ForwardType.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/ForwardType.kt new file mode 100644 index 0000000000..b5d7e751e0 --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/ForwardType.kt @@ -0,0 +1,33 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2022 Tencent. 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.bkrepo.opdata.pojo.enums + +enum class ForwardType { + Horizontal, + Vertical +} \ No newline at end of file diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/LevelType.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/LevelType.kt new file mode 100644 index 0000000000..34cfa12300 --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/enums/LevelType.kt @@ -0,0 +1,33 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2022 Tencent. 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.bkrepo.opdata.pojo.enums + +enum class LevelType { + GATEWAY, + SERVICE, +} \ No newline at end of file diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/gateway/GatewayHeartbeatInfo.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/gateway/GatewayHeartbeatInfo.kt new file mode 100644 index 0000000000..355eb50326 --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/gateway/GatewayHeartbeatInfo.kt @@ -0,0 +1,39 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2021 Tencent. 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.bkrepo.opdata.pojo.gateway + + +/** + * Gateway心跳信息 + */ +data class GatewayHeartbeatInfo( + val ip: String, + val tag: String, + val lastUpdate: String, + val online: Boolean = true +) diff --git a/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/gateway/GatewayHeartbeatRequest.kt b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/gateway/GatewayHeartbeatRequest.kt new file mode 100644 index 0000000000..c1b485fe44 --- /dev/null +++ b/src/backend/opdata/api-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/pojo/gateway/GatewayHeartbeatRequest.kt @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2021 Tencent. 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.bkrepo.opdata.pojo.gateway + + +/** + * Gateway心跳上报请求 + */ +data class GatewayHeartbeatRequest( + val ip: String, + val tag: String +) + diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/GatewayHeartbeatController.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/GatewayHeartbeatController.kt new file mode 100644 index 0000000000..5f7bc4349b --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/GatewayHeartbeatController.kt @@ -0,0 +1,87 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2021 Tencent. 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.bkrepo.opdata.controller + +import com.tencent.bkrepo.common.api.pojo.Response +import com.tencent.bkrepo.common.security.permission.Principal +import com.tencent.bkrepo.common.security.permission.PrincipalType +import com.tencent.bkrepo.common.service.util.ResponseBuilder.success +import com.tencent.bkrepo.opdata.pojo.gateway.GatewayHeartbeatInfo +import com.tencent.bkrepo.opdata.pojo.gateway.GatewayHeartbeatRequest +import com.tencent.bkrepo.opdata.service.GatewayHeartbeatService +import org.apache.pulsar.shade.io.swagger.annotations.ApiOperation +import org.springframework.beans.factory.annotation.Autowired +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.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +/** + * Gateway心跳管理接口 + */ +@RestController +@RequestMapping("/api/heartbeat") +class GatewayHeartbeatController @Autowired constructor( + private val gatewayHeartbeatService: GatewayHeartbeatService +) { + + /** + * 接收Gateway心跳上报 + * 此接口不需要权限验证,允许Gateway直接调用 + */ + @ApiOperation("接收Gateway心跳上报") + @PostMapping("/gateway") + fun reportHeartbeat(@RequestBody request: GatewayHeartbeatRequest): Response { + gatewayHeartbeatService.saveHeartbeat(request) + return success() + } + + /** + * 获取所有Gateway的tag列表 + */ + @ApiOperation("获取所有Gateway的tag列表") + @GetMapping("/gateway/tags") + @Principal(PrincipalType.ADMIN) + fun listAllTags(): Response> { + return success(gatewayHeartbeatService.listAllTags()) + } + + /** + * 根据tag获取Gateway列表(只返回在线的Gateway) + */ + @ApiOperation("根据tag获取Gateway列表") + @GetMapping("/gateway/tag/{tag}") + @Principal(PrincipalType.ADMIN) + fun listGatewaysByTag( + @PathVariable tag: String + ): Response> { + return success(gatewayHeartbeatService.listGatewaysByTag(tag)) + } +} diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/InternalFlowController.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/InternalFlowController.kt new file mode 100644 index 0000000000..4348c41ffa --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/controller/InternalFlowController.kt @@ -0,0 +1,88 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 Tencent. 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.bkrepo.opdata.controller + +import com.tencent.bkrepo.common.api.pojo.Response +import com.tencent.bkrepo.common.security.permission.Principal +import com.tencent.bkrepo.common.security.permission.PrincipalType +import com.tencent.bkrepo.common.service.util.ResponseBuilder +import com.tencent.bkrepo.opdata.model.TInternalFlow +import com.tencent.bkrepo.opdata.pojo.InternalFlowRequest +import com.tencent.bkrepo.opdata.pojo.NameWithTag +import com.tencent.bkrepo.opdata.pojo.enums.LevelType +import com.tencent.bkrepo.opdata.service.InternalFlowService +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.web.bind.annotation.DeleteMapping +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.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +/** + * 内部流转接口 + */ +@Tag(name = "内部流转服务接口") +@RestController +@RequestMapping("/api/internal-flow") +@Principal(PrincipalType.ADMIN) +class InternalFlowController( + private val internalFlowService: InternalFlowService +) { + + @Operation(summary = "根据级别查询去重后的名称列表及对应的tag") + @GetMapping("/names/level/{level}") + fun getDistinctNamesByLevel(@PathVariable level: LevelType): Response> { + val names = internalFlowService.getDistinctNamesByLevel(level) + return ResponseBuilder.success(names) + } + + @Operation(summary = "根据名称查询关联节点") + @GetMapping("/related/name/{name}") + fun getRelatedFlowsByName(@PathVariable name: String): Response> { + val result = internalFlowService.getRelatedFlowsByName(name) + return ResponseBuilder.success(result) + } + + @Operation(summary = "创建内部流转配置") + @PostMapping("/create") + fun createInternalFlow(@RequestBody request: InternalFlowRequest): Response { + val flow = internalFlowService.createInternalFlow(request) + return ResponseBuilder.success(flow) + } + + + @Operation(summary = "删除内部流转配置") + @DeleteMapping("/{id}") + fun deleteInternalFlow(@PathVariable id: String): Response { + internalFlowService.deleteInternalFlow(id) + return ResponseBuilder.success() + } +} diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ArchiveInfoHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ArchiveInfoHandler.kt index 75e5c550c4..42fd8c18c9 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ArchiveInfoHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/ArchiveInfoHandler.kt @@ -6,7 +6,7 @@ import com.tencent.bkrepo.opdata.constant.OPDATA_NODE_NUM import com.tencent.bkrepo.opdata.constant.OPDATA_NODE_SIZE import com.tencent.bkrepo.opdata.constant.OPDATA_REPO_NAME import com.tencent.bkrepo.opdata.handler.QueryHandler -import com.tencent.bkrepo.opdata.model.ArchiveInfoModel +import com.tencent.bkrepo.opdata.service.model.ArchiveInfoModel import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult import com.tencent.bkrepo.opdata.pojo.Target diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/FileExtensionHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/FileExtensionHandler.kt index 26068a6a31..2c278c6d23 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/FileExtensionHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/FileExtensionHandler.kt @@ -34,7 +34,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.constant.OPDATA_PROJECT_ID import com.tencent.bkrepo.opdata.constant.OPDATA_REPO_NAME import com.tencent.bkrepo.opdata.handler.QueryHandler -import com.tencent.bkrepo.opdata.model.FileExtensionMetricsModel +import com.tencent.bkrepo.opdata.service.model.FileExtensionMetricsModel import com.tencent.bkrepo.opdata.model.TFileExtensionMetrics import com.tencent.bkrepo.opdata.pojo.NodeResult import com.tencent.bkrepo.opdata.pojo.Target diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GcInfoHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GcInfoHandler.kt index 63de6bd9f0..1b8780ddb1 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GcInfoHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/GcInfoHandler.kt @@ -7,7 +7,7 @@ import com.tencent.bkrepo.opdata.constant.OPDATA_PRE_GC import com.tencent.bkrepo.opdata.constant.OPDATA_RATIO import com.tencent.bkrepo.opdata.constant.OPDATA_REPO_NAME import com.tencent.bkrepo.opdata.handler.QueryHandler -import com.tencent.bkrepo.opdata.model.GcInfoModel +import com.tencent.bkrepo.opdata.service.model.GcInfoModel import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult import com.tencent.bkrepo.opdata.pojo.Target diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeCollectionHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeCollectionHandler.kt index 7b0d81adef..c4edbb6c60 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeCollectionHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/NodeCollectionHandler.kt @@ -35,7 +35,7 @@ import com.tencent.bkrepo.opdata.constant.OPDATA_GRAFANA_NUMBER import com.tencent.bkrepo.opdata.constant.OPDATA_GRAFANA_STRING import com.tencent.bkrepo.opdata.constant.OPDATA_NODE_NUM import com.tencent.bkrepo.opdata.handler.QueryHandler -import com.tencent.bkrepo.opdata.model.NodeCollectionModel +import com.tencent.bkrepo.opdata.service.model.NodeCollectionModel import com.tencent.bkrepo.opdata.pojo.Columns import com.tencent.bkrepo.opdata.pojo.QueryResult import com.tencent.bkrepo.opdata.pojo.Target diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/StorageCredentialHandler.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/StorageCredentialHandler.kt index 020790cbfe..d577441995 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/StorageCredentialHandler.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/handler/impl/StorageCredentialHandler.kt @@ -32,7 +32,7 @@ package com.tencent.bkrepo.opdata.handler.impl import com.tencent.bkrepo.opdata.handler.QueryHandler -import com.tencent.bkrepo.opdata.model.StorageCredentialsModel +import com.tencent.bkrepo.opdata.service.model.StorageCredentialsModel import com.tencent.bkrepo.opdata.pojo.NodeResult import com.tencent.bkrepo.opdata.pojo.Target import com.tencent.bkrepo.opdata.pojo.enums.Metrics diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/NodeCollectionModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/NodeCollectionModel.kt deleted file mode 100644 index 15d9960ee7..0000000000 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/NodeCollectionModel.kt +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model - -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.data.mongodb.core.MongoTemplate -import org.springframework.stereotype.Service - -@Service -class NodeCollectionModel @Autowired constructor( - private val mongoTemplate: MongoTemplate -) { - companion object { - private const val COLLECTION_NAME = "node" - private const val SHARDING_COUNT = 256 - } - - fun statNodeNum(): MutableMap { - val result = mutableMapOf() - for (i in 0..SHARDING_COUNT) { - val collection = "${COLLECTION_NAME}_$i" - result[collection] = mongoTemplate.getCollection(collection).estimatedDocumentCount() - } - return result - } -} diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoInfo.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoInfo.kt index fbd92aa855..2a9f6c108f 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoInfo.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoInfo.kt @@ -4,4 +4,4 @@ data class RepoInfo( val projectId: String, val name: String, val credentialsKey: String? = "default" -) +) \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt deleted file mode 100644 index 60ed15326f..0000000000 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/StorageCredentialsModel.kt +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model - -import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE -import com.tencent.bkrepo.opdata.pojo.enums.StatMetrics -import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository -import com.tencent.bkrepo.opdata.util.StatDateUtil -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.stereotype.Service - -@Service -class StorageCredentialsModel @Autowired constructor( - private val projectMetricsRepository: ProjectMetricsRepository -) { - - fun getStorageCredentialsStat(metrics: StatMetrics): Map { - val result = mutableMapOf() - val projectMetricsList = projectMetricsRepository.findAllByCreatedDate(StatDateUtil.getStatDate()) - projectMetricsList.forEach { projectMetrics -> - projectMetrics.repoMetrics.forEach { - val value = if (metrics == StatMetrics.NUM) it.num else it.size - if (result.containsKey(it.credentialsKey)) { - result[it.credentialsKey!!] = result[it.credentialsKey!!]!! + value - } else { - result[it.credentialsKey!!] = value - } - } - } - if (metrics == StatMetrics.SIZE) { - result.keys.forEach { - result[it] = result[it]!! / TO_GIGABYTE - } - } - return result - } -} diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/TInternalFlow.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/TInternalFlow.kt new file mode 100644 index 0000000000..57df93952c --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/TInternalFlow.kt @@ -0,0 +1,17 @@ +package com.tencent.bkrepo.opdata.model + +import com.tencent.bkrepo.opdata.pojo.enums.LevelType +import com.tencent.bkrepo.opdata.pojo.enums.ForwardType +import org.springframework.data.mongodb.core.mapping.Document + +@Document("internal_flow") +data class TInternalFlow( + var id: String? = null, + val level: LevelType, + val name: String, + val tag: String? = null, + val region: String, + val next: String?, + val forward: ForwardType?, + val forwardTip: String? +) \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/InternalFlowRepository.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/InternalFlowRepository.kt new file mode 100644 index 0000000000..f66418dd4c --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/repository/InternalFlowRepository.kt @@ -0,0 +1,56 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 Tencent. 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.bkrepo.opdata.repository + +import com.tencent.bkrepo.opdata.model.TInternalFlow +import com.tencent.bkrepo.opdata.pojo.enums.LevelType +import org.springframework.data.mongodb.repository.MongoRepository +import org.springframework.stereotype.Repository + +/** + * 内部流转数据访问层 + */ +@Repository +interface InternalFlowRepository : MongoRepository { + + /** + * 根据级别查询所有流转记录 + */ + fun findByLevel(level: LevelType): List + + /** + * 根据名称查询流转记录 + */ + fun findByName(name: String): List + +} + + + + + diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GatewayHeartbeatService.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GatewayHeartbeatService.kt new file mode 100644 index 0000000000..2d1b3d2b77 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GatewayHeartbeatService.kt @@ -0,0 +1,155 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2021 Tencent. 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.bkrepo.opdata.service + +import com.tencent.bkrepo.opdata.pojo.gateway.GatewayHeartbeatInfo +import com.tencent.bkrepo.opdata.pojo.gateway.GatewayHeartbeatRequest +import org.slf4j.LoggerFactory +import org.springframework.data.redis.core.RedisTemplate +import org.springframework.stereotype.Service +import java.time.LocalDateTime +import java.util.concurrent.TimeUnit + +/** + * Gateway心跳服务 + */ +@Service +class GatewayHeartbeatService( + private val redisTemplate: RedisTemplate +) { + + /** + * 保存Gateway心跳数据 + */ + fun saveHeartbeat(request: GatewayHeartbeatRequest) { + try { + val ip = request.ip + val tag = request.tag + val lastUpdate = LocalDateTime.now() + .format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + + logger.info("Saving heartbeat for gateway: ip=$ip, tag=$tag") + + // 存储心跳信息到Redis Hash + val heartbeatKey = "$GATEWAY_HEARTBEAT_PREFIX$ip" + val heartbeatData: Map = mapOf( + "ip" to ip, + "tag" to tag, + "last_update" to lastUpdate + ) + redisTemplate.opsForHash().putAll(heartbeatKey, heartbeatData) + + // 设置过期时间为180秒(心跳间隔60秒,允许最多丢失2次心跳) + redisTemplate.expire(heartbeatKey, HEARTBEAT_EXPIRE_SECONDS, TimeUnit.SECONDS) + + // 维护Gateway列表 + redisTemplate.opsForSet().add(GATEWAY_LIST_KEY, ip) + redisTemplate.expire(GATEWAY_LIST_KEY, HEARTBEAT_EXPIRE_SECONDS, TimeUnit.SECONDS) + + logger.info("Heartbeat saved successfully for gateway: $ip") + } catch (e: Exception) { + logger.error("Failed to save heartbeat for gateway: ${request.ip}", e) + throw e + } + } + + /** + * 获取所有Gateway的tag列表 + */ + fun listAllTags(): List { + return try { + // 从gateway列表中获取所有IP + val gatewayIps = redisTemplate.opsForSet().members(GATEWAY_LIST_KEY) ?: emptySet() + logger.info("Found ${gatewayIps.size} gateway IPs in Redis") + + // 获取每个IP的tag,去重后返回 + val tags = mutableSetOf() + gatewayIps.forEach { ip -> + val key = "$GATEWAY_HEARTBEAT_PREFIX$ip" + val tag = redisTemplate.opsForHash().get(key, "tag") + if (!tag.isNullOrBlank()) { + tags.add(tag) + } + } + + logger.info("Found ${tags.size} unique tags") + tags.sorted() + } catch (e: Exception) { + logger.error("Failed to list all tags", e) + emptyList() + } + } + + /** + * 根据tag获取Gateway列表(只返回在线的Gateway) + */ + fun listGatewaysByTag(tag: String): List { + return try { + // 从gateway列表中获取所有在线IP + val gatewayIps = redisTemplate.opsForSet().members(GATEWAY_LIST_KEY) ?: emptySet() + logger.info("Searching for gateways with tag: $tag from ${gatewayIps.size} online IPs") + + // 过滤出指定tag的Gateway,并获取心跳信息 + val result = gatewayIps.mapNotNull { ip -> + val key = "$GATEWAY_HEARTBEAT_PREFIX$ip" + val heartbeatData = redisTemplate.opsForHash().entries(key) + + // 检查是否有数据且tag匹配 + if (heartbeatData.isNotEmpty() && heartbeatData["tag"] == tag) { + GatewayHeartbeatInfo( + ip = heartbeatData["ip"] ?: ip, + tag = heartbeatData["tag"] ?: "", + lastUpdate = heartbeatData["last_update"] ?: "", + online = true + ) + } else { + null + } + } + + logger.info("Found ${result.size} online gateways with tag: $tag") + result + } catch (e: Exception) { + logger.error("Failed to list gateways by tag: $tag", e) + emptyList() + } + } + + companion object { + private val logger = LoggerFactory.getLogger(GatewayHeartbeatService::class.java) + + // Redis Key常量 + private const val GATEWAY_HEARTBEAT_PREFIX = "bkrepo:gateway:heartbeat:" + private const val GATEWAY_LIST_KEY = "bkrepo:gateway:list" + + // 心跳过期时间(秒) + private const val HEARTBEAT_EXPIRE_SECONDS = 180L + } +} + + diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GrafanaService.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GrafanaService.kt index 779c1f7629..0368a41b20 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GrafanaService.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/GrafanaService.kt @@ -32,8 +32,8 @@ package com.tencent.bkrepo.opdata.service import com.tencent.bkrepo.opdata.handler.QueryHandler -import com.tencent.bkrepo.opdata.model.ProjectModel -import com.tencent.bkrepo.opdata.model.RepoModel +import com.tencent.bkrepo.opdata.service.model.ProjectModel +import com.tencent.bkrepo.opdata.service.model.RepoModel import com.tencent.bkrepo.opdata.pojo.QueryRequest import com.tencent.bkrepo.opdata.pojo.SearchRequest import com.tencent.bkrepo.opdata.pojo.enums.Metrics diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/InternalFlowService.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/InternalFlowService.kt new file mode 100644 index 0000000000..3ba01aba2e --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/InternalFlowService.kt @@ -0,0 +1,102 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2019 Tencent. 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.bkrepo.opdata.service + +import com.tencent.bkrepo.opdata.model.TInternalFlow +import com.tencent.bkrepo.opdata.pojo.InternalFlowRequest +import com.tencent.bkrepo.opdata.pojo.NameWithTag +import com.tencent.bkrepo.opdata.pojo.enums.LevelType +import com.tencent.bkrepo.opdata.repository.InternalFlowRepository +import org.springframework.stereotype.Service + +/** + * 内部流转服务 + */ +@Service +class InternalFlowService( + private val internalFlowRepository: InternalFlowRepository +) { + + /** + * 根据级别查询去重后的名称列表及对应的tag和region + * @param level 级别类型 + * @return 去重后的名称和tag、region列表(每个name只取第一个元素的tag和region) + */ + fun getDistinctNamesByLevel(level: LevelType): List { + val flows = internalFlowRepository.findByLevel(level) + // 按name分组,每个name只取第一个元素的tag和region + return flows.groupBy { it.name } + .map { (name, flowList) -> + val firstFlow = flowList.first() + NameWithTag( + name = name, + tag = firstFlow.tag, + region = firstFlow.region + ) + }.sortedBy { it.name } + } + + /** + * 根据名称查询关联节点 + * @param name 节点名称 + * @return 关联节点列表,包括当前节点、前驱节点和后继节点 + */ + fun getRelatedFlowsByName(name: String): List { + // 1. 查找当前节点(所有匹配该 name 的节点) + val currentNodes = internalFlowRepository.findByName(name) + return currentNodes + } + + /** + * 创建内部流转配置 + * @param request 内部流转请求 + * @return 创建后的内部流转对象 + */ + fun createInternalFlow(request: InternalFlowRequest): TInternalFlow { + val flow = TInternalFlow( + level = request.level, + name = request.name, + tag = request.tag, + region = request.region, + next = request.next, + forward = request.forward, + forwardTip = request.forwardTip + ) + return internalFlowRepository.save(flow) + } + + /** + * 删除内部流转配置 + * @param id 流转配置ID + */ + fun deleteInternalFlow(id: String) { + internalFlowRepository.deleteById(id) + } +} + + diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/ArchiveInfoModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/ArchiveInfoModel.kt similarity index 72% rename from src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/ArchiveInfoModel.kt rename to src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/ArchiveInfoModel.kt index 010fd3bda8..9a185b3767 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/ArchiveInfoModel.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/ArchiveInfoModel.kt @@ -1,39 +1,7 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model +package com.tencent.bkrepo.opdata.service.model import com.tencent.bkrepo.common.api.util.readJsonString import com.tencent.bkrepo.opdata.config.OpArchiveOrGcProperties -import com.tencent.bkrepo.opdata.model.GcInfoModel.Companion.BATCH_SIZE import com.tencent.bkrepo.repository.pojo.project.ProjectMetadata import net.javacrumbs.shedlock.spring.annotation.SchedulerLock import org.slf4j.LoggerFactory @@ -47,7 +15,6 @@ import org.springframework.stereotype.Service import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicLong - @Service class ArchiveInfoModel @Autowired constructor( private val mongoTemplate: MongoTemplate, @@ -87,7 +54,7 @@ class ArchiveInfoModel @Autowired constructor( processSpecificProjects(statistics) } - statistics[SUM] = GcInfoModel.reduce(statistics) + statistics[SUM] = GcInfoModel.Companion.reduce(statistics) logger.info("Update archive metrics successful.") refreshing = false return statistics.mapValues { arrayOf(it.value[0].get(), it.value[1].get()) } @@ -95,7 +62,7 @@ class ArchiveInfoModel @Autowired constructor( private fun processAllProjects(statistics: ConcurrentHashMap>) { // 处理所有项目的归档数据 - val query = Query().cursorBatchSize(BATCH_SIZE) + val query = Query().cursorBatchSize(GcInfoModel.Companion.BATCH_SIZE) // 遍历节点表 processProject(query, statistics) } @@ -150,4 +117,4 @@ class ArchiveInfoModel @Autowired constructor( private val logger = LoggerFactory.getLogger(ArchiveInfoModel::class.java) private const val SUM = "SUM" } -} +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/FileExtensionMetricsModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/FileExtensionMetricsModel.kt similarity index 50% rename from src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/FileExtensionMetricsModel.kt rename to src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/FileExtensionMetricsModel.kt index 1da6c5ebe6..137d6e34d8 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/FileExtensionMetricsModel.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/FileExtensionMetricsModel.kt @@ -1,46 +1,12 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model +package com.tencent.bkrepo.opdata.service.model import com.tencent.bkrepo.opdata.constant.OPDATA_FILE_EXTENSION_METRICS +import com.tencent.bkrepo.opdata.model.TFileExtensionMetrics import com.tencent.bkrepo.opdata.pojo.enums.StatMetrics import org.springframework.beans.factory.annotation.Autowired import org.springframework.data.domain.Sort import org.springframework.data.mongodb.core.MongoTemplate -import org.springframework.data.mongodb.core.aggregation.Aggregation.group -import org.springframework.data.mongodb.core.aggregation.Aggregation.match -import org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation -import org.springframework.data.mongodb.core.aggregation.Aggregation.project -import org.springframework.data.mongodb.core.aggregation.Aggregation.sort +import org.springframework.data.mongodb.core.aggregation.Aggregation import org.springframework.data.mongodb.core.query.Criteria import org.springframework.data.mongodb.core.query.and import org.springframework.data.mongodb.core.query.isEqualTo @@ -82,11 +48,11 @@ class FileExtensionMetricsModel @Autowired constructor( @Suppress("UNCHECKED_CAST") private fun aggregateQuery(criteria: Criteria, metrics: StatMetrics): List> { val field = metrics.name.toLowerCase() - val aggregate = newAggregation( - match(criteria), - group(TFileExtensionMetrics::extension.name).sum(field).`as`(field), - sort(Sort.Direction.DESC, field), - project().andInclude(field).and(ID).`as`(TFileExtensionMetrics::extension.name).andExclude(ID) + val aggregate = Aggregation.newAggregation( + Aggregation.match(criteria), + Aggregation.group(TFileExtensionMetrics::extension.name).sum(field).`as`(field), + Aggregation.sort(Sort.Direction.DESC, field), + Aggregation.project().andInclude(field).and(ID).`as`(TFileExtensionMetrics::extension.name).andExclude(ID) ) val aggregateResult = mongoTemplate.aggregate(aggregate, OPDATA_FILE_EXTENSION_METRICS, HashMap::class.java) @@ -96,4 +62,4 @@ class FileExtensionMetricsModel @Autowired constructor( companion object { private const val ID = "_id" } -} +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/GcInfoModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/GcInfoModel.kt similarity index 81% rename from src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/GcInfoModel.kt rename to src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/GcInfoModel.kt index d24333b9dd..245924568e 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/GcInfoModel.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/GcInfoModel.kt @@ -1,35 +1,4 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model +package com.tencent.bkrepo.opdata.service.model import com.google.common.util.concurrent.ThreadFactoryBuilder import com.tencent.bkrepo.common.api.constant.retry @@ -198,4 +167,4 @@ class GcInfoModel @Autowired constructor( ThreadFactoryBuilder().setNameFormat("gc-info-model-%d").build(), ) } -} +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/NodeCollectionModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/NodeCollectionModel.kt new file mode 100644 index 0000000000..b1403b30d9 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/NodeCollectionModel.kt @@ -0,0 +1,24 @@ +package com.tencent.bkrepo.opdata.service.model + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.data.mongodb.core.MongoTemplate +import org.springframework.stereotype.Service + +@Service +class NodeCollectionModel @Autowired constructor( + private val mongoTemplate: MongoTemplate +) { + companion object { + private const val COLLECTION_NAME = "node" + private const val SHARDING_COUNT = 256 + } + + fun statNodeNum(): MutableMap { + val result = mutableMapOf() + for (i in 0..SHARDING_COUNT) { + val collection = "${COLLECTION_NAME}_$i" + result[collection] = mongoTemplate.getCollection(collection).estimatedDocumentCount() + } + return result + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/ProjectModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/ProjectModel.kt similarity index 58% rename from src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/ProjectModel.kt rename to src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/ProjectModel.kt index 940088cfa8..28064c13f5 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/ProjectModel.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/ProjectModel.kt @@ -1,35 +1,4 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model +package com.tencent.bkrepo.opdata.service.model import com.tencent.bkrepo.opdata.constant.OPDATA_PROJECT import com.tencent.bkrepo.opdata.pojo.enums.ProjectType @@ -88,4 +57,4 @@ class ProjectModel @Autowired constructor( } } } -} +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/RepoModel.kt similarity index 53% rename from src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoModel.kt rename to src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/RepoModel.kt index fa3224b9c2..84d4a55684 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/model/RepoModel.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/RepoModel.kt @@ -1,39 +1,9 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2020 Tencent. 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.bkrepo.opdata.model +package com.tencent.bkrepo.opdata.service.model import com.google.common.cache.CacheBuilder import com.tencent.bkrepo.opdata.constant.OPDATA_PROJECT_ID import com.tencent.bkrepo.opdata.constant.OPDATA_REPOSITORY +import com.tencent.bkrepo.opdata.model.RepoInfo import org.springframework.beans.factory.annotation.Autowired import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.query.Criteria @@ -73,4 +43,4 @@ class RepoModel @Autowired constructor( private const val DEFAULT_CACHE_SIZE = 1000L private const val DEFAULT_CACHE_DURATION_SECONDS = 5L } -} +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/StorageCredentialsModel.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/StorageCredentialsModel.kt new file mode 100644 index 0000000000..53d451efa8 --- /dev/null +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/service/model/StorageCredentialsModel.kt @@ -0,0 +1,35 @@ +package com.tencent.bkrepo.opdata.service.model + +import com.tencent.bkrepo.opdata.constant.TO_GIGABYTE +import com.tencent.bkrepo.opdata.pojo.enums.StatMetrics +import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository +import com.tencent.bkrepo.opdata.util.StatDateUtil +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Service + +@Service +class StorageCredentialsModel @Autowired constructor( + private val projectMetricsRepository: ProjectMetricsRepository +) { + + fun getStorageCredentialsStat(metrics: StatMetrics): Map { + val result = mutableMapOf() + val projectMetricsList = projectMetricsRepository.findAllByCreatedDate(StatDateUtil.getStatDate()) + projectMetricsList.forEach { projectMetrics -> + projectMetrics.repoMetrics.forEach { + val value = if (metrics == StatMetrics.NUM) it.num else it.size + if (result.containsKey(it.credentialsKey)) { + result[it.credentialsKey!!] = result[it.credentialsKey!!]!! + value + } else { + result[it.credentialsKey!!] = value + } + } + } + if (metrics == StatMetrics.SIZE) { + result.keys.forEach { + result[it] = result[it]!! / TO_GIGABYTE + } + } + return result + } +} \ No newline at end of file diff --git a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/MetricsCacheUtil.kt b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/MetricsCacheUtil.kt index c254ff557b..592370e52a 100644 --- a/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/MetricsCacheUtil.kt +++ b/src/backend/opdata/biz-opdata/src/main/kotlin/com/tencent/bkrepo/opdata/util/MetricsCacheUtil.kt @@ -30,7 +30,7 @@ package com.tencent.bkrepo.opdata.util import com.google.common.cache.CacheBuilder import com.google.common.cache.CacheLoader import com.google.common.cache.LoadingCache -import com.tencent.bkrepo.opdata.model.ProjectModel +import com.tencent.bkrepo.opdata.service.model.ProjectModel import com.tencent.bkrepo.opdata.model.TProjectMetrics import com.tencent.bkrepo.opdata.pojo.enums.ProjectType import com.tencent.bkrepo.opdata.repository.ProjectMetricsRepository diff --git a/src/frontend/core/devops-op/package.json b/src/frontend/core/devops-op/package.json index 5e62dd1674..9759e7c37c 100644 --- a/src/frontend/core/devops-op/package.json +++ b/src/frontend/core/devops-op/package.json @@ -29,6 +29,7 @@ "nprogress": "0.2.0", "path-browserify": "^1.0.1", "path-to-regexp": "8.0.0", + "relation-graph": "^2.2.11", "vue": "2.6.14", "vue-router": "3.0.6", "vuex": "3.1.0" diff --git a/src/frontend/core/devops-op/src/api/nt.js b/src/frontend/core/devops-op/src/api/nt.js new file mode 100644 index 0000000000..c42a7fa2b7 --- /dev/null +++ b/src/frontend/core/devops-op/src/api/nt.js @@ -0,0 +1,17 @@ +import request from '@/utils/request' + +const PREFIX_SERVICES = '/opdata/api/internal-flow' + +export function queryByLevel(level) { + return request({ + url: `${PREFIX_SERVICES}/names/level/${level}`, + method: 'get' + }) +} + +export function queryRelationByName(name) { + return request({ + url: `${PREFIX_SERVICES}/related/name/${name}`, + method: 'get' + }) +} diff --git a/src/frontend/core/devops-op/src/icons/svg/nt.svg b/src/frontend/core/devops-op/src/icons/svg/nt.svg new file mode 100644 index 0000000000..268cfb3ba6 --- /dev/null +++ b/src/frontend/core/devops-op/src/icons/svg/nt.svg @@ -0,0 +1 @@ + diff --git a/src/frontend/core/devops-op/src/router/index.js b/src/frontend/core/devops-op/src/router/index.js index cec633cdd9..faa43896b6 100644 --- a/src/frontend/core/devops-op/src/router/index.js +++ b/src/frontend/core/devops-op/src/router/index.js @@ -34,6 +34,7 @@ export const ROUTER_NAME_MIGRATE_REPO_STORAGE_CONFIG = 'MigrationConfig' export const ROUTER_NAME_MIGRATE_PROJECT_GRAYSCALE_CONFIG = 'ProjectGrayscaleConfig' export const ROUTER_NAME_SERVER_LOG = 'ServerLog' export const ROUTER_NAME_SIGN_CONFIG = 'SignConfig' +export const ROUTER_NAME_NT = 'NT' Vue.use(Router) @@ -98,6 +99,12 @@ export const asyncRoutes = [ redirect: '/system/account', meta: { title: '系统管理', icon: 'setting' }, children: [ + { + path: 'nt', + name: ROUTER_NAME_NT, + meta: { title: '网络拓扑', icon: 'nt' }, + component: () => import('@/views/net-topology/index') + }, { path: 'account', name: ROUTER_NAME_ACCOUNT, diff --git a/src/frontend/core/devops-op/src/views/net-topology/components/detail.vue b/src/frontend/core/devops-op/src/views/net-topology/components/detail.vue new file mode 100644 index 0000000000..fde0fd3a29 --- /dev/null +++ b/src/frontend/core/devops-op/src/views/net-topology/components/detail.vue @@ -0,0 +1,144 @@ + + + + + diff --git a/src/frontend/core/devops-op/src/views/net-topology/index.vue b/src/frontend/core/devops-op/src/views/net-topology/index.vue new file mode 100644 index 0000000000..2b4529d9a2 --- /dev/null +++ b/src/frontend/core/devops-op/src/views/net-topology/index.vue @@ -0,0 +1,263 @@ + + + + + + diff --git a/src/gateway/lua/auth/auth_generic.lua b/src/gateway/lua/auth/auth_generic.lua index e2975b3bfe..8eb632cffe 100644 --- a/src/gateway/lua/auth/auth_generic.lua +++ b/src/gateway/lua/auth/auth_generic.lua @@ -20,7 +20,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI --- head 请求发送到generic服务,顺带鉴权 local httpc = http.new() -local addr = "http://" .. hostUtil:get_addr("generic") +local addr = "http://" .. hostUtil:get_addr("generic",true) local path = string.gsub(ngx.var.request_uri, "/generic", "") local headers = ngx.req.get_headers() diff --git a/src/gateway/lua/router_srv.lua b/src/gateway/lua/router_srv.lua index 9a9af137c7..718b252429 100644 --- a/src/gateway/lua/router_srv.lua +++ b/src/gateway/lua/router_srv.lua @@ -82,8 +82,7 @@ if healthUtil:check_path(service_name) == false then return end - -ngx.var.target = hostUtil:get_addr(service_name) +ngx.var.target = hostUtil:get_addr(service_name, true) if ngx.var.assembly ~= nil and ngx.var.assembly ~= "" then ngx.var.target = ngx.var.target .. "/" .. service_name diff --git a/src/gateway/lua/util/health_util.lua b/src/gateway/lua/util/health_util.lua index 23b34a5dd5..ed8467a95f 100644 --- a/src/gateway/lua/util/health_util.lua +++ b/src/gateway/lua/util/health_util.lua @@ -19,6 +19,71 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI local _M = {} +-- 上报心跳信息到opdata服务 +function _M:report_heartbeat(domain) + + -- 构建心跳数据(timestamp 和 last_update 由服务端生成) + local ip = string.gsub(internal_ip, "[\r\n]+", "") + local heartbeat_data = { + ip = ip, + tag = config.ns.tag + } + + -- 转换为JSON + local request_body = json.encode(heartbeat_data) + if not request_body then + ngx.log(ngx.ERR, "failed to encode heartbeat data to json") + return + end + + -- 初始化HTTP连接 + local httpc = http.new() + local addr + if not domain then + domain = hostUtil:get_addr("opdata", false) + addr = "http://" .. domain .. "/api/heartbeat/gateway" + else + addr = "http://" .. domain .. "/opdata/api/heartbeat/gateway" + end + + -- 设置超时时间 + httpc:set_timeout(3000) + httpc:connect(addr) + + -- 构建请求头 + local request_headers = { + ["Content-Type"] = "application/json", + ["Accept"] = "application/json", + ["X-BKREPO-UID"] = "admin", + ["Authorization"] = config.bkrepo.authorization + } + + -- 发送HTTP请求 + local res, err = httpc:request_uri(addr, { + path = path, + method = "POST", + headers = request_headers, + body = request_body + }) + + -- 判断是否出错 + if not res then + ngx.log(ngx.ERR, "failed to report heartbeat to opdata: ", err) + return + end + + -- 判断返回的状态码 + if res.status ~= 200 then + ngx.log(ngx.ERR, "failed to report heartbeat, status: ", res.status, ", body: ", res.body or "") + return + end + + -- 设置HTTP保持连接 + httpc:set_keepalive(60000, 5) + + ngx.log(ngx.INFO, "heartbeat reported successfully for ip: ", ip) +end + --[[判断字符串是否在数组中]] function _M:check_path(service_name) local security_paths = config.security_paths diff --git a/src/gateway/lua/util/host_util.lua b/src/gateway/lua/util/host_util.lua index 913490a6b7..11d870ee2b 100644 --- a/src/gateway/lua/util/host_util.lua +++ b/src/gateway/lua/util/host_util.lua @@ -20,7 +20,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI local _M = {} --[[获取微服务真实地址]] -function _M:get_addr(service_name) +function _M:get_addr(service_name, check_name_space) local service_prefix = config.service_prefix if service_prefix == nil then @@ -28,7 +28,7 @@ function _M:get_addr(service_name) end -- return k8s service address - if ngx.var.name_space ~= "" then + if check_name_space and ngx.var.name_space ~= "" then return service_prefix .. service_name .. "." .. ngx.var.name_space .. ".svc.cluster.local" end diff --git a/src/gateway/lua/util/oauth_util.lua b/src/gateway/lua/util/oauth_util.lua index 69bc955f55..95ae164c30 100644 --- a/src/gateway/lua/util/oauth_util.lua +++ b/src/gateway/lua/util/oauth_util.lua @@ -271,7 +271,7 @@ function _M:verify_bkrepo_token(bkrepo_login_token) if user_cache_value == nil then --- 初始化HTTP连接 local httpc = http.new() - local addr = "http://" .. hostUtil:get_addr("auth") + local addr = "http://" .. hostUtil:get_addr("auth", true) local path = "/api/user/verify?bkrepo_ticket=" .. bkrepo_login_token if config.service_name ~= nil and config.service_name ~= "" then path = "/auth" .. path