forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod-name.ts
63 lines (47 loc) · 1.89 KB
/
pod-name.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
import {NodeStatus} from '../../models';
export const POD_NAME_V1 = 'v1';
export const POD_NAME_V2 = 'v2';
export const maxK8sResourceNameLength = 253;
export const k8sNamingHashLength = 10;
// getPodName returns a deterministic pod name. It returns a combination of the
// workflow name, template name, and a hash if the POD_NAME_V2 annotation is
// set. If the templateName or templateRef is not defined on a given node, it
// falls back to POD_NAME_V1
export const getPodName = (workflowName: string, nodeName: string, templateName: string, nodeID: string, version: string): string => {
if (version === POD_NAME_V2 && templateName !== '') {
if (workflowName === nodeName) {
return workflowName;
}
const prefix = ensurePodNamePrefixLength(`${workflowName}-${templateName}`);
const hash = createFNVHash(nodeName);
return `${prefix}-${hash}`;
}
return nodeID;
};
export const ensurePodNamePrefixLength = (prefix: string): string => {
const maxPrefixLength = maxK8sResourceNameLength - k8sNamingHashLength;
if (prefix.length > maxPrefixLength - 1) {
return prefix.substring(0, maxPrefixLength - 1);
}
return prefix;
};
export const createFNVHash = (input: string): number => {
const data = Buffer.from(input);
let hashint = 2166136261;
/* tslint:disable:no-bitwise */
for (const character of data) {
hashint = hashint ^ character;
hashint += (hashint << 1) + (hashint << 4) + (hashint << 7) + (hashint << 8) + (hashint << 24);
}
return hashint >>> 0;
};
export const getTemplateNameFromNode = (node: NodeStatus): string => {
if (node.templateName && node.templateName !== '') {
return node.templateName;
}
// fall back to v1 pod names if no templateName or templateRef defined
if (!node.templateRef) {
return '';
}
return node.templateRef.template;
};