@@ -14,6 +14,7 @@ package com.redhat.devtools.gateway.devworkspace
1414import com.intellij.openapi.application.EDT
1515import com.intellij.openapi.application.ModalityState
1616import com.intellij.openapi.application.asContextElement
17+ import io.kubernetes.client.openapi.ApiException
1718import io.kubernetes.client.util.Watch
1819import kotlinx.coroutines.*
1920
@@ -49,30 +50,39 @@ class DevWorkspaceWatcher(
4950
5051 private suspend fun watchLoop (latestResourceVersion : String? = null) {
5152 while (scope.isActive && ! stopped) {
52- val watcher = createWatcher(namespace, latestResourceVersion)
53- var matches = createFilter(namespace)
54-
5553 try {
56- for (event in watcher) {
57- if (! scope.isActive || stopped) break
54+ val watcher = createWatcher(namespace, latestResourceVersion)
55+ watcher.use { watcher ->
56+ var matches = createFilter(namespace)
57+ for (event in watcher) {
58+ if (! scope.isActive || stopped) break
5859
59- val dw = DevWorkspace .from(event.`object `)
60- if (event.type == " ADDED" ) {
61- matches = createFilter(namespace)
62- }
63- withContext(Dispatchers .EDT + ModalityState .any().asContextElement()) {
64- if (stopped) return @withContext
65- when (event.type) {
66- " ADDED" -> if (matches(dw)) listener.onAdded(dw)
67- " MODIFIED" -> if (matches(dw)) listener.onUpdated(dw) else listener.onDeleted(dw)
68- " DELETED" -> listener.onDeleted(dw)
60+ val dw = DevWorkspace .from(event.`object `)
61+ if (event.type == " ADDED" ) {
62+ matches = createFilter(namespace)
63+ }
64+ withContext(Dispatchers .EDT + ModalityState .any().asContextElement()) {
65+ if (stopped) return @withContext
66+ when (event.type) {
67+ " ADDED" -> if (matches(dw)) listener.onAdded(dw)
68+ " MODIFIED" -> if (matches(dw)) listener.onUpdated(dw) else listener.onDeleted(dw)
69+ " DELETED" -> listener.onDeleted(dw)
70+ }
6971 }
7072 }
73+ // connection dropped or closed — reconnect
74+ }
75+ } catch (e: CancellationException ) {
76+ throw e
77+ } catch (e: ApiException ) {
78+ if (e.code == 403 || e.code == 404 ) {
79+ // don't retry, user cannot watch this namespace/resource.
80+ stopped = true
81+ return
7182 }
83+ // Other Kubernetes API errors — retry.
7284 } catch (_: Exception ) {
73- // connection dropped or closed — reconnect
74- } finally {
75- watcher.close()
85+ // Connection dropped or closed — reconnect.
7686 }
7787
7888 @Suppress(" ConvertLongToDuration" )
0 commit comments