Skip to content

Commit b864d7b

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

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

Diff for: 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)

Diff for: 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

Diff for: src/main/kotlin/com/redhat/devtools/intellij/kubernetes/tree/KubernetesDescriptors.kt

+3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
package com.redhat.devtools.intellij.kubernetes.tree
1212

1313
import com.intellij.ide.util.treeView.NodeDescriptor
14+
import com.intellij.openapi.diagnostic.logger
1415
import com.intellij.openapi.project.Project
1516
import com.intellij.openapi.util.IconLoader
1617
import com.redhat.devtools.intellij.kubernetes.model.IResourceModel
1718
import com.redhat.devtools.intellij.kubernetes.model.context.KubernetesContext
1819
import com.redhat.devtools.intellij.kubernetes.model.resource.ResourceKind
1920
import com.redhat.devtools.intellij.kubernetes.model.util.getHighestPriorityVersion
21+
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
22+
import com.redhat.devtools.intellij.kubernetes.model.util.trimWithEllipsis
2023
import com.redhat.devtools.intellij.kubernetes.tree.AbstractTreeStructureContribution.DescriptorFactory
2124
import com.redhat.devtools.intellij.kubernetes.tree.KubernetesStructure.NamespacesFolder
2225
import com.redhat.devtools.intellij.kubernetes.tree.TreeStructure.ContextDescriptor

Diff for: 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)