-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94f8012
commit 16be8f3
Showing
2 changed files
with
50 additions
and
36 deletions.
There are no files selected for viewing
38 changes: 2 additions & 36 deletions
38
.../kotlin/xquare/app/xquareinfra/infrastructure/kubernetes/config/KubernetesClientConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...quare/app/xquareinfra/infrastructure/kubernetes/config/KubernetesTokenRefreshScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |