Skip to content

Commit 99d1109

Browse files
JPZ13sarabala1979
authored andcommitted
chore(ui): Move pod name functions and add tests. Fixes argoproj#6946 (argoproj#6954)
Signed-off-by: J.P. Zivalich <[email protected]>
1 parent 74182fb commit 99d1109

File tree

7 files changed

+84
-49
lines changed

7 files changed

+84
-49
lines changed

ui/src/app/archived-workflows/components/archived-workflow-details/archived-workflow-details.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {ResourceEditor} from '../../../shared/components/resource-editor/resourc
1212
import {services} from '../../../shared/services';
1313
import {WorkflowArtifacts} from '../../../workflows/components/workflow-artifacts';
1414

15-
import {Utils} from '../../../shared/utils';
15+
import {getPodName} from '../../../shared/pod-name';
1616
import {WorkflowResourcePanel} from '../../../workflows/components/workflow-details/workflow-resource-panel';
1717
import {WorkflowLogsViewer} from '../../../workflows/components/workflow-logs-viewer/workflow-logs-viewer';
1818
import {WorkflowNodeInfo} from '../../../workflows/components/workflow-node-info/workflow-node-info';
@@ -253,7 +253,7 @@ export class ArchivedWorkflowDetails extends BasePage<RouteComponentProps<any>,
253253
if (this.nodeId && this.state.workflow) {
254254
const workflowName = this.state.workflow.metadata.name;
255255
const {name, templateName} = this.node;
256-
return Utils.getPodName(workflowName, name, templateName, this.nodeId);
256+
return getPodName(workflowName, name, templateName, this.nodeId);
257257
}
258258
}
259259

ui/src/app/shared/pod-name.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {createFNVHash, ensurePodNamePrefixLength, getPodName, k8sNamingHashLength, maxK8sResourceNameLength} from './pod-name';
2+
3+
describe('pod names', () => {
4+
test('createFNVHash', () => {
5+
expect(createFNVHash('hello')).toEqual(1335831723);
6+
expect(createFNVHash('world')).toEqual(933488787);
7+
expect(createFNVHash('You cannot alter your fate. However, you can rise to meet it.')).toEqual(827171719);
8+
});
9+
10+
const nodeName = 'nodename';
11+
const nodeID = '1';
12+
13+
const shortWfName = 'wfname';
14+
const shortTemplateName = 'templatename';
15+
16+
const longWfName = 'alongworkflownamethatincludeslotsofdetailsandisessentiallyalargerunonsentencewithpoorstyleandnopunctuationtobehadwhatsoever';
17+
const longTemplateName =
18+
'alongtemplatenamethatincludessliightlymoredetailsandiscertainlyalargerunonstnencewithevenworsestylisticconcernsandpreposterouslyeliminatespunctuation';
19+
20+
test('ensurePodNamePrefixLength', () => {
21+
let expected = `${shortWfName}-${shortTemplateName}`;
22+
expect(ensurePodNamePrefixLength(expected)).toEqual(expected);
23+
24+
expected = `${longWfName}-${longTemplateName}`;
25+
const actual = ensurePodNamePrefixLength(expected);
26+
expect(actual.length).toEqual(maxK8sResourceNameLength - k8sNamingHashLength - 1);
27+
});
28+
29+
test('getPodName', () => {
30+
expect(getPodName(shortWfName, nodeName, shortTemplateName, nodeID)).toEqual('wfname-templatename-1454367246');
31+
32+
const name = getPodName(longWfName, nodeName, longTemplateName, nodeID);
33+
expect(name.length).toEqual(maxK8sResourceNameLength);
34+
});
35+
});

ui/src/app/shared/pod-name.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export const maxK8sResourceNameLength = 253;
2+
export const k8sNamingHashLength = 10;
3+
4+
// getPodName returns a deterministic pod name
5+
export const getPodName = (workflowName: string, nodeName: string, templateName: string, nodeID: string): string => {
6+
if (workflowName === nodeName) {
7+
return workflowName;
8+
}
9+
10+
let prefix = `${workflowName}-${templateName}`;
11+
prefix = ensurePodNamePrefixLength(prefix);
12+
13+
const hash = createFNVHash(nodeName);
14+
return `${prefix}-${hash}`;
15+
};
16+
17+
export const ensurePodNamePrefixLength = (prefix: string): string => {
18+
const maxPrefixLength = maxK8sResourceNameLength - k8sNamingHashLength;
19+
20+
if (prefix.length > maxPrefixLength - 1) {
21+
return prefix.substring(0, maxPrefixLength - 1);
22+
}
23+
24+
return prefix;
25+
};
26+
27+
export const createFNVHash = (input: string): number => {
28+
const data = Buffer.from(input);
29+
30+
let hashint = 2166136261;
31+
32+
/* tslint:disable:no-bitwise */
33+
for (const character of data) {
34+
hashint = hashint ^ character;
35+
hashint += (hashint << 1) + (hashint << 4) + (hashint << 7) + (hashint << 8) + (hashint << 24);
36+
}
37+
38+
return hashint >>> 0;
39+
};

ui/src/app/shared/utils.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import {NODE_PHASE} from '../../models';
44

55
const managedNamespaceKey = 'managedNamespace';
66
const currentNamespaceKey = 'current_namespace';
7-
const maxK8sResourceNameLength = 253;
8-
const k8sNamingHashLength = 10;
97

