Skip to content

Commit

Permalink
fix: 强制删除依赖源仓库时未删除包数据 TencentBlueKing#1720
Browse files Browse the repository at this point in the history
  • Loading branch information
scplsy authored Feb 1, 2024
1 parent 870632c commit 176cd49
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum class ArtifactMessageCode(private val key: String) : MessageCode {
PROJECT_EXISTED("artifact.project.existed"),
REPOSITORY_NOT_FOUND("artifact.repository.not-found"),
REPOSITORY_EXISTED("artifact.repository.existed"),
REPOSITORY_CONTAINS_FILE("artifact.repository.not-empty"),
REPOSITORY_CONTAINS_ARTIFACT("artifact.repository.not-empty"),
FOLDER_CONTAINS_FILE("artifact.folder.not-empty"),
NODE_NOT_FOUND("artifact.node.not-found"),
NODE_PATH_INVALID("artifact.node.path.invalid"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ package com.tencent.bkrepo.common.artifact.pojo
/**
* 仓库类型
*/
enum class RepositoryType {
NONE,
GENERIC,
DOCKER,
MAVEN,
PYPI,
NPM,
HELM,
RDS,
COMPOSER,
RPM,
NUGET,
GIT,
OCI,
CONAN,
LFS,
DDC,
SVN,
S3
enum class RepositoryType(val supportPackage: Boolean) {
NONE(false),
GENERIC(false),
DOCKER(true),
MAVEN(true),
PYPI(true),
NPM(true),
HELM(true),
RDS(true),
COMPOSER(true),
RPM(true),
NUGET(true),
GIT(true),
OCI(true),
CONAN(true),
LFS(false),
DDC(false),
SVN(false),
S3(false)
;

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ artifact.project.not-found=项目[{0}]不存在
artifact.project.existed=项目[{0}]已存在
artifact.repository.not-found=仓库[{0}]不存在
artifact.repository.existed=仓库[{0}]已存在
artifact.repository.not-empty=仓库包含文件
artifact.repository.not-empty=仓库包含制品
artifact.repository.over-quota=超出仓库[{0}]配额值[{1}]
artifact.folder.not-empty=目录包含文件
artifact.node.not-found=节点[{0}]不存在
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ artifact.project.not-found=項目[{0}]不存在
artifact.project.existed=項目[{0}]已存在
artifact.repository.not-found=倉庫[{0}]不存在
artifact.repository.existed=倉庫[{0}]已存在
artifact.repository.not-empty=倉庫包含文件
artifact.repository.not-empty=倉庫包含製品
artifact.repository.over-quota=超過倉庫[{0}]配額值[{1}]
artifact.folder.not-empty=目录包含文件
artifact.node.not-found=節點[{0}]不存在
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ interface PackageService {
realIpAddress: String? = null
)

/**
* 删除所有包
*
* @param projectId 项目id
* @param repoName 仓库名称
*/
fun deleteAllPackage(projectId: String, repoName: String)

/**
* 更新包
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ class PackageServiceImpl(
logger.info("Delete package version[$projectId/$repoName/$packageKey-$versionName] success")
}

override fun deleteAllPackage(projectId: String, repoName: String) {
val query = PackageQueryHelper.packageListQuery(projectId, repoName, null)
var pageNumber = 1
do {
val packageList = packageDao.find(query.with(Pages.ofRequest(pageNumber, 1000)))
packageList.forEach { deletePackage(it.projectId, it.repoName, it.key) }
pageNumber++
} while (packageList.isNotEmpty())
}

override fun updatePackage(request: PackageUpdateRequest, realIpAddress: String?) {
val projectId = request.projectId
val repoName = request.repoName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import com.tencent.bkrepo.repository.pojo.repo.RepoUpdateRequest
import com.tencent.bkrepo.repository.pojo.repo.RepositoryDetail
import com.tencent.bkrepo.repository.pojo.repo.RepositoryInfo
import com.tencent.bkrepo.repository.service.node.NodeService
import com.tencent.bkrepo.repository.service.packages.PackageService
import com.tencent.bkrepo.repository.service.repo.ProjectService
import com.tencent.bkrepo.repository.service.repo.ProxyChannelService
import com.tencent.bkrepo.repository.service.repo.RepositoryService
Expand All @@ -84,7 +85,9 @@ import com.tencent.bkrepo.repository.util.RepoEventFactory.buildDeletedEvent
import com.tencent.bkrepo.repository.util.RepoEventFactory.buildUpdatedEvent
import java.time.Duration
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Conditional
import org.springframework.context.annotation.Lazy
import org.springframework.dao.DuplicateKeyException
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
Expand Down Expand Up @@ -118,6 +121,10 @@ class RepositoryServiceImpl(
private val projectMetricsRepository: ProjectMetricsRepository,
) : RepositoryService {

@Autowired
@Lazy
lateinit var packageService: PackageService

init {
Companion.repositoryProperties = repositoryProperties
}
Expand Down Expand Up @@ -336,24 +343,12 @@ class RepositoryServiceImpl(
override fun deleteRepo(repoDeleteRequest: RepoDeleteRequest) {
repoDeleteRequest.apply {
val repository = checkRepository(projectId, name)
if (repoDeleteRequest.forced) {
nodeService.deleteByPath(projectId, name, ROOT, operator)
} else {
val artifactInfo = DefaultArtifactInfo(projectId, name, ROOT)
nodeService.countFileNode(artifactInfo).takeIf { it == 0L } ?: throw ErrorCodeException(
ArtifactMessageCode.REPOSITORY_CONTAINS_FILE,
)
nodeService.deleteByPath(projectId, name, ROOT, operator)
}

clearRepo(projectId, name, repository.type.supportPackage, forced, operator)
// 为避免仓库被删除后节点无法被自动清理的问题,对仓库实行假删除
if (!repository.id.isNullOrBlank()) {
repositoryDao.updateFirst(
Query(Criteria.where(ID).isEqualTo(repository.id)),
Update().set(TRepository::deleted.name, LocalDateTime.now()),
)
}

repositoryDao.updateFirst(
Query(Criteria.where(ID).isEqualTo(repository.id!!)),
Update().set(TRepository::deleted.name, LocalDateTime.now())
)
// 删除关联的库
if (repository.category == RepositoryCategory.COMPOSITE) {
val configuration = repository.configuration.readJsonString<CompositeConfiguration>()
Expand Down Expand Up @@ -385,6 +380,34 @@ class RepositoryServiceImpl(
)
}

/**
* 删除仓库内容
*/
protected fun clearRepo(
projectId: String,
repoName: String,
supportPackage: Boolean,
forced: Boolean,
operator: String
) {
if (forced) {
logger.info("Force clear repository [$projectId/$repoName] by [$operator]")
if (supportPackage) {
packageService.deleteAllPackage(projectId, repoName)
}
} else {
// 当仓库类型支持包管理,仓库下没有包时视为空仓库,删除仓库下所有节点
val isEmpty = if (supportPackage) {
packageService.getPackageCount(projectId, repoName) == 0L
} else {
val artifactInfo = DefaultArtifactInfo(projectId, repoName, ROOT)
nodeService.countFileNode(artifactInfo) == 0L
}
if (!isEmpty) throw ErrorCodeException(ArtifactMessageCode.REPOSITORY_CONTAINS_ARTIFACT)
}
nodeService.deleteByPath(projectId, repoName, ROOT, operator)
}

/**
* 获取仓库下的代理地址信息
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ import com.tencent.bkrepo.auth.api.ServicePermissionClient
import com.tencent.bkrepo.common.api.exception.ErrorCodeException
import com.tencent.bkrepo.common.api.message.CommonMessageCode
import com.tencent.bkrepo.common.api.util.readJsonString
import com.tencent.bkrepo.common.artifact.api.DefaultArtifactInfo
import com.tencent.bkrepo.common.artifact.message.ArtifactMessageCode
import com.tencent.bkrepo.common.artifact.path.PathUtils
import com.tencent.bkrepo.common.artifact.pojo.RepositoryCategory
import com.tencent.bkrepo.common.artifact.pojo.RepositoryType
import com.tencent.bkrepo.common.artifact.pojo.configuration.RepositoryConfiguration
import com.tencent.bkrepo.common.artifact.pojo.configuration.composite.CompositeConfiguration
import com.tencent.bkrepo.common.artifact.util.ClusterUtils
import com.tencent.bkrepo.common.mongo.dao.AbstractMongoDao
import com.tencent.bkrepo.common.security.util.SecurityUtils
import com.tencent.bkrepo.common.service.cluster.ClusterProperties
import com.tencent.bkrepo.common.service.cluster.CommitEdgeCenterCondition
Expand All @@ -59,8 +58,12 @@ import com.tencent.bkrepo.repository.service.repo.impl.RepositoryServiceImpl
import com.tencent.bkrepo.repository.util.RepoEventFactory
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Conditional
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.Update
import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.stereotype.Service
import java.time.LocalDateTime

@Service
@Conditional(CommitEdgeCenterCondition::class)
Expand Down Expand Up @@ -142,19 +145,14 @@ class CommitEdgeCenterRepositoryServiceImpl(
override fun deleteRepo(repoDeleteRequest: RepoDeleteRequest) {
repoDeleteRequest.apply {
val repository = checkRepository(projectId, name)
if (repoDeleteRequest.forced) {
nodeService.deleteByPath(projectId, name, PathUtils.ROOT, operator)
} else {
val artifactInfo = DefaultArtifactInfo(projectId, name, PathUtils.ROOT)
nodeService.countFileNode(artifactInfo).takeIf { it == 0L } ?: throw ErrorCodeException(
ArtifactMessageCode.REPOSITORY_CONTAINS_FILE
)
nodeService.deleteByPath(projectId, name, PathUtils.ROOT, operator)
}
clearRepo(projectId, name, repository.type.supportPackage, forced, operator)
val clusterNames = repository.clusterNames.orEmpty().toMutableSet()
clusterNames.remove(SecurityUtils.getClusterName() ?: clusterProperties.self.name)
if (clusterNames.isEmpty()) {
repositoryDao.deleteById(repository.id)
repositoryDao.updateFirst(
Query(Criteria.where(AbstractMongoDao.ID).isEqualTo(repository.id!!)),
Update().set(TRepository::deleted.name, LocalDateTime.now()),
)
} else {
repository.clusterNames = clusterNames
repositoryDao.save(repository)
Expand Down

0 comments on commit 176cd49

Please sign in to comment.