Skip to content

Commit f427375

Browse files
feat(Tenants): correct links with relative path in balancer (#2125)
1 parent 9e7a235 commit f427375

File tree

11 files changed

+36
-126
lines changed

11 files changed

+36
-126
lines changed

src/components/TenantNameWrapper/TenantNameWrapper.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {DefinitionList, Flex} from '@gravity-ui/uikit';
22

33
import {getTenantPath} from '../../containers/Tenant/TenantPages';
44
import type {PreparedTenant} from '../../store/reducers/tenants/types';
5-
import type {AdditionalTenantsProps, NodeAddress} from '../../types/additionalProps';
5+
import type {AdditionalTenantsProps} from '../../types/additionalProps';
66
import {useIsUserAllowedToMakeChanges} from '../../utils/hooks/useIsUserAllowedToMakeChanges';
77
import {EntityStatus} from '../EntityStatus/EntityStatus';
88
import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon';
@@ -22,13 +22,13 @@ const getTenantBackend = (
2222
return undefined;
2323
}
2424

25-
let backend: string | NodeAddress | undefined = tenant.MonitoringEndpoint ?? tenant.backend;
25+
let nodeId: string | undefined;
2626
const nodeIds = tenant.NodeIds ?? tenant.sharedNodeIds;
27-
if (!backend && nodeIds && nodeIds.length > 0) {
27+
if (nodeIds && nodeIds.length > 0) {
2828
const index = Math.floor(Math.random() * nodeIds.length);
29-
backend = {NodeId: nodeIds[index]};
29+
nodeId = nodeIds[index].toString();
3030
}
31-
return additionalTenantsProps.prepareTenantBackend(backend);
31+
return additionalTenantsProps.prepareTenantBackend(nodeId);
3232
};
3333

3434
export function TenantNameWrapper({tenant, additionalTenantsProps}: TenantNameWrapperProps) {

src/containers/AppWithClusters/ExtendedCluster/ExtendedCluster.tsx

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {ClipboardButton} from '@gravity-ui/uikit';
2+
import {isNil} from 'lodash';
23

34
import {useClusterBaseInfo} from '../../../store/reducers/cluster/cluster';
4-
import type {
5-
AdditionalClusterProps,
6-
AdditionalTenantsProps,
7-
NodeAddress,
8-
} from '../../../types/additionalProps';
5+
import type {AdditionalClusterProps, AdditionalTenantsProps} from '../../../types/additionalProps';
96
import type {ETenantType} from '../../../types/api/tenant';
107
import {cn} from '../../../utils/cn';
118
import {USE_CLUSTER_BALANCER_AS_BACKEND_KEY} from '../../../utils/constants';
@@ -16,8 +13,8 @@ import type {
1613
GetMonitoringClusterLink,
1714
GetMonitoringLink,
1815
} from '../../../utils/monitoring';
19-
import {getCleanBalancerValue, removeViewerPathname} from '../../../utils/parseBalancer';
20-
import {getBackendFromNodeHost, getBackendFromRawNodeData} from '../../../utils/prepareBackend';
16+
import {getCleanBalancerValue, prepareBackendFromBalancer} from '../../../utils/parseBalancer';
17+
import {getBackendFromBalancerAndNodeId} from '../../../utils/prepareBackend';
2118
import type {Cluster} from '../../Cluster/Cluster';
2219

2320
import './ExtendedCluster.scss';
@@ -89,27 +86,21 @@ const getAdditionalTenantsProps = ({
8986
}: GetAdditionalTenantsProps) => {
9087
const additionalTenantsProps: AdditionalTenantsProps = {};
9188

92-
additionalTenantsProps.prepareTenantBackend = (
93-
nodeHostOrAddress: string | NodeAddress | undefined,
94-
) => {
95-
// Proxy received from balancer value, so it's necessary
89+
additionalTenantsProps.prepareTenantBackend = (nodeId) => {
90+
// Balancer value is used to create path, so it's necessary
9691
if (!balancer) {
9792
return undefined;
9893
}
9994

10095
if (useClusterBalancerAsBackend) {
101-
return removeViewerPathname(balancer);
96+
return prepareBackendFromBalancer(balancer);
10297
}
10398

104-
if (!nodeHostOrAddress) {
99+
if (isNil(nodeId)) {
105100
return undefined;
106101
}
107102

108-
if (typeof nodeHostOrAddress === 'string') {
109-
return getBackendFromNodeHost(nodeHostOrAddress, balancer);
110-
}
111-
112-
return getBackendFromRawNodeData(nodeHostOrAddress, balancer, true) ?? undefined;
103+
return getBackendFromBalancerAndNodeId(nodeId, balancer) ?? undefined;
113104
};
114105

115106
if (monitoring && getMonitoringLink) {

src/store/reducers/tenants/tenants.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {createSlice} from '@reduxjs/toolkit';
22
import type {PayloadAction} from '@reduxjs/toolkit';
33

4-
import type {RootState} from '../../defaultStore';
54
import {api} from '../api';
65

76
import type {PreparedTenant, TenantsState} from './types';
@@ -25,15 +24,14 @@ export default slice.reducer;
2524
export const tenantsApi = api.injectEndpoints({
2625
endpoints: (build) => ({
2726
getTenantsInfo: build.query({
28-
queryFn: async ({clusterName}: {clusterName?: string}, {signal, getState}) => {
27+
queryFn: async ({clusterName}: {clusterName?: string}, {signal}) => {
2928
try {
3029
const response = window.api.meta
3130
? await window.api.meta.getTenants(clusterName, {signal})
3231
: await window.api.viewer.getTenants(clusterName, {signal});
3332
let data: PreparedTenant[];
3433
if (Array.isArray(response.TenantInfo)) {
35-
const {singleClusterMode} = getState() as RootState;
36-
data = prepareTenants(response.TenantInfo, singleClusterMode);
34+
data = prepareTenants(response.TenantInfo);
3735
} else {
3836
data = [];
3937
}

src/store/reducers/tenants/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {ValueOf} from '../../../types/common';
44
import type {METRIC_STATUS} from './contants';
55

66
export interface PreparedTenant extends TTenant {
7-
backend: string | undefined;
87
sharedTenantName: string | undefined;
98
sharedNodeIds: number[] | undefined;
109
controlPlaneName: string;

src/store/reducers/tenants/utils.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ const getControlPlaneValue = (tenant: TTenant) => {
1515
return controlPlaneName ?? defaultValue;
1616
};
1717

18-
const getTenantBackend = (tenant: TTenant) => {
19-
const node = tenant.Nodes ? tenant.Nodes[0] : {};
20-
const address =
21-
node.Host && node.Endpoints
22-
? node.Endpoints.find((endpoint) => endpoint.Name === 'http-mon')?.Address
23-
: undefined;
24-
return node.Host ? `${node.Host}${address ? address : ''}` : undefined;
25-
};
26-
2718
export interface TenantMetricStats<T = string> {
2819
name: T;
2920
usage?: number;
@@ -174,9 +165,8 @@ const calculateTenantEntities = (tenant: TTenant) => {
174165
return {nodesCount, groupsCount};
175166
};
176167

177-
export const prepareTenants = (tenants: TTenant[], useNodeAsBackend: boolean): PreparedTenant[] => {
168+
export const prepareTenants = (tenants: TTenant[]): PreparedTenant[] => {
178169
return tenants.map((tenant) => {
179-
const backend = useNodeAsBackend ? getTenantBackend(tenant) : undefined;
180170
const sharedDatabase = tenants.find((item) => item.Id === tenant.ResourceId);
181171
const sharedTenantName = sharedDatabase?.Name;
182172
const sharedNodeIds = sharedDatabase?.NodeIds;
@@ -187,7 +177,6 @@ export const prepareTenants = (tenants: TTenant[], useNodeAsBackend: boolean): P
187177
return {
188178
...tenant,
189179

190-
backend,
191180
sharedTenantName,
192181
sharedNodeIds,
193182
controlPlaneName,

src/types/additionalProps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface AdditionalClusterProps {
1313
}
1414

1515
export interface AdditionalTenantsProps {
16-
prepareTenantBackend?: (backend: string | NodeAddress | undefined) => string | undefined;
16+
prepareTenantBackend?: (nodeId?: string | number) => string | undefined;
1717
getMonitoringLink?: (name?: string, type?: ETenantType) => string | null;
1818
getLogsLink?: (name?: string) => string | null;
1919
}

src/types/api/tenant.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {EFlag} from './enums';
2-
import type {TMemoryStats, TPoolStats, TSystemStateInfo} from './nodes';
2+
import type {TMemoryStats, TPoolStats} from './nodes';
33
import type {TTabletStateInfo} from './tablet';
44

55
/**
@@ -45,7 +45,6 @@ export interface TTenant {
4545
StorageAllocatedSize?: string; // Actual database size
4646
/** uint64 */
4747
StorageMinAvailableSize?: string;
48-
Nodes?: TSystemStateInfo[];
4948
/** uint64 */
5049
MemoryUsed?: string; // Actual memory consumption
5150
/** uint64 */
@@ -56,7 +55,6 @@ export interface TTenant {
5655
/** uint64 */
5756
StorageGroups?: string;
5857

59-
MonitoringEndpoint?: string; // additional
6058
ControlPlane?: ControlPlane; // additional
6159

6260
StorageAllocatedLimit?: string;
+6-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getBackendFromNodeHost, getBackendFromRawNodeData, prepareHost} from '../prepareBackend';
1+
import {getBackendFromBalancerAndNodeId, prepareHost} from '../prepareBackend';
22

33
describe('prepareHost', () => {
44
test('should add u- prefix to cloud din nodes', () => {
@@ -13,37 +13,12 @@ describe('prepareHost', () => {
1313
expect(prepareHost(commonNodeHost)).toBe(commonNodeHost);
1414
});
1515
});
16-
describe('getBackendFromNodeHost', () => {
17-
test('should prepare correct backend value from node host', () => {
16+
describe('getBackendFromBalancerAndNodeId', () => {
17+
test('should prepare correct backend from balancer and node id', () => {
1818
const balancer = 'https://viewer.ydb.ru:443/dev02.ydb.net:8765';
19-
const nodeHost = 'ydb-dev02-001.search.net:31012';
20-
const result = 'https://viewer.ydb.ru:443/ydb-dev02-001.search.net:31012';
19+
const nodeId = '50016';
20+
const result = 'https://viewer.ydb.ru:443/dev02.ydb.net:8765/node/50016';
2121

22-
expect(getBackendFromNodeHost(nodeHost, balancer)).toBe(result);
23-
});
24-
});
25-
describe('getBackendFromRawNodeData', () => {
26-
test('should prepare correct backend value from raw node data', () => {
27-
const balancer = 'https://viewer.ydb.ru:443/dev02.ydb.net:8765';
28-
const node = {
29-
Host: 'ydb-dev02-000.search.net',
30-
Endpoints: [
31-
{
32-
Name: 'http-mon',
33-
Address: ':8765',
34-
},
35-
{
36-
Name: 'ic',
37-
Address: ':19001',
38-
},
39-
{
40-
Name: 'grpc',
41-
Address: ':2135',
42-
},
43-
],
44-
};
45-
const result = 'https://viewer.ydb.ru:443/ydb-dev02-000.search.net:8765';
46-
47-
expect(getBackendFromRawNodeData(node, balancer)).toBe(result);
22+
expect(getBackendFromBalancerAndNodeId(nodeId, balancer)).toBe(result);
4823
});
4924
});

src/utils/additionalProps.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import {backend} from '../store';
2-
import type {AdditionalNodesProps, NodeAddress} from '../types/additionalProps';
2+
import type {AdditionalNodesProps} from '../types/additionalProps';
33

4-
import {getBackendFromRawNodeData} from './prepareBackend';
4+
import {getBackendFromBalancerAndNodeId} from './prepareBackend';
55

6-
export const getAdditionalNodesProps = (
7-
balancer = backend,
8-
useClusterBalancerAsBackend?: boolean,
9-
): AdditionalNodesProps => {
6+
export const getAdditionalNodesProps = (balancer = backend): AdditionalNodesProps => {
107
return {
11-
getNodeRef: (node: NodeAddress = {}) =>
12-
getBackendFromRawNodeData(node, balancer ?? '', useClusterBalancerAsBackend),
8+
getNodeRef: (node) => getBackendFromBalancerAndNodeId(node?.NodeId, balancer ?? ''),
139
};
1410
};
+1-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import {useClusterBaseInfo} from '../../store/reducers/cluster/cluster';
22
import {getAdditionalNodesProps} from '../additionalProps';
3-
import {USE_CLUSTER_BALANCER_AS_BACKEND_KEY} from '../constants';
43

5-
import {useSetting} from './useSetting';
64
import {useTypedSelector} from './useTypedSelector';
75

86
/** For multi-cluster version */
97
export function useAdditionalNodesProps() {
108
const {balancer} = useClusterBaseInfo();
11-
const [useClusterBalancerAsBackend] = useSetting<boolean>(USE_CLUSTER_BALANCER_AS_BACKEND_KEY);
129
const singleClusterMode = useTypedSelector((state) => state.singleClusterMode);
1310

14-
const additionalNodesProps = getAdditionalNodesProps(balancer, useClusterBalancerAsBackend);
11+
const additionalNodesProps = getAdditionalNodesProps(balancer);
1512

1613
return singleClusterMode ? undefined : additionalNodesProps;
1714
}

src/utils/prepareBackend.ts

+6-39
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,17 @@
1-
import type {NodeAddress} from '../types/additionalProps';
1+
import {prepareBackendFromBalancer} from './parseBalancer';
22

3-
import {parseBalancer, removeViewerPathname} from './parseBalancer';
4-
5-
const https = 'https://';
3+
import {valueIsDefined} from '.';
64

75
export const prepareHost = (host?: string) => {
86
// add "u-" prefix to cloud din nodes
97
return host?.startsWith('vm-') ? `u-${host}` : host;
108
};
119

12-
export const getBackendFromNodeHost = (nodeHost: string, balancer: string) => {
13-
const preparedHost = prepareHost(nodeHost);
14-
const proxy = parseBalancer(balancer).proxy;
15-
16-
if (proxy) {
17-
return https + proxy + '/' + preparedHost;
18-
}
19-
20-
return https + preparedHost;
21-
};
22-
2310
/** For multi-cluster version */
24-
export const getBackendFromRawNodeData = (
25-
node: NodeAddress,
26-
balancer: string,
27-
useBalancerAsBackend?: boolean,
28-
) => {
29-
const {Host, Endpoints, NodeId} = node;
30-
31-
if (useBalancerAsBackend && NodeId) {
32-
const preparedBalancer = removeViewerPathname(balancer);
33-
return `${preparedBalancer}/node/${NodeId}`;
34-
}
35-
36-
if (Host && Endpoints) {
37-
const nodePort = Endpoints.find((endpoint) => endpoint.Name === 'http-mon')?.Address;
38-
39-
if (!nodePort || !Host) {
40-
return undefined;
41-
}
42-
43-
const hostWithPort = Host + nodePort;
44-
45-
// Currently this func is used to get link to developerUI for specific node
46-
// It's expected with / at the end (code in embedded version)
47-
return getBackendFromNodeHost(hostWithPort, balancer);
11+
export const getBackendFromBalancerAndNodeId = (nodeId?: string | number, balancer?: string) => {
12+
if (valueIsDefined(nodeId) && valueIsDefined(balancer)) {
13+
const preparedBalancer = prepareBackendFromBalancer(balancer);
14+
return `${preparedBalancer}/node/${nodeId}`;
4815
}
4916

5017
return undefined;

0 commit comments

Comments
 (0)