Skip to content

Commit f8f16ad

Browse files
committed
refactor: merge 충돌 해결
2 parents 890594c + be1d8e9 commit f8f16ad

File tree

7 files changed

+147
-91
lines changed

7 files changed

+147
-91
lines changed

DBManager/package-lock.json

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DBManager/src/K8S/KubernetesService.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

DBManager/src/app.controller.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import { Controller, Delete, Get, Query } from '@nestjs/common';
2-
import { KubernetesService } from './K8S/KubernetesService';
2+
import { K8SApiService } from './k8s/K8SApi.service';
33

44
@Controller()
55
export class AppController {
6-
constructor(private readonly kubernetesService: KubernetesService) {}
6+
constructor(private readonly k8SApiService: K8SApiService) {}
77

88
@Get('/pods')
99
async getAllPods() {
10-
const pods = await this.kubernetesService.getAllPods();
11-
return pods;
10+
return await this.k8SApiService.getAllPods();
1211
}
1312

1413
@Get('/create-pod')
1514
async createPod() {
16-
const pod = await this.kubernetesService.createPod();
17-
return pod;
15+
return await this.k8SApiService.createPod();
1816
}
1917

2018
@Delete('/delete-pod')
2119
async deletePod(@Query('podName') podName: string) {
22-
console.log(podName);
23-
const pod = await this.kubernetesService.deletePod(podName);
24-
return pod;
20+
return await this.k8SApiService.deletePod(podName);
2521
}
2622
}

DBManager/src/app.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Module } from '@nestjs/common';
22
import { AppController } from './app.controller';
3-
import { KubernetesService } from './K8S/KubernetesService';
43
import { ConfigModule } from '@nestjs/config';
4+
import { K8SApiModule } from './k8s/K8SApi.module';
55
import { ScheduleModule } from '@nestjs/schedule';
66

77
@Module({
@@ -10,8 +10,9 @@ import { ScheduleModule } from '@nestjs/schedule';
1010
isGlobal: true,
1111
}),
1212
ScheduleModule.forRoot(),
13+
K8SApiModule,
1314
],
1415
controllers: [AppController],
15-
providers: [KubernetesService],
16+
providers: [],
1617
})
1718
export class AppModule {}

DBManager/src/config/redis/redis.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export class RedisService {
4747
await this.podConnection.hset(key, field, value);
4848
}
4949

50+
async delPod(key: string): Promise<void> {
51+
await this.podConnection.del(key);
52+
}
53+
5054
async subscribeSession(
5155
channel: string,
5256
onMessage: (message: string) => void,

DBManager/src/k8s/K8SApi.module.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Module } from '@nestjs/common';
2+
import { K8SApiService } from './K8SApi.service';
3+
import { RedisModule } from 'src/config/redis/redis.module';
4+
5+
@Module({
6+
imports: [RedisModule],
7+
exports: [K8SApiService],
8+
providers: [K8SApiService],
9+
})
10+
export class K8SApiModule {}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { Injectable, OnModuleInit } from '@nestjs/common';
2+
import { RedisService } from 'src/config/redis/redis.service';
3+
import { CoreV1Api, KubeConfig, Watch } from '@kubernetes/client-node';
4+
5+
@Injectable()
6+
export class K8SApiService implements OnModuleInit {
7+
private k8sApi: CoreV1Api;
8+
private k8sWatch: Watch;
9+
private namespace = 'default';
10+
11+
constructor(private readonly redisService: RedisService) {}
12+
13+
async onModuleInit() {
14+
const kc = new KubeConfig();
15+
kc.loadFromDefault();
16+
this.k8sApi = kc.makeApiClient(CoreV1Api);
17+
this.k8sWatch = new Watch(kc);
18+
this.startWatchPod();
19+
}
20+
21+
async getPodIp(podName: string): Promise<string> {
22+
const podInfo = await this.k8sApi.readNamespacedPod(
23+
podName,
24+
this.namespace,
25+
);
26+
return podInfo.body.status.podIP || 'Pending';
27+
}
28+
29+
startWatchPod() {
30+
const path = `/api/v1/namespaces/${this.namespace}/pods`;
31+
const queryParams = {
32+
allowWatchBookmarks: true,
33+
labelSelector: 'app=mysql',
34+
};
35+
const handlePodEvent = async (type: string, apiObj: any, watchObj: any) => {
36+
const podName = watchObj.object.metadata.name;
37+
const podStatus = watchObj.object.status;
38+
const curPodIp = await this.redisService.hgetPod(podName, 'podIp');
39+
40+
if (type === 'ADDED') {
41+
await this.redisService.hsetPod(podName, 'activeUser', 0);
42+
await this.redisService.hsetPod(
43+
podName,
44+
'podIp',
45+
podStatus.podIp || '',
46+
);
47+
} else if (type === 'MODIFIED' && podStatus.podIP && curPodIp == '') {
48+
const podIp = podStatus.podIP;
49+
await this.redisService.hsetPod(podName, 'podIp', podIp);
50+
} else if (type === 'DELETED') {
51+
await this.redisService.delPod(podName);
52+
}
53+
};
54+
55+
this.k8sWatch.watch(path, queryParams, handlePodEvent, (err) => {
56+
console.error(err);
57+
});
58+
}
59+
60+
async createPod() {
61+
const mysqlPod = {
62+
metadata: {
63+
generateName: 'mysql-',
64+
labels: {
65+
app: 'mysql',
66+
},
67+
},
68+
spec: {
69+
containers: [
70+
{
71+
name: 'mysql',
72+
image: 'mysql',
73+
},
74+
],
75+
},
76+
};
77+
78+
return await this.k8sApi.createNamespacedPod(this.namespace, mysqlPod);
79+
}
80+
81+
async deletePod(podName: string) {
82+
return await this.k8sApi.deleteNamespacedPod(podName, this.namespace);
83+
}
84+
85+
async getAllPods() {
86+
const podList = await this.k8sApi.listNamespacedPod(this.namespace);
87+
const podResult = {};
88+
89+
for (const pod of podList.body.items) {
90+
const podName = pod.metadata.name;
91+
console.log(podName);
92+
93+
const podInfo = await this.k8sApi.readNamespacedPod(
94+
podName,
95+
this.namespace,
96+
);
97+
const podIp = podInfo.body.status.podIP;
98+
console.log(podIp);
99+
100+
podResult[podName] = podIp;
101+
}
102+
return {
103+
podCnt: Object.keys(podResult).length,
104+
podResult,
105+
};
106+
}
107+
}

0 commit comments

Comments
 (0)