Skip to content

Commit 8f1bc2c

Browse files
committed
feat: Gateway: "Connect to Dev Spaces" wizard is to be added with "Start" [Workspace] button
Fixes: eclipse-che/che#23585 Signed-off-by: Victor Rubezhny <[email protected]>
1 parent 4f4753e commit 8f1bc2c

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import java.io.IOException
2424
import java.util.concurrent.Executors
2525
import java.util.concurrent.TimeUnit
2626

27+
2728
class DevWorkspaces(private val client: ApiClient) {
2829
private val customApi = CustomObjectsApi(client)
2930

@@ -181,6 +182,48 @@ class DevWorkspaces(private val client: ApiClient) {
181182
return phaseIsDesiredState
182183
}
183184

185+
// Waits until the DevWorkspace goes out of any the given phases
186+
@Throws(ApiException::class, IOException::class)
187+
fun waitPhaseChanges(
188+
namespace: String,
189+
name: String,
190+
currentPhases: Collection<String>,
191+
timeout: Long
192+
): Boolean {
193+
var phaseChanged = false
194+
195+
val watcher = createWatcher(namespace, "metadata.name=$name")
196+
val executor = Executors.newSingleThreadScheduledExecutor()
197+
198+
executor.schedule (
199+
{
200+
try {
201+
for (item in watcher) {
202+
val devWorkspace = DevWorkspace.from(item.`object`)
203+
if (devWorkspace.phase !in currentPhases) {
204+
phaseChanged = true
205+
break
206+
}
207+
}
208+
} finally {
209+
watcher.close()
210+
executor.shutdown()
211+
}
212+
},
213+
0,
214+
TimeUnit.SECONDS
215+
)
216+
217+
try {
218+
executor.awaitTermination(timeout, TimeUnit.SECONDS)
219+
} finally {
220+
watcher.close()
221+
executor.shutdownNow()
222+
}
223+
224+
return phaseChanged
225+
}
226+
184227
// Example:
185228
// https://github.com/kubernetes-client/java/blob/master/examples/examples-release-20/src/main/java/io/kubernetes/client/examples/PatchExample.java
186229
@Throws(ApiException::class)

src/main/kotlin/com/redhat/devtools/gateway/view/steps/DevSpacesWorkspacesStepView.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class DevSpacesWorkspacesStepView(
5757
// 'true' when there are DevWorkspaces come from multiple namespaces
5858
private var multipleNamespaces = false
5959

60+
private lateinit var startDevWorkspaceButton: JButton
6061
private lateinit var stopDevWorkspaceButton: JButton
6162

6263
override val component = panel {
@@ -71,6 +72,10 @@ class DevSpacesWorkspacesStepView(
7172
row {
7273
label("").resizableColumn().align(AlignX.FILL)
7374

75+
startDevWorkspaceButton =
76+
button(DevSpacesBundle.message("connector.wizard_step.remote_server_connection.button.start")) {
77+
startDevWorkspace()
78+
}.gap(RightGap.SMALL).align(AlignX.RIGHT).component
7479
stopDevWorkspaceButton =
7580
button(DevSpacesBundle.message("connector.wizard_step.remote_server_connection.button.stop")) {
7681
stopDevWorkspace()
@@ -160,6 +165,34 @@ class DevSpacesWorkspacesStepView(
160165
listDevWorkspaces.selectedIndex = selectedIndex
161166
}
162167

168+
private fun startDevWorkspace() {
169+
if (!listDevWorkspaces.isSelectionEmpty) {
170+
listDWDataModel
171+
.get(listDevWorkspaces.selectedIndex)
172+
.also {
173+
DevWorkspaces(devSpacesContext.client)
174+
.start(
175+
it.namespace,
176+
it.name
177+
)
178+
ProgressManager.getInstance().runProcessWithProgressSynchronously(
179+
{
180+
if (waitDevWorkspaceNotStopped(it)) {
181+
refreshDevWorkspace(
182+
it.namespace,
183+
it.name
184+
)
185+
enableButtons()
186+
}
187+
},
188+
"Refreshing Workspace",
189+
true,
190+
null
191+
)
192+
}
193+
}
194+
}
195+
163196
private fun stopDevWorkspace() {
164197
if (!listDevWorkspaces.isSelectionEmpty) {
165198
listDWDataModel
@@ -235,6 +268,16 @@ class DevSpacesWorkspacesStepView(
235268
)
236269
}
237270

271+
private fun waitDevWorkspaceNotStopped(devWorkspace: DevWorkspace): Boolean {
272+
return DevWorkspaces(devSpacesContext.client)
273+
.waitPhaseChanges(
274+
devWorkspace.namespace,
275+
devWorkspace.name,
276+
listOf(DevWorkspaces.STOPPED, DevWorkspaces.FAILED),
277+
30
278+
)
279+
}
280+
238281
private fun waitDevWorkspaceStopped(devWorkspace: DevWorkspace): Boolean {
239282
return DevWorkspaces(devSpacesContext.client)
240283
.waitPhase(
@@ -249,9 +292,11 @@ class DevSpacesWorkspacesStepView(
249292
runInEdt {
250293
val workspace = getSelectedWorkspace()
251294
val running = isRunning(workspace)
295+
val stopped = isStopped(workspace)
252296
val alreadyConnected = isAlreadyConnected(workspace)
253297

254298
// stop button enabled only if workspace is running
299+
startDevWorkspaceButton.isEnabled = stopped
255300
stopDevWorkspaceButton.isEnabled = running
256301

257302
// Enable/disable "Next" (connect) button dynamically
@@ -279,6 +324,10 @@ class DevSpacesWorkspacesStepView(
279324
return isRunning(workspace) && !isAlreadyConnected(workspace)
280325
}
281326

327+
private fun isStopped(workspace: DevWorkspace?): Boolean {
328+
return workspace?.started == false
329+
}
330+
282331
private fun isRunning(workspace: DevWorkspace?): Boolean {
283332
return workspace?.running ?: false
284333
}

src/main/resources/messages/DevSpacesBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ connector.wizard_step.openshift_connection.button.next=Check connection and cont
1313
connector.wizard_step.remote_server_connection.title=Select running DevWorkspace
1414
connector.wizard_step.remote_server_connection.button.previous=Back
1515
connector.wizard_step.remote_server_connection.button.next=Connect
16+
connector.wizard_step.remote_server_connection.button.start=Start
1617
connector.wizard_step.remote_server_connection.button.stop=Stop
1718
connector.wizard_step.remote_server_connection.button.refresh=Refresh
1819
connector.wizard_step.remote_server_connection.list.empty_text=There are no DevWorkspaces

0 commit comments

Comments
 (0)