10
10
******************************************************************************/
11
11
package com.redhat.devtools.intellij.kubernetes.model.client
12
12
13
+ import com.intellij.openapi.diagnostic.logger
13
14
import com.redhat.devtools.intellij.common.utils.ConfigHelper
14
15
import com.redhat.devtools.intellij.kubernetes.CompletableFutureUtils.PLATFORM_EXECUTOR
15
- import io.fabric8.kubernetes.api.model.Context
16
16
import io.fabric8.kubernetes.api.model.NamedContext
17
17
import io.fabric8.kubernetes.client.Client
18
18
import io.fabric8.kubernetes.client.Config
19
19
import io.fabric8.kubernetes.client.internal.KubeConfigUtils
20
+ import java.io.File
20
21
import java.util.concurrent.CompletableFuture
21
22
import java.util.concurrent.Executor
22
23
23
24
/* *
24
25
* An adapter to access [io.fabric8.kubernetes.client.Config].
25
26
* It also saves the kube config [KubeConfigUtils] when it changes the client config.
26
27
*/
27
- open class ClientConfig (private val client : Client , private val executor : Executor = PLATFORM_EXECUTOR ) {
28
+ open class ClientConfig (
29
+ private val client : Client ,
30
+ private val executor : Executor = PLATFORM_EXECUTOR ,
31
+ private val persistence : (io.fabric8.kubernetes.api.model.Config ? , String? ) -> Unit = KubeConfigUtils : :persistKubeConfigIntoFile
32
+ ) {
28
33
29
- open var currentContext: NamedContext ?
34
+ open val currentContext: NamedContext ?
30
35
get() {
31
36
return configuration.currentContext
32
37
}
33
- set(context) {
34
- configuration.currentContext = context
35
- }
36
38
37
39
open val allContexts: List <NamedContext >
38
40
get() {
@@ -43,73 +45,57 @@ open class ClientConfig(private val client: Client, private val executor: Execut
43
45
client.configuration
44
46
}
45
47
46
- protected open val kubeConfig: KubeConfigAdapter by lazy {
47
- KubeConfigAdapter ()
48
- }
49
-
50
48
fun save (): CompletableFuture <Boolean > {
51
49
return CompletableFuture .supplyAsync(
52
50
{
53
- if (! kubeConfig.exists()) {
54
- return @supplyAsync false
51
+ val toSave = mutableMapOf<File , io.fabric8.kubernetes.api.model.Config >()
52
+ val withCurrentContext = configuration.fileWithCurrentContext
53
+ if (withCurrentContext != null
54
+ && setCurrentContext(withCurrentContext.config)
55
+ ) {
56
+ toSave[withCurrentContext.file] = withCurrentContext.config
55
57
}
56
- val fromFile = kubeConfig.load() ? : return @supplyAsync false
57
- if (setCurrentContext(
58
- currentContext,
59
- KubeConfigUtils .getCurrentContext(fromFile),
60
- fromFile
61
- ).or ( // no short-circuit
62
- setCurrentNamespace(
63
- currentContext?.context,
64
- KubeConfigUtils .getCurrentContext(fromFile)?.context
65
- )
66
- )
58
+ val withCurrentNamespace = configuration.getFileWithContext(currentContext?.name)
59
+ if (withCurrentNamespace != null
60
+ && setCurrentNamespace(withCurrentNamespace.config)
67
61
) {
68
- kubeConfig.save(fromFile)
69
- return @supplyAsync true
70
- } else {
71
- return @supplyAsync false
62
+ toSave[withCurrentNamespace.file] = withCurrentNamespace.config
63
+ }
64
+ toSave.forEach {
65
+ save(it.value, it.key)
72
66
}
67
+ toSave.isNotEmpty()
73
68
},
74
69
executor
75
70
)
76
71
}
77
72
78
- private fun setCurrentContext (
79
- currentContext : NamedContext ? ,
80
- kubeConfigCurrentContext : NamedContext ? ,
81
- kubeConfig : io.fabric8.kubernetes.api.model.Config
82
- ): Boolean {
83
- return if (currentContext != null
84
- && ! ConfigHelper .areEqual(currentContext, kubeConfigCurrentContext)
85
- ) {
86
- kubeConfig.currentContext = currentContext.name
73
+ private fun save (kubeConfig : io.fabric8.kubernetes.api.model.Config ? , file : File ? ) {
74
+ if (kubeConfig != null
75
+ && file?.absolutePath != null ) {
76
+ logger<ClientConfig >().debug(" Saving ${file.absolutePath} ." )
77
+ persistence.invoke(kubeConfig, file.absolutePath)
78
+ }
79
+ }
80
+
81
+ private fun setCurrentNamespace (kubeConfig : io.fabric8.kubernetes.api.model.Config ? ): Boolean {
82
+ val currentNamespace = currentContext?.context?.namespace ? : return false
83
+ val context = KubeConfigUtils .getContext(kubeConfig, currentContext?.name)
84
+ return if (context?.context != null
85
+ && context.context.namespace != currentNamespace) {
86
+ context.context.namespace = currentNamespace
87
87
true
88
88
} else {
89
89
false
90
90
}
91
91
}
92
92
93
- /* *
94
- * Sets the namespace in the given source [Context] to the given target [Context].
95
- * Does nothing if the target config has no current context
96
- * or if the source config has no current context
97
- * or if setting it would not change it.
98
- *
99
- * @param source Context whose namespace should be copied
100
- * @param target Context whose namespace should be overriden
101
- * @return
102
- */
103
- private fun setCurrentNamespace (
104
- source : Context ? ,
105
- target : Context ?
106
- ): Boolean {
107
- val sourceNamespace = source?.namespace ? : return false
108
- val targetNamespace = target?.namespace
109
- return if (target != null
110
- && sourceNamespace != targetNamespace
111
- ) {
112
- target.namespace = source.namespace
93
+ private fun setCurrentContext (kubeConfig : io.fabric8.kubernetes.api.model.Config ? ): Boolean {
94
+ val currentContext = currentContext?.name ? : return false
95
+ return if (
96
+ kubeConfig != null
97
+ && currentContext != kubeConfig.currentContext) {
98
+ kubeConfig.currentContext = currentContext
113
99
true
114
100
} else {
115
101
false
@@ -119,4 +105,8 @@ open class ClientConfig(private val client: Client, private val executor: Execut
119
105
fun isCurrent (context : NamedContext ): Boolean {
120
106
return context == currentContext
121
107
}
108
+
109
+ fun isEqual (config : io.fabric8.kubernetes.api.model.Config ): Boolean {
110
+ return ConfigHelper .areEqual(config, configuration)
111
+ }
122
112
}
0 commit comments