Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,11 @@ object KubeConfigUtils {

fun getCurrentClusterName(kubeconfigEnv: String? = null): String? {
return try {
getAllConfigFiles(kubeconfigEnv).firstNotNullOfOrNull { path ->
if (!isValid(path)) {
null
} else {
val allKubeConfigs = getAllConfigFiles(kubeconfigEnv)
.filter { isValid(it) }
.mapNotNull { path ->
try {
val kubeConfig = KubeConfig.loadKubeConfig(path.toFile().bufferedReader())
if (!kubeConfig.currentContext.isNullOrBlank()) {
getCurrentContext(kubeConfig)?.context?.cluster
} else {
null
}
KubeConfig.loadKubeConfig(path.toFile().bufferedReader())
} catch (e: Throwable) {
logger.warn(
"Error parsing kubeconfig file '$path' while determining current context: ${e.message}",
Expand All @@ -194,7 +188,17 @@ object KubeConfigUtils {
null
}
}
}

val currentContextName = allKubeConfigs
.firstOrNull { it.currentContext?.isNotEmpty() == true }
?.currentContext
?: return null

allKubeConfigs
.flatMap { it.contexts ?: emptyList() }
.mapNotNull { KubeConfigNamedContext.fromMap(it as? Map<*, *>) }
.firstOrNull { it.name == currentContextName }
?.context?.cluster
} catch (e: Exception) {
logger.warn("Failed to get current context cluster name from kubeconfig: ${e.message}", e)
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,110 @@ class KubeConfigUtilsTest {
assertThat(clusterName).isEqualTo("bespin-cluster")
}

@Test
fun `#getCurrentClusterName returns cluster name from first config file with current context when multiple files exist`() {
// given
val kubeConfigFile1 = createTempKubeConfigFile(
"config1", """
apiVersion: v1
clusters:
- name: tatooine-cluster
cluster:
server: https://api.tatooine.starwars.com:6443
users:
- name: luke-skywalker
user:
token: jedi-token
contexts:
- context:
cluster: tatooine-cluster
user: luke-skywalker
name: tatooine-context
current-context: tatooine-context
""".trimIndent()
)
val kubeConfigFile2 = createTempKubeConfigFile(
"config2", """
apiVersion: v1
clusters:
- name: dagobah-cluster
cluster:
server: https://api.dagobah.starwars.com:6443
users:
- name: yoda-master
user:
token: force-token
contexts:
- context:
cluster: dagobah-cluster
user: yoda-master
name: dagobah-context
current-context: dagobah-context
""".trimIndent()
)
val kubeConfigFile3 = createTempKubeConfigFile(
"config3", """
apiVersion: v1
clusters:
- name: hoth-cluster
cluster:
server: https://api.hoth.starwars.com:6443
users:
- name: han-solo
user:
token: smuggler-token
contexts:
- context:
cluster: hoth-cluster
user: han-solo
name: hoth-context
""".trimIndent()
)
val kubeconfigEnv = "${kubeConfigFile1}${File.pathSeparator}${kubeConfigFile2}${File.pathSeparator}${kubeConfigFile3}"

// when
val clusterName = KubeConfigUtils.getCurrentClusterName(kubeconfigEnv)

// then
assertThat(clusterName).isEqualTo("tatooine-cluster")
}

@Test
fun `#getCurrentClusterName returns cluster name when current-context is in one file and context definition is in another`() {
// given
val kubeConfigFile1 = createTempKubeConfigFile(
"config1", """
apiVersion: v1
current-context: dagobah-context
""".trimIndent()
)
val kubeConfigFile2 = createTempKubeConfigFile(
"config2", """
apiVersion: v1
clusters:
- name: dagobah-cluster
cluster:
server: https://api.dagobah.starwars.com:6443
users:
- name: yoda-master
user:
token: force-token
contexts:
- context:
cluster: dagobah-cluster
user: yoda-master
name: dagobah-context
""".trimIndent()
)
val kubeconfigEnv = "${kubeConfigFile1}${File.pathSeparator}${kubeConfigFile2}"

// when
val clusterName = KubeConfigUtils.getCurrentClusterName(kubeconfigEnv)

// then
assertThat(clusterName).isEqualTo("dagobah-cluster")
}

@Test
fun `#getAllConfigs returns configs with path set`() {
// given
Expand Down
Loading