Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/k8s resource cleanup api #8

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package xquare.app.xquareinfra.adapter.`in`.deploy

import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.DeleteContainerResponse
import xquare.app.xquareinfra.application.auth.port.out.SecurityPort
import xquare.app.xquareinfra.application.deploy.port.`in`.DeployUseCase
import java.util.*

@RequestMapping("/v2/deploy")
@RestController
class V2DeployWebAdapter(
private val deployUseCase: DeployUseCase,
private val securityPort: SecurityPort
) {
@DeleteMapping("/{deployId}")
fun deleteContainer(
@PathVariable
deployId: UUID,
): DeleteContainerResponse {
return deployUseCase.deleteDeploy(securityPort.getCurrentUser(), deployId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package xquare.app.xquareinfra.adapter.`in`.deploy.dto.request

import java.util.UUID

data class DeleteContainerRequest(
val deployId: UUID
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package xquare.app.xquareinfra.adapter.`in`.deploy.dto.response

import java.util.UUID

data class DeleteContainerResponse (
val deployId: UUID,
val deployName: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import xquare.app.xquareinfra.adapter.out.external.github.client.dto.request.Doc
import xquare.app.xquareinfra.adapter.out.external.github.env.GithubProperties
import xquare.app.xquareinfra.application.container.port.out.CreateDockerfilePort
import xquare.app.xquareinfra.application.team.port.out.SyncTeamPort
import java.util.*

@Component
class GithubAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class DeployPersistenceAdapter(
override fun saveDeploy(deploy: Deploy): Deploy {
return deployMapper.toModel(deployRepository.save(deployMapper.toEntity(deploy)))
}
override fun deleteDeploy(deploy: Deploy) {
return deployRepository.delete(deployMapper.toEntity(deploy))
}
override fun existByDeployName(deployName: String): Boolean {
return deployRepository.existsByDeployName(deployName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ class ContainerService(
saveDeployPort.saveDeploy(deploy.migrationToV2())
}



override fun getContainerDetails(deployId: UUID, environment: ContainerEnvironment): GetContainerDetailsResponse {
val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND
val container = findContainerPort.findByDeployAndEnvironment(deploy, environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package xquare.app.xquareinfra.application.deploy.port.`in`

import xquare.app.xquareinfra.adapter.`in`.deploy.dto.request.ApproveDeployRequest
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.request.CreateDeployRequest
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.request.DeleteContainerRequest
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.CreateDeployResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.DeleteContainerResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.DeployDetailsResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.SimpleDeployListResponse
import xquare.app.xquareinfra.domain.user.model.User
Expand All @@ -20,4 +22,6 @@ interface DeployUseCase {
fun getDeployDetails(deployId: UUID, user: User): DeployDetailsResponse

fun migrationDeploy(user: User)

fun deleteDeploy(user: User, deployId: UUID): DeleteContainerResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import xquare.app.xquareinfra.domain.deploy.model.Deploy

interface SaveDeployPort {
fun saveDeploy(deploy: Deploy): Deploy
fun deleteDeploy(deploy: Deploy)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.request.ApproveDeployRequest
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.request.CreateDeployRequest
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.CreateDeployResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.DeployDetailsResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.SimpleDeployListResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.SimpleDeployResponse
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.request.DeleteContainerRequest
import xquare.app.xquareinfra.adapter.`in`.deploy.dto.response.*
import xquare.app.xquareinfra.adapter.out.external.deploy.client.DeployClient
import xquare.app.xquareinfra.adapter.out.external.deploy.client.dto.request.FeignCreateDeployRequest
import xquare.app.xquareinfra.adapter.out.external.github.client.GithubClient
import xquare.app.xquareinfra.adapter.out.external.github.client.dto.request.DispatchEventRequest
import xquare.app.xquareinfra.adapter.out.external.github.env.GithubProperties
import xquare.app.xquareinfra.application.container.port.out.FindContainerPort
import xquare.app.xquareinfra.application.deploy.port.`in`.DeployUseCase
import xquare.app.xquareinfra.application.deploy.port.out.ExistDeployPort
Expand Down Expand Up @@ -39,7 +40,9 @@ class DeployService(
private val findTeamPort: FindTeamPort,
private val existDeployPort: ExistDeployPort,
private val findContainerPort: FindContainerPort,
private val existsUserTeamPort: ExistsUserTeamPort
private val existsUserTeamPort: ExistsUserTeamPort,
private val githubProperties: GithubProperties,
private val githubClient: GithubClient
): DeployUseCase {

override fun approveDeploy(deployNameEn: String, req: ApproveDeployRequest) {
Expand Down Expand Up @@ -184,4 +187,34 @@ class DeployService(
)
}
}

override fun deleteDeploy(user: User, deployId: UUID): DeleteContainerResponse {
val deploy = findDeployPort.findById(deployId) ?: throw BusinessLogicException.DEPLOY_NOT_FOUND

if(!existsUserTeamPort.existsByTeamIdAndUser(deploy.teamId, user)) {
throw XquareException.FORBIDDEN
}
val authorization = "Bearer ${githubProperties.token}"
val accept = "application/vnd.github.v3+json"
val request = DispatchEventRequest(
event_type = "delete-service",
client_payload = mapOf(
"deployId" to deployId.toString(),
"deployName" to deploy.deployName,
"teamId" to deploy.teamId.toString(),
"organization" to deploy.organization,
"repository" to deploy.repository
)
)

githubClient.dispatchWorkflowGitops(authorization, accept, request)

saveDeployPort.deleteDeploy(deploy)

return DeleteContainerResponse(
deployId = deployId,
deployName = deploy.deployName
)
}

}