Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -82,17 +79,18 @@ class DevWorkspaceWatcher(
}

class DevWorkspaceWatchManager(
private val client: ApiClient,
private val createWatcher: (String, String?) -> Watch<Any>,
private val createFilter: (String) -> ((DevWorkspace) -> Boolean),
private val listener: DevWorkspaceListener,
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
) {
private val watchers = mutableListOf<DevWorkspaceWatcher>()

fun start(lastResourceVersions: Map<String, String?>? = null) {
Projects(client).list().onEach { project ->
val ns = Utils.getValue(project, arrayOf("metadata","name")) as String
fun start(lastResourceVersions: Map<String, String?> = emptyMap()) {
lastResourceVersions.forEach { (ns, resourceVersion) ->
if (resourceVersion == null) {
return@forEach
}
val w = DevWorkspaceWatcher(
namespace = ns,
createWatcher = createWatcher,
Expand All @@ -101,13 +99,12 @@ class DevWorkspaceWatchManager(
scope = scope
)
watchers += w
w.start(lastResourceVersions?.get(ns))
w.start(resourceVersion)
}
}

fun stop() {
watchers.forEach { it.stop() }
watchers.clear()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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<String, List<DevWorkspaceTemplate>> {
val dwTemplateList = customApi
.listNamespacedCustomObject(
"workspace.devfile.io",
"v1alpha2",
namespace,
"devworkspacetemplates",
)
.execute()

val items = Utils.getValue(dwTemplateList, arrayOf("items")) as? List<*> ?: emptyList<Any>()
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<Any>()
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)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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
Expand Down Expand Up @@ -87,10 +86,10 @@

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)
Expand Down Expand Up @@ -143,7 +142,7 @@
return false
}
devSpacesContext.devWorkspace = workspace
val serverStatus = try {
try {
getServerStatus()
} catch (e: Exception) {
if (e.isCancellationException()) {
Expand Down Expand Up @@ -342,7 +341,7 @@
}
}

private fun connect() {
private fun connect() {
ProgressManager.getInstance().runProcessWithProgressSynchronously(
{
try {
Expand Down Expand Up @@ -503,12 +502,11 @@
}

private class WorkspacesWatch(
private val client: ApiClient,

Check warning on line 505 in src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesWorkspacesStepView.kt

View workflow job for this annotation

GitHub Actions / Inspect code

Constructor parameter is never used as a property

Constructor parameter is never used as a property
private val workspacesDataModel: DefaultListModel<DevWorkspace>
) {
private val devWorkspaces = DevWorkspaces(client)
private val watchManager = DevWorkspaceWatchManager(
client = client,
createWatcher = { ns, latestResourceVersion ->
devWorkspaces.createWatcher(ns, latestResourceVersion = latestResourceVersion)
},
Expand Down Expand Up @@ -575,4 +573,4 @@
watchManager.stop()
}
}
}
}
Loading