Skip to content

Commit d09944d

Browse files
committed
MK8S-197 - Always pass token to alertmanager
1 parent 97eeed1 commit d09944d

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

shell-ui/src/alerts/AlertProvider.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ export default function AlertProvider({
1919
alertManagerUrl: string;
2020
children: React.ReactNode;
2121
}) {
22-
const { userData } = useAuth();
22+
const { getToken } = useAuth();
2323
const query = useQuery(
24-
['activeAlerts', userData?.token],
25-
() => getAlerts(alertManagerUrl, userData?.token),
24+
['activeAlerts'],
25+
async () => getAlerts(alertManagerUrl, await getToken()),
2626
{
27-
// refetch the alerts every 30 seconds
2827
refetchInterval: 30000,
2928
// TODO manage this refresh interval globally
3029
// avoid stucking at the hard loading state before alertmanager is ready

shell-ui/src/alerts/alertHooks.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import AlertProvider from './AlertProvider';
77
import { useHighestSeverityAlerts } from './alertHooks';
88
import { afterAll, beforeAll, jest } from '@jest/globals';
99
import { QueryClientProvider } from '../QueryClientProvider';
10+
11+
jest.mock('../auth/AuthProvider', () => ({
12+
useAuth: () => ({
13+
userData: {
14+
token: 'test-token',
15+
},
16+
getToken: () => Promise.resolve('test-token'),
17+
}),
18+
}));
19+
1020
const testService = 'http://10.0.0.1/api/alertmanager';
1121

1222
const VOLUME_DEGRADED_ALERT = {

shell-ui/src/alerts/services/alertManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export type AlertLabels = {
2929
selectors?: string[];
3030
[labelName: string]: string;
3131
};
32-
export function getAlerts(alertManagerUrl: string, token?: string) {
32+
export function getAlerts(alertManagerUrl: string, token: string) {
3333
return fetch(alertManagerUrl + '/api/v2/alerts', {
34-
headers: token ? { Authorization: `Bearer ${token}` } : {},
34+
headers: { Authorization: `Bearer ${token}` },
3535
})
3636
.then((r) => {
3737
if (r.ok) {

ui/src/components/DashboardAlerts.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const DashboardAlerts = () => {
6161
const totalAlerts = criticalAlerts.length + warningAlerts.length;
6262
return (
6363
<AlertsContainer>
64-
<div style={{ display: 'flex', alignItems: 'center', gap: spacing.r8 }}>
64+
<Box display="flex" alignItems="center" gap={spacing.r8}>
6565
<div>
6666
<Text isEmphazed>
6767
{intl.formatMessage({
@@ -98,7 +98,7 @@ const DashboardAlerts = () => {
9898
</Banner>
9999
</div>
100100
)}
101-
</div>
101+
</Box>
102102
{totalAlerts === 0 ? null : (
103103
<Box pr={24}>
104104
<BadgesContainer>
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import { ReactNode, useEffect, useState } from 'react';
1+
import { ReactNode } from 'react';
22
import { useAuth } from './PrivateRoute';
33
import { setHeaders } from '../services/prometheus/api';
44

5+
// Temporary solution: The Prometheus API client is initialized with the URL in the saga
6+
// (setApiConfig), but the auth token is not available at that point. Rather than modifying
7+
// the saga (which will be removed soon), this provider sets the auth header on the shared
8+
// Prometheus client once the token is available, and blocks rendering until it's ready.
9+
// TODO: Remove this provider once the saga is removed.
10+
511
export default function PrometheusAuthProvider({
612
children,
713
}: {
814
children: ReactNode;
915
}) {
1016
const { userData } = useAuth();
11-
const [ready, setReady] = useState(false);
1217

13-
useEffect(() => {
14-
if (userData?.token) {
15-
setHeaders({ Authorization: `Bearer ${userData.token}` });
16-
setReady(true);
17-
}
18-
}, [userData?.token]);
18+
if (!userData?.token) return null;
1919

20-
if (!ready) return null;
20+
setHeaders({ Authorization: `Bearer ${userData.token}` });
2121

2222
return <>{children}</>;
2323
}

0 commit comments

Comments
 (0)