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