Skip to content

Commit

Permalink
fix: properly work if listing ns is denied (redhat-developer#717)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Mar 18, 2024
1 parent eff7074 commit 684f91d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ open class AllContexts(
override fun setCurrentNamespace(namespace: String): IActiveContext<out HasMetadata, out KubernetesClient>? {
val old = this.current ?: return null
val newClient = clientFactory.invoke(namespace, old.context.name)
throwIfNotAccessible(namespace, newClient.get())
val new = setCurrentContext(newClient, old.getWatched())
if (new != null) {
modelChange.fireCurrentNamespaceChanged(new, old)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,16 @@ abstract class ActiveContext<N : HasMetadata, C : KubernetesClient>(
return if (!current.isNullOrEmpty()) {
current
} else {
val allNamespaces = getAllResources(namespaceKind, NO_NAMESPACE)
val namespace = allNamespaces.find { namespace:HasMetadata -> DEFAULT_NAMESPACE == namespace.metadata.name } ?: allNamespaces.firstOrNull()
return namespace?.metadata?.name
return try {
val allNamespaces = getAllResources(namespaceKind, NO_NAMESPACE)
val namespace =
allNamespaces.find { namespace: HasMetadata -> DEFAULT_NAMESPACE == namespace.metadata.name }
?: allNamespaces.firstOrNull()
namespace?.metadata?.name
} catch (e: ResourceException) {
logger<ActiveContext<*,*>>().warn("Could not list all namespaces to use 1st as current namespace.", e)
null
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,6 @@ class AllContextsTest {
assertThat(contextName.allValues[1]).isEqualTo(activeContext.context.name)
}

@Test(expected = ResourceException::class)
fun `#setCurrentNamespace(namespace) should throw if current namespace is forbidden`() {
// given
val client = client(KubernetesClientException("a disturbance in the force")) // throws upon client#namespaces
val clientConfig = clientConfig(currentContext, contexts, configuration)
val clientAdapter = clientAdapter(clientConfig, client) // no config so there are no contexts
val clientFactory = clientFactory(clientAdapter)
val allContexts = TestableAllContexts(modelChange, contextFactory, clientFactory)
// when
allContexts.setCurrentNamespace("dark side")
// then
verify(modelChange, never()).fireCurrentNamespaceChanged(anyOrNull(), anyOrNull())
}

@Test
fun `#onKubeConfigChanged() should NOT fire if new config is null`() {
// given
Expand Down

0 comments on commit 684f91d

Please sign in to comment.