Skip to content

Commit 8d39cb1

Browse files
committed
feat: ip 저장
1 parent 4ec6ee0 commit 8d39cb1

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class RedisService {
3636
return this.podConnection.hget(key, field);
3737
}
3838

39-
async hsetPod(key: string, field: string, value: number): Promise<void> {
39+
async hsetPod(key: string, field: string, value: number | string): Promise<void> {
4040
await this.podConnection.hset(key, field, value);
4141
}
4242

DBManager/src/k8s/K8SApi.service.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,38 @@ export class K8SApiService {
1818
this.k8sWatch = new k8s.Watch(kc);
1919
this.startWatchPod();
2020
}
21+
22+
async getPodIp(podName: string): Promise<string> {
23+
const podInfo = await this.k8sApi.readNamespacedPod(podName, this.namespace);
24+
return podInfo.body.status.podIP || 'Pending';
25+
}
2126

2227
startWatchPod() {
2328
const path = `/api/v1/namespaces/${this.namespace}/pods`;
2429
const queryParams = { allowWatchBookmarks: true };
25-
const handlePodEvent = (type : String, apiObj: any, watchObj: any) => {
30+
const handlePodEvent = async (type : String, apiObj: any, watchObj: any) => {
2631
if (type === 'ADDED') {
2732
const createdPod = watchObj.object.metadata.name;
28-
this.redisService.hsetPod(createdPod, 'activeUser', 0);
33+
const podIp = await this.getPodIp(createdPod);
34+
35+
await this.redisService.hsetPod(createdPod, 'activeUser', 0);
36+
//TODO ip가 바로 생성되지 않는 문제 해결 필요
37+
await this.redisService.hsetPod(createdPod, 'podIp', podIp);
2938
} else if (type === 'DELETED') {
3039
const deletedPod = watchObj.object.metadata.name;
31-
this.redisService.delPod(deletedPod);
40+
await this.redisService.delPod(deletedPod);
3241
}
3342
};
43+
3444

3545
this.k8sWatch.watch(path, queryParams, handlePodEvent, err => {});
3646
}
3747

3848
async createPod() {
49+
const podName = `mysql-pod${this.podCnt++}`;
3950
const mysqlPod = {
4051
metadata: {
41-
name: `mysql-pod${this.podCnt++}`,
52+
name: podName,
4253
},
4354
spec: {
4455
containers: [
@@ -60,14 +71,22 @@ export class K8SApiService {
6071
}
6172

6273
async getAllPods() {
63-
const allPodInfos = await this.k8sApi.listNamespacedPod(this.namespace);
64-
const podNameList = [];
65-
allPodInfos.body.items.forEach(item => {
66-
podNameList.push(item.metadata.name);
67-
});
74+
const podList = await this.k8sApi.listNamespacedPod(this.namespace);
75+
const podResult = {};
76+
77+
for (const pod of podList.body.items) {
78+
const podName = pod.metadata.name;
79+
console.log(podName);
80+
81+
const podInfo = await this.k8sApi.readNamespacedPod(podName, this.namespace);
82+
const podIp = podInfo.body.status.podIP;
83+
console.log(podIp);
84+
85+
podResult[podName] = podIp;
86+
}
6887
return {
69-
podCnt : podNameList.length,
70-
podNameList,
88+
podCnt : Object.keys(podResult).length,
89+
podResult,
7190
};
7291
}
7392
}

0 commit comments

Comments
 (0)