Skip to content

Commit 764f1e5

Browse files
authored
Don't fail when trying to list the devworkspaces in the namespace that the user has no access to (#134)
Signed-off-by: Artem Zatsarynnyi <[email protected]>
1 parent f13b7c5 commit 764f1e5

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/main/kotlin/com/redhat/devtools/gateway/openshift/DevWorkspaces.kt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Red Hat, Inc.
2+
* Copyright (c) 2024-2025 Red Hat, Inc.
33
* This program and the accompanying materials are made
44
* available under the terms of the Eclipse Public License 2.0
55
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -11,7 +11,9 @@
1111
*/
1212
package com.redhat.devtools.gateway.openshift
1313

14+
import com.google.gson.Gson
1415
import com.google.gson.reflect.TypeToken
16+
import com.intellij.openapi.diagnostic.thisLogger
1517
import io.kubernetes.client.custom.V1Patch
1618
import io.kubernetes.client.openapi.ApiClient
1719
import io.kubernetes.client.openapi.ApiException
@@ -34,18 +36,35 @@ class DevWorkspaces(private val client: ApiClient) {
3436
@Throws(ApiException::class)
3537
fun list(namespace: String): List<DevWorkspace> {
3638
val customApi = CustomObjectsApi(client)
37-
val response = customApi.listNamespacedCustomObject(
38-
"workspace.devfile.io",
39-
"v1alpha2",
40-
namespace,
41-
"devworkspaces"
42-
).execute()
39+
try {
40+
val response = customApi.listNamespacedCustomObject(
41+
"workspace.devfile.io",
42+
"v1alpha2",
43+
namespace,
44+
"devworkspaces"
45+
).execute()
4346

44-
val dwItems = Utils.getValue(response, arrayOf("items")) as List<*>
45-
return dwItems
46-
.stream()
47-
.map { dwItem -> DevWorkspace.from(dwItem) }
48-
.toList()
47+
val dwItems = Utils.getValue(response, arrayOf("items")) as List<*>
48+
return dwItems
49+
.stream()
50+
.map { dwItem -> DevWorkspace.from(dwItem) }
51+
.toList()
52+
} catch (e: ApiException) {
53+
thisLogger().info(e.message)
54+
55+
val response = Gson().fromJson(e.responseBody, Map::class.java)
56+
// There might be some namespaces (OpenShift projects) in which the user cannot list resource "devworkspaces"
57+
// e.g. "openshift-virtualization-os-images" on Red Hat Dev Sandbox, etc.
58+
// It doesn't make sense to show an error to the user in such cases,
59+
// so let's skip it silently.
60+
if ((response["code"] as Double) == 403.0) {
61+
return emptyList()
62+
} else {
63+
// The error will be shown in the Gateway UI.
64+
thisLogger().error(e.message)
65+
throw e
66+
}
67+
}
4968
}
5069

5170
fun get(namespace: String, name: String): DevWorkspace {

0 commit comments

Comments
 (0)