Skip to content

Commit

Permalink
Allow pulling shard index from pod index variable
Browse files Browse the repository at this point in the history
This allows direct access of the pod's ordinal rather than needing to parse the pod name
  • Loading branch information
NovaFox161 committed Oct 22, 2024
1 parent 503878a commit b40ed04
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions core/src/main/kotlin/org/dreamexposure/discal/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,9 @@ class Application {
val instanceId: UUID = UUID.randomUUID()

fun getShardIndex(): Int {
/*
This fucking sucks. So k8s doesn't expose the pod ordinal for a pod in a stateful set
https://github.com/kubernetes/kubernetes/pull/68719
This has been an open issue and PR for over 3 years now, and has gone stale as of March 3rd 2021.
So, in order to get the pod ordinal since its not directly exposed, we have to get the hostname, and parse
the ordinal out of that.
To make sure we don't use this when running anywhere but inside of k8s, we are mapping the hostname to an env
variable SHARD_POD_NAME and if that is present, parsing it for the pod ordinal to tell the bot its shard index.
This will be removed when/if they add this feature directly and SHARD_INDEX will be an env variable...
*/

//Check if we are running in k8s or not...
val shardPodName = System.getenv("SHARD_POD_NAME")
return if (shardPodName != null) {
//In k8s, parse this shit
val parts = shardPodName.split("-")
parts[parts.size -1].toInt()
} else {
//Fall back to config value
Config.SHARD_INDEX.getInt()
}
val k8sPodIndex = System.getenv("KUBERNETES_POD_INDEX")
return k8sPodIndex?.toInt() ?: // Fall back to config
Config.SHARD_INDEX.getInt()
}

fun getShardCount(): Int {
Expand Down

0 comments on commit b40ed04

Please sign in to comment.