(null);
const termRef = React.useRef(null);
@@ -109,7 +112,13 @@ export const TerminalComponent = () => {
}
let workspaceID;
let phase;
- while (phase !== 'Running') {
+ const waitUntil = Date.now() + 5 * 60 * 1000; // wait max 5 minutes
+ for (
+ let retry = 0;
+ retry < 1000 && Date.now() < waitUntil && phase !== 'Running';
+ retry++
+ ) {
+ await waitBetweenRetries(retry);
[workspaceID, phase] = await getWorkspace(
restServerUrl,
link,
@@ -173,24 +182,32 @@ export const TerminalComponent = () => {
return (
-
+ ) : (
+
-
-
+ )}
{displayModal && cluster && token && (
getNamespaces(restServerUrl, cluster, token)}
diff --git a/plugins/web-terminal/src/components/TerminalComponent/utils/annotations.ts b/plugins/web-terminal/src/components/TerminalComponent/utils/annotations.ts
new file mode 100644
index 0000000000..66f4c60bff
--- /dev/null
+++ b/plugins/web-terminal/src/components/TerminalComponent/utils/annotations.ts
@@ -0,0 +1,7 @@
+import { Entity } from '@backstage/catalog-model';
+
+export const KUBERNETES_API_SERVER = 'kubernetes.io/api-server';
+
+/** @public */
+export const isWebTerminalAvailable = (entity: Entity): boolean =>
+ Boolean(entity.metadata.annotations?.[KUBERNETES_API_SERVER]);
diff --git a/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.test.ts b/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.test.ts
new file mode 100644
index 0000000000..cf706592ee
--- /dev/null
+++ b/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.test.ts
@@ -0,0 +1,19 @@
+import { getTimeInMsBetweenRetries } from './helpers';
+
+describe('getTimeInMsBetweenRetries', () => {
+ it('should return 0 for retry 0', () => {
+ expect(getTimeInMsBetweenRetries(0)).toBe(0);
+ });
+
+ it('should return more then 0 for the next retries', () => {
+ expect(getTimeInMsBetweenRetries(1)).toBeGreaterThan(0);
+ expect(getTimeInMsBetweenRetries(2)).toBeGreaterThan(0);
+ });
+
+ it('should return not go above 5 seconds', () => {
+ expect(getTimeInMsBetweenRetries(9)).toBe(3000);
+ expect(getTimeInMsBetweenRetries(10)).toBe(5000);
+ expect(getTimeInMsBetweenRetries(11)).toBe(5000);
+ expect(getTimeInMsBetweenRetries(100)).toBe(5000);
+ });
+});
diff --git a/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.ts b/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.ts
index 1d7df21696..adbc00cb16 100644
--- a/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.ts
+++ b/plugins/web-terminal/src/components/TerminalComponent/utils/helpers.ts
@@ -5,3 +5,19 @@ const OPENSHIFT_TERMINAL_DEFAULT_NAMESPACE = 'openshift-terminal';
export const getDefaultNamespace = (config: Config) =>
config.getOptionalString('webTerminal.defaultNamespace') ??
OPENSHIFT_TERMINAL_DEFAULT_NAMESPACE;
+
+const timeInMsBetweenRetries = [
+ 0, 100, 500, 1000, 1000, 1000, 2000, 2000, 2000, 3000, 5000,
+];
+
+export const getTimeInMsBetweenRetries = (retry: number) => {
+ return timeInMsBetweenRetries[
+ Math.min(retry, timeInMsBetweenRetries.length - 1)
+ ];
+};
+
+export const waitBetweenRetries = (retry: number) => {
+ return new Promise(resolve => {
+ setTimeout(() => resolve(), getTimeInMsBetweenRetries(retry));
+ });
+};
diff --git a/plugins/web-terminal/src/components/TerminalComponent/utils/index.ts b/plugins/web-terminal/src/components/TerminalComponent/utils/index.ts
index b42e526f3f..44f1b9ca57 100644
--- a/plugins/web-terminal/src/components/TerminalComponent/utils/index.ts
+++ b/plugins/web-terminal/src/components/TerminalComponent/utils/index.ts
@@ -1,2 +1,2 @@
export { createWorkspace, getWorkspace, getNamespaces } from './requests';
-export { getDefaultNamespace } from './helpers';
+export { getDefaultNamespace, waitBetweenRetries } from './helpers';
diff --git a/plugins/web-terminal/src/components/TerminalComponent/utils/requests.ts b/plugins/web-terminal/src/components/TerminalComponent/utils/requests.ts
index 59cf4ee35c..f2f3859786 100644
--- a/plugins/web-terminal/src/components/TerminalComponent/utils/requests.ts
+++ b/plugins/web-terminal/src/components/TerminalComponent/utils/requests.ts
@@ -83,7 +83,7 @@ export const getWorkspace = async (
},
);
const data = await response.json();
- return [data.status.devworkspaceId, data.status.phase];
+ return [data.status?.devworkspaceId, data.status?.phase];
};
export const getNamespaces = async (
diff --git a/plugins/web-terminal/src/index.ts b/plugins/web-terminal/src/index.ts
index a45bf62b9b..f62460949d 100644
--- a/plugins/web-terminal/src/index.ts
+++ b/plugins/web-terminal/src/index.ts
@@ -1 +1,2 @@
export { webTerminalPlugin, WebTerminal } from './plugin';
+export { isWebTerminalAvailable } from './components/TerminalComponent/utils/annotations';