Skip to content

Commit 87cf59a

Browse files
alanconwayjgbernalp
authored andcommitted
fix(coo-982): Remove console.log statements, fix apiVersion parsing
1 parent 29af5dc commit 87cf59a

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

web/src/hooks/useLocationQuery.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export const useLocationQuery = (): Query | undefined => {
88
try {
99
const link = new URIRef(location.pathname + location.search);
1010
const q = allDomains.linkToQuery(link);
11-
// eslint-disable-next-line no-console
12-
console.log('korrel8r linkToQuery', "\nlink", link, "\nquery", q);
1311
return q;
1412
} catch (e) {
1513
// eslint-disable-next-line no-console

web/src/korrel8r/k8s.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type Selector = {
2121
const PATH_RE = new RegExp(
2222
'(?:k8s|search)/(?:(?:ns/([^/]+))|cluster|all-namespaces)(?:/([^/]+)(?:/([^/]+))?)?(/events)?/?$',
2323
);
24-
const CLASS_RE = new RegExp('^([^./]+)(?:.(v[0-9]+(?:(?:alpha|beta)[0-9]*)?))?(?:.([^/]*))?$');
24+
const VERSION_RE = '(v[0-9]+(?:(?:alpha|beta)[0-9]*)?)';
25+
const CLASS_RE = new RegExp(`^([^./]+)(?:.${VERSION_RE})?(?:.([^/]*))?$`);
2526

2627
export class K8sDomain extends Domain {
2728
constructor() {
@@ -79,20 +80,24 @@ export class K8sDomain extends Domain {
7980
throw this.badQuery(query, e.message);
8081
}
8182
const m = query.class.name.match(/^([^.]+)(?:\.([^.]*)(?:\.(.*))?)?$/) ?? [];
82-
if (!m) throw this.badQuery(query, "incorrect format");
83+
if (!m) throw this.badQuery(query, 'incorrect format');
8384
let model = findGVK(m[3], m[2], m[1]);
84-
if (!model) throw this.badQuery(query, "no matching resource");
85+
if (!model) throw this.badQuery(query, 'no matching resource');
8586
let namespace = data.namespace;
8687
let name = data.name;
8788
let events = '';
8889
if (isEvent(model)) {
8990
// Special treatment for event objects: focus on the involved object, not the event.
9091
events = '/events';
9192
const about = eventAboutField(model);
92-
const [group, version] = data.fields[`${about}.apiVersion`]?.split("/") ?? []
93-
const kind = data.fields[`${about}.kind`]
94-
model = findGVK(group, version, kind)
95-
if (!model) throw this.badQuery(query, `no resource for group=${group} version=${version} kind=${kind}`);
93+
const [group, version] = parseAPIVersion(data.fields[`${about}.apiVersion`]);
94+
const kind = data.fields[`${about}.kind`];
95+
model = findGVK(group, version, kind);
96+
if (!model)
97+
throw this.badQuery(
98+
query,
99+
`no resource for group=${group} version=${version} kind=${kind}`,
100+
);
96101
namespace = data.fields[`${about}.namespace`] || '';
97102
name = data.fields[`${about}.name`] || '';
98103
}
@@ -101,15 +106,16 @@ export class K8sDomain extends Domain {
101106
const params = {
102107
labels: keyValueList(data.labels) || undefined,
103108
fields: (!events && keyValueList(data.fields)) || undefined,
104-
}
105-
if (!name && !namespace && (params.labels || params.fields)) { // This is a search URL
109+
};
110+
if (!name && !namespace && (params.labels || params.fields)) {
111+
// This is a search URL
106112
return new URIRef(`search/${nsPath}`, {
107113
...params,
108114
kind: `${model.apiGroup || 'core'}~${model.apiVersion}~${model.kind}`,
109-
})
110-
} else { // Specific resource URL
111-
return new URIRef(
112-
`k8s/${nsPath}/${model.path}${name ? `/${name}` : ''}${events}`, params)
115+
});
116+
} else {
117+
// Specific resource URL
118+
return new URIRef(`k8s/${nsPath}/${model.path}${name ? `/${name}` : ''}${events}`, params);
113119
}
114120
}
115121

@@ -182,3 +188,16 @@ function findResource(resource: string, kind: string): Model {
182188
parts[2],
183189
);
184190
}
191+
192+
const VERSION_ONLY_RE = new RegExp(`^${VERSION_RE}$`);
193+
function parseAPIVersion(apiVersion: string): [group: string, version: string] | undefined {
194+
const gv = apiVersion.split('/') || [];
195+
switch (gv.length) {
196+
case 1:
197+
return gv[0].match(VERSION_ONLY_RE) ? ['', gv[0]] : [gv[0], ''];
198+
case 2:
199+
return [gv[0], gv[1]];
200+
default:
201+
return ['', ''];
202+
}
203+
}

0 commit comments

Comments
 (0)