-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
51 lines (44 loc) · 1.75 KB
/
main.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as grpc from '@grpc/grpc-js'
import * as protoLoader from '@grpc/proto-loader'
import { ProtoGrpcType } from './types/proto/externalscaler'
import { ExternalScalerHandlers } from './types/proto/externalscaler/ExternalScaler'
import { isActive } from './src/IsActive'
import { streamIsActive } from './src/streamIsActive'
import { getMetric } from './src/getMetrics'
import { getMetricSpec } from './src/getMetricSpec'
import path from 'path'
import logger from './src/common/logger'
import { getDeploymentsInNamespace } from './src/kubernetes'
const GlobalPodAutoscaler: ExternalScalerHandlers = {
IsActive: isActive,
StreamIsActive: streamIsActive,
GetMetrics: getMetric,
GetMetricSpec: getMetricSpec
}
const packageDefinition = protoLoader.loadSync(path.join(__dirname, 'proto/externalscaler.proto'), {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
})
const proto = grpc.loadPackageDefinition(packageDefinition) as unknown as ProtoGrpcType
const server = new grpc.Server()
server.addService(proto.externalscaler.ExternalScaler.service, GlobalPodAutoscaler)
server.bindAsync('0.0.0.0:3000', grpc.ServerCredentials.createInsecure(), async (error, port: number) => {
if (error) logger.error(error.message)
server.start()
logger.info(`Server listening on port: ${port}✅`)
try {
const deployments = await getDeploymentsInNamespace()
if (deployments.response.statusCode === 200) logger.info('Connection with kubernetes sucessfully stablished✅')
else {
logger.error(`Connection with kubernetes failed with the error code ${deployments.response.statusCode}`)
process.exit()
}
} catch (e) {
logger.error(e)
process.exit()
}
})