@@ -12,7 +12,6 @@ package com.redhat.devtools.intellij.kubernetes.model
12
12
13
13
import com.intellij.openapi.application.ApplicationManager
14
14
import com.intellij.openapi.diagnostic.logger
15
- import com.redhat.devtools.intellij.common.utils.ConfigHelper
16
15
import com.redhat.devtools.intellij.common.utils.ConfigWatcher
17
16
import com.redhat.devtools.intellij.common.utils.ExecHelper
18
17
import com.redhat.devtools.intellij.kubernetes.model.client.ClientAdapter
@@ -30,7 +29,6 @@ import com.redhat.devtools.intellij.kubernetes.telemetry.TelemetryService.PROP_O
30
29
import io.fabric8.kubernetes.api.model.HasMetadata
31
30
import io.fabric8.kubernetes.client.Config
32
31
import io.fabric8.kubernetes.client.KubernetesClient
33
- import java.nio.file.Paths
34
32
import java.util.concurrent.CompletionException
35
33
import java.util.concurrent.locks.ReentrantReadWriteLock
36
34
import kotlin.concurrent.read
@@ -83,11 +81,13 @@ open class AllContexts(
83
81
namespace: String? ,
84
82
context: String?
85
83
) -> ClientAdapter <out KubernetesClient >
86
- = { namespace, context -> ClientAdapter .Factory .create(namespace, context) }
84
+ = { namespace, config ->
85
+ ClientAdapter .Factory .create(namespace, config)
86
+ }
87
87
) : IAllContexts {
88
88
89
89
init {
90
- watchKubeConfig ()
90
+ watchKubeConfigs ()
91
91
}
92
92
93
93
private val lock = ReentrantReadWriteLock ()
@@ -148,7 +148,8 @@ open class AllContexts(
148
148
) : IActiveContext <out HasMetadata , out KubernetesClient >? {
149
149
lock.write {
150
150
try {
151
- replaceClient(newClient, this .client.get())
151
+ client.get()?.close()
152
+ client.set(newClient)
152
153
newClient.config.save().join()
153
154
current?.close()
154
155
clearAllContexts() // causes reload of all contexts when accessed afterwards
@@ -204,13 +205,6 @@ open class AllContexts(
204
205
}
205
206
}
206
207
207
- private fun replaceClient (new : ClientAdapter <out KubernetesClient >, old : ClientAdapter <out KubernetesClient >? )
208
- : ClientAdapter <out KubernetesClient > {
209
- old?.close()
210
- this .client.set(new)
211
- return new
212
- }
213
-
214
208
private fun createActiveContext (client : ClientAdapter <out KubernetesClient >? )
215
209
: IActiveContext <out HasMetadata , out KubernetesClient >? {
216
210
if (client == null ) {
@@ -241,8 +235,7 @@ open class AllContexts(
241
235
}
242
236
}
243
237
244
- protected open fun watchKubeConfig () {
245
- val path = Paths .get(Config .getKubeconfigFilename())
238
+ protected open fun watchKubeConfigs () {
246
239
/* *
247
240
* [ConfigWatcher] cannot add/remove listeners nor can it get closed (and stop the [java.nio.file.WatchService]).
248
241
* We therefore have to create a single instance in here rather than using it in a shielded/private way within
@@ -251,16 +244,16 @@ open class AllContexts(
251
244
* The latter gets closed/recreated whenever the context changes in
252
245
* [com.redhat.devtools.intellij.kubernetes.model.client.KubeConfigAdapter].
253
246
*/
254
- val watcher = ConfigWatcher (path) { _, config: io.fabric8.kubernetes.api.model. Config ? -> onKubeConfigChanged(config) }
247
+ val watcher = ConfigWatcher { config: Config ? -> onKubeConfigChanged(config) }
255
248
runAsync(watcher::run)
256
249
}
257
250
258
- protected open fun onKubeConfigChanged (fileConfig : io.fabric8.kubernetes.api.model. Config ? ) {
251
+ protected open fun onKubeConfigChanged (updated : Config ? ) {
259
252
lock.read {
260
- fileConfig ? : return
253
+ updated ? : return
261
254
val client = client.get() ? : return
262
- val clientConfig = client.config.configuration
263
- if (ConfigHelper .areEqual(fileConfig, clientConfig )) {
255
+ val existing = client.config
256
+ if (existing.isEqualConfig(updated )) {
264
257
return
265
258
}
266
259
this .client.reset() // create new client when accessed
0 commit comments