forked from skupperproject/openshift-site-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeploymentNetworkConsoleButton.tsx
More file actions
123 lines (104 loc) · 3.32 KB
/
DeploymentNetworkConsoleButton.tsx
File metadata and controls
123 lines (104 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useEffect, useState } from 'react';
import { RESTApi } from '@API/REST.api';
import { I18nNamespace } from '@config/config';
import { NamespaceManager } from '@config/db';
import ExternalLink from '@core/components/ExternalLink';
import { K8sResourceCommon, useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk';
import { Button, Flex } from '@patternfly/react-core';
import { CubesIcon } from '@patternfly/react-icons';
import { useMutation } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
const groupVersionKind = {
group: 'route.openshift.io',
version: 'v1',
kind: 'Route'
};
const groupVersionKindPod = {
group: '',
version: 'v1',
kind: 'Pod'
};
interface RouteResource extends K8sResourceCommon {
spec?: {
host?: string;
port?: {
targetPort?: string;
};
};
status?: {
ingress?: unknown[];
};
}
interface PodResource extends K8sResourceCommon {
status?: {
phase?: string;
};
}
const ROUTE = 'network-observer';
const POD_SELECTOR = { 'app.kubernetes.io/name': 'network-observer' };
const POD_LOADED_STATUS = 'Running';
const DeploymentNetworkConsoleButton = function () {
const { t } = useTranslation(I18nNamespace);
const [url, setUrl] = useState<string | undefined>();
const watchResource = {
groupVersionKind,
namespace: NamespaceManager.getNamespace(),
isList: true,
name: ROUTE
};
const watchResourcePod = {
groupVersionKind: groupVersionKindPod,
namespace: NamespaceManager.getNamespace(),
isList: false,
selector: {
matchLabels: POD_SELECTOR
}
};
const [route] = useK8sWatchResource<RouteResource[]>(watchResource);
const [deployment] = useK8sWatchResource<PodResource>(watchResourcePod);
const mutationCreate = useMutation({
mutationFn: () => RESTApi.createDeployment()
});
const mutationDelete = useMutation({
mutationFn: () => RESTApi.deleteDeployment(),
onSuccess: () => {
setUrl(undefined);
}
});
const handleDeployConsole = async () => {
mutationCreate.mutate();
};
const handleDeleteConsole = async () => {
mutationDelete.mutate();
};
useEffect(() => {
console.log('route', route);
const data = route?.find((r) => r.metadata?.name?.includes(ROUTE));
if (data?.spec?.host && data?.spec?.port?.targetPort) {
const newUrl = data?.spec?.host ? `${data?.spec?.port?.targetPort}://${data?.spec?.host}` : undefined;
setUrl(newUrl);
}
}, [route]);
const loaded = deployment?.status?.phase === POD_LOADED_STATUS && url;
return (
<Flex justifyContent={{ default: 'justifyContentSpaceBetween' }}>
{!loaded && (
<Button
isDisabled={!!url && !!deployment?.status && !(deployment?.status?.phase === POD_LOADED_STATUS)}
onClick={handleDeployConsole}
isLoading={!!url && !!deployment?.status && !(deployment?.status?.phase === POD_LOADED_STATUS)}
icon={<CubesIcon />}
>
{t('Deploy the Network Console')}
</Button>
)}
{loaded && <ExternalLink href={url} text={t('Open the Network Console')} />}
{loaded && (
<Button onClick={handleDeleteConsole} variant="secondary" icon={<CubesIcon />}>
{t('Delete the Network Console')}
</Button>
)}
</Flex>
);
};
export default DeploymentNetworkConsoleButton;