-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andre Dietisheim <[email protected]>
- Loading branch information
Showing
18 changed files
with
1,179 additions
and
98 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/kotlin/com/redhat/devtools/intellij/kubernetes/actions/ScaleReplicaAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.intellij.kubernetes.actions | ||
|
||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.redhat.devtools.intellij.common.actions.StructureTreeAction | ||
import com.redhat.devtools.intellij.kubernetes.dialogs.ScaleReplicaDialog | ||
import com.redhat.devtools.intellij.kubernetes.model.Notification | ||
import com.redhat.devtools.intellij.kubernetes.model.util.toMessage | ||
import io.fabric8.kubernetes.api.model.HasMetadata | ||
import io.fabric8.kubernetes.api.model.Pod | ||
import io.fabric8.kubernetes.api.model.ReplicationController | ||
import io.fabric8.kubernetes.api.model.apps.Deployment | ||
import io.fabric8.kubernetes.api.model.apps.ReplicaSet | ||
import io.fabric8.kubernetes.api.model.apps.StatefulSet | ||
import io.fabric8.openshift.api.model.DeploymentConfig | ||
import java.awt.event.MouseEvent | ||
import javax.swing.tree.TreePath | ||
|
||
class ScaleReplicaAction: StructureTreeAction() { | ||
|
||
override fun actionPerformed(event: AnActionEvent?, path: TreePath?, selected: Any?) { | ||
// not called | ||
} | ||
|
||
override fun actionPerformed(event: AnActionEvent?, path: Array<out TreePath>?, selected: Array<out Any>?) { | ||
val project = getEventProject(event) ?: return | ||
val toScale = selected?.firstOrNull()?.getElement<HasMetadata>() ?: return | ||
val model = getResourceModel() ?: return | ||
try { | ||
val replicator = model.getReplicas(toScale) | ||
val replicas = replicator?.replicas | ||
if (replicator == null | ||
|| replicas == null) { | ||
Notification().error( | ||
"Error Scaling", | ||
"Could not scale ${toScale.kind} '${toScale.metadata.name}: unsupported resource" | ||
) | ||
return | ||
} | ||
val resourceLabel = "${replicator.resource.kind} ${replicator.resource.metadata.name}" | ||
ScaleReplicaDialog( | ||
project, | ||
resourceLabel, | ||
replicas, | ||
{ replicas: Int -> model.setReplicas(replicas, replicator)}, | ||
(event?.inputEvent as? MouseEvent)?.locationOnScreen | ||
).show() | ||
} catch (e: RuntimeException) { | ||
Notification().error( | ||
"Error Scaling", | ||
"Could not scale ${toScale.kind} '${toScale.metadata.name}': ${toMessage(e)}" | ||
) | ||
} | ||
} | ||
|
||
override fun isVisible(selected: Array<out Any>?): Boolean { | ||
return selected?.size == 1 | ||
&& isVisible(selected.firstOrNull()) | ||
} | ||
|
||
override fun isVisible(selected: Any?): Boolean { | ||
val element = selected?.getElement<HasMetadata>() | ||
return element is Deployment | ||
|| element is DeploymentConfig | ||
|| element is ReplicationController | ||
|| element is ReplicaSet | ||
|| element is StatefulSet | ||
|| element is Pod | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/kotlin/com/redhat/devtools/intellij/kubernetes/dialogs/ScaleReplicaDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.intellij.kubernetes.dialogs | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.intellij.openapi.ui.ValidationInfo | ||
import com.intellij.ui.JBIntSpinner | ||
import com.intellij.ui.components.JBLabel | ||
import java.awt.BorderLayout | ||
import java.awt.Point | ||
import javax.swing.JComponent | ||
import javax.swing.JPanel | ||
import javax.swing.SwingConstants | ||
|
||
class ScaleReplicaDialog( | ||
project: Project, | ||
private val resourceLabel: String, | ||
private val currentReplicas: Int, | ||
private val onOk: (Int) -> Unit, | ||
private val location: Point? | ||
) : DialogWrapper(project, false) { | ||
|
||
private val replicasSpinner = JBIntSpinner( | ||
currentReplicas, | ||
0, | ||
Int.MAX_VALUE, | ||
1 | ||
) | ||
|
||
override fun createCenterPanel(): JComponent { | ||
return JPanel(BorderLayout()).apply { | ||
layout = BorderLayout(10, 10) | ||
add(JBLabel(resourceLabel, SwingConstants.LEFT), BorderLayout.NORTH) | ||
add(JBLabel("Replicas:", SwingConstants.LEFT), BorderLayout.LINE_START) | ||
add(replicasSpinner, BorderLayout.CENTER) | ||
} | ||
} | ||
|
||
override fun init() { | ||
title = "Set Replicas" | ||
setResizable(false) | ||
setOKButtonText("Scale") | ||
isModal = false | ||
if (location != null) { | ||
setLocation(location.x, location.y) | ||
} | ||
super.init() | ||
} | ||
|
||
override fun getPreferredFocusedComponent(): JComponent { | ||
return replicasSpinner | ||
} | ||
|
||
override fun show() { | ||
init() | ||
super.show() | ||
} | ||
|
||
override fun doValidate(): ValidationInfo? { | ||
return if (replicasSpinner.number == currentReplicas) { | ||
ValidationInfo("Replicas unchanged", replicasSpinner) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun doOKAction() { | ||
super.doOKAction() | ||
onOk.invoke(replicasSpinner.number) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.