Skip to content

Commit 8a16f6e

Browse files
committed
fix: allow updating kube config file without contexts, clusters, users (#23714)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 08a72b4 commit 8a16f6e

File tree

5 files changed

+109
-8
lines changed

5 files changed

+109
-8
lines changed

src/main/kotlin/com/redhat/devtools/gateway/kubeconfig/BlockStyleFilePersister.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package com.redhat.devtools.gateway.kubeconfig
1414
import com.fasterxml.jackson.databind.ObjectMapper
1515
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
1616
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
17+
import com.redhat.devtools.gateway.openshift.mapOfNotNull
1718
import io.kubernetes.client.persister.ConfigPersister
1819
import org.yaml.snakeyaml.DumperOptions
1920
import java.io.File
@@ -22,13 +23,13 @@ class BlockStyleFilePersister(private val file: File) : ConfigPersister {
2223

2324
@Throws(java.io.IOException::class)
2425
override fun save(
25-
contexts: ArrayList<Any?>,
26-
clusters: ArrayList<Any?>,
27-
users: ArrayList<Any?>,
26+
contexts: ArrayList<Any?>?,
27+
clusters: ArrayList<Any?>?,
28+
users: ArrayList<Any?>?,
2829
preferences: Any?,
2930
currentContext: String?
3031
) {
31-
val config = mapOf(
32+
val config = mapOfNotNull(
3233
"apiVersion" to "v1",
3334
"kind" to "Config",
3435
"current-context" to currentContext,

src/main/kotlin/com/redhat/devtools/gateway/kubeconfig/KubeConfigUpdate.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ abstract class KubeConfigUpdate private constructor(
4040
abstract fun apply()
4141

4242
protected fun save(
43-
contexts: ArrayList<Any?>,
44-
clusters: ArrayList<Any?>,
45-
users: ArrayList<Any?>,
43+
contexts: ArrayList<Any?>?,
44+
clusters: ArrayList<Any?>?,
45+
users: ArrayList<Any?>?,
4646
preferences: Any,
4747
currentContext: String?,
4848
path: Path?

src/main/kotlin/com/redhat/devtools/gateway/openshift/Utils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ object Utils {
5050

5151
}
5252

53+
fun <K, V> mapOfNotNull(vararg pairs: Pair<K, V?>): Map<K, V> {
54+
return pairs.mapNotNull { (key, value) ->
55+
value?.let { key to it }
56+
}.toMap()
57+
}
58+

src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesServerStepView.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package com.redhat.devtools.gateway.view.steps
1313

1414
import com.intellij.openapi.application.invokeLater
1515
import com.intellij.openapi.components.service
16+
import com.intellij.openapi.diagnostic.thisLogger
1617
import com.intellij.openapi.progress.ProgressIndicator
1718
import com.intellij.openapi.progress.ProgressManager
1819
import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeScreenUIManager
@@ -231,7 +232,8 @@ class DevSpacesServerStepView(
231232
token.trim())
232233
.apply()
233234
} catch (e: Exception) {
234-
Dialogs.error( e.message ?: "Could not update kube config file", "Kubeconfig Update Failed")
235+
thisLogger().warn(e.message ?: "Could not save configuration file", e)
236+
Dialogs.error( e.message ?: "Could not save configuration file", "Save Config Failed")
235237
}
236238
}
237239

src/test/kotlin/com/redhat/devtools/gateway/openshift/UtilsTest.kt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,96 @@ class UtilsTest {
124124
// then
125125
assertThat(value).isNull()
126126
}
127+
128+
@Test
129+
fun `#mapOfNotNull returns map with all non-null values`() {
130+
// when
131+
val result = mapOfNotNull(
132+
"key1" to "value1",
133+
"key2" to "value2",
134+
"key3" to "value3"
135+
)
136+
137+
// then
138+
assertThat(result).hasSize(3)
139+
assertThat(result).containsEntry("key1", "value1")
140+
assertThat(result).containsEntry("key2", "value2")
141+
assertThat(result).containsEntry("key3", "value3")
142+
}
143+
144+
@Test
145+
fun `#mapOfNotNull filters out null values`() {
146+
// when
147+
val result = mapOfNotNull(
148+
"key1" to "value1",
149+
"key2" to null,
150+
"key3" to "value3",
151+
"key4" to null
152+
)
153+
154+
// then
155+
assertThat(result).hasSize(2)
156+
assertThat(result).containsEntry("key1", "value1")
157+
assertThat(result).containsEntry("key3", "value3")
158+
assertThat(result).doesNotContainKey("key2")
159+
assertThat(result).doesNotContainKey("key4")
160+
}
161+
162+
@Test
163+
fun `#mapOfNotNull returns empty map when all values are null`() {
164+
// when
165+
val result = mapOfNotNull(
166+
"key1" to null,
167+
"key2" to null,
168+
"key3" to null
169+
)
170+
171+
// then
172+
assertThat(result).isEmpty()
173+
}
174+
175+
@Test
176+
fun `#mapOfNotNull returns empty map when no pairs provided`() {
177+
// when
178+
val result = mapOfNotNull<String, String>()
179+
180+
// then
181+
assertThat(result).isEmpty()
182+
}
183+
184+
@Test
185+
fun `#mapOfNotNull works with different value types`() {
186+
// when
187+
val stringResult = mapOfNotNull("key1" to "value", "key2" to null)
188+
val intResult = mapOfNotNull("key1" to 42, "key2" to null, "key3" to 100)
189+
val booleanResult = mapOfNotNull("key1" to true, "key2" to null, "key3" to false)
190+
191+
// then
192+
assertThat(stringResult).hasSize(1)
193+
assertThat(stringResult).containsEntry("key1", "value")
194+
195+
assertThat(intResult).hasSize(2)
196+
assertThat(intResult).containsEntry("key1", 42)
197+
assertThat(intResult).containsEntry("key3", 100)
198+
199+
assertThat(booleanResult).hasSize(2)
200+
assertThat(booleanResult).containsEntry("key1", true)
201+
assertThat(booleanResult).containsEntry("key3", false)
202+
}
203+
204+
@Test
205+
fun `#mapOfNotNull preserves order of non-null entries`() {
206+
// when
207+
val result = mapOfNotNull(
208+
"key1" to "value1",
209+
"key2" to null,
210+
"key3" to "value3"
211+
)
212+
213+
// then
214+
val entries = result.entries.toList()
215+
assertThat(entries).hasSize(3)
216+
assertThat(entries[0].key).isEqualTo("key1")
217+
assertThat(entries[1].key).isEqualTo("key3")
218+
}
127219
}

0 commit comments

Comments
 (0)