Skip to content

Commit 3f33a13

Browse files
authored
Merge pull request #145 from openshift/manual-cherry-pick-api-version
COO-982: Manual cherry pick api version and sync with main changes
2 parents c4355da + 87cf59a commit 3f33a13

17 files changed

Lines changed: 123 additions & 109 deletions

web/src/__tests__/alert.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { URIRef, Query } from '../korrel8r/types';
21
import { AlertDomain } from '../korrel8r/alert';
2+
import { Query, URIRef } from '../korrel8r/types';
33

44
describe('AlertNode.fromURL', () => {
55
it.each([
@@ -33,7 +33,7 @@ describe('AlertDomain.fromQuery', () => {
3333
},
3434
{ query: 'alert:alert:{}', url: 'monitoring/alerts' },
3535
])('converts $query', ({ url, query }) => {
36-
expect(new AlertDomain().queryToLink(Query.parse(query))).toEqual(url);
36+
expect(new AlertDomain().queryToLink(Query.parse(query))).toEqual(new URIRef(url));
3737
});
3838

3939
it('Query => URL => Query', () => {
@@ -42,7 +42,7 @@ describe('AlertDomain.fromQuery', () => {
4242
const got =
4343
'monitoring/alerts?alerts=alertname%3DKubePodCrashLooping%2Ccontainer%3Dbad-deployment%2Cnamespace%3Ddefault%2Cpod%3Dbad-pod';
4444
const want = new AlertDomain().queryToLink(Query.parse(query));
45-
expect(want).toEqual(got);
46-
expect(new AlertDomain().linkToQuery(new URIRef(want))).toEqual(Query.parse(query));
45+
expect(want).toEqual(new URIRef(got));
46+
expect(new AlertDomain().linkToQuery(want)).toEqual(Query.parse(query));
4747
});
4848
});

web/src/__tests__/all-domains.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ it.each([
7272
},
7373
])('convert URL<=>link', ({ url, query, constraint }) => {
7474
expect(allDomains.linkToQuery(new URIRef(url))).toEqual(Query.parse(query));
75-
expect(allDomains.queryToLink(Query.parse(query), Constraint.fromAPI(constraint))).toEqual(url);
75+
expect(allDomains.queryToLink(Query.parse(query), Constraint.fromAPI(constraint))).toEqual(new URIRef(url));
7676
});

web/src/__tests__/k8s.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,44 +107,44 @@ describe('K8sNode.fromQuery', () => {
107107
// Note "fields" are ignored.
108108
{
109109
query: 'k8s:Pod.v1:{"namespace":"default","name":"bad-deployment-000000000-00000"}',
110-
u: 'k8s/ns/default/pods/bad-deployment-000000000-00000',
110+
url: 'k8s/ns/default/pods/bad-deployment-000000000-00000',
111111
},
112112
{
113113
query: `k8s:Pod:{"namespace":"x","name":"y","labels":{"a":"b","c":"d"},"fields": {"x":"y"}}`,
114-
u: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b,c=d')}`,
114+
url: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b,c=d')}&fields=${encodeURIComponent('x=y')}`,
115115
},
116116
{
117117
query:
118118
'k8s:Event.v1:{"fields":{"involvedObject.namespace":"default","involvedObject.name":"bad-deployment-000000000-00000","involvedObject.apiVersion":"v1","involvedObject.kind":"Pod"}}',
119-
u: 'k8s/ns/default/pods/bad-deployment-000000000-00000/events',
119+
url: 'k8s/ns/default/pods/bad-deployment-000000000-00000/events',
120120
},
121121
{
122-
query: `k8s:Pod:{"namespace":"x","name":"y","labels":{"a":"b"}}`,
123-
u: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b')}`,
122+
query: `k8s:Pod:{ "namespace":"x", "name":"y", "labels":{ "a":"b" } }`,
123+
url: `k8s/ns/x/pods/y?labels=${encodeURIComponent('a=b')}`,
124124
},
125125
{
126-
query: `k8s:Pod:{"namespace":"x","labels":{"a":"b"}}`,
127-
u: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`,
126+
query: `k8s:Pod:{ "namespace":"x", "labels":{ "a":"b" } }`,
127+
url: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`,
128128
},
129-
{ query: `k8s:Pod.v1:{"namespace":"x","name":"y"}`, u: `k8s/ns/x/pods/y` },
130-
{ query: `k8s:Pod.v1:{"namespace":"x"}`, u: `k8s/ns/x/pods` },
129+
{ query: `k8s:Pod.v1:{ "namespace":"x", "name":"y" }`, url: `k8s/ns/x/pods/y` },
130+
{ query: `k8s:Pod.v1:{ "namespace":"x" }`, url: `k8s/ns/x/pods` },
131131
{
132-
query: `k8s:Pod.v1:{"labels":{"a":"b"}}`,
133-
u: `search/all-namespaces?labels=${encodeURIComponent('a=b')}&kind=core~v1~Pod`,
132+
query: `k8s:Pod.v1:{ "labels":{ "a":"b" } }`,
133+
url: `search/all-namespaces?labels=${encodeURIComponent('a=b')}&kind=core~v1~Pod`,
134134
},
135135
{
136136
query: `k8s:Pod.v1:{"namespace":"x","labels":{"a":"b"}}`,
137-
u: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`,
137+
url: `k8s/ns/x/pods?labels=${encodeURIComponent('a=b')}`,
138138
},
139139

140140
// Variations on korrel8r class spec.
141141
{
142-
query: `k8s:Role.v1.rbac.authorization.k8s.io:{"namespace":"x", "name":"y"}`,
143-
u: `k8s/ns/x/roles/y`,
142+
query: `k8s:Role.v1.rbac.authorization.k8s.io:{ "namespace":"x", "name":"y" }`,
143+
url: `k8s/ns/x/roles/y`,
144144
},
145-
{ query: `k8s:Pod:{}`, u: 'k8s/all-namespaces/pods' },
146-
])('converts $query to $url', ({ u: u, query }) => {
147-
expect(k8s.queryToLink(Query.parse(query))).toEqual(u);
145+
{ query: `k8s:Pod:{}`, url: 'k8s/all-namespaces/pods' },
146+
])('converts $query to $url', ({ url, query }) => {
147+
expect(k8s.queryToLink(Query.parse(query))).toEqual(new URIRef(url));
148148
});
149149

150150
it.each([{ query: `foo:bar:baz` }])('raises error on $query', ({ query }) =>

web/src/__tests__/log.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('LogDomain.fromURL', () => {
1313
{
1414
url: `monitoring/logs?q=${encodeURIComponent(
1515
'{kubernetes_namespace_name="default",' +
16-
'kubernetes_pod_name="foo",log_type="infrastructure"}',
16+
'kubernetes_pod_name="foo",log_type="infrastructure"}',
1717
)}`,
1818
query:
1919
`log:infrastructure:{kubernetes_namespace_name="default",` +
@@ -75,7 +75,7 @@ describe('LogDomain.fromQuery', () => {
7575
}),
7676
},
7777
])('$query', ({ url, query, constraint }) =>
78-
expect(new LogDomain().queryToLink(Query.parse(query), constraint)).toEqual(url),
78+
expect(new LogDomain().queryToLink(Query.parse(query), constraint)).toEqual(new URIRef(url)),
7979
);
8080
});
8181

web/src/__tests__/metric.spec.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,61 @@
1-
import { Query, URIRef } from '../korrel8r/types';
21
import { MetricDomain } from '../korrel8r/metric';
2+
import { Query, URIRef } from '../korrel8r/types';
33

44
/**
55
* Bad deployment is the suggested deployment from korrel8r to show its functionality within the
66
* logging plugin. https://korrel8r.github.io/korrel8r/#troubleshooting-no-related-logs
77
*
88
*/
9-
describe('Test MetricNode Parsing', () => {
10-
it('Query => URL => Query', () => {
11-
const query =
12-
'metric:metric:max_over_time(kube_pod_container_status_waiting_reason{job="kube-state-metrics",namespace=~"(openshift-.*|kube-.*|default)",reason="CrashLoopBackOff"}[5m]) <= 1';
13-
const expectedKorrel8rURL =
14-
'monitoring/query-browser?query0=max_over_time(kube_pod_container_status_waiting_reason{job="kube-state-metrics",namespace=~"(openshift-.*|kube-.*|default)",reason="CrashLoopBackOff"}[5m]) <= 1';
15-
const d = new MetricDomain();
16-
const actualKorrel8rURL = d.queryToLink(Query.parse(query));
17-
expect(actualKorrel8rURL).toEqual(expectedKorrel8rURL);
18-
expect(d.linkToQuery(new URIRef(actualKorrel8rURL))).toEqual(Query.parse(query));
19-
});
9+
describe('metric', () => {
10+
const metric = new MetricDomain()
11+
12+
describe('query-link round trip', () => {
13+
it.each([
14+
{
15+
query: 'metric:metric:max_over_time(kube_pod_container_status_waiting_reason{job="kube-state-metrics",namespace=~"(openshift-.*|kube-.*|default)",reason="CrashLoopBackOff"}[5m]) <= 1',
16+
link: 'monitoring/query-browser?query0=max_over_time%28kube_pod_container_status_waiting_reason%7Bjob%3D%22kube-state-metrics%22%2Cnamespace%3D%7E%22%28openshift-.*%7Ckube-.*%7Cdefault%29%22%2Creason%3D%22CrashLoopBackOff%22%7D%5B5m%5D%29+%3C%3D+1'
17+
},
18+
{
19+
// Verify issue: https://github.com/openshift/troubleshooting-panel-console-plugin/issues/127
20+
query: 'metric:metric:(1 - sum(node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes and on (instance) label_replace(kube_node_role{role="master"}, "instance", "$1", "node", "(.+)")) / sum(node_memory_MemTotal_bytes and on (instance) label_replace(kube_node_role{role="master"}, "instance", "$1", "node", "(.+)"))) * 100 > 60',
21+
link: 'monitoring/query-browser?query0=%281+-+sum%28node_memory_MemFree_bytes+%2B+node_memory_Buffers_bytes+%2B+node_memory_Cached_bytes+and+on+%28instance%29+label_replace%28kube_node_role%7Brole%3D%22master%22%7D%2C+%22instance%22%2C+%22%241%22%2C+%22node%22%2C+%22%28.%2B%29%22%29%29+%2F+sum%28node_memory_MemTotal_bytes+and+on+%28instance%29+label_replace%28kube_node_role%7Brole%3D%22master%22%7D%2C+%22instance%22%2C+%22%241%22%2C+%22node%22%2C+%22%28.%2B%29%22%29%29%29+*+100+%3E+60'
22+
},
23+
])('', ({ query, link }) => {
24+
const q = Query.parse(query)
25+
expect(metric.queryToLink(q).toString()).toEqual(link)
26+
expect(metric.linkToQuery(new URIRef(link))).toEqual(q);
27+
})
2028

21-
it('URL => Query => URL', () => {
22-
const url =
23-
'monitoring/query-browser?query0=max_over_time(kube_pod_container_status_waiting_reason{job="kube-state-metrics",namespace=~"(openshift-.*|kube-.*|default)",reason="CrashLoopBackOff"}[5m]) <= 1';
24-
const expectedQuery =
25-
'metric:metric:max_over_time(kube_pod_container_status_waiting_reason{job="kube-state-metrics",namespace=~"(openshift-.*|kube-.*|default)",reason="CrashLoopBackOff"}[5m]) <= 1';
26-
const d = new MetricDomain();
27-
const actualQuery = d.linkToQuery(new URIRef(url));
28-
expect(actualQuery).toEqual(Query.parse(expectedQuery));
29-
expect(d.queryToLink(Query.parse(expectedQuery))).toEqual(url);
3029
});
3130

32-
describe('Test url to query parsing with expected errors', () => {
33-
[
31+
describe('linkToQuery errors', () => {
32+
it.each([
3433
{
3534
url: 'foobar',
36-
expected: new TypeError('domain metric: invalid link: foobar'),
35+
error: new TypeError('domain metric: invalid link: foobar'),
3736
},
3837
{
3938
url: 'monitoring/query-browser',
40-
expected: 'domain metric: invalid link: monitoring/query-browser',
39+
error: 'domain metric: invalid link: monitoring/query-browser',
4140
},
42-
].forEach(({ url, expected }) => {
43-
it(`converts from ${url}`, () => {
44-
expect(() => new MetricDomain().linkToQuery(new URIRef(url))).toThrow(expected);
45-
});
41+
])('$url', ({ url, error }) => {
42+
expect(() => metric.linkToQuery(new URIRef(url))).toThrow(error);
43+
4644
});
4745
});
4846

49-
it('Test query to url parsing with expected errors', () => {
50-
[
47+
describe('queryToLink errors', () => {
48+
it.each([
5149
{
5250
query: 'wrongdomain:metric:foo',
53-
expected: 'domain metric: invalid query, wrong domain: wrongdomain:metric:foo',
51+
error: 'domain metric: invalid query, wrong domain: wrongdomain:metric:foo',
5452
},
5553
{
5654
query: 'metric:badclass:foo',
57-
expected: 'domain metric: invalid class: badclass',
55+
error: 'domain metric: invalid class: badclass',
5856
},
59-
].forEach(({ query, expected }) => {
60-
expect(() => new MetricDomain().queryToLink(Query.parse(query))).toThrow(expected);
57+
])('$query', ({ query, error }) => {
58+
expect(() => metric.queryToLink(Query.parse(query))).toThrow(error);
6159
});
6260
});
6361
});

web/src/__tests__/netflow.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('NetflowNode.fromQuery', () => {
4646
])(`from $query`, ({ query, url, constraint }) =>
4747
expect(
4848
new NetflowDomain().queryToLink(Query.parse(query), Constraint.fromAPI(constraint)),
49-
).toEqual(url),
49+
).toEqual(new URIRef(url)),
5050
);
5151
});
5252

web/src/__tests__/trace.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { URIRef, Query } from '../korrel8r/types';
21
import { TraceDomain } from '../korrel8r/trace';
2+
import { Query, URIRef } from '../korrel8r/types';
33

44
const tempo = 'namespace=openshift-tracing&name=platform&tenant=platform';
55

@@ -38,6 +38,6 @@ describe('TraceDomain.fromQuery', () => {
3838
url: `observe/traces?${tempo}&q=%7Bresource.service.name%3D%22shop-backend%22%7D`,
3939
},
4040
])('$query', ({ query, url }) => {
41-
expect(new TraceDomain().queryToLink(Query.parse(query))).toEqual(url);
41+
expect(new TraceDomain().queryToLink(Query.parse(query)).toString()).toEqual(url);
4242
});
4343
});

web/src/__tests__/types.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ class FakeDomain extends Domain {
3636
return new Class(this.name, m[1]).query(link.searchParams.toString());
3737
}
3838

39-
queryToLink(query: Query, constraint?: Constraint): string {
39+
queryToLink(query: Query, constraint?: Constraint): URIRef {
4040
if (!query || !query.class || query.class.domain != this.name) throw this.badQuery(query);
41-
const c = constraint ? '&constraint=' + JSON.stringify(constraint) : '';
42-
return `${query.class.domain}/${query.class.name}?${query.selector}${c}`;
41+
return new URIRef(`${query.class.domain}/${query.class.name}?${query.selector}`, { constraint: constraint });
4342
}
4443
}
4544

@@ -65,7 +64,7 @@ describe('Domain', () => {
6564
const d = new FakeDomain('a');
6665
const abc = d.class('b').query('c=d');
6766
it('queryToLink', () => {
68-
expect(d.queryToLink(abc)).toEqual('a/b?c=d');
67+
expect(d.queryToLink(abc).toString()).toEqual('a/b?c=d');
6968
const query = Query.parse('x:b:c');
7069
expect(() => d.queryToLink(query)).toThrow('domain a: invalid query: x:b:c');
7170
});
@@ -81,8 +80,8 @@ describe('Domains', () => {
8180
const abc = Query.parse('a:b:c=d');
8281
const xyz = Query.parse('x:y:z=z');
8382
it('queryToLink', () => {
84-
expect(ds.queryToLink(abc)).toEqual('a/b?c=d');
85-
expect(ds.queryToLink(xyz)).toEqual('x/y?z=z');
83+
expect(ds.queryToLink(abc).toString()).toEqual('a/b?c=d');
84+
expect(ds.queryToLink(xyz).toString()).toEqual('x/y?z=z');
8685
expect(() => ds.queryToLink(Query.parse('z:b:c'))).toThrow(/unknown domain .*: z:b:c/);
8786
});
8887
it('linkToQuery', () => {

web/src/components/topology/Korrel8rTopology.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ export const Korrel8rTopology: React.FC<{
147147
const navigateToQuery = React.useCallback(
148148
(query: korrel8r.Query) => {
149149
try {
150-
const link = allDomains.queryToLink(query);
151-
navigate(link.startsWith('/') ? link : `/${link}`);
150+
const link = allDomains.queryToLink(query)?.toString();
151+
if (link) navigate(link.startsWith('/') ? link : `/${link}`);
152152
} catch (e) {
153153
// eslint-disable-next-line no-console
154154
console.error(`korrel8r navigateToQuery: ${e}`, "\nquery", query);

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

0 commit comments

Comments
 (0)