Skip to content

Commit

Permalink
fix: display kubeconfig parse error on startup (#809)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jan 14, 2025
1 parent 52ad428 commit e31cf56
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class SetAsCurrentClusterAction: StructureTreeAction(IContext::class.java) {
val context: IContext = selectedNode?.getElement() ?: return
val telemetry = TelemetryService.instance
.action(NAME_PREFIX_CONTEXT + "switch")
run("Setting ${context.context.name} as current cluster...", true,
run("Setting ${context.name} as current cluster...", true,
Progressive {
try {
getResourceModel()?.setCurrentContext(context)
telemetry.success().send()
} catch (e: Exception) {
logger<SetAsCurrentClusterAction>().warn(
"Could not set current context to ${context.context.name}.", e
"Could not set current context to ${context.name}.", e
)
telemetry.error(e).send()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.redhat.devtools.intellij.kubernetes.model.context.IActiveContext
import com.redhat.devtools.intellij.kubernetes.model.context.IContext
import com.redhat.devtools.intellij.kubernetes.model.resource.ResourceKind
import com.redhat.devtools.intellij.kubernetes.model.util.ResettableLazyProperty
import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.NAME_PREFIX_CONTEXT
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_IS_OPENSHIFT
Expand Down Expand Up @@ -113,7 +115,7 @@ open class AllContexts(
val all = createContexts(client.get(), client.get()?.config)
_all.addAll(all)
} catch (e: Exception) {
//
throw ResourceException("Could not parse kube config: ${toMessage(e)}", e)
}
}
return _all
Expand All @@ -124,7 +126,7 @@ open class AllContexts(
if (current == context) {
return current
}
val newClient = clientFactory.invoke(context.context.context.namespace, context.context.name)
val newClient = clientFactory.invoke(context.namespace, context.name)
val new = setCurrentContext(newClient, emptyList())
if (new != null) {
modelChange.fireAllContextsChanged()
Expand All @@ -134,7 +136,7 @@ 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)
val newClient = clientFactory.invoke(namespace, old.name)
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 @@ -487,7 +487,7 @@ abstract class ActiveContext<N : HasMetadata, C : KubernetesClient>(
}

override fun close() {
logger<ActiveContext<*, *>>().debug("Closing context ${context.name}.")
logger<ActiveContext<*, *>>().debug("Closing context $name.")
watch.close()
dashboard.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ package com.redhat.devtools.intellij.kubernetes.model.context
import io.fabric8.kubernetes.api.model.NamedContext

interface IContext {
val context: NamedContext
val active: Boolean
val name: String?
val namespace: String?
}

open class Context(override val context: NamedContext): IContext {
open class Context(private val context: NamedContext): IContext {
override val active: Boolean = false
}
override val name: String?
get() = context.name
override val namespace: String?
get() = context.context?.namespace
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ interface IActiveContext<N: HasMetadata, C: KubernetesClient>: IContext {
}
}

val name: String?
get() {
return context.name
}
/**
* The master url for this context. This is the url of the cluster for this context.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ import javax.swing.Icon

object KubernetesDescriptors {

fun createDescriptor(element: Any, childrenKind: ResourceKind<out HasMetadata>?, parent: NodeDescriptor<*>?, model: IResourceModel, project: Project): NodeDescriptor<*>? {
fun createDescriptor(
element: Any,
childrenKind: ResourceKind<out HasMetadata>?,
parent: NodeDescriptor<*>?,
model: IResourceModel,
project: Project
): NodeDescriptor<*>? {
return when {
element is DescriptorFactory<*> ->
element.create(parent, model, project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ open class TreeStructure(
private fun getChildElements(element: Any, contribution: ITreeStructureContribution): Collection<Any> {
return try {
contribution.getChildElements(element)
} catch (e: java.lang.Exception) {
} catch (e: Exception) {
logger<TreeStructure>().warn(e)
listOf(e)
}
Expand Down Expand Up @@ -101,24 +101,31 @@ open class TreeStructure(
}

override fun createDescriptor(element: Any, parent: NodeDescriptor<*>?): NodeDescriptor<*> {
val descriptor: NodeDescriptor<*>? =
return try {
val descriptor: NodeDescriptor<*>? =
getValidContributions()
.map { it.createDescriptor(element, parent, project) }
.find { it != null }
if (descriptor != null) {
return descriptor
}
return when (element) {
is IContext -> ContextDescriptor(element, parent, model, project)
is Exception -> ErrorDescriptor(element, parent, model, project)
is Folder -> FolderDescriptor(element, parent, model, project)
else -> Descriptor(element, null, parent, model, project)
.map { it.createDescriptor(element, parent, project) }
.find { it != null }
descriptor ?: when (element) {
is IContext -> ContextDescriptor(element, parent, model, project)
is Exception -> ErrorDescriptor(element, parent, model, project)
is Folder -> FolderDescriptor(element, parent, model, project)
else -> Descriptor(element, null, parent, model, project)
}
} catch (e: Exception) {
ErrorDescriptor(e, parent, model, project)
}
}

private fun getValidContributions(): Collection<ITreeStructureContribution> {
return contributions
.filter { it.canContribute() }
.filter {
try {
it.canContribute()
} catch (e: Exception) {
false
}
}
}

private fun getTreeStructureExtensions(model: IResourceModel): List<ITreeStructureContribution> {
Expand Down Expand Up @@ -167,11 +174,7 @@ open class TreeStructure(
project
) {
override fun getLabel(element: C?): String {
return if (element?.context?.name == null) {
"<unknown context>"
} else {
element.context.name
}
return element?.name ?: "<unknown context>"
}

override fun getIcon(element: C): Icon? {
Expand Down Expand Up @@ -237,26 +240,22 @@ open class TreeStructure(
}

private class ErrorDescriptor(
exception: java.lang.Exception,
exception: Exception,
parent: NodeDescriptor<*>?,
model: IResourceModel,
project: Project
) : Descriptor<java.lang.Exception>(
) : Descriptor<Exception>(
exception,
null,
parent,
model,
project
) {
override fun getLabel(element: java.lang.Exception?): String {
return getMessage(element)
}

private fun getMessage(e: Exception?): String {
return toMessage(e)
override fun getLabel(element: Exception?): String {
return toMessage(element)
}

override fun getIcon(element: java.lang.Exception): Icon {
override fun getIcon(element: Exception): Icon {
return AllIcons.General.BalloonError
}
}
Expand Down

0 comments on commit e31cf56

Please sign in to comment.