Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 37bdd93

Browse files
authored
fix: update tree, query cluster info and send telemetry in backgroud (#912) (#915)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 8232bf8 commit 37bdd93

File tree

6 files changed

+91
-61
lines changed

6 files changed

+91
-61
lines changed

src/main/java/org/jboss/tools/intellij/openshift/WindowToolFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.intellij.ui.tree.AsyncTreeModel;
2525
import com.intellij.ui.tree.StructureTreeModel;
2626
import com.intellij.ui.treeStructure.Tree;
27+
import com.intellij.util.concurrency.Invoker;
2728
import com.redhat.devtools.intellij.common.tree.MutableModelSynchronizer;
2829
import com.redhat.devtools.intellij.common.tree.TreeHelper;
2930
import com.redhat.devtools.intellij.common.utils.IDEAContentFactory;
@@ -45,7 +46,11 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
4546
panel.setLayout(new BorderLayout());
4647
Content content = contentFactory.createContent(panel, "", false);
4748
ApplicationsTreeStructure structure = new ApplicationsTreeStructure(project, content);
48-
StructureTreeModel<ApplicationsTreeStructure> model = new StructureTreeModel<>(structure, content);
49+
StructureTreeModel<ApplicationsTreeStructure> model = new StructureTreeModel<>(
50+
structure,
51+
null,
52+
Invoker.forBackgroundPoolWithoutReadAction(content),
53+
content);
4954
content.setDisposer(structure);
5055
new MutableModelSynchronizer<>(model, structure, structure);
5156
Tree tree = new Tree(new AsyncTreeModel(model, content));

src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsRootNode.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.intellij.openapi.Disposable;
1515
import com.intellij.openapi.module.Module;
1616
import com.intellij.openapi.module.ModuleManager;
17+
import com.intellij.openapi.project.DumbAware;
1718
import com.intellij.openapi.project.ModuleListener;
1819
import com.intellij.openapi.project.Project;
1920
import com.intellij.openapi.util.Disposer;
@@ -24,6 +25,13 @@
2425
import com.redhat.devtools.intellij.common.utils.ConfigWatcher;
2526
import com.redhat.devtools.intellij.common.utils.ExecHelper;
2627
import io.fabric8.kubernetes.api.model.Config;
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.nio.file.Paths;
31+
import java.util.HashMap;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.concurrent.CompletableFuture;
2735
import org.jboss.tools.intellij.openshift.actions.NotificationUtils;
2836
import org.jboss.tools.intellij.openshift.utils.ProjectUtils;
2937
import org.jboss.tools.intellij.openshift.utils.ToolFactory;
@@ -38,16 +46,8 @@
3846
import org.slf4j.Logger;
3947
import org.slf4j.LoggerFactory;
4048

41-
import java.io.File;
42-
import java.io.IOException;
43-
import java.nio.file.Paths;
44-
import java.util.HashMap;
45-
import java.util.List;
46-
import java.util.Map;
47-
import java.util.concurrent.CompletableFuture;
48-
4949
public class ApplicationsRootNode
50-
implements ModuleListener, ConfigWatcher.Listener, ProcessingNode, StructureAwareNode, ParentableNode<ApplicationsRootNode>, Disposable {
50+
implements ModuleListener, ConfigWatcher.Listener, ProcessingNode, StructureAwareNode, ParentableNode<ApplicationsRootNode>, Disposable, DumbAware {
5151

5252
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationsRootNode.class);
5353
private final Project project;
@@ -102,7 +102,8 @@ private CompletableFuture<ApplicationRootNodeOdo> doGetOdo() {
102102
public CompletableFuture<ApplicationRootNodeOdo> getOdo() {
103103
return doGetOdo()
104104
.whenComplete((ApplicationRootNodeOdo odo, Throwable err) -> {
105-
if (odo.isDownloaded()) {
105+
if (odo != null
106+
&& odo.isDownloaded()) {
106107
structure.fireModified(this);
107108
}
108109
});

src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsTreeStructure.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private Object getCurrentNamespace(ApplicationsRootNode element) {
163163
}
164164
}
165165
} catch (Exception e) {
166-
node = createErrorNode(element, e);
166+
node = createCurrentNamespaceErrorNode(element, e);
167167
element.setLogged(false);
168168
}
169169
return node;
@@ -188,7 +188,7 @@ private Object[] createHelmRepositoriesChildren(HelmRepositoriesNode parent) {
188188
}
189189
}
190190

191-
private MessageNode<?> createErrorNode(ParentableNode<?> parent, Exception e) {
191+
private MessageNode<?> createCurrentNamespaceErrorNode(ParentableNode<?> parent, Exception e) {
192192
if (e instanceof KubernetesClientException kce) {
193193
if (KubernetesClientExceptionUtils.isForbidden(kce)
194194
|| KubernetesClientExceptionUtils.isUnauthorized(kce)) {
@@ -203,7 +203,7 @@ private MessageNode<?> createErrorNode(ParentableNode<?> parent, Exception e) {
203203
return new MessageNode<>(root, parent, CLUSTER_UNREACHABLE);
204204
}
205205
}
206-
return new MessageNode<>(root, parent, "Could not get namespaces: " + ExceptionUtils.getMessage(e));
206+
return new MessageNode<>(root, parent, "Could not get current namespace: " + ExceptionUtils.getMessage(e));
207207
}
208208

209209
private List<BaseNode<?>> getComponents(NamespaceNode namespaceNode, OdoFacade odo) {

src/main/java/org/jboss/tools/intellij/openshift/utils/Cli.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
******************************************************************************/
1111
package org.jboss.tools.intellij.openshift.utils;
1212

13+
import com.intellij.openapi.application.ApplicationManager;
14+
import com.intellij.util.messages.MessageBus;
1315
import com.redhat.devtools.intellij.common.kubernetes.ClusterHelper;
1416
import com.redhat.devtools.intellij.common.kubernetes.ClusterInfo;
1517
import com.redhat.devtools.intellij.common.ssl.IDEATrustManager;
@@ -22,13 +24,6 @@
2224
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
2325
import io.fabric8.kubernetes.client.http.HttpClient;
2426
import io.fabric8.kubernetes.client.internal.SSLUtils;
25-
import org.jetbrains.annotations.NotNull;
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
29-
import javax.net.ssl.TrustManager;
30-
import javax.net.ssl.X509ExtendedTrustManager;
31-
import javax.net.ssl.X509TrustManager;
3227
import java.io.IOException;
3328
import java.net.URISyntaxException;
3429
import java.security.KeyStoreException;
@@ -42,12 +37,17 @@
4237
import java.util.Map;
4338
import java.util.function.Function;
4439
import java.util.function.Supplier;
40+
import javax.net.ssl.TrustManager;
41+
import javax.net.ssl.X509ExtendedTrustManager;
42+
import javax.net.ssl.X509TrustManager;
43+
import org.jetbrains.annotations.NotNull;
44+
import org.slf4j.Logger;
45+
import org.slf4j.LoggerFactory;
4546

4647
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.IS_OPENSHIFT;
4748
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.KUBERNETES_VERSION;
4849
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.NAME_PREFIX_MISC;
4950
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.OPENSHIFT_VERSION;
50-
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.asyncSend;
5151
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.instance;
5252

5353
public class Cli {
@@ -97,17 +97,27 @@ public Map<String, String> apply(String url) {
9797
public static final class TelemetryReport {
9898

9999
public void report(KubernetesClient client) {
100-
TelemetryMessageBuilder.ActionMessage telemetry = instance().getBuilder().action(NAME_PREFIX_MISC + "login");
101-
try {
102-
ClusterInfo info = ClusterHelper.getClusterInfo(client);
103-
telemetry.property(KUBERNETES_VERSION, info.getKubernetesVersion());
104-
telemetry.property(IS_OPENSHIFT, Boolean.toString(info.isOpenshift()));
105-
telemetry.property(OPENSHIFT_VERSION, info.getOpenshiftVersion());
106-
asyncSend(telemetry);
107-
} catch (RuntimeException e) {
108-
// do not send telemetry when there is no context ( ie default kube URL as master URL )
109-
asyncSend(telemetry.error(e));
110-
}
100+
ApplicationManager.getApplication().executeOnPooledThread(() -> {
101+
TelemetryMessageBuilder.ActionMessage telemetry = instance().getBuilder().action(NAME_PREFIX_MISC + "login");
102+
try {
103+
ClusterInfo info = ClusterHelper.getClusterInfo(client);
104+
telemetry
105+
.property(KUBERNETES_VERSION, info.getKubernetesVersion())
106+
.property(IS_OPENSHIFT, Boolean.toString(info.isOpenshift()))
107+
.property(OPENSHIFT_VERSION, info.getOpenshiftVersion())
108+
.send();
109+
} catch (RuntimeException e) {
110+
// do not send telemetry when there is no context ( ie default kube URL as master URL )
111+
telemetry.error(e).send();
112+
}
113+
});
114+
}
115+
116+
public void subscribe(MessageBus bus, Map<String, String> envVars) {
117+
bus.connect().subscribe(
118+
TelemetryConfiguration.ConfigurationChangedListener.CONFIGURATION_CHANGED,
119+
onTelemetryConfigurationChanged(envVars)
120+
);
111121
}
112122

113123
@NotNull

src/main/java/org/jboss/tools/intellij/openshift/utils/oc/OcCli.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
import com.intellij.openapi.application.ApplicationManager;
1414
import com.intellij.util.messages.MessageBus;
15-
import com.intellij.util.messages.MessageBusConnection;
1615
import com.redhat.devtools.intellij.common.utils.ExecHelper;
17-
import com.redhat.devtools.intellij.telemetry.core.configuration.TelemetryConfiguration;
1816
import io.fabric8.kubernetes.client.KubernetesClient;
1917
import org.jboss.tools.intellij.openshift.utils.Cli;
2018
import org.jetbrains.annotations.NotNull;
@@ -49,11 +47,13 @@ public OcCli(
4947
Cli.TelemetryReport telemetryReport) {
5048
super(kubernetesClientFactory);
5149
this.command = command;
52-
MessageBusConnection connection = bus.connect();
5350
this.envVars = envVarFactory.apply(String.valueOf(client.getMasterUrl()));
51+
initTelemetry(telemetryReport, bus);
52+
}
53+
54+
private void initTelemetry(TelemetryReport telemetryReport, MessageBus bus) {
5455
telemetryReport.addTelemetryVars(envVars);
55-
connection.subscribe(TelemetryConfiguration.ConfigurationChangedListener.CONFIGURATION_CHANGED,
56-
telemetryReport.onTelemetryConfigurationChanged(this.envVars));
56+
telemetryReport.subscribe(bus, envVars);
5757
telemetryReport.report(client);
5858
}
5959

src/main/java/org/jboss/tools/intellij/openshift/utils/odo/OdoCli.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import com.intellij.openapi.application.ApplicationManager;
2121
import com.intellij.openapi.util.text.Strings;
2222
import com.intellij.util.messages.MessageBus;
23-
import com.intellij.util.messages.MessageBusConnection;
2423
import com.redhat.devtools.intellij.common.utils.ExecHelper;
25-
import com.redhat.devtools.intellij.telemetry.core.configuration.TelemetryConfiguration;
2624
import io.fabric8.kubernetes.api.Pluralize;
2725
import io.fabric8.kubernetes.api.model.ConfigMap;
2826
import io.fabric8.kubernetes.api.model.DeletionPropagation;
@@ -38,16 +36,6 @@
3836
import io.fabric8.openshift.client.OpenShiftClient;
3937
import io.fabric8.openshift.client.dsl.OpenShiftOperatorHubAPIGroupDSL;
4038
import io.fabric8.openshift.client.impl.OpenShiftOperatorHubAPIGroupClient;
41-
import org.apache.commons.io.FileUtils;
42-
import org.jboss.tools.intellij.openshift.KubernetesLabels;
43-
import org.jboss.tools.intellij.openshift.utils.Cli;
44-
import org.jboss.tools.intellij.openshift.utils.KubernetesClientExceptionUtils;
45-
import org.jboss.tools.intellij.openshift.utils.Serialization;
46-
import org.jetbrains.annotations.NotNull;
47-
import org.jetbrains.annotations.Nullable;
48-
import org.slf4j.Logger;
49-
import org.slf4j.LoggerFactory;
50-
5139
import java.io.BufferedReader;
5240
import java.io.File;
5341
import java.io.IOException;
@@ -60,10 +48,21 @@
6048
import java.util.Map;
6149
import java.util.concurrent.CompletableFuture;
6250
import java.util.concurrent.ExecutionException;
51+
import java.util.concurrent.TimeUnit;
52+
import java.util.concurrent.TimeoutException;
6353
import java.util.concurrent.atomic.AtomicBoolean;
6454
import java.util.function.BinaryOperator;
6555
import java.util.function.Function;
6656
import java.util.function.Supplier;
57+
import org.apache.commons.io.FileUtils;
58+
import org.jboss.tools.intellij.openshift.KubernetesLabels;
59+
import org.jboss.tools.intellij.openshift.utils.Cli;
60+
import org.jboss.tools.intellij.openshift.utils.KubernetesClientExceptionUtils;
61+
import org.jboss.tools.intellij.openshift.utils.Serialization;
62+
import org.jetbrains.annotations.NotNull;
63+
import org.jetbrains.annotations.Nullable;
64+
import org.slf4j.Logger;
65+
import org.slf4j.LoggerFactory;
6766

6867
import static io.fabric8.openshift.client.OpenShiftClient.BASE_API_GROUP;
6968
import static org.jboss.tools.intellij.openshift.Constants.HOME_FOLDER;
@@ -93,7 +92,7 @@ public class OdoCli extends Cli implements OdoDelegate {
9392
private final AtomicBoolean swaggerLoaded = new AtomicBoolean();
9493
private String currentNamespace;
9594
private JSonParser swagger;
96-
private final boolean isPodmanPresent;
95+
private CompletableFuture<Boolean> isPodmanPresent;
9796

9897
public OdoCli(com.intellij.openapi.project.Project project, String command) {
9998
this(project,
@@ -116,14 +115,16 @@ protected OdoCli(
116115
super(kubernetesClientFactory);
117116
this.command = command;
118117
this.project = project;
119-
MessageBusConnection connection = bus.connect();
120118
this.openshiftClient = openshiftClientFactory.apply(client);
121119
this.envVars = envVarFactory.apply(String.valueOf(client.getMasterUrl()));
120+
this.isPodmanPresent = processPodmanPresent(command);
121+
initTelemetry(bus, telemetryReport);
122+
}
123+
124+
private void initTelemetry(MessageBus bus, TelemetryReport telemetryReport) {
122125
telemetryReport.addTelemetryVars(envVars);
123-
connection.subscribe(TelemetryConfiguration.ConfigurationChangedListener.CONFIGURATION_CHANGED,
124-
telemetryReport.onTelemetryConfigurationChanged(this.envVars));
126+
telemetryReport.subscribe(bus, envVars);
125127
telemetryReport.report(client);
126-
isPodmanPresent = checkPodmanPresence();
127128
}
128129

129130

@@ -418,7 +419,7 @@ public ComponentInfo getComponentInfo(String project, String component, String p
418419

419420
private ComponentInfo parseComponentInfo(String json, ComponentKind kind) throws IOException {
420421
JSonParser parser = new JSonParser(Serialization.json().readTree(json));
421-
return parser.parseDescribeComponentInfo(kind, isPodmanPresent);
422+
return parser.parseDescribeComponentInfo(kind, isPodmanPresent());
422423
}
423424

424425
/*
@@ -747,20 +748,33 @@ public List<DevfileComponentType> getComponentTypesFromRegistry(String name) thr
747748
filter(type -> name.equals(type.getDevfileRegistry().getName())).toList();
748749
}
749750

750-
private boolean checkPodmanPresence() {
751-
CompletableFuture<ExecHelper.ExecResult> result = new CompletableFuture<>();
751+
private boolean isPodmanPresent() {
752752
try {
753-
result.complete(ExecHelper.executeWithResult(command, false, new File(HOME_FOLDER), envVars, "version"));
754-
return !result.get().getStdOut().contains("unable to fetch the podman client version");
753+
if (isPodmanPresent == null) {
754+
this.isPodmanPresent = processPodmanPresent(command);
755+
}
756+
return isPodmanPresent.get(2, TimeUnit.SECONDS);
755757
} catch (InterruptedException e) {
756758
Thread.currentThread().interrupt();
757759
LOGGER.warn(e.getLocalizedMessage(), e);
758-
} catch (ExecutionException | IOException e) {
760+
} catch (ExecutionException | TimeoutException e) {
759761
LOGGER.warn(e.getLocalizedMessage(), e);
760762
}
761763
return false;
762764
}
763765

766+
private @NotNull CompletableFuture<Boolean> processPodmanPresent(String command) {
767+
return CompletableFuture
768+
.supplyAsync(() -> {
769+
try {
770+
ExecHelper.ExecResult result = ExecHelper.executeWithResult(command, false, new File(HOME_FOLDER), envVars, "version");
771+
return result.getStdOut().contains("unable to fetch the podman client version");
772+
} catch (IOException e) {
773+
LOGGER.warn(e.getLocalizedMessage(), e);
774+
return false;
775+
}
776+
}, runnable -> ApplicationManager.getApplication().executeOnPooledThread(runnable));
777+
}
764778

765779
private static final class OpenShiftClientFactory implements Function<KubernetesClient, OpenShiftClient> {
766780
@Override

0 commit comments

Comments
 (0)