Skip to content

Commit

Permalink
refactor(shulker-proxy-agent): factory function to create InetSocketA…
Browse files Browse the repository at this point in the history
…ddress
  • Loading branch information
jeremylvln committed Aug 25, 2024
1 parent 86b4804 commit 17f4f86
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.shulkermc.proxyagent

import io.shulkermc.proxyagent.utils.addressFromHostString
import java.net.InetSocketAddress
import java.util.Optional
import java.util.UUID
Expand All @@ -18,15 +19,7 @@ object Configuration {
.getOrDefault(15)
val PROXY_PREFERRED_RECONNECT_ADDRESS: Optional<InetSocketAddress> =
getOptionalStringEnv("SHULKER_PROXY_PREFERRED_RECONNECT_ADDRESS")
.map {
val parts = it.split(":")

if (parts.size == 2) {
InetSocketAddress(parts[0], parts[1].toInt())
} else {
InetSocketAddress(parts[0], 25565)
}
}
.map { str -> addressFromHostString(str) }

val NETWORK_ADMINS: List<UUID> =
getOptionalStringEnv("SHULKER_NETWORK_ADMINS")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.shulkermc.proxyagent.adapters.filesystem

import io.shulkermc.proxyagent.utils.addressFromHostString
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor
import org.apache.commons.io.monitor.FileAlterationMonitor
import org.apache.commons.io.monitor.FileAlterationObserver
import org.yaml.snakeyaml.Yaml
import java.io.File
import java.net.InetSocketAddress
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.exists
Expand Down Expand Up @@ -69,16 +69,10 @@ class LocalFileSystemAdapter : FileSystemAdapter {
val address = entry["address"] as String
val tags = entry.getOrDefault("tags", emptyList<String>()) as List<String>

val addressParts = address.split(":")

name to
FileSystemAdapter.ExternalServer(
name,
if (addressParts.size == 2) {
InetSocketAddress(addressParts[0], addressParts[1].toInt())
} else {
InetSocketAddress(address, 25565)
},
addressFromHostString(address),
tags.toSet(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.fabric8.kubernetes.client.informers.SharedIndexInformer
import io.fabric8.kubernetes.client.okhttp.OkHttpClientFactory
import io.shulkermc.proxyagent.Configuration
import io.shulkermc.proxyagent.adapters.kubernetes.models.AgonesV1GameServer
import io.shulkermc.proxyagent.utils.addressFromHostString
import java.net.InetSocketAddress
import java.util.Optional
import java.util.concurrent.CompletionStage
Expand Down Expand Up @@ -107,7 +108,7 @@ class ImplKubernetesGatewayAdapter(proxyNamespace: String, proxyName: String) :
private fun getExternalAddressFromService(service: Service): Optional<InetSocketAddress> {
return if (service.spec.type == "LoadBalancer") {
Optional.ofNullable(service.status.loadBalancer?.ingress?.firstOrNull())
.map { ingress -> InetSocketAddress(ingress.ip, MINECRAFT_DEFAULT_PORT) }
.map { ingress -> addressFromHostString(ingress.ip) }
} else {
Optional.empty()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import io.shulkermc.proxyagent.ShulkerProxyAgentCommon
import io.shulkermc.proxyagent.adapters.filesystem.FileSystemAdapter
import io.shulkermc.proxyagent.adapters.kubernetes.WatchAction
import io.shulkermc.proxyagent.adapters.kubernetes.models.AgonesV1GameServer
import io.shulkermc.proxyagent.utils.addressFromHostString
import java.net.InetSocketAddress
import java.util.Optional

class ServerDirectoryService(
private val agent: ShulkerProxyAgentCommon,
) {
companion object {
private const val DEFAULT_MINECRAFT_PORT = 25565
}

private val serversByTag = HashMap<String, MutableSet<String>>()
private val tagsByServer = HashMap<String, Set<String>>()

Expand Down Expand Up @@ -52,9 +49,8 @@ class ServerDirectoryService(

this.registerServer(
minecraftServer.metadata.name,
InetSocketAddress(
addressFromHostString(
"${minecraftServer.metadata.name}.${Configuration.CLUSTER_NAME}-cluster.${minecraftServer.metadata.namespace}",
DEFAULT_MINECRAFT_PORT,
),
tags?.split(",")?.toSet().orEmpty(),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.shulkermc.proxyagent.utils

import java.net.InetSocketAddress

fun addressFromHostString(
hostAndPort: String,
defaultPort: Int = 25565,
): InetSocketAddress {
val parts = hostAndPort.split(":")

return if (parts.size == 2) {
InetSocketAddress(parts[0], parts[1].toInt())
} else {
InetSocketAddress(parts[0], defaultPort)
}
}

0 comments on commit 17f4f86

Please sign in to comment.