Alerting: Fix external alertmanager duplication (#49980)

* Fix external alertmanager duplication

* Add tests
This commit is contained in:
Konrad Lalik 2022-06-01 15:38:23 +02:00 committed by GitHub
parent bdff63d4a8
commit 9da41140aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 17 deletions

View File

@ -127,4 +127,41 @@ describe('useExternalAmSelector', () => {
},
]);
});
it('The number of alert managers should match config entries when there are multiple entries of the same url', () => {
useSelectorMock.mockImplementation((callback) => {
return callback(
createMockStoreState(
[
{ url: 'same/url/to/am/api/v2/alerts' },
{ url: 'same/url/to/am/api/v2/alerts' },
{ url: 'same/url/to/am/api/v2/alerts' },
],
[],
['same/url/to/am', 'same/url/to/am', 'same/url/to/am']
)
);
});
const alertmanagers = useExternalAmSelector();
expect(alertmanagers.length).toBe(3);
expect(alertmanagers).toEqual([
{
url: 'same/url/to/am',
actualUrl: 'same/url/to/am/api/v2/alerts',
status: 'active',
},
{
url: 'same/url/to/am',
actualUrl: 'same/url/to/am/api/v2/alerts',
status: 'active',
},
{
url: 'same/url/to/am',
actualUrl: 'same/url/to/am/api/v2/alerts',
status: 'active',
},
]);
});
});

View File

@ -18,7 +18,7 @@ export function useExternalAmSelector(): AlertmanagerConfig[] | [] {
}
const enabledAlertmanagers: AlertmanagerConfig[] = [];
const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers?.droppedAlertManagers.map((am) => ({
const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers.droppedAlertManagers.map((am) => ({
url: am.url.replace(SUFFIX_REGEX, ''),
status: 'dropped',
actualUrl: am.url,
@ -32,24 +32,20 @@ export function useExternalAmSelector(): AlertmanagerConfig[] | [] {
actualUrl: '',
});
} else {
let found = false;
for (const activeAM of discoveredAlertmanagers.activeAlertManagers) {
if (activeAM.url === `${url}/api/v2/alerts`) {
found = true;
enabledAlertmanagers.push({
url: activeAM.url.replace(SUFFIX_REGEX, ''),
const matchingActiveAM = discoveredAlertmanagers.activeAlertManagers.find(
(am) => am.url === `${url}/api/v2/alerts`
);
matchingActiveAM
? enabledAlertmanagers.push({
url: matchingActiveAM.url.replace(SUFFIX_REGEX, ''),
status: 'active',
actualUrl: activeAM.url,
actualUrl: matchingActiveAM.url,
})
: enabledAlertmanagers.push({
url: url,
status: 'pending',
actualUrl: '',
});
}
}
if (!found) {
enabledAlertmanagers.push({
url: url,
status: 'pending',
actualUrl: '',
});
}
}
}