Skip to content

Commit

Permalink
show current project when installing helm chart (redhat-developer#613)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Feb 1, 2024
1 parent 929dcea commit c0a9e16
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2024 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 org.jboss.tools.intellij.openshift.utils.helm;


import org.jboss.tools.intellij.openshift.utils.helm.HelmCli.HelmEnv;

import static org.fest.assertions.Assertions.assertThat;

public class HelmCliEnvTest extends HelmCliTest {

public void testEnv_should_return_current_namespace() throws Exception {
// given
// when
HelmEnv env = helm.env();
// then
assertThat(env.get(HelmEnv.HELM_NAMESPACE)).isNotEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ protected void init() {

setupTable(chartsTable, chartsTableModel, statusIcon)
.thenCompose((Void) -> addDefaultRepo(helm))
.whenComplete((Void, throwable) -> {
if (throwable == null) {
load(chartsTable, chartsTableModel, statusIcon, helm);
}
});
.thenCompose((Void) -> load(chartsTable, chartsTableModel, statusIcon, helm));
}

private static void setBorders(JRootPane rootPane) {
Expand All @@ -114,9 +110,10 @@ private static void setBorders(JRootPane rootPane) {
private void registerShortcuts(JRootPane rootPane) {
AnAction escape = ActionManager.getInstance().getAction("EditorEscape");
DumbAwareAction.create(e -> closeImmediately())
.registerCustomShortcutSet(escape == null ?
CommonShortcuts.ESCAPE
: escape.getShortcutSet(), rootPane, myDisposable);
.registerCustomShortcutSet(
escape == null ? CommonShortcuts.ESCAPE : escape.getShortcutSet(),
rootPane,
myDisposable);
}

@Override
Expand Down Expand Up @@ -233,7 +230,7 @@ private CompletableFuture<Void> load(
LOGGER.warn("Could not load all helm charts.", e);
return Collections.emptyList();
}
}, SwingUtils.EXECUTOR_BACKGROUND)
}, EXECUTOR_BACKGROUND)
.thenAcceptAsync((charts) -> {
List<ChartVersions> chartVersions = toChartVersions(charts);
tableModel.setCharts(chartVersions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jboss.tools.intellij.openshift.ui.StatusIcon;
import org.jboss.tools.intellij.openshift.ui.SwingUtils;
import org.jboss.tools.intellij.openshift.utils.helm.Helm;
import org.jboss.tools.intellij.openshift.utils.helm.HelmCli;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,6 +51,7 @@
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Supplier;
import java.util.regex.Pattern;

Expand All @@ -74,6 +76,7 @@ class InstallPanel extends JBPanel<InstallPanel> implements ChartPanel, Disposab

private JLabel icon;
private JTextField releaseNameText;
private JBLabel currentProject;
private JLabel chartNameLabel;
private ComboBox<String> versionsCombo;
private JBTextField parameters;
Expand Down Expand Up @@ -120,6 +123,11 @@ private void initComponents() {
releaseNameText.addKeyListener(onKeyPressed(installButton));
add(releaseNameText, "spanx 2, pushx, growx, wrap");

add(new JBLabel("Active Project:"),"skip");
this.currentProject = new JBLabel();
// value set in validation
add(currentProject, "pushx, wrap");

add(new JBLabel("Version:"), "skip");
this.versionsCombo = new ComboBox<>(new String[]{});
versionsCombo.addItemListener(onVersionSelected());
Expand All @@ -144,6 +152,18 @@ private void initComponents() {
setChart(chart);
}

private CompletableFuture<String> loadCurrentNamespace() {
return CompletableFuture.supplyAsync(() -> {
try {
HelmCli.HelmEnv env = helm.env();
return env.get(HelmCli.HelmEnv.HELM_NAMESPACE);
} catch (IOException e) {
throw new CompletionException(e);
}
},
EXECUTOR_BACKGROUND);
}

private ItemListener onVersionSelected() {
return e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
Expand Down Expand Up @@ -309,6 +329,12 @@ private void updateComponents(PanelState panelState) {
}
}

private void updateCurrentProjectLabel() {
loadCurrentNamespace()
.thenAcceptAsync(currentProject -> InstallPanel.this.currentProject.setText(currentProject)
, EXECUTOR_UI);
}

@Override
public void dispose() {
disposable.dispose();
Expand All @@ -332,6 +358,7 @@ public ValidationInfo get() {
}
ValidationInfo validation = validate(field.getText());
enableInstallButton(validation);
updateCurrentProjectLabel(); // update in validation to have most frequent polling
return validation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface Helm {
String install(String name, String chart, String version, String parameters) throws IOException;

String uninstall(String... names) throws IOException;

HelmCli.HelmEnv env() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage;
import static org.jboss.tools.intellij.openshift.Constants.HOME_FOLDER;
Expand Down Expand Up @@ -130,9 +131,67 @@ public String uninstall(String... names) throws IOException {
arguments);
}

@Override
public HelmEnv env() throws IOException {
String result = execute(command, Collections.emptyMap(), "env");
/*
* HELM_NAMESPACE="default"
* HELM_KUBECONTEXT=""
*/
Map<String, String> env = Stream.of(result.split("\n"))
.collect(Collectors.toMap(
(String line) -> line.split("=", 2)[0],
(String line) -> {
String[] keyValue = line.split("=", 2);
if (keyValue.length > 2) {
return "";
} else {
return keyValue[1].replaceAll("\"", "");
}
}
));
return new HelmEnv(env);
}

private static String execute(String command, Map<String, String> envs, String... args) throws IOException {
File workingDirectory = new File(HOME_FOLDER);
ExecHelper.ExecResult output = ExecHelper.executeWithResult(command, true, workingDirectory, envs, args);
return output.getStdOut();
}

public static class HelmEnv {

public static final String HELM_BIN = "HELM_BIN";
public static final String HELM_BURST_LIMIT = "HELM_BURST_LIMIT";
public static final String HELM_CACHE_HOME = "HELM_CACHE_HOME";
public static final String HELM_CONFIG_HOME = "HELM_CONFIG_HOME";
public static final String HELM_DATA_HOME = "HELM_DATA_HOME";
public static final String HELM_DEBUG = "HELM_DEBUG";
public static final String HELM_KUBEAPISERVER = "HELM_KUBEAPISERVER";
public static final String HELM_KUBEASGROUPS = "HELM_KUBEASGROUPS";
public static final String HELM_KUBEASUSER = "HELM_KUBEASUSER";
public static final String HELM_KUBECAFILE = "HELM_KUBECAFILE";
public static final String HELM_KUBECONTEXT = "HELM_KUBECONTEXT";
public static final String HELM_KUBEINSECURE_SKIP_TLS_VERIFY = "HELM_KUBEINSECURE_SKIP_TLS_VERIFY";
public static final String HELM_KUBETLS_SERVER_NAME = "HELM_KUBETLS_SERVER_NAME";
public static final String HELM_KUBETOKEN = "HELM_KUBETOKEN";
public static final String HELM_MAX_HISTORY = "HELM_MAX_HISTORY";
public static final String HELM_NAMESPACE = "HELM_NAMESPACE";
public static final String HELM_PLUGINS = "HELM_PLUGINS";
public static final String HELM_QPS = "HELM_QPS";
public static final String HELM_REGISTRY_CONFIG = "HELM_REGISTRY_CONFIG";
public static final String HELM_REPOSITORY_CACHE = "HELM_REPOSITORY_CACHE";
public static final String HELM_REPOSITORY_CONFIG = "HELM_REPOSITORY_CONFIG";

private final Map<String, String> env;

public HelmEnv(Map<String, String> env) {
this.env = env;
}

public String get(final String key) {
return env.get(key);
}
}

}

0 comments on commit c0a9e16

Please sign in to comment.