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

Commit 6e41f9a

Browse files
authored
fix: report 'cluster unreachable' instead of 'failed to list secrets' (#682) (#683)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 226809e commit 6e41f9a

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class ApplicationsTreeStructure extends AbstractTreeStructure implements
5151
private final DevfileRegistriesNode registries;
5252

5353
private static final String LOGIN = "Please log in to the cluster";
54+
private static final String CLUSTER_UNREACHABLE = "Error: Cluster not reachable";
5455

5556
public ApplicationsTreeStructure(Project project) {
5657
this.project = project;
@@ -173,6 +174,10 @@ private MessageNode<?> createErrorNode(ParentableNode<?> parent, Exception e) {
173174
return new MessageNode<>(root, parent, kce.getCause().getMessage());
174175
} else if (kce.getCause().getMessage().contains(Constants.DEFAULT_KUBE_URL)) {
175176
return new MessageNode<>(root, parent, LOGIN);
177+
} else if (KubernetesClientExceptionUtils.isHostDown(kce)
178+
|| KubernetesClientExceptionUtils.isConnectionReset(kce)
179+
|| KubernetesClientExceptionUtils.isCouldNotConnect(kce)) {
180+
return new MessageNode<>(root, parent, CLUSTER_UNREACHABLE);
176181
}
177182
}
178183
return new MessageNode<>(root, parent, "Could not get namespaces: " + ExceptionUtils.getMessage(e));

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,33 @@ public static boolean isForbidden(KubernetesClientException e) {
2525
public static boolean isUnauthorized(KubernetesClientException e) {
2626
return HttpURLConnection.HTTP_UNAUTHORIZED == e.getCode();
2727
}
28+
29+
public static boolean isHostDown(KubernetesClientException e) {
30+
if (e.getCause() == null) {
31+
return false;
32+
}
33+
return messageContains("host is down", e.getCause());
34+
}
35+
36+
public static boolean isConnectionReset(KubernetesClientException e) {
37+
if (e.getCause() == null) {
38+
return false;
39+
}
40+
return messageContains("connection reset", e.getCause());
41+
}
42+
43+
public static boolean isCouldNotConnect(KubernetesClientException e) {
44+
if (e.getCause() == null) {
45+
return false;
46+
}
47+
return messageContains("failed to connect", e.getCause());
48+
}
49+
50+
public static boolean messageContains(String message, Throwable e) {
51+
return e != null
52+
&& e.getMessage() != null
53+
&& e.getMessage().toLowerCase().contains(message);
54+
}
55+
2856
}
2957

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package org.jboss.tools.intellij.openshift.utils;
12+
13+
import io.fabric8.kubernetes.client.KubernetesClientException;
14+
import org.junit.Test;
15+
16+
import java.io.IOException;
17+
import java.net.HttpURLConnection;
18+
19+
import static org.fest.assertions.Assertions.assertThat;
20+
21+
public class KubernetesClientExceptionUtilsTest {
22+
23+
private final KubernetesClientException notFoundClientException = new KubernetesClientException("Dagobah was not found", HttpURLConnection.HTTP_NOT_FOUND, null);
24+
25+
@Test
26+
public void should_be_forbidden() {
27+
KubernetesClientException e = new KubernetesClientException("it's forbidden to contradict the emperor", HttpURLConnection.HTTP_FORBIDDEN, null);
28+
assertThat(KubernetesClientExceptionUtils.isForbidden(e)).isTrue();
29+
}
30+
31+
@Test
32+
public void should_not_be_forbidden() {
33+
assertThat(KubernetesClientExceptionUtils.isForbidden(notFoundClientException)).isFalse();
34+
}
35+
36+
@Test
37+
public void should_be_unauthorized() {
38+
KubernetesClientException e = new KubernetesClientException("yoda is unauthorized to enter the jedi temple", HttpURLConnection.HTTP_UNAUTHORIZED, null);
39+
assertThat(KubernetesClientExceptionUtils.isUnauthorized(e)).isTrue();
40+
}
41+
42+
@Test
43+
public void should_not_be_unauthorized() {
44+
assertThat(KubernetesClientExceptionUtils.isForbidden(notFoundClientException)).isFalse();
45+
}
46+
47+
@Test
48+
public void should_be_host_down() {
49+
IOException hostDownException = new IOException("Host is down");
50+
KubernetesClientException e = new KubernetesClientException("the rebel base is down", hostDownException);
51+
assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isTrue();
52+
}
53+
54+
@Test
55+
public void should_not_be_host_down() {
56+
IOException hostRunningException = new IOException("Host is up and running");
57+
KubernetesClientException e = new KubernetesClientException("the rebel base is up and running", hostRunningException);
58+
assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isFalse();
59+
}
60+
61+
@Test
62+
public void should_not_be_host_down_null_cause() {
63+
KubernetesClientException e = new KubernetesClientException("the rebel base is up and running", null);
64+
assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isFalse();
65+
}
66+
67+
@Test
68+
public void should_not_be_host_down_different_cause() {
69+
KubernetesClientException e = new KubernetesClientException("the rebel base is up and running", notFoundClientException);
70+
assertThat(KubernetesClientExceptionUtils.isHostDown(e)).isFalse();
71+
}
72+
73+
@Test
74+
public void should_be_connection_reset() {
75+
IOException connectionResetException = new IOException("Is always fear that the connection reset.");
76+
KubernetesClientException e = new KubernetesClientException("the connection to the rebel base was cut down", connectionResetException);
77+
assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isTrue();
78+
}
79+
80+
@Test
81+
public void should_not_be_connection_reset() {
82+
IOException connectionEstablishedException = new IOException("Connection is established");
83+
KubernetesClientException e = new KubernetesClientException("the connection to the rebel base is stable", connectionEstablishedException);
84+
assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isFalse();
85+
}
86+
87+
@Test
88+
public void should_not_be_connection_reset_null_cause() {
89+
KubernetesClientException e = new KubernetesClientException("the connection to the rebel base is stable", null);
90+
assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isFalse();
91+
}
92+
93+
@Test
94+
public void should_not_be_connection_reset_different_cause() {
95+
KubernetesClientException e = new KubernetesClientException("the connection to the rebel base is stable", notFoundClientException);
96+
assertThat(KubernetesClientExceptionUtils.isConnectionReset(e)).isFalse();
97+
}
98+
99+
@Test
100+
public void should_be_could_not_connect() {
101+
IOException couldNotConnectException = new IOException("Alarming: failed to connect to Dagobah");
102+
KubernetesClientException e = new KubernetesClientException("Operation: connection to the rebel base failed", couldNotConnectException);
103+
assertThat(KubernetesClientExceptionUtils.isCouldNotConnect(e)).isTrue();
104+
}
105+
106+
@Test
107+
public void should_not_be_could_not_connect_null_cause() {
108+
KubernetesClientException e = new KubernetesClientException("Operation: connection to the rebel base failed", null);
109+
assertThat(KubernetesClientExceptionUtils.isCouldNotConnect(e)).isFalse();
110+
}
111+
112+
@Test
113+
public void should_not_be_could_not_connect_different_cause() {
114+
KubernetesClientException e = new KubernetesClientException("Operation: connection to the rebel base failed", notFoundClientException);
115+
assertThat(KubernetesClientExceptionUtils.isCouldNotConnect(e)).isFalse();
116+
}
117+
118+
}

0 commit comments

Comments
 (0)