-
Notifications
You must be signed in to change notification settings - Fork 705
/
utils.ts
406 lines (385 loc) · 13.9 KB
/
utils.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2018-2023 the Kubeapps contributors.
// SPDX-License-Identifier: Apache-2.0
import { Code } from "@connectrpc/connect";
import { proto3 } from "@bufbuild/protobuf";
import { InstalledPackageStatus_StatusReason } from "gen/kubeappsapis/core/packages/v1alpha1/packages_pb";
import { PackageRepositoryAuth_PackageRepositoryAuthType } from "gen/kubeappsapis/core/packages/v1alpha1/repositories_pb";
import { Plugin } from "gen/kubeappsapis/core/plugins/v1alpha1/plugins_pb";
import carvelIcon from "icons/carvel.svg";
import fluxIcon from "icons/flux.svg";
import helmIcon from "icons/helm.svg";
import olmIcon from "icons/olm-icon.svg";
import placeholder from "icons/placeholder.svg";
import { toNumber } from "lodash";
import { IConfig } from "./Config";
import {
BadRequestNetworkError,
ConflictNetworkError,
CustomError,
ForbiddenNetworkError,
GatewayTimeoutNetworkError,
InternalServerNetworkError,
NotFoundNetworkError,
NotImplementedNetworkError,
PluginNames,
RepositoryStorageTypes,
RequestTimeoutNetworkError,
ServerUnavailableNetworkError,
TooManyRequestsNetworkError,
UnauthorizedNetworkError,
} from "./types";
export const k8sObjectNameRegex = "[a-z0-9]([-a-z0-9]*[a-z0-9])?(.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*";
export const basicFormsDebounceTime = 500;
export function escapeRegExp(str: string) {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
}
export function getStringValue(value: any, type?: string) {
const usedType = type || typeof value;
let result = value?.toString();
if (["array", "object"].includes(usedType)) {
try {
result = JSON.stringify(value);
} catch (e) {
result = value?.toString();
}
}
return result || "";
}
export function getValueFromString(value: string, type?: string) {
const usedType = type || typeof value;
let result = value?.toString();
if (["array", "object"].includes(usedType)) {
try {
result = JSON.parse(value);
if (usedType === "object" && typeof result !== "object") {
result = value?.toString();
}
} catch (e) {
result = value?.toString();
}
}
return result;
}
export function getValueFromEvent(
e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>,
) {
const value: any = e.currentTarget.value;
switch (e.currentTarget.type) {
case "checkbox":
// value is a boolean
return value === "true";
case "number":
case "range":
// value is a number
return toNumber(value);
case "array":
return getValueFromString(value, "array");
case "object":
return getValueFromString(value, "object");
default:
return getValueFromString(value);
}
}
export function getOptionalMin(num1?: number, num2?: number) {
if (num1 && num2) {
return Math.min(num1, num2);
}
return num1 || num2 || undefined;
}
// 3 lines description max
export const MAX_DESC_LENGTH = 90;
export function trimDescription(desc: string): string {
if (desc.length > MAX_DESC_LENGTH) {
// Trim to the last word under the max length
return desc.substr(0, desc.lastIndexOf(" ", MAX_DESC_LENGTH)).concat("...");
}
return desc;
}
// Perhaps the logic of these functions should be provided by each plugin itself, namely:
// i) return its icon; ii) return its user-faced name; iii) return its user-faced package name
export function getPluginIcon(plugin?: Plugin | string) {
// Temporary case while operators are not supported as kubeapps apis plugin
if (typeof plugin === "string") {
switch (plugin) {
case "chart":
case "helm":
return helmIcon;
case "operator":
return olmIcon;
default:
return placeholder;
}
} else {
switch (plugin?.name) {
case PluginNames.PACKAGES_HELM:
return helmIcon;
case PluginNames.PACKAGES_FLUX:
return fluxIcon;
case PluginNames.PACKAGES_KAPP:
return carvelIcon;
default:
return placeholder;
}
}
}
export function getPluginName(plugin?: Plugin | string) {
// Temporary case while operators are not supported as kubeapps apis plugin
if (typeof plugin === "string") {
switch (plugin) {
case "chart":
return "Helm";
case "helm":
return "Helm";
case "operator":
return "Operator";
default:
return "unknown plugin";
}
} else {
switch (plugin?.name) {
case PluginNames.PACKAGES_HELM:
return "Helm";
case PluginNames.PACKAGES_FLUX:
return "Flux";
case PluginNames.PACKAGES_KAPP:
return "Carvel";
default:
return plugin?.name;
}
}
}
export function getPluginPackageName(plugin?: Plugin | PluginNames | string, plural = false) {
// Temporary case while operators are not supported as kubeapps apis plugin
if (typeof plugin === "string") {
switch (plugin) {
case "chart":
case "helm":
case PluginNames.PACKAGES_HELM:
return plural ? "Helm Charts" : "Helm Chart";
case PluginNames.PACKAGES_FLUX:
return plural ? "Helm Charts via Flux" : "Helm Chart via Flux";
case PluginNames.PACKAGES_KAPP:
return plural ? "Carvel Packages" : "Carvel Package";
case "operator":
return plural ? "Operators" : "Operator";
default:
return `unknown plugin ${plural ? "packages" : "package"}`;
}
} else {
switch (plugin?.name) {
case PluginNames.PACKAGES_HELM:
return plural ? "Helm Charts" : "Helm Chart";
case PluginNames.PACKAGES_FLUX:
return plural ? "Helm Charts via Flux" : "Helm Chart via Flux";
case PluginNames.PACKAGES_KAPP:
return plural ? "Carvel Packages" : "Carvel Package";
default:
return `${plugin?.name ? plugin.name : "unknown"} ${plural ? "packages" : "package"}`;
}
}
}
// TODO(agamez): replace with a proper call to the plugins server (see getPluginsServiceClientImpl)
export function getPluginByName(pluginName: PluginNames | string) {
switch (pluginName) {
case PluginNames.PACKAGES_HELM:
return {
name: PluginNames.PACKAGES_HELM,
version: "v1alpha1",
} as Plugin;
case PluginNames.PACKAGES_FLUX:
return {
name: PluginNames.PACKAGES_FLUX,
version: "v1alpha1",
} as Plugin;
case PluginNames.PACKAGES_KAPP:
return {
name: PluginNames.PACKAGES_KAPP,
version: "v1alpha1",
} as Plugin;
default:
return {
name: "",
version: "",
} as Plugin;
}
}
export function getPluginsAllowingSA(): string[] {
return [PluginNames.PACKAGES_FLUX, PluginNames.PACKAGES_KAPP];
}
// getPluginsRequiringSA should return a subset of getPluginsAllowingSA
export function getPluginsRequiringSA(): string[] {
return [PluginNames.PACKAGES_KAPP];
}
export function getPluginsSupportingRollback(): string[] {
return [PluginNames.PACKAGES_HELM];
}
export function getAppStatusLabel(
statusReason: InstalledPackageStatus_StatusReason = InstalledPackageStatus_StatusReason.UNSPECIFIED,
): string {
const statusReasonName = proto3
.getEnumType(InstalledPackageStatus_StatusReason)
.findNumber(statusReason)!.name;
return statusReasonName.toString().replace("STATUS_REASON_", "").toLowerCase();
}
export function getSupportedPackageRepositoryAuthTypes(
plugin: Plugin,
type?: string,
): PackageRepositoryAuth_PackageRepositoryAuthType[] {
switch (plugin.name) {
case PluginNames.PACKAGES_HELM:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.AUTHORIZATION_HEADER,
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.BEARER,
PackageRepositoryAuth_PackageRepositoryAuthType.DOCKER_CONFIG_JSON,
];
case PluginNames.PACKAGES_FLUX:
switch (type) {
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_HELM:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.TLS,
];
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_OCI:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.DOCKER_CONFIG_JSON,
];
default:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.TLS,
];
}
case PluginNames.PACKAGES_KAPP:
// the available auth options in Carvel are type-specific
// extracted from https://github.com/vmware-tanzu/carvel-kapp-controller/blob/v0.40.0/pkg/apis/kappctrl/v1alpha1/types_fetch.go
// by looking for "Secret may include one"
// also see https://carvel.dev/kapp-controller/docs/v0.43.2/app-overview/#specfetch
switch (type) {
// "Secret with auth details. allowed keys: ssh-privatekey, ssh-knownhosts, username, password"
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_CARVEL_GIT:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.SSH,
];
// "Secret may include one or more keys: username, password"
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_CARVEL_HTTP:
return [PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH];
// "Secret may include one or more keys: username, password, token"
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_CARVEL_IMAGE:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.BEARER,
PackageRepositoryAuth_PackageRepositoryAuthType.DOCKER_CONFIG_JSON,
];
// "Secret may include one or more keys: username, password, token"
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_CARVEL_IMGPKGBUNDLE:
return [
PackageRepositoryAuth_PackageRepositoryAuthType.BASIC_AUTH,
PackageRepositoryAuth_PackageRepositoryAuthType.BEARER,
PackageRepositoryAuth_PackageRepositoryAuthType.DOCKER_CONFIG_JSON,
];
// inline is not supported for write
case RepositoryStorageTypes.PACKAGE_REPOSITORY_STORAGE_CARVEL_INLINE:
return [];
default:
return [];
}
default:
return [];
}
}
/**
* Check if namespace is global depending on the plugin
*
* @param namespace Namespace to check
* @param pluginName Plugin name
* @param kubeappsConfig Kubeapps configuration
* @returns true if namespace is global
*/
export function isGlobalNamespace(namespace: string, pluginName: string, kubeappsConfig: IConfig) {
switch (pluginName) {
case PluginNames.PACKAGES_HELM:
return namespace === kubeappsConfig.helmGlobalNamespace;
case PluginNames.PACKAGES_KAPP:
return namespace === kubeappsConfig.carvelGlobalNamespace;
// Currently, Flux doesn't support global repositories
case PluginNames.PACKAGES_FLUX:
return false;
default:
return false;
}
}
export function getGlobalNamespaceOrNamespace(
namespace: string,
pluginName: string,
kubeappsConfig: IConfig,
) {
switch (pluginName) {
case PluginNames.PACKAGES_HELM:
return kubeappsConfig.helmGlobalNamespace;
case PluginNames.PACKAGES_KAPP:
return kubeappsConfig.carvelGlobalNamespace;
// Currently, Flux doesn't support global repositories, so returning the namespace so we have a value
case PluginNames.PACKAGES_FLUX:
return namespace;
default:
return "unknown";
}
}
// Using the mapping from GRPC and grpc-gateway
// See https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
// See https://github.com/grpc-ecosystem/grpc-gateway/blob/master/runtime/errors.go
export function convertGrpcAuthError(e: any): CustomError | any {
const msg = (e?.metadata?.get("grpc-message") || "").toString();
switch (e?.code) {
case Code.Unauthenticated:
return new UnauthorizedNetworkError(msg);
case Code.FailedPrecondition:
// Use `FAILED_PRECONDITION` if the client should not retry until the system state has been explicitly fixed.
//TODO(agamez): this code shouldn't be returned by the API, but it is
if (["credentials", "unauthorized"].some(p => msg?.toLowerCase()?.includes(p))) {
return new UnauthorizedNetworkError(msg);
} else {
return new BadRequestNetworkError(msg);
}
case Code.Internal:
//TODO(agamez): this code shouldn't be returned by the API, but it is
if (["credentials", "unauthorized"].some(p => msg?.toLowerCase()?.includes(p))) {
return new UnauthorizedNetworkError(msg);
} else {
return new InternalServerNetworkError(msg);
}
case Code.PermissionDenied:
return new ForbiddenNetworkError(msg);
case Code.NotFound:
return new NotFoundNetworkError(msg);
case Code.AlreadyExists:
return new ConflictNetworkError(msg);
case Code.InvalidArgument:
return new BadRequestNetworkError(msg);
case Code.DeadlineExceeded:
return new GatewayTimeoutNetworkError(msg);
case Code.ResourceExhausted:
return new TooManyRequestsNetworkError(msg);
case Code.Aborted:
// Use `ABORTED` if the client should retry at a higher level
return new ConflictNetworkError(msg);
case Code.Unimplemented:
return new NotImplementedNetworkError(msg);
case Code.OutOfRange:
return new BadRequestNetworkError(msg);
case Code.Unavailable:
// Use `UNAVAILABLE` if the client can retry just the failing call.
return new ServerUnavailableNetworkError(msg);
case Code.DataLoss:
return new InternalServerNetworkError(msg);
case Code.Unknown:
return new InternalServerNetworkError(msg);
case Code.Canceled:
return new RequestTimeoutNetworkError(msg);
default:
return e;
}
}