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

Database connection #86

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion src/main/kotlin/one/oktw/galaxy/proxy/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Main {
// Start lobby TODO auto scale lobby
GlobalScope.launch {
try {
lobby = kubernetesClient.getOrCreateGalaxyAndVolume("galaxy-lobby", config.galaxies["lobby"]!!)
lobby = kubernetesClient.getOrCreateGalaxyAndVolume("galaxy-lobby", config.galaxies["lobby"]!!, config.mongoConfig)
.let { if (!Readiness.isReady(it)) kubernetesClient.waitReady(it) else it }
.let {
proxy.registerServer(
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/one/oktw/galaxy/proxy/config/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package one.oktw.galaxy.proxy.config

import com.google.gson.Gson
import one.oktw.galaxy.proxy.config.model.GalaxySpec
import one.oktw.galaxy.proxy.config.model.MongoConfig
import one.oktw.galaxy.proxy.config.model.ProxyConfig
import one.oktw.galaxy.proxy.config.model.RedisConfig
import java.io.InputStream
Expand All @@ -17,6 +18,8 @@ class ConfigManager(private val basePath: Path = Paths.get("config")) {
private set
lateinit var redisConfig: RedisConfig
private set
lateinit var mongoConfig: MongoConfig
private set
val galaxies = HashMap<String, GalaxySpec>()

init {
Expand All @@ -41,6 +44,7 @@ class ConfigManager(private val basePath: Path = Paths.get("config")) {
private fun readConfig() {
proxyConfig = fallbackToResource("proxy.json").reader().use { gson.fromJson(it, ProxyConfig::class.java) }
redisConfig = fallbackToResource("redis.json").reader().use { gson.fromJson(it, RedisConfig::class.java) }
mongoConfig = fallbackToResource("galaxies/mongo.json").reader().use { gson.fromJson(it, MongoConfig::class.java) }
}

private fun readGalaxies(path: Path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package one.oktw.galaxy.proxy.config.model

data class MongoConfig(
val URI: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要多一個Class,直接放在 model/Galaxy.kt 內

)
3 changes: 2 additions & 1 deletion src/main/kotlin/one/oktw/galaxy/proxy/event/GalaxyPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import one.oktw.galaxy.proxy.Main
import one.oktw.galaxy.proxy.Main.Companion.main
import one.oktw.galaxy.proxy.api.ProxyAPI
import one.oktw.galaxy.proxy.api.packet.*
Expand Down Expand Up @@ -71,7 +72,7 @@ class GalaxyPacket : CoroutineScope by CoroutineScope(Dispatchers.Default + Supe
.let { source.sendPluginMessage(MESSAGE_CHANNEL_ID, it) }

// Start galaxy
galaxy = kubernetes.getOrCreateGalaxyAndVolume(id, main.config.galaxies["normal_galaxy"]!!) // TODO multi type
galaxy = kubernetes.getOrCreateGalaxyAndVolume(id, main.config.galaxies["normal_galaxy"]!!, Main.main.config.mongoConfig) // TODO multi type

// Send packet to server: Galaxy is starting
ProxyAPI.encode(CreateGalaxy.CreateProgress(data.uuid, ProgressStage.Starting))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.fabric8.kubernetes.client.internal.readiness.ReadinessWatcher
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import one.oktw.galaxy.proxy.config.model.GalaxySpec
import one.oktw.galaxy.proxy.config.model.MongoConfig
import one.oktw.galaxy.proxy.kubernetes.Templates.galaxy
import one.oktw.galaxy.proxy.kubernetes.Templates.volume
import java.util.concurrent.TimeUnit
Expand All @@ -17,17 +18,17 @@ class KubernetesClient {

suspend fun info(): VersionInfo = withContext(IO) { client.version }

suspend fun getOrCreateGalaxyAndVolume(name: String, spec: GalaxySpec): Pod {
return getGalaxy(name) ?: createGalaxy(name, spec)
suspend fun getOrCreateGalaxyAndVolume(name: String, spec: GalaxySpec, mongoConfig: MongoConfig): Pod {
return getGalaxy(name) ?: createGalaxy(name, spec, mongoConfig)
}

suspend fun getGalaxy(name: String): Pod? = withContext(IO) {
client.pods().withName(name).get()
}

suspend fun createGalaxy(name: String, spec: GalaxySpec): Pod = withContext(IO) {
suspend fun createGalaxy(name: String, spec: GalaxySpec, mongoConfig: MongoConfig): Pod = withContext(IO) {
getOrCreateVolume(name, spec.Storage!!)
client.pods().create(galaxy(name, spec))
client.pods().create(galaxy(name, spec, mongoConfig))
}

suspend fun getOrCreateVolume(name: String, spec: GalaxySpec.GalaxyStorage): PersistentVolumeClaim {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.velocitypowered.api.proxy.config.ProxyConfig
import io.fabric8.kubernetes.api.model.*
import one.oktw.galaxy.proxy.Main.Companion.main
import one.oktw.galaxy.proxy.config.model.GalaxySpec
import one.oktw.galaxy.proxy.config.model.MongoConfig
import java.nio.charset.StandardCharsets

// Velocity config hack
Expand Down Expand Up @@ -32,7 +33,7 @@ object Templates {
}
}

fun galaxy(name: String, spec: GalaxySpec): Pod {
fun galaxy(name: String, spec: GalaxySpec, mongoConfig: MongoConfig): Pod {
requireNotNull(spec.Storage) { "Storage spec undefined!" }

return newPod {
Expand All @@ -52,6 +53,7 @@ object Templates {
env = listOf(
EnvVar("FABRIC_PROXY_SECRET", forwardSecret, null),
EnvVar("resourcePack", spec.ResourcePack, null),
EnvVar("dbPath", mongoConfig.URI, null),
EnvVar("GALAXY_ID", name, null)
)

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config/galaxies/mongo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"URI": "mongo://localhost"
}