Skip to content

Commit 752cc1e

Browse files
committed
watch all config files (redhat-developer#779)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 0526f10 commit 752cc1e

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jetBrainsToken=invalid
77
jetBrainsChannel=stable
88
intellijPluginVersion=1.16.1
99
kotlinJvmPluginVersion=1.8.0
10-
intellijCommonVersion=1.9.6-SNAPSHOT
10+
intellijCommonVersion=1.9.7-SNAPSHOT
1111
telemetryPluginVersion=1.1.0.52
1212
kotlin.stdlib.default.dependency = false
1313
kotlinVersion=1.6.21

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

+16-13
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.NAME_P
2626
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_IS_OPENSHIFT
2727
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_KUBERNETES_VERSION
2828
import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_OPENSHIFT_VERSION
29+
import io.fabric8.kubernetes.api.model.Config
2930
import io.fabric8.kubernetes.api.model.HasMetadata
30-
import io.fabric8.kubernetes.client.Config
3131
import io.fabric8.kubernetes.client.KubernetesClient
32-
import java.nio.file.Paths
3332
import java.util.concurrent.CompletionException
3433
import java.util.concurrent.locks.ReentrantReadWriteLock
3534
import kotlin.concurrent.read
@@ -85,16 +84,14 @@ open class AllContexts(
8584
= { namespace, context -> ClientAdapter.Factory.create(namespace, context) }
8685
) : IAllContexts {
8786

88-
init {
89-
watchKubeConfig()
90-
}
91-
9287
private val lock = ReentrantReadWriteLock()
9388

9489
private val client = ResettableLazyProperty {
95-
lock.write {
90+
val client = lock.write {
9691
clientFactory.invoke(null, null)
9792
}
93+
watchKubeConfig(client)
94+
client
9895
}
9996

10097
override val current: IActiveContext<out HasMetadata, out KubernetesClient>?
@@ -109,10 +106,11 @@ open class AllContexts(
109106
lock.write {
110107
if (_all.isEmpty()) {
111108
try {
112-
val all = createContexts(client.get(), client.get()?.config)
109+
val client = client.get()
110+
val all = createContexts(client, client?.config)
113111
_all.addAll(all)
114112
} catch (e: Exception) {
115-
//
113+
logger<AllContexts>().warn("Could not load all contexts.", e)
116114
}
117115
}
118116
return _all
@@ -195,8 +193,10 @@ open class AllContexts(
195193
return config.allContexts
196194
.map {
197195
if (config.isCurrent(it)) {
196+
logger<AllContexts>().debug("Adding active context ${it.name}")
198197
createActiveContext(client) ?: Context(it)
199198
} else {
199+
logger<AllContexts>().debug("Adding inactive context ${it.name}")
200200
Context(it)
201201
}
202202
}
@@ -240,8 +240,9 @@ open class AllContexts(
240240
}
241241
}
242242

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

257-
protected open fun onKubeConfigChanged(fileConfig: io.fabric8.kubernetes.api.model.Config?) {
260+
protected open fun onKubeConfigChanged(fileConfig: Config?) {
258261
lock.read {
259262
fileConfig ?: return
260263
val client = client.get() ?: return

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

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ open class ClientConfig(
4545
client.configuration
4646
}
4747

48+
val files: List<File>
49+
get() {
50+
return configuration.files
51+
}
52+
4853
fun save(): CompletableFuture<Boolean> {
4954
return CompletableFuture.supplyAsync(
5055
{

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

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
******************************************************************************/
1111
package com.redhat.devtools.intellij.kubernetes.model.context
1212

13+
import com.intellij.openapi.diagnostic.logger
1314
import com.redhat.devtools.intellij.common.kubernetes.ClusterInfo
1415
import com.redhat.devtools.intellij.kubernetes.model.IResourceModelObservable
1516
import com.redhat.devtools.intellij.kubernetes.model.client.ClientAdapter
@@ -34,12 +35,14 @@ interface IActiveContext<N: HasMetadata, C: KubernetesClient>: IContext {
3435
): IActiveContext<out HasMetadata, out KubernetesClient>? {
3536
val currentContext = client.config.currentContext ?: return null
3637
return if (client.isOpenShift()) {
38+
logger<Factory>().warn("Current context ${currentContext.name} is OpenShift")
3739
OpenShiftContext(
3840
currentContext,
3941
observable,
4042
client as OSClientAdapter
4143
)
4244
} else {
45+
logger<Factory>().warn("Current context ${currentContext.name} is Kubernetes")
4346
KubernetesContext(
4447
currentContext,
4548
observable,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ class AllContextsTest {
464464
runnable.invoke() // run directly, not in IDEA pooled threads
465465
}
466466

467-
override fun watchKubeConfig() {
467+
override fun watchKubeConfig(client: ClientAdapter<out KubernetesClient>) {
468468
// don't watch filesystem (override super method)
469469
watchStarted = true
470470
}

0 commit comments

Comments
 (0)