Skip to content

Commit

Permalink
wait for save of kubeconfig to finish (redhat-developer#640)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jul 24, 2023
1 parent f501828 commit ca92fb0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ open class AllContexts(
this.client.get()
)
this.current?.close()
newClient.config.save().join()
all.clear() // causes reload of all contexts when accessed afterwards
val newCurrent = this.current // gets new current from all
if (toWatch != null) {
Expand Down Expand Up @@ -197,7 +198,6 @@ open class AllContexts(
private fun replaceClient(new: ClientAdapter<out KubernetesClient>, old: ClientAdapter<out KubernetesClient>?)
: ClientAdapter<out KubernetesClient> {
old?.close()
new.config.save()
this.client.set(new)
return new
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ package com.redhat.devtools.intellij.kubernetes.model.client

import com.intellij.openapi.application.ApplicationManager
import com.redhat.devtools.intellij.common.utils.ConfigHelper
import com.redhat.devtools.intellij.kubernetes.CompletableFutureUtils.PLATFORM_EXECUTOR
import io.fabric8.kubernetes.api.model.Context
import io.fabric8.kubernetes.api.model.NamedContext
import io.fabric8.kubernetes.client.Client
import io.fabric8.kubernetes.client.Config
import io.fabric8.kubernetes.client.internal.KubeConfigUtils
import java.util.concurrent.CompletableFuture

/**
* An adapter to access [io.fabric8.kubernetes.client.Config].
Expand Down Expand Up @@ -45,26 +47,31 @@ open class ClientConfig(private val client: Client) {
KubeConfigAdapter()
}

fun save() {
runAsync {
if (!kubeConfig.exists()) {
return@runAsync
}
val fromFile = kubeConfig.load() ?: return@runAsync
val currentContextInFile = KubeConfigUtils.getCurrentContext(fromFile)
if (setCurrentContext(
currentContext,
currentContextInFile,
fromFile
).or( // no short-circuit
setCurrentNamespace(
currentContext?.context,
currentContextInFile?.context)
)
) {
kubeConfig.save(fromFile)
}
}
fun save(): CompletableFuture<Boolean> {
return CompletableFuture.supplyAsync(
{
if (!kubeConfig.exists()) {
return@supplyAsync false
}
val fromFile = kubeConfig.load() ?: return@supplyAsync false
val currentContextInFile = KubeConfigUtils.getCurrentContext(fromFile)
if (setCurrentContext(
currentContext,
currentContextInFile,
fromFile
).or( // no short-circuit
setCurrentNamespace(
currentContext?.context,
currentContextInFile?.context
)
)
) {
kubeConfig.save(fromFile)
}
return@supplyAsync true
},
PLATFORM_EXECUTOR
)
}

private fun setCurrentContext(
Expand Down

0 comments on commit ca92fb0

Please sign in to comment.