108
export const Utils = {
119
statusIconClasses(status: string): string {
@@ -120,42 +118,5 @@ export const Utils = {
120118
// return a namespace, never return null/undefined, defaults to "default"
121119
getNamespaceWithDefault(namespace: string) {
122120
return this.managedNamespace || namespace || this.currentNamespace || 'default';
123-
},
124-
125-
ensurePodNamePrefixLength(prefix: string): string {
126-
const maxPrefixLength = maxK8sResourceNameLength - k8sNamingHashLength;
127-
128-
if (prefix.length > maxPrefixLength - 1) {
129-
return prefix.substring(0, maxPrefixLength - 1);
130-
}
131-
132-
return prefix;
133-
},
134-
135-
// getPodName returns a deterministic pod name
136-
getPodName(workflowName: string, nodeName: string, templateName: string, nodeID: string): string {
137-
if (workflowName === nodeName) {
138-
return workflowName;
139-
}
140-
141-
let prefix = `${workflowName}-${templateName}`;
142-
prefix = this.ensurePodNamePrefixLength(prefix);
143-
144-
const hash = createFNVHash(nodeName);
145-
return `${prefix}-${hash}`;
146-
}
147-
};
148-
149-
const createFNVHash = (input: string): number => {
150-
const data = new Buffer(input);
151-
152-
let hashint = 2166136261;
153-
154-
/* tslint:disable:no-bitwise */
155-
for (const character of data) {
156-
hashint = hashint ^ character;
157-
hashint += (hashint << 1) + (hashint << 4) + (hashint << 7) + (hashint << 8) + (hashint << 24);
158121
}
159-
160-
return hashint >>> 0;
161122
};

ui/src/app/workflows/components/workflow-details/workflow-details.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import {SecurityNudge} from '../../../shared/components/security-nudge';
1313
import {hasWarningConditionBadge} from '../../../shared/conditions-panel';
1414
import {Context} from '../../../shared/context';
1515
import {historyUrl} from '../../../shared/history';
16+
import {getPodName} from '../../../shared/pod-name';
1617
import {RetryWatch} from '../../../shared/retry-watch';
1718
import {services} from '../../../shared/services';
1819
import {useQueryParams} from '../../../shared/use-query-params';
19-
import {Utils} from '../../../shared/utils';
2020
import * as Operations from '../../../shared/workflow-operations-map';
2121
import {WorkflowOperations} from '../../../shared/workflow-operations-map';
2222
import {WidgetGallery} from '../../../widgets/widget-gallery';
@@ -224,16 +224,16 @@ export const WorkflowDetails = ({history, location, match}: RouteComponentProps<
224224
document.location.href = url;
225225
}
226226
};
227-
const getPodName = (wf: Workflow, node: NodeStatus, nodeID: string): string => {
227+
const ensurePodName = (wf: Workflow, node: NodeStatus, nodeID: string): string => {
228228
if (workflow && node) {
229-
return Utils.getPodName(wf.metadata.name, node.name, node.templateName, node.id);
229+
return getPodName(wf.metadata.name, node.name, node.templateName, node.id);
230230
}
231231

232232
return nodeID;
233233
};
234234

235235
const selectedNode = workflow && workflow.status && workflow.status.nodes && workflow.status.nodes[nodeId];
236-
const podName = getPodName(workflow, selectedNode, nodeId);
236+
const podName = ensurePodName(workflow, selectedNode, nodeId);
237237

238238
return (
239239
<Page

ui/src/app/workflows/components/workflow-logs-viewer/workflow-logs-viewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {execSpec} from '../../../../models';
88
import {ErrorNotice} from '../../../shared/components/error-notice';
99
import {InfoIcon, WarningIcon} from '../../../shared/components/fa-icons';
1010
import {Links} from '../../../shared/components/links';
11+
import {getPodName} from '../../../shared/pod-name';
1112
import {services} from '../../../shared/services';
12-
import {Utils} from '../../../shared/utils';
1313
import {FullHeightLogsViewer} from './full-height-logs-viewer';
1414

1515
interface WorkflowLogsViewerProps {
@@ -63,7 +63,7 @@ export const WorkflowLogsViewer = ({workflow, nodeId, initialPodName, container,
6363
.filter(x => x.type === 'Pod')
6464
.map(targetNode => {
6565
const {name, id, templateName, displayName} = targetNode;
66-
const targetPodName = Utils.getPodName(workflow.metadata.name, name, templateName, id);
66+
const targetPodName = getPodName(workflow.metadata.name, name, templateName, id);
6767
return {value: targetPodName, label: (displayName || name) + ' (' + targetPodName + ')'};
6868
})
6969
);

ui/src/app/workflows/components/workflow-node-info/workflow-node-info.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {InlineTable} from '../../../shared/components/inline-table/inline-table'
1212
import {Links} from '../../../shared/components/links';
1313
import {Phase} from '../../../shared/components/phase';
1414
import {Timestamp} from '../../../shared/components/timestamp';
15+
import {getPodName} from '../../../shared/pod-name';
1516
import {ResourcesDuration} from '../../../shared/resources-duration';
1617
import {services} from '../../../shared/services';
1718
import {getResolvedTemplates} from '../../../shared/template-resolution';
18-
import {Utils} from '../../../shared/utils';
1919

2020
require('./workflow-node-info.scss');
2121

@@ -81,7 +81,7 @@ const AttributeRows = (props: {attributes: {title: string; value: any}[]}) => (
8181
const WorkflowNodeSummary = (props: Props) => {
8282
const {workflow, node} = props;
8383

84-
const podName = Utils.getPodName(workflow.metadata.name, node.name, node.templateName, node.id);
84+
const podName = getPodName(workflow.metadata.name, node.name, node.templateName, node.id);
8585

8686
const attributes = [
8787
{title: 'NAME', value: <ClipboardText text={props.node.name} />},

0 commit comments

Comments
 (0)