diff --git a/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaceWatcher.kt b/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaceWatcher.kt index bb477666..99f46dca 100644 --- a/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaceWatcher.kt +++ b/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaceWatcher.kt @@ -12,9 +12,6 @@ package com.redhat.devtools.gateway.devworkspace import com.intellij.openapi.application.EDT -import com.redhat.devtools.gateway.openshift.Projects -import com.redhat.devtools.gateway.openshift.Utils -import io.kubernetes.client.openapi.ApiClient import io.kubernetes.client.util.Watch import kotlinx.coroutines.* @@ -82,7 +79,6 @@ class DevWorkspaceWatcher( } class DevWorkspaceWatchManager( - private val client: ApiClient, private val createWatcher: (String, String?) -> Watch, private val createFilter: (String) -> ((DevWorkspace) -> Boolean), private val listener: DevWorkspaceListener, @@ -90,9 +86,11 @@ class DevWorkspaceWatchManager( ) { private val watchers = mutableListOf() - fun start(lastResourceVersions: Map? = null) { - Projects(client).list().onEach { project -> - val ns = Utils.getValue(project, arrayOf("metadata","name")) as String + fun start(lastResourceVersions: Map = emptyMap()) { + lastResourceVersions.forEach { (ns, resourceVersion) -> + if (resourceVersion == null) { + return@forEach + } val w = DevWorkspaceWatcher( namespace = ns, createWatcher = createWatcher, @@ -101,7 +99,7 @@ class DevWorkspaceWatchManager( scope = scope ) watchers += w - w.start(lastResourceVersions?.get(ns)) + w.start(resourceVersion) } } @@ -109,5 +107,4 @@ class DevWorkspaceWatchManager( watchers.forEach { it.stop() } watchers.clear() } -} - +} \ No newline at end of file diff --git a/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaces.kt b/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaces.kt index 3680221b..87fa26c6 100644 --- a/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaces.kt +++ b/src/main/kotlin/com/redhat/devtools/gateway/devworkspace/DevWorkspaces.kt @@ -12,8 +12,8 @@ package com.redhat.devtools.gateway.devworkspace import com.google.gson.reflect.TypeToken -import com.redhat.devtools.gateway.openshift.Utils import com.intellij.openapi.diagnostic.thisLogger +import com.redhat.devtools.gateway.openshift.Utils import io.kubernetes.client.openapi.ApiClient import io.kubernetes.client.openapi.ApiException import io.kubernetes.client.openapi.apis.CustomObjectsApi @@ -23,7 +23,6 @@ import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeoutOrNull import java.io.IOException import java.util.concurrent.CancellationException -import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.seconds data class DevWorkspaceListResult( @@ -140,28 +139,38 @@ class DevWorkspaces(private val client: ApiClient) { // Returns a map of DW Owner UID tp list of DW Templates private fun getTemplateMap(namespace: String): Map> { - val dwTemplateList = customApi - .listNamespacedCustomObject( - "workspace.devfile.io", - "v1alpha2", - namespace, - "devworkspacetemplates", - ) - .execute() - - val items = Utils.getValue(dwTemplateList, arrayOf("items")) as? List<*> ?: emptyList() - return items - .map { DevWorkspaceTemplate.from(it) } - .flatMap { templ -> - templ.ownerRefencesUids.map { uid -> uid to templ } + try { + val dwTemplateList = customApi + .listNamespacedCustomObject( + "workspace.devfile.io", + "v1alpha2", + namespace, + "devworkspacetemplates", + ) + .execute() + + val items = Utils.getValue(dwTemplateList, arrayOf("items")) as? List<*> ?: emptyList() + return items + .map { DevWorkspaceTemplate.from(it) } + .flatMap { templ -> + templ.ownerRefencesUids.map { uid -> uid to templ } + } + .groupBy( + keySelector = { it.first }, // UID + valueTransform = { it.second } // DevWorkspaceTemplate + ) + } catch (e: ApiException) { + thisLogger().info(e.message) + + if (e.code == 403) { + return emptyMap() } - .groupBy( - keySelector = { it.first }, // UID - valueTransform = { it.second } // DevWorkspaceTemplate - ) + + throw e + } } - @Throws(ApiException::class) +@Throws(ApiException::class) fun start(namespace: String, name: String) { DevWorkspacePatch(namespace, name, client) { get(namespace, name) @@ -240,9 +249,12 @@ class DevWorkspaces(private val client: ApiClient) { checkCancelled?.invoke() val devWorkspace = try { DevWorkspaces(client).get(namespace, name) - } catch (_: Exception) { - delay(1.seconds) - continue + } catch (e: ApiException) { + if (e.code in setOf(429, 500, 502, 503, 504)) { + delay(1.seconds) + continue + } + throw e } checkCancelled?.invoke() diff --git a/src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesWorkspacesStepView.kt b/src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesWorkspacesStepView.kt index 98990592..274153ca 100644 --- a/src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesWorkspacesStepView.kt +++ b/src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesWorkspacesStepView.kt @@ -17,7 +17,6 @@ import com.intellij.openapi.application.invokeLater import com.intellij.openapi.application.runInEdt import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.progress.ProgressManager -import com.intellij.openapi.ui.MessageDialogBuilder import com.intellij.openapi.util.Disposer import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeScreenUIManager import com.intellij.ui.ColoredListCellRenderer @@ -87,10 +86,10 @@ class DevSpacesWorkspacesStepView( row { cell(JBScrollPane(listDevWorkspaces) - .apply { - preferredSize = Dimension(preferredSize.width, 200) - minimumSize = Dimension(minimumSize.width, 100) - }) + .apply { + preferredSize = Dimension(preferredSize.width, 200) + minimumSize = Dimension(minimumSize.width, 100) + }) .align(AlignX.FILL) .align(AlignY.FILL) }.resizableRow().bottomGap(BottomGap.MEDIUM) @@ -143,7 +142,7 @@ class DevSpacesWorkspacesStepView( return false } devSpacesContext.devWorkspace = workspace - val serverStatus = try { + try { getServerStatus() } catch (e: Exception) { if (e.isCancellationException()) { @@ -342,7 +341,7 @@ class DevSpacesWorkspacesStepView( } } - private fun connect() { + private fun connect() { ProgressManager.getInstance().runProcessWithProgressSynchronously( { try { @@ -508,7 +507,6 @@ class DevSpacesWorkspacesStepView( ) { private val devWorkspaces = DevWorkspaces(client) private val watchManager = DevWorkspaceWatchManager( - client = client, createWatcher = { ns, latestResourceVersion -> devWorkspaces.createWatcher(ns, latestResourceVersion = latestResourceVersion) }, @@ -575,4 +573,4 @@ class DevSpacesWorkspacesStepView( watchManager.stop() } } -} +} \ No newline at end of file