Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of [ui] Fix: Wildcard-datacenter system/sysbatch jobs stopped showing client links/chart into release/1.5.x #16341

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/16274.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: fixed an issue where system/sysbatch jobs with wildcard datacenters (like ["dc*"]) were not showing client status charts
```
39 changes: 2 additions & 37 deletions ui/app/abilities/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { computed, get } from '@ember/object';
import { or } from '@ember/object/computed';
import AbstractAbility from './abstract';
import doesMatchPattern from 'nomad-ui/utils/match-glob';

const WILDCARD_GLOB = '*';
const WILDCARD_PATTERN = '/';
Expand Down Expand Up @@ -188,7 +189,7 @@ export default class Variable extends AbstractAbility {
const matches = [];

for (const pathName of pathNames) {
if (this._doesMatchPattern(pathName, path)) matches.push(pathName);
if (doesMatchPattern(pathName, path)) matches.push(pathName);
}

return matches;
Expand All @@ -207,42 +208,6 @@ export default class Variable extends AbstractAbility {
return this._smallestDifference(allMatchingPaths, path);
}

_doesMatchPattern(pattern, path) {
const parts = pattern?.split(WILDCARD_GLOB);
const hasLeadingGlob = pattern?.startsWith(WILDCARD_GLOB);
const hasTrailingGlob = pattern?.endsWith(WILDCARD_GLOB);
const lastPartOfPattern = parts[parts.length - 1];
const isPatternWithoutGlob = parts.length === 1 && !hasLeadingGlob;

if (!pattern || !path || isPatternWithoutGlob) {
return pattern === path;
}

if (pattern === WILDCARD_GLOB) {
return true;
}

let subPathToMatchOn = path;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const idx = subPathToMatchOn?.indexOf(part);
const doesPathIncludeSubPattern = idx > -1;
const doesMatchOnFirstSubpattern = idx === 0;

if (i === 0 && !hasLeadingGlob && !doesMatchOnFirstSubpattern) {
return false;
}

if (!doesPathIncludeSubPattern) {
return false;
}

subPathToMatchOn = subPathToMatchOn.slice(0, idx + path.length);
}

return hasTrailingGlob || path.endsWith(lastPartOfPattern);
}

_computeLengthDiff(pattern, path) {
const countGlobsInPattern = pattern
?.split('')
Expand Down
52 changes: 52 additions & 0 deletions ui/app/utils/match-glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @ts-check

const WILDCARD_GLOB = '*';

/**
*
* @param {string} pattern
* @param {string} comparable
* @example matchGlob('foo', 'foo') // true
* @example matchGlob('foo', 'bar') // false
* @example matchGlob('foo*', 'footbar') // true
* @example matchGlob('foo*', 'bar') // false
* @example matchGlob('*foo', 'foobar') // false
* @example matchGlob('*foo', 'barfoo') // true
* @example matchGlob('foo*bar', 'footbar') // true
* @returns {boolean}
*/
export default function matchGlob(pattern, comparable) {
const parts = pattern?.split(WILDCARD_GLOB);
const hasLeadingGlob = pattern?.startsWith(WILDCARD_GLOB);
const hasTrailingGlob = pattern?.endsWith(WILDCARD_GLOB);
const lastPartOfPattern = parts[parts.length - 1];
const isPatternWithoutGlob = parts.length === 1 && !hasLeadingGlob;

if (!pattern || !comparable || isPatternWithoutGlob) {
return pattern === comparable;
}

if (pattern === WILDCARD_GLOB) {
return true;
}

let subStringToMatchOn = comparable;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const idx = subStringToMatchOn?.indexOf(part);
const doesStringIncludeSubPattern = idx > -1;
const doesMatchOnFirstSubpattern = idx === 0;

if (i === 0 && !hasLeadingGlob && !doesMatchOnFirstSubpattern) {
return false;
}

if (!doesStringIncludeSubPattern) {
return false;
}

subStringToMatchOn = subStringToMatchOn.slice(0, idx + comparable.length);
}

return hasTrailingGlob || comparable.endsWith(lastPartOfPattern);
}
5 changes: 4 additions & 1 deletion ui/app/utils/properties/job-client-status.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { computed } from '@ember/object';
import matchGlob from '../match-glob';

const STATUS = [
'queued',
Expand Down Expand Up @@ -26,7 +27,9 @@ export default function jobClientStatus(nodesKey, jobKey) {

// Filter nodes by the datacenters defined in the job.
const filteredNodes = nodes.filter((n) => {
return job.datacenters.indexOf(n.datacenter) >= 0;
return job.datacenters.find((dc) => {
return !!matchGlob(dc, n.datacenter);
});
});

if (job.status === 'pending') {
Expand Down
40 changes: 40 additions & 0 deletions ui/tests/acceptance/job-detail-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ moduleForJobWithClientStatus(
})
);

moduleForJobWithClientStatus(
'Acceptance | job detail with client status (system with wildcard dc)',
() =>
server.create('job', {
id: 'system-wildcard-dc',
status: 'running',
datacenters: ['canada-*-1'],
type: 'system',
createAllocations: false,
})
);

moduleForJob('Acceptance | job detail (sysbatch)', 'allocations', () =>
server.create('job', { type: 'sysbatch', shallow: true })
);
Expand Down Expand Up @@ -59,6 +71,20 @@ moduleForJobWithClientStatus(
}
);

moduleForJobWithClientStatus(
'Acceptance | job detail with client status (sysbatch with namespace and wildcard dc)',
() => {
const namespace = server.create('namespace', { id: 'test' });
return server.create('job', {
status: 'running',
datacenters: ['*'],
type: 'sysbatch',
namespaceId: namespace.name,
createAllocations: false,
});
}
);

moduleForJob('Acceptance | job detail (sysbatch child)', 'allocations', () => {
const parent = server.create('job', 'periodicSysbatch', {
childrenCount: 1,
Expand Down Expand Up @@ -94,6 +120,20 @@ moduleForJobWithClientStatus(
}
);

moduleForJobWithClientStatus(
'Acceptance | job detail with client status (sysbatch child with namespace and wildcard dc)',
() => {
const namespace = server.create('namespace', { id: 'test' });
const parent = server.create('job', 'periodicSysbatch', {
childrenCount: 1,
shallow: true,
namespaceId: namespace.name,
datacenters: ['*'],
});
return server.db.jobs.where({ parentId: parent.id })[0];
}
);

moduleForJob(
'Acceptance | job detail (periodic)',
'children',
Expand Down
10 changes: 10 additions & 0 deletions ui/tests/helpers/module-for-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ export function moduleForJobWithClientStatus(
datacenter: 'dc1',
status: 'ready',
});

clients.push(
server.create('node', { datacenter: 'dc2', status: 'ready' })
);
clients.push(
server.create('node', { datacenter: 'dc3', status: 'ready' })
);
clients.push(
server.create('node', { datacenter: 'canada-west-1', status: 'ready' })
);
job = jobFactory();
clients.forEach((c) => {
server.create('allocation', { jobId: job.id, nodeId: c.id });
Expand Down
Loading