Skip to content

Commit

Permalink
feat :: 상태확인 API
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSu1768 committed Feb 20, 2024
1 parent 5ef13b5 commit f70b6bf
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ class DeployController(
@RequestParam("env_type")
envType: EnvType
): DeployLogResponse = deployService.getLogs(deployId, envType)

@GetMapping("/status")
fun getPodStatus(
@RequestParam("deploy_id")
deployId: UUID
) = deployService.checkPodStatus(deployId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ interface DeployService {
fun getDeployList(projectId: UUID): DeployListResponse
fun updateUrl(containerName: String, serviceType: ServiceType, prefix: String?, domain: Map<String,String>?)
fun getLogs(deployId: UUID, envType: EnvType): DeployLogResponse
fun checkPodStatus(deployId: UUID): Map<String, String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ import com.example.mergebackend.infra.feign.deploy.dto.FeignCreateDeployRequest
import com.example.mergebackend.infra.feign.tsdata.TsDataClient
import com.example.mergebackend.infra.feign.tsdata.dto.GetLogRequest
import com.example.mergebackend.infra.feign.tsdata.dto.QueryDto
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import com.example.mergebackend.infra.kubernetes.KubernetesClientUtil
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.Instant
import java.util.*
import javax.management.Query

@Transactional
@Service
Expand All @@ -38,7 +36,8 @@ private class DeployServiceImpl(
private val projectRepository: ProjectRepository,
private val deployClient: DeployClient,
private val userFacade: UserFacade,
private val tsDataClient: TsDataClient
private val tsDataClient: TsDataClient,
private val kubernetesClientUtil: KubernetesClientUtil
): DeployService {
override fun createDeploy(createDeployRequest: CreateDeployRequest): CreateDeployResponse {
val project = projectRepository.findByIdOrNull(createDeployRequest.projectId) ?: throw ProjectNotFoundException
Expand Down Expand Up @@ -148,4 +147,19 @@ private class DeployServiceImpl(
return DeployLogResponse(response.results.a.frames[0].data.values[2])

}

override fun checkPodStatus(deployId: UUID): Map<String, String> {
val deploy = deployRepository.findByIdOrNull(deployId) ?: throw DeployNotFoundException

val namespacePrefix = "${deploy.project.teamNameEn}-"
val deployPrefix = "${deploy.containerName}-${deploy.serviceType.toString().lowercase()}-"

val stagStatus = kubernetesClientUtil.checkContainerStatus(namespace = "${namespacePrefix}stag", deploymentName = "${deployPrefix}stag")
val prodStatus = kubernetesClientUtil.checkContainerStatus(namespace = "${namespacePrefix}prod", deploymentName = "${deployPrefix}prod")

return mapOf(
"stag" to stagStatus,
"prod" to prodStatus
)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.example.mergebackend.global.config.kubernetes

import com.example.mergebackend.global.config.kubernetes.dto.KubernetesToken
import com.example.mergebackend.global.env.kubernetes.KubernetesProperty
import com.example.mergebackend.global.env.kubernetes.XquareAwsProperty
import com.example.mergebackend.infra.kubernetes.exception.KubernetesException
import io.kubernetes.client.openapi.Configuration
import io.kubernetes.client.openapi.apis.CoreV1Api
import io.kubernetes.client.openapi.apis.CustomObjectsApi
import io.kubernetes.client.util.ClientBuilder
import io.kubernetes.client.util.KubeConfig
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.springframework.context.annotation.Bean
import software.amazon.awssdk.regions.Region
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.StringReader
import java.nio.charset.Charset
import java.util.*
Expand Down Expand Up @@ -55,4 +50,7 @@ class KubernetesClientConfig(

@Bean
fun customObjectsApi() = CustomObjectsApi()

@Bean
fun coreV1API() = CoreV1Api()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.example.mergebackend.infra.kubernetes

interface KubernetesClientUtil {
fun deleteSecret(namespace: String, crName: String)
fun checkContainerStatus(deploymentName: String, namespace: String): String
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.example.mergebackend.infra.kubernetes

import com.example.mergebackend.infra.kubernetes.exception.KubernetesException
import io.kubernetes.client.openapi.ApiException
import io.kubernetes.client.openapi.apis.CoreV1Api
import io.kubernetes.client.openapi.apis.CustomObjectsApi
import io.kubernetes.client.openapi.models.V1DeleteOptions
import org.springframework.stereotype.Service
import java.lang.Exception

@Service
class KubernetesClientUtilImpl(
private val customObjectsApi: CustomObjectsApi
private val customObjectsApi: CustomObjectsApi,
private val coreV1Api: CoreV1Api
): KubernetesClientUtil {
override fun deleteSecret(namespace: String, crName: String) {
val v1DeleteOption = V1DeleteOptions()
Expand All @@ -30,4 +32,30 @@ class KubernetesClientUtilImpl(
throw KubernetesException
}
}

override fun checkContainerStatus(deploymentName: String, namespace: String): String {
try {
val podList = coreV1Api.listNamespacedPod(
namespace,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
)
podList.items.forEach { pod ->
if (pod.metadata?.name?.startsWith(deploymentName) == true) {
return pod.status?.phase ?: "배포되지 않았습니다."
}
}
} catch (e: ApiException) {
e.printStackTrace()
}
return "d"
}
}

0 comments on commit f70b6bf

Please sign in to comment.