Skip to content

Commit e241c49

Browse files
committed
fix: properly work if listing ns is denied (#717)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent f06f547 commit e241c49

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/AllContexts.kt

-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ open class AllContexts(
126126
override fun setCurrentNamespace(namespace: String): IActiveContext<out HasMetadata, out KubernetesClient>? {
127127
val old = this.current ?: return null
128128
val newClient = clientFactory.invoke(namespace, old.context.name)
129-
throwIfNotAccessible(namespace, newClient.get())
130129
val new = setCurrentContext(newClient, old.getWatched())
131130
if (new != null) {
132131
modelChange.fireCurrentNamespaceChanged(new, old)

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/context/ActiveContext.kt

+10-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,16 @@ abstract class ActiveContext<N : HasMetadata, C : KubernetesClient>(
119119
return if (!current.isNullOrEmpty()) {
120120
current
121121
} else {
122-
val allNamespaces = getAllResources(namespaceKind, NO_NAMESPACE)
123-
val namespace = allNamespaces.find { namespace:HasMetadata -> DEFAULT_NAMESPACE == namespace.metadata.name } ?: allNamespaces.firstOrNull()
124-
return namespace?.metadata?.name
122+
return try {
123+
val allNamespaces = getAllResources(namespaceKind, NO_NAMESPACE)
124+
val namespace =
125+
allNamespaces.find { namespace: HasMetadata -> DEFAULT_NAMESPACE == namespace.metadata.name }
126+
?: allNamespaces.firstOrNull()
127+
namespace?.metadata?.name
128+
} catch (e: ResourceException) {
129+
logger<ActiveContext<*,*>>().warn("Could not list all namespaces to use 1st as current namespace.", e)
130+
null
131+
}
125132
}
126133
}
127134

src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/AllContextsTest.kt

-14
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,6 @@ class AllContextsTest {
384384
assertThat(contextName.allValues[1]).isEqualTo(activeContext.context.name)
385385
}
386386

387-
@Test(expected = ResourceException::class)
388-
fun `#setCurrentNamespace(namespace) should throw if current namespace is forbidden`() {
389-
// given
390-
val client = client(KubernetesClientException("a disturbance in the force")) // throws upon client#namespaces
391-
val clientConfig = clientConfig(currentContext, contexts, configuration)
392-
val clientAdapter = clientAdapter(clientConfig, client) // no config so there are no contexts
393-
val clientFactory = clientFactory(clientAdapter)
394-
val allContexts = TestableAllContexts(modelChange, contextFactory, clientFactory)
395-
// when
396-
allContexts.setCurrentNamespace("dark side")
397-
// then
398-
verify(modelChange, never()).fireCurrentNamespaceChanged(anyOrNull(), anyOrNull())
399-
}
400-
401387
@Test
402388
fun `#onKubeConfigChanged() should NOT fire if new config is null`() {
403389
// given

0 commit comments

Comments
 (0)