This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
utils.js
182 lines (155 loc) · 4.95 KB
/
utils.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { isArray, mergeWith } from 'lodash';
import { getModelIndexId, NamespaceModel, ProjectModel } from '../models';
import {
NETWORK_BINDING_BRIDGE,
NETWORK_BINDING_MASQUERADE,
NETWORK_BINDING_SRIOV,
NETWORK_TYPE_MULTUS,
NETWORK_TYPE_POD,
} from '../components/Wizard/CreateVmWizard/constants';
import { getName, getNamespace } from '../selectors';
export function prefixedId(idPrefix, id) {
return idPrefix && id ? `${idPrefix}-${id}` : null;
}
export const getFullResourceId = obj => `${getModelIndexId(obj)}/${getNamespace(obj)}/${getName(obj)}`;
export const parseUrl = url => {
try {
return new URL(url);
} catch (e) {
return null;
}
};
export const parseNumber = (value, defaultValue = null) => {
const number = Number(value);
return !Number.isNaN(parseFloat(value)) && !Number.isNaN(number) ? number : defaultValue;
};
export const getSequence = (from, to) => Array.from({ length: to - from + 1 }, (v, i) => i + from);
export const setNativeValue = (element, value) => {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}
};
export const generateDiskName = (vmName, diskName, clone) => {
const name = [vmName, diskName];
if (clone) {
name.push('clone');
}
return name.join('-');
};
export const getBootDeviceIndex = (devices, bootOrder) => devices.findIndex(device => device.bootOrder === bootOrder);
export const getResource = (
model,
{
name,
namespaced = true,
namespace,
isList = true,
matchLabels,
matchExpressions,
prop,
fieldSelector,
optional,
} = {
namespaced: true,
isList: true,
}
) => {
const res = {
// non-admin user cannot list namespaces (k8s wont return only namespaces available to user but 403 forbidden, ).
// Instead we need to use ProjectModel which will return available projects (namespaces)
kind: model.kind === NamespaceModel.kind ? ProjectModel.kind : model.kind,
namespaced,
namespace,
isList,
prop: prop || model.kind,
optional,
};
if (name) {
res.name = name;
}
if (matchLabels) {
res.selector = { matchLabels };
}
if (matchExpressions) {
res.selector = { matchExpressions };
}
if (fieldSelector) {
res.fieldSelector = fieldSelector;
}
return res;
};
const BYTE_UNITS_CONFIG = {
conversion: 1024,
units: {
B: 0,
Ki: 1,
Mi: 2,
Gi: 3,
Ti: 4,
Pi: 5,
},
};
const NETWORK_BYTE_UNITS_CONFIG = {
conversion: 1000,
units: {
Bps: 0,
KBps: 1,
MBps: 2,
GBps: 3,
TBps: 4,
PBps: 5,
},
};
export const formatBytes = (bytes, unit, fixed = 2, conversionConfig = BYTE_UNITS_CONFIG) => {
const { units, conversion } = conversionConfig;
const unitKeys = Object.keys(units);
unit = unit || unitKeys.find(key => bytes < conversion ** (units[key] + 1)) || unitKeys[unitKeys.length - 1];
return { value: Number((bytes / conversion ** units[unit]).toFixed(fixed)), unit };
};
export const formatCores = cores => ({ value: cores, unit: 'cores' });
export const formatPercents = percents => ({ value: percents, unit: '%' });
export const formatNetTraffic = (bytesPerSecond, preferredUnit, fixed = 2) =>
formatBytes(bytesPerSecond, preferredUnit, fixed, NETWORK_BYTE_UNITS_CONFIG);
export const getNetworkBindings = networkType => {
switch (networkType) {
case NETWORK_TYPE_MULTUS:
return [NETWORK_BINDING_BRIDGE, NETWORK_BINDING_SRIOV];
case NETWORK_TYPE_POD:
default:
return [NETWORK_BINDING_MASQUERADE, NETWORK_BINDING_BRIDGE, NETWORK_BINDING_SRIOV];
}
};
export const getDefaultNetworkBinding = networkType => {
switch (networkType) {
case NETWORK_TYPE_MULTUS:
return NETWORK_BINDING_BRIDGE;
case NETWORK_TYPE_POD:
default:
return NETWORK_BINDING_MASQUERADE;
}
};
export const formatToShortTime = timestamp => {
const dt = new Date(timestamp);
// returns in HH:MM format
return dt.toString().substring(16, 21);
};
export const hasObjectTruthyValue = obj => !!(obj && !!Object.keys(obj).find(key => obj[key]));
// merge but keep only keys eligible for update (dest)
export const objectMerge = (dest, ...sources) =>
mergeWith(dest, ...sources, (objValue, srcValue) => {
if (isArray(objValue) || isArray(srcValue)) {
return srcValue;
}
return undefined;
});
// https://github.com/kubernetes/community/blob/master/contributors/design-proposals/scheduling/resources.md#resource-quantities
export const getValidK8SSize = (size, units, sizeUnit = 'Gi') => {
const baseSize = units.dehumanize(`${size}${sizeUnit}`, 'binaryBytesWithoutB');
return units.humanize(baseSize.value, 'binaryBytesWithoutB');
};
export const delay = ms => new Promise(resolve => setTimeout(resolve, ms));