@@ -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