Skip to content

Commit

Permalink
watch all config files (#779)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Sep 17, 2024
1 parent 0526f10 commit 752cc1e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jetBrainsToken=invalid
jetBrainsChannel=stable
intellijPluginVersion=1.16.1
kotlinJvmPluginVersion=1.8.0
intellijCommonVersion=1.9.6-SNAPSHOT
intellijCommonVersion=1.9.7-SNAPSHOT
telemetryPluginVersion=1.1.0.52
kotlin.stdlib.default.dependency = false
kotlinVersion=1.6.21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.NAME_P
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_IS_OPENSHIFT
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_KUBERNETES_VERSION
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_OPENSHIFT_VERSION
import io.fabric8.kubernetes.api.model.Config
import io.fabric8.kubernetes.api.model.HasMetadata
import io.fabric8.kubernetes.client.Config
import io.fabric8.kubernetes.client.KubernetesClient
import java.nio.file.Paths
import java.util.concurrent.CompletionException
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
Expand Down Expand Up @@ -85,16 +84,14 @@ open class AllContexts(
= { namespace, context -> ClientAdapter.Factory.create(namespace, context) }
) : IAllContexts {

init {
watchKubeConfig()
}

private val lock = ReentrantReadWriteLock()

private val client = ResettableLazyProperty {
lock.write {
val client = lock.write {
clientFactory.invoke(null, null)
}
watchKubeConfig(client)
client
}

override val current: IActiveContext<out HasMetadata, out KubernetesClient>?
Expand All @@ -109,10 +106,11 @@ open class AllContexts(
lock.write {
if (_all.isEmpty()) {
try {
val all = createContexts(client.get(), client.get()?.config)
val client = client.get()
val all = createContexts(client, client?.config)
_all.addAll(all)
} catch (e: Exception) {
//
logger<AllContexts>().warn("Could not load all contexts.", e)
}
}
return _all
Expand Down Expand Up @@ -195,8 +193,10 @@ open class AllContexts(
return config.allContexts
.map {
if (config.isCurrent(it)) {
logger<AllContexts>().debug("Adding active context ${it.name}")
createActiveContext(client) ?: Context(it)
} else {
logger<AllContexts>().debug("Adding inactive context ${it.name}")
Context(it)
}
}
Expand Down Expand Up @@ -240,8 +240,9 @@ open class AllContexts(
}
}

protected open fun watchKubeConfig() {
val filename = Config.getKubeconfigFilename() ?: return
protected open fun watchKubeConfig(client: ClientAdapter<out KubernetesClient>) {
val files = client.config.files
val paths = files.map { file -> file.toPath() }
/**
* [ConfigWatcher] cannot add/remove listeners nor can it get closed (and stop the [java.nio.file.WatchService]).
* We therefore have to create a single instance in here rather than using it in a shielded/private way within
Expand All @@ -250,11 +251,13 @@ open class AllContexts(
* The latter gets closed/recreated whenever the context changes in
* [com.redhat.devtools.intellij.kubernetes.model.client.KubeConfigPersistence].
*/
val watcher = ConfigWatcher(Paths.get(filename)) { _, config: io.fabric8.kubernetes.api.model.Config? -> onKubeConfigChanged(config) }
val watcher = ConfigWatcher(paths) { _, config: Config? ->
onKubeConfigChanged(config)
}
runAsync(watcher::run)
}

protected open fun onKubeConfigChanged(fileConfig: io.fabric8.kubernetes.api.model.Config?) {
protected open fun onKubeConfigChanged(fileConfig: Config?) {
lock.read {
fileConfig ?: return
val client = client.get() ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ open class ClientConfig(
client.configuration
}

val files: List<File>
get() {
return configuration.files
}

fun save(): CompletableFuture<Boolean> {
return CompletableFuture.supplyAsync(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.model.context

import com.intellij.openapi.diagnostic.logger
import com.redhat.devtools.intellij.common.kubernetes.ClusterInfo
import com.redhat.devtools.intellij.kubernetes.model.IResourceModelObservable
import com.redhat.devtools.intellij.kubernetes.model.client.ClientAdapter
Expand All @@ -34,12 +35,14 @@ interface IActiveContext<N: HasMetadata, C: KubernetesClient>: IContext {
): IActiveContext<out HasMetadata, out KubernetesClient>? {
val currentContext = client.config.currentContext ?: return null
return if (client.isOpenShift()) {
logger<Factory>().warn("Current context ${currentContext.name} is OpenShift")
OpenShiftContext(
currentContext,
observable,
client as OSClientAdapter
)
} else {
logger<Factory>().warn("Current context ${currentContext.name} is Kubernetes")
KubernetesContext(
currentContext,
observable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class AllContextsTest {
runnable.invoke() // run directly, not in IDEA pooled threads
}

override fun watchKubeConfig() {
override fun watchKubeConfig(client: ClientAdapter<out KubernetesClient>) {
// don't watch filesystem (override super method)
watchStarted = true
}
Expand Down

0 comments on commit 752cc1e

Please sign in to comment.