|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Red Hat, Inc. - initial API and implementation |
| 11 | + */ |
| 12 | +package com.redhat.devtools.gateway.kubeconfig |
| 13 | + |
| 14 | +import io.kubernetes.client.util.KubeConfig |
| 15 | +import java.io.File |
| 16 | +import java.io.FileReader |
| 17 | +import kotlinx.coroutines.Dispatchers |
| 18 | +import kotlinx.coroutines.withContext |
| 19 | +import org.yaml.snakeyaml.Yaml |
| 20 | +import java.io.FileWriter |
| 21 | + |
| 22 | +class KubeConfigWriter { |
| 23 | + companion object { |
| 24 | + /** |
| 25 | + * Finds the path to the kubeconfig file that contains a specific user. |
| 26 | + * |
| 27 | + * @param userName The name of the user to find. |
| 28 | + * @param kubeConfigEnv The value of the KUBECONFIG environment variable. |
| 29 | + * @return The absolute path to the kubeconfig file, or null if the user is not found. |
| 30 | + */ |
| 31 | + fun findKubeConfigFileForUser(userName: String, kubeConfigEnv: String?): String? { |
| 32 | + val files = if (kubeConfigEnv.isNullOrEmpty()) { |
| 33 | + listOf(File(System.getProperty("user.home"), ".kube/config")) |
| 34 | + } else { |
| 35 | + kubeConfigEnv.split(File.pathSeparator).map { File(it) } |
| 36 | + } |
| 37 | + for (file in files) { |
| 38 | + if (!file.exists() || !file.isFile) { |
| 39 | + continue |
| 40 | + } |
| 41 | + try { |
| 42 | + val kubeConfig = KubeConfig.loadKubeConfig(FileReader(file)) |
| 43 | + val users = kubeConfig.users as? List<*> |
| 44 | + val userFound = users?.any { userObject -> |
| 45 | + val userMap = userObject as? Map<*, *> |
| 46 | + userMap?.get("name") as? String == userName |
| 47 | + } |
| 48 | + if (userFound == true) { |
| 49 | + return file.absolutePath |
| 50 | + } |
| 51 | + } catch (e: Exception) { |
| 52 | + // Log the exception, e.g., failed to parse |
| 53 | + } |
| 54 | + } |
| 55 | + return null |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Applies changes to a kubeconfig file and saves it. |
| 60 | + * |
| 61 | + * @param filePath The path to the kubeconfig file to modify. |
| 62 | + * @param serverUrl The new server URL. |
| 63 | + * @param token The new token. |
| 64 | + */ |
| 65 | + suspend fun applyChangesAndSave(filePath: String, serverUrl: String, token: String) = withContext(Dispatchers.IO) { |
| 66 | + val yaml = Yaml() |
| 67 | + val file = File(filePath) |
| 68 | + val kubeConfigMap = yaml.load(file.inputStream()) as? MutableMap<String, Any> ?: mutableMapOf() |
| 69 | + |
| 70 | + val clusters = kubeConfigMap["clusters"] as? MutableList<MutableMap<String, Any>> ?: mutableListOf() |
| 71 | + val existingCluster = clusters.find { (it["cluster"] as? Map<*, *>)?.get("server") == serverUrl } |
| 72 | + |
| 73 | + if (existingCluster != null) { |
| 74 | + // Update existing |
| 75 | + val clusterName = existingCluster["name"] as? String |
| 76 | + val contexts = kubeConfigMap["contexts"] as? List<Map<String, Any>> ?: emptyList() |
| 77 | + val currentContext = contexts.find { it["name"] == kubeConfigMap["current-context"] } |
| 78 | + val userName = (currentContext?.get("context") as? Map<*, *>)?.get("user") as? String |
| 79 | + |
| 80 | + if (userName != null) { |
| 81 | + val users = kubeConfigMap["users"] as? MutableList<MutableMap<String, Any>> ?: mutableListOf() |
| 82 | + val userToUpdate = users.find { it["name"] == userName } |
| 83 | + (userToUpdate?.get("user") as? MutableMap<String, Any>)?.set("token", token) |
| 84 | + } |
| 85 | + } else { |
| 86 | + // Create new |
| 87 | + val newName = serverUrl.substringAfter("://").replace(".", "-").replace(":", "-") |
| 88 | + val newClusterName = newName |
| 89 | + val newUserName = "$newName-user" |
| 90 | + val newContextName = "$newName-context" |
| 91 | + |
| 92 | + val newCluster = mutableMapOf<String, Any>( |
| 93 | + "name" to newClusterName, |
| 94 | + "cluster" to mutableMapOf("server" to serverUrl) |
| 95 | + ) |
| 96 | + clusters.add(newCluster) |
| 97 | + |
| 98 | + val newUser = mutableMapOf<String, Any>( |
| 99 | + "name" to newUserName, |
| 100 | + "user" to mutableMapOf("token" to token) |
| 101 | + ) |
| 102 | + val users = kubeConfigMap["users"] as? MutableList<MutableMap<String, Any>> ?: mutableListOf() |
| 103 | + users.add(newUser) |
| 104 | + |
| 105 | + val newContext = mutableMapOf<String, Any>( |
| 106 | + "name" to newContextName, |
| 107 | + "context" to mutableMapOf( |
| 108 | + "cluster" to newClusterName, |
| 109 | + "user" to newUserName |
| 110 | + ) |
| 111 | + ) |
| 112 | + val contexts = kubeConfigMap["contexts"] as? MutableList<MutableMap<String, Any>> ?: mutableListOf() |
| 113 | + contexts.add(newContext) |
| 114 | + |
| 115 | + kubeConfigMap["clusters"] = clusters |
| 116 | + kubeConfigMap["users"] = users |
| 117 | + kubeConfigMap["contexts"] = contexts |
| 118 | + kubeConfigMap["current-context"] = newContextName |
| 119 | + } |
| 120 | + |
| 121 | + val writer = FileWriter(file) |
| 122 | + yaml.dump(kubeConfigMap, writer) |
| 123 | + writer.close() |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments