|
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + ConflictException, |
| 4 | + HttpException, |
| 5 | + HttpStatus, |
| 6 | + Injectable, |
| 7 | + Logger, |
| 8 | + NotFoundException, |
| 9 | + OnModuleInit, |
| 10 | + UnauthorizedException, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import * as Api from 'kubernetes-client'; |
| 13 | +import { ConfigService } from '../../config'; |
| 14 | +import { environment as env } from '@env-api/environment'; |
| 15 | + |
| 16 | +const Client = Api.Client1_10; |
| 17 | +const config = Api.config; |
| 18 | + |
| 19 | +@Injectable() |
| 20 | +export class KubernetesService implements OnModuleInit { |
| 21 | + private readonly logger = new Logger(KubernetesService.name); |
| 22 | + |
| 23 | + private readonly clients = new Map( |
| 24 | + Object.entries(env.kubernetes).map<[string, Api.Api]>(([key, value]) => [ |
| 25 | + key, |
| 26 | + new Client({ |
| 27 | + config: { |
| 28 | + url: value.baseUrl, |
| 29 | + auth: { |
| 30 | + bearer: value.token, |
| 31 | + }, |
| 32 | + insecureSkipTlsVerify: true, |
| 33 | + version: 'v1', |
| 34 | + promises: true, |
| 35 | + }, |
| 36 | + version: value.version || '1.10', |
| 37 | + }), |
| 38 | + ]), |
| 39 | + ); |
| 40 | + |
| 41 | + constructor(private readonly appConfig: ConfigService) {} |
| 42 | + |
| 43 | + async onModuleInit() { |
| 44 | + // @ts-ignore |
| 45 | + // for (const [key, client] of this.clients.entries()) { |
| 46 | + // try { |
| 47 | + // await client.loadSpec(); |
| 48 | + // } catch (err) { |
| 49 | + // console.error(`Unable to connect to ${key}`, err); |
| 50 | + // } |
| 51 | + // } |
| 52 | + } |
| 53 | + |
| 54 | + public async listNamespaces(cluster: string) { |
| 55 | + try { |
| 56 | + const namespaces = await this.clients.get(cluster).api.v1.namespaces.get(); |
| 57 | + return namespaces.body.items; |
| 58 | + } catch (error) { |
| 59 | + KubernetesService.handleError(error); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public async myNamespaces(cluster: string, token: string) { |
| 64 | + try { |
| 65 | + // this.client.get(cluster).setToken(token) |
| 66 | + const namespaces = await this.clients.get(cluster).api.v1.namespaces.get(); |
| 67 | + return namespaces.items; |
| 68 | + } catch (error) { |
| 69 | + KubernetesService.handleError(error); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public async getNamespace(cluster: string, namespace: string) { |
| 74 | + try { |
| 75 | + const namespace1 = await this.clients |
| 76 | + .get(cluster) |
| 77 | + .api.v1.namespaces(namespace) |
| 78 | + .get(); |
| 79 | + return namespace1.body; |
| 80 | + } catch (error) { |
| 81 | + KubernetesService.handleError(error); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public async myServiceAccounts(cluster: string, namespace: string) { |
| 86 | + try { |
| 87 | + const namespaces = await this.clients |
| 88 | + .get(cluster) |
| 89 | + .api.v1.namespaces(namespace) |
| 90 | + .serviceaccounts.get(); |
| 91 | + return namespaces.body.items; |
| 92 | + } catch (error) { |
| 93 | + KubernetesService.handleError(error); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public async hasNamespace(cluster: string, namespace: string) { |
| 98 | + try { |
| 99 | + const foundNamespace = await this.clients |
| 100 | + .get(cluster) |
| 101 | + .api.v1.namespaces(namespace) |
| 102 | + .get(); |
| 103 | + return !!foundNamespace; |
| 104 | + } catch (error) { |
| 105 | + if (error.code === 404) return false; |
| 106 | + KubernetesService.handleError(error); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + static handleError(error: Error & { code?: number; statusCode?: number }) { |
| 111 | + const message = error.message || 'unknown error'; |
| 112 | + const statusCode = error.statusCode || error.code || HttpStatus.I_AM_A_TEAPOT; |
| 113 | + console.log(message, statusCode); |
| 114 | + switch (statusCode) { |
| 115 | + case HttpStatus.CONFLICT: |
| 116 | + throw new ConflictException(error.message); |
| 117 | + case HttpStatus.UNAUTHORIZED: |
| 118 | + throw new UnauthorizedException(error.message); |
| 119 | + case HttpStatus.NOT_FOUND: |
| 120 | + throw new NotFoundException(error.message); |
| 121 | + case HttpStatus.BAD_REQUEST: |
| 122 | + throw new BadRequestException(error.message); |
| 123 | + default: |
| 124 | + throw new HttpException(message, statusCode); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments