Skip to content

Commit

Permalink
feat :: kubernetes token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunSu1768 committed Jul 31, 2024
1 parent 94f8012 commit 16be8f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,51 +1,17 @@
package xquare.app.xquareinfra.infrastructure.kubernetes.config

import xquare.app.xquareinfra.infrastructure.kubernetes.env.KubernetesProperty
import xquare.app.xquareinfra.infrastructure.kubernetes.env.XquareProperties
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 org.springframework.context.annotation.Bean
import software.amazon.awssdk.regions.Region
import java.io.StringReader
import java.nio.charset.Charset
import java.util.*
import javax.annotation.PostConstruct


@org.springframework.context.annotation.Configuration
class KubernetesClientConfig(
private val xquareProperties: XquareProperties,
private val kubernetesProperty: KubernetesProperty
private val kubernetesTokenRefreshScheduler: KubernetesTokenRefreshScheduler
) {
@PostConstruct
fun initKubernetesConfig() {
configureAWS("default", xquareProperties.accessKey, xquareProperties.secretKey, Region.AP_NORTHEAST_2.toString())
val decodedBytes = Base64.getDecoder().decode(kubernetesProperty.kubeConfig)
val kubeconfig = String(decodedBytes, Charset.defaultCharset())
val client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(StringReader(kubeconfig))).build()
Configuration.setDefaultApiClient(client)
}

private fun configureAWS(profileName: String, accessKeyId: String, secretAccessKey: String, region: String) {
try {
val processBuilder = ProcessBuilder()
processBuilder.command("aws", "configure", "set", "aws_access_key_id", accessKeyId, "--profile", profileName)
var process = processBuilder.start()
process.waitFor()

processBuilder.command("aws", "configure", "set", "aws_secret_access_key", secretAccessKey, "--profile", profileName)
process = processBuilder.start()
process.waitFor()

processBuilder.command("aws", "configure", "set", "region", region, "--profile", profileName)
process = processBuilder.start()
process.waitFor()
} catch (e: Exception) {
e.printStackTrace()
}
kubernetesTokenRefreshScheduler.refreshKubernetesToken()
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package xquare.app.xquareinfra.infrastructure.kubernetes.config

import io.kubernetes.client.openapi.Configuration
import io.kubernetes.client.util.ClientBuilder
import io.kubernetes.client.util.KubeConfig
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import software.amazon.awssdk.regions.Region
import xquare.app.xquareinfra.infrastructure.kubernetes.env.KubernetesProperty
import xquare.app.xquareinfra.infrastructure.kubernetes.env.XquareProperties
import java.io.StringReader
import java.nio.charset.Charset
import java.util.*

@org.springframework.context.annotation.Configuration
@EnableScheduling
class KubernetesTokenRefreshScheduler(
private val xquareProperties: XquareProperties,
private val kubernetesProperty: KubernetesProperty
) {
@Scheduled(fixedRate = 14 * 60 * 1000) // 14 minutes
fun refreshKubernetesToken() {
configureAWS("default", xquareProperties.accessKey, xquareProperties.secretKey, Region.AP_NORTHEAST_2.toString())
val decodedBytes = Base64.getDecoder().decode(kubernetesProperty.kubeConfig)
val kubeconfig = String(decodedBytes, Charset.defaultCharset())
val client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(StringReader(kubeconfig))).build()
Configuration.setDefaultApiClient(client)
}

private fun configureAWS(profileName: String, accessKeyId: String, secretAccessKey: String, region: String) {
try {
val processBuilder = ProcessBuilder()
processBuilder.command("aws", "configure", "set", "aws_access_key_id", accessKeyId, "--profile", profileName)
var process = processBuilder.start()
process.waitFor()

processBuilder.command("aws", "configure", "set", "aws_secret_access_key", secretAccessKey, "--profile", profileName)
process = processBuilder.start()
process.waitFor()

processBuilder.command("aws", "configure", "set", "region", region, "--profile", profileName)
process = processBuilder.start()
process.waitFor()
} catch (e: Exception) {
e.printStackTrace()
}
}
}

0 comments on commit 16be8f3

Please sign in to comment.