Repository navigation
refactor(service-monitor): migrate module from server #2775
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
Open
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b051900
refactor(service-monitor): use DeepMockProxy instead of ReturnType al…
shikanime b0ab76e
refactor(service-monitor): type the cause as a string in the contract
shikanime e50548e
refactor(service-monitor): use a Set for core components and drop non…
shikanime c03fb45
refactor(service-monitor): pass the indicator key explicitly to fromH…
shikanime aa67145
refactor(server-nestjs): share isRecord from utils/record.utils
shikanime ec0286f
refactor(service-monitor): move the ProbeOutcome type import into the…
shikanime 8c355a5
refactor(service-monitor): fold monitor into check via the terminus s…
shikanime b53856a
refactor(service-monitor): store last-known statuses in CACHE_MANAGER
shikanime 6a10499
refactor(service-monitor): drive the periodic refresh with @Cron
shikanime f543b78
refactor(service-monitor): always return cause in the service
shikanime cbe6bfb
refactor(service-monitor): move cause formatting to the controller
shikanime bab4e6d
refactor(service-monitor): name the public-route cause stripping
shikanime 313d496
docs(service-monitor): compress the admin-only cause comment
shikanime d820902
docs(service-monitor): drop the legacy cron narration comment
shikanime 5f0bc29
fix(sonarqube): send the raw api token under Bearer in the health probe
shikanime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
apps/server-nestjs/src/modules/registry/registry-health.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import type { HarborConfig } from '../../config/harbor.config' | ||
| import { HealthIndicatorService } from '@nestjs/terminus' | ||
| import { Test } from '@nestjs/testing' | ||
| import { http, HttpResponse } from 'msw' | ||
| import { setupServer } from 'msw/node' | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest' | ||
| import { mockDeep } from 'vitest-mock-extended' | ||
| import { harborConfigFactory } from '../../config/harbor.config' | ||
| import { RegistryHealthService } from './registry-health.service' | ||
|
|
||
| const harborUrl = 'https://harbor.example' | ||
| const healthUrl = `${harborUrl}/api/v2.0/health` | ||
|
|
||
| const server = setupServer() | ||
|
|
||
| describe('registryHealthService', () => { | ||
| let service: RegistryHealthService | ||
|
|
||
| beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) | ||
|
|
||
| beforeEach(async () => { | ||
| const config = mockDeep<HarborConfig>({ | ||
| url: harborUrl, | ||
| internalUrl: undefined, | ||
| admin: 'admin', | ||
| adminPassword: 'password', | ||
| }) | ||
|
|
||
| const module = await Test.createTestingModule({ | ||
| providers: [ | ||
| RegistryHealthService, | ||
| { provide: harborConfigFactory.KEY, useValue: config }, | ||
| HealthIndicatorService, | ||
| ], | ||
| }).compile() | ||
|
|
||
| service = module.get(RegistryHealthService) | ||
| }) | ||
| afterEach(() => server.resetHandlers()) | ||
| afterAll(() => server.close()) | ||
|
|
||
| it('reports up when every component is healthy', async () => { | ||
| server.use(http.get(healthUrl, () => HttpResponse.json({ status: 'healthy', components: [] }))) | ||
|
|
||
| await expect(service.check()).resolves.toEqual({ harbor: { status: 'up' } }) | ||
| }) | ||
|
|
||
| it('reports down when a core component is unhealthy', async () => { | ||
| server.use(http.get(healthUrl, () => HttpResponse.json({ | ||
| status: 'unhealthy', | ||
| components: [{ name: 'registryctl', status: 'unhealthy' }], | ||
| }))) | ||
|
|
||
| await expect(service.check()).resolves.toEqual({ harbor: { status: 'down', message: 'Service en erreur' } }) | ||
| }) | ||
|
|
||
| it('reports a degraded up when only a non-core component is unhealthy', async () => { | ||
| server.use(http.get(healthUrl, () => HttpResponse.json({ | ||
| status: 'unhealthy', | ||
| components: [{ name: 'jobservice', status: 'unhealthy' }], | ||
| }))) | ||
|
|
||
| await expect(service.check()).resolves.toEqual({ harbor: { status: 'up', message: 'Service dégradé' } }) | ||
| }) | ||
|
|
||
| it('reports down when the health endpoint does not answer with a 200', async () => { | ||
| server.use(http.get(healthUrl, () => new HttpResponse(null, { status: 503 }))) | ||
|
|
||
| await expect(service.check()).resolves.toEqual({ harbor: { status: 'down', message: 'Fatal Error' } }) | ||
| }) | ||
|
|
||
| it('reports down with the cause when the request fails', async () => { | ||
| server.use(http.get(healthUrl, () => HttpResponse.error())) | ||
|
|
||
| await expect(service.check()).resolves.toMatchObject({ | ||
| harbor: { status: 'down', message: 'Erreur lors la requête', cause: expect.any(Error) }, | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
apps/server-nestjs/src/modules/service-monitor/service-monitor.constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export const INTERVAL_MS = 5 * 60 * 1000 | ||
|
|
||
| export const ERROR_MESSAGE = 'Service en erreur' | ||
|
|
||
| export const REQUEST_ERROR_MESSAGE = 'Erreur lors la requête' | ||
|
|
||
| export const PENDING_MESSAGE = 'En attente d\'une première vérification' |
60 changes: 60 additions & 0 deletions
60
apps/server-nestjs/src/modules/service-monitor/service-monitor.controller.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import type { TestingModule } from '@nestjs/testing' | ||
| import type { MockProxy } from 'vitest-mock-extended' | ||
| import type { ServiceHealth } from './service-monitor.service' | ||
| import { MonitorStatus } from '@cpn-console/shared' | ||
| import { Test } from '@nestjs/testing' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { mock } from 'vitest-mock-extended' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { ServiceMonitorController } from './service-monitor.controller' | ||
| import { ServiceMonitorService } from './service-monitor.service' | ||
|
|
||
| describe('serviceMonitorController', () => { | ||
| let module: TestingModule | ||
| let controller: ServiceMonitorController | ||
| let service: MockProxy<ServiceMonitorService> | ||
|
|
||
| beforeEach(async () => { | ||
| service = mock<ServiceMonitorService>() | ||
|
|
||
| module = await Test.createTestingModule({ | ||
| controllers: [ServiceMonitorController], | ||
| providers: [ | ||
| { provide: ServiceMonitorService, useValue: service }, | ||
| ], | ||
| }) | ||
| .overrideGuard(UserGuard) | ||
| .useValue({ canActivate: () => true }) | ||
| .compile() | ||
|
|
||
| controller = module.get<ServiceMonitorController>(ServiceMonitorController) | ||
| }) | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined() | ||
| }) | ||
|
|
||
| it('strips the cause from the public route', async () => { | ||
| const health: ServiceHealth[] = [{ name: 'keycloak', status: MonitorStatus.ERROR, message: 'down', interval: 1000, lastUpdateTimestamp: 0, cause: 'connection refused' }] | ||
| service.getServiceHealth.mockResolvedValue(health) | ||
|
|
||
| await expect(controller.getServiceHealth()).resolves.toEqual([{ name: 'keycloak', status: MonitorStatus.ERROR, message: 'down', interval: 1000, lastUpdateTimestamp: 0 }]) | ||
| expect(service.getServiceHealth).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('delegates complete-services to the service, formatting the cause', async () => { | ||
| const health: ServiceHealth[] = [{ name: 'keycloak', status: MonitorStatus.OK, message: MonitorStatus.OK, interval: 1000, lastUpdateTimestamp: 0, cause: new Error('boom') }] | ||
| service.getServiceHealth.mockResolvedValue(health) | ||
|
|
||
| await expect(controller.getCompleteServiceHealth()).resolves.toEqual([{ name: 'keycloak', status: MonitorStatus.OK, message: MonitorStatus.OK, interval: 1000, lastUpdateTimestamp: 0, cause: 'boom' }]) | ||
| expect(service.getServiceHealth).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('delegates refresh-services to the service, formatting the cause', async () => { | ||
| const health: ServiceHealth[] = [{ name: 'keycloak', status: MonitorStatus.ERROR, message: 'down', interval: 1000, lastUpdateTimestamp: 0, cause: new Error('boom') }] | ||
| service.refreshServiceHealth.mockResolvedValue(health) | ||
|
|
||
| await expect(controller.refreshServiceHealth()).resolves.toEqual([{ name: 'keycloak', status: MonitorStatus.ERROR, message: 'down', interval: 1000, lastUpdateTimestamp: 0, cause: 'boom' }]) | ||
| expect(service.refreshServiceHealth).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
41 changes: 41 additions & 0 deletions
41
apps/server-nestjs/src/modules/service-monitor/service-monitor.controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type { ServiceHealth } from './service-monitor.service' | ||
| import { Controller, Get, Inject, UseGuards } from '@nestjs/common' | ||
| import { RequireAdminPermission } from '../infrastructure/permission/user/user-admin-permission.decorator' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { ServiceMonitorService } from './service-monitor.service' | ||
| import { toCause } from './service-monitor.utils' | ||
|
|
||
| function toServiceHealth({ cause, ...health }: ServiceHealth): ServiceHealth { | ||
| return { ...health, ...(cause !== undefined ? { cause: toCause(cause) } : {}) } | ||
| } | ||
|
|
||
| // MonitorInfos.cause is admin-only | ||
| function toPublicServiceHealth({ cause, ...health }: ServiceHealth): ServiceHealth { | ||
| return health | ||
| } | ||
|
|
||
| @Controller('api/v1') | ||
| export class ServiceMonitorController { | ||
| constructor( | ||
| @Inject(ServiceMonitorService) private readonly service: ServiceMonitorService, | ||
| ) {} | ||
|
|
||
| @Get('health-services') | ||
| getServiceHealth() { | ||
| return this.service.getServiceHealth().then(health => health.map(toPublicServiceHealth)) | ||
| } | ||
|
|
||
| @Get('complete-services') | ||
| @UseGuards(UserGuard) | ||
| @RequireAdminPermission('ListSystem') | ||
| getCompleteServiceHealth() { | ||
| return this.service.getServiceHealth().then(health => health.map(toServiceHealth)) | ||
| } | ||
|
|
||
| @Get('refresh-services') | ||
| @UseGuards(UserGuard) | ||
| @RequireAdminPermission('ManageSystem') | ||
| refreshServiceHealth() { | ||
| return this.service.refreshServiceHealth().then(health => health.map(toServiceHealth)) | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
apps/server-nestjs/src/modules/service-monitor/service-monitor.module.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { CacheModule } from '@nestjs/cache-manager' | ||
| import { Module } from '@nestjs/common' | ||
| import { ConditionalModule } from '@nestjs/config' | ||
| import { ArgoCDModule } from '../argocd/argocd.module' | ||
| import { GitlabModule } from '../gitlab/gitlab.module' | ||
| import { AuthModule } from '../infrastructure/auth/auth.module' | ||
| import { UserPermissionModule } from '../infrastructure/permission/user/user.module' | ||
| import { KeycloakModule } from '../keycloak/keycloak.module' | ||
| import { NexusModule } from '../nexus/nexus.module' | ||
| import { RegistryModule } from '../registry/registry.module' | ||
| import { SonarqubeModule } from '../sonarqube/sonarqube.module' | ||
| import { VaultModule } from '../vault/vault.module' | ||
| import { ServiceMonitorController } from './service-monitor.controller' | ||
| import { ServiceMonitorService } from './service-monitor.service' | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| CacheModule.register(), | ||
| AuthModule, | ||
| UserPermissionModule, | ||
| KeycloakModule, | ||
| ConditionalModule.registerWhen(ArgoCDModule, 'USE_ARGOCD'), | ||
| ConditionalModule.registerWhen(GitlabModule, 'USE_GITLAB'), | ||
| ConditionalModule.registerWhen(RegistryModule, 'USE_HARBOR'), | ||
| ConditionalModule.registerWhen(NexusModule, 'USE_NEXUS'), | ||
| ConditionalModule.registerWhen(SonarqubeModule, 'USE_SONARQUBE'), | ||
| ConditionalModule.registerWhen(VaultModule, 'USE_VAULT'), | ||
| ], | ||
| controllers: [ServiceMonitorController], | ||
| providers: [ServiceMonitorService], | ||
| }) | ||
| export class ServiceMonitorModule {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.