diff --git a/apps/server-nestjs/src/modules/argocd/argocd-health.service.ts b/apps/server-nestjs/src/modules/argocd/argocd-health.service.ts index 8c1c1f2b60..05dca67fe3 100644 --- a/apps/server-nestjs/src/modules/argocd/argocd-health.service.ts +++ b/apps/server-nestjs/src/modules/argocd/argocd-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from './argocd.constants' @Injectable() export class ArgoCDHealthService { @@ -9,8 +10,8 @@ export class ArgoCDHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) const urlBase = this.config.getInternalOrPublicArgoCDUrl() if (!urlBase) return indicator.down('Not configured') diff --git a/apps/server-nestjs/src/modules/gitlab/gitlab-health.service.ts b/apps/server-nestjs/src/modules/gitlab/gitlab-health.service.ts index fc50892690..68a562e3b1 100644 --- a/apps/server-nestjs/src/modules/gitlab/gitlab-health.service.ts +++ b/apps/server-nestjs/src/modules/gitlab/gitlab-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from '../vault/vault.constants' @Injectable() export class GitlabHealthService { @@ -9,8 +10,8 @@ export class GitlabHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) const urlBase = this.config.getInternalOrPublicGitlabUrl() if (!urlBase) return indicator.down('Not configured') diff --git a/apps/server-nestjs/src/modules/healthz/healthz.controller.ts b/apps/server-nestjs/src/modules/healthz/healthz.controller.ts index 3b7abf9207..2358ced57a 100644 --- a/apps/server-nestjs/src/modules/healthz/healthz.controller.ts +++ b/apps/server-nestjs/src/modules/healthz/healthz.controller.ts @@ -1,57 +1,14 @@ import { Controller, Get, Inject } from '@nestjs/common' -import { HealthCheck, HealthCheckService } from '@nestjs/terminus' -import { ArgoCDHealthService } from '../argocd/argocd-health.service' -import { GitlabHealthService } from '../gitlab/gitlab-health.service' -import { ConfigurationService } from '../infrastructure/configuration/configuration.service' -import { DatabaseHealthService } from '../infrastructure/database/database-health.service' -import { KeycloakHealthService } from '../keycloak/keycloak-health.service' -import { NexusHealthService } from '../nexus/nexus-health.service' -import { RegistryHealthService } from '../registry/registry-health.service' -import { ServiceChainHealthService } from '../service-chain/service-chain-health.service' -import { VaultHealthService } from '../vault/vault-health.service' +import { HealthCheck } from '@nestjs/terminus' +import { HealthzService } from './healthz.service' @Controller('api/v1/healthz') export class HealthzController { - constructor( - @Inject(HealthCheckService) private readonly health: HealthCheckService, - @Inject(DatabaseHealthService) private readonly database: DatabaseHealthService, - @Inject(KeycloakHealthService) private readonly keycloak: KeycloakHealthService, - @Inject(GitlabHealthService) private readonly gitlab: GitlabHealthService, - @Inject(VaultHealthService) private readonly vault: VaultHealthService, - @Inject(NexusHealthService) private readonly nexus: NexusHealthService, - @Inject(RegistryHealthService) private readonly registry: RegistryHealthService, - @Inject(ArgoCDHealthService) private readonly argocd: ArgoCDHealthService, - @Inject(ServiceChainHealthService) private readonly opencds: ServiceChainHealthService, - @Inject(ConfigurationService) private readonly config: ConfigurationService, - ) {} + constructor(@Inject(HealthzService) private readonly healthz: HealthzService) {} @Get() @HealthCheck() check() { - const checks = [ - () => this.database.check('database'), - () => this.keycloak.check('keycloak'), - ] - - if (this.config.openCdsUrl) { - checks.push(() => this.opencds.check('opencds')) - } - if (this.config.gitlabUrl) { - checks.push(() => this.gitlab.check('gitlab')) - } - if (this.config.vaultUrl) { - checks.push(() => this.vault.check('vault')) - } - if (this.config.nexusUrl) { - checks.push(() => this.nexus.check('nexus')) - } - if (this.config.harborUrl) { - checks.push(() => this.registry.check('registry')) - } - if (this.config.argocdUrl) { - checks.push(() => this.argocd.check('argocd')) - } - - return this.health.check(checks) + return this.healthz.check() } } diff --git a/apps/server-nestjs/src/modules/healthz/healthz.module.ts b/apps/server-nestjs/src/modules/healthz/healthz.module.ts index 729875eda3..ae3cccd2bb 100644 --- a/apps/server-nestjs/src/modules/healthz/healthz.module.ts +++ b/apps/server-nestjs/src/modules/healthz/healthz.module.ts @@ -10,6 +10,7 @@ import { RegistryModule } from '../registry/registry.module' import { ServiceChainModule } from '../service-chain/service-chain.module' import { VaultModule } from '../vault/vault.module' import { HealthzController } from './healthz.controller' +import { HealthzService } from './healthz.service' @Module({ imports: [ @@ -25,5 +26,6 @@ import { HealthzController } from './healthz.controller' ServiceChainModule, ], controllers: [HealthzController], + providers: [HealthzService], }) export class HealthzModule {} diff --git a/apps/server-nestjs/src/modules/healthz/healthz.service.ts b/apps/server-nestjs/src/modules/healthz/healthz.service.ts new file mode 100644 index 0000000000..81e7a086ab --- /dev/null +++ b/apps/server-nestjs/src/modules/healthz/healthz.service.ts @@ -0,0 +1,44 @@ +import type { HealthIndicatorFunction } from '@nestjs/terminus' +import { Inject, Injectable } from '@nestjs/common' +import { HealthCheckService } from '@nestjs/terminus' +import { ArgoCDHealthService } from '../argocd/argocd-health.service' +import { GitlabHealthService } from '../gitlab/gitlab-health.service' +import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { DatabaseHealthService } from '../infrastructure/database/database-health.service' +import { KeycloakHealthService } from '../keycloak/keycloak-health.service' +import { NexusHealthService } from '../nexus/nexus-health.service' +import { RegistryHealthService } from '../registry/registry-health.service' +import { ServiceChainHealthService } from '../service-chain/service-chain-health.service' +import { VaultHealthService } from '../vault/vault-health.service' + +@Injectable() +export class HealthzService { + constructor( + @Inject(ConfigurationService) private readonly config: ConfigurationService, + @Inject(HealthCheckService) private readonly health: HealthCheckService, + @Inject(DatabaseHealthService) private readonly database: DatabaseHealthService, + @Inject(KeycloakHealthService) private readonly keycloak: KeycloakHealthService, + @Inject(GitlabHealthService) private readonly gitlab: GitlabHealthService, + @Inject(VaultHealthService) private readonly vault: VaultHealthService, + @Inject(NexusHealthService) private readonly nexus: NexusHealthService, + @Inject(RegistryHealthService) private readonly registry: RegistryHealthService, + @Inject(ArgoCDHealthService) private readonly argocd: ArgoCDHealthService, + @Inject(ServiceChainHealthService) private readonly serviceChainHealth: ServiceChainHealthService, + ) {} + + check() { + const checks: HealthIndicatorFunction[] = [ + () => this.database.check(), + () => this.keycloak.check(), + ] + + if (this.config.gitlabUrl) checks.push(() => this.gitlab.check()) + if (this.config.vaultUrl) checks.push(() => this.vault.check()) + if (this.config.nexusUrl) checks.push(() => this.nexus.check()) + if (this.config.harborUrl) checks.push(() => this.registry.check()) + if (this.config.argocdUrl) checks.push(() => this.argocd.check()) + if (this.config.openCdsUrl) checks.push(() => this.serviceChainHealth.check()) + + return this.health.check(checks) + } +} diff --git a/apps/server-nestjs/src/modules/infrastructure/database/database-health.service.ts b/apps/server-nestjs/src/modules/infrastructure/database/database-health.service.ts index 1e37899c9b..be55e07a96 100644 --- a/apps/server-nestjs/src/modules/infrastructure/database/database-health.service.ts +++ b/apps/server-nestjs/src/modules/infrastructure/database/database-health.service.ts @@ -1,5 +1,6 @@ import { Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' +import { SERVICE_NAME } from './database.constants' import { PrismaService } from './prisma.service' @Injectable() @@ -9,8 +10,8 @@ export class DatabaseHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(SERVICE_NAME) try { await this.prisma.$queryRaw`SELECT 1` return indicator.up() diff --git a/apps/server-nestjs/src/modules/infrastructure/database/database.constants.ts b/apps/server-nestjs/src/modules/infrastructure/database/database.constants.ts new file mode 100644 index 0000000000..53eed0fd61 --- /dev/null +++ b/apps/server-nestjs/src/modules/infrastructure/database/database.constants.ts @@ -0,0 +1,2 @@ +// Health indicator name for the database +export const SERVICE_NAME = 'database' diff --git a/apps/server-nestjs/src/modules/keycloak/keycloak-health.service.ts b/apps/server-nestjs/src/modules/keycloak/keycloak-health.service.ts index e2d1c31286..f769a99ef1 100644 --- a/apps/server-nestjs/src/modules/keycloak/keycloak-health.service.ts +++ b/apps/server-nestjs/src/modules/keycloak/keycloak-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from './keycloak.constants' @Injectable() export class KeycloakHealthService { @@ -11,8 +12,8 @@ export class KeycloakHealthService { private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) const url = this.config.getKeycloakOpenidConfigurationUrl() if (!url) return indicator.down('Not configured') diff --git a/apps/server-nestjs/src/modules/nexus/nexus-health.service.ts b/apps/server-nestjs/src/modules/nexus/nexus-health.service.ts index 9453ee1d10..70362e7b53 100644 --- a/apps/server-nestjs/src/modules/nexus/nexus-health.service.ts +++ b/apps/server-nestjs/src/modules/nexus/nexus-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from './nexus.constants' @Injectable() export class NexusHealthService { @@ -9,8 +10,8 @@ export class NexusHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) if (!this.config.nexusInternalUrl) return indicator.down('Not configured') const url = new URL('/service/rest/v1/status', this.config.nexusInternalUrl).toString() diff --git a/apps/server-nestjs/src/modules/registry/registry-health.service.ts b/apps/server-nestjs/src/modules/registry/registry-health.service.ts index 2a1b33454c..35b4284605 100644 --- a/apps/server-nestjs/src/modules/registry/registry-health.service.ts +++ b/apps/server-nestjs/src/modules/registry/registry-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from './registry.constants' @Injectable() export class RegistryHealthService { @@ -9,8 +10,8 @@ export class RegistryHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) if (!this.config.harborInternalUrl) return indicator.down('Not configured') const url = new URL('/api/v2.0/ping', this.config.harborInternalUrl).toString() diff --git a/apps/server-nestjs/src/modules/service-chain/service-chain-health.service.ts b/apps/server-nestjs/src/modules/service-chain/service-chain-health.service.ts index cb5a2af84d..0c68567668 100644 --- a/apps/server-nestjs/src/modules/service-chain/service-chain-health.service.ts +++ b/apps/server-nestjs/src/modules/service-chain/service-chain-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from './service-chain.constants' @Injectable() export class ServiceChainHealthService { @@ -9,8 +10,8 @@ export class ServiceChainHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) if (!this.config.openCdsUrl) return indicator.down('Not configured') try { diff --git a/apps/server-nestjs/src/modules/service-chain/service-chain.constants.ts b/apps/server-nestjs/src/modules/service-chain/service-chain.constants.ts new file mode 100644 index 0000000000..c3e0792fe2 --- /dev/null +++ b/apps/server-nestjs/src/modules/service-chain/service-chain.constants.ts @@ -0,0 +1,2 @@ +// Plugin name for the service-chain module +export const PLUGIN_NAME = 'service-chain' diff --git a/apps/server-nestjs/src/modules/vault/vault-health.service.ts b/apps/server-nestjs/src/modules/vault/vault-health.service.ts index 649770a558..ddfbc817db 100644 --- a/apps/server-nestjs/src/modules/vault/vault-health.service.ts +++ b/apps/server-nestjs/src/modules/vault/vault-health.service.ts @@ -1,6 +1,7 @@ import { HttpStatus, Inject, Injectable } from '@nestjs/common' import { HealthIndicatorService } from '@nestjs/terminus' import { ConfigurationService } from '../infrastructure/configuration/configuration.service' +import { PLUGIN_NAME } from './vault.constants' @Injectable() export class VaultHealthService { @@ -9,8 +10,8 @@ export class VaultHealthService { @Inject(HealthIndicatorService) private readonly healthIndicator: HealthIndicatorService, ) {} - async check(key: string) { - const indicator = this.healthIndicator.check(key) + async check() { + const indicator = this.healthIndicator.check(PLUGIN_NAME) const urlBase = this.config.getInternalOrPublicVaultUrl() if (!urlBase) return indicator.down('Not configured')