1
1
const k8s = require ( '@kubernetes/client-node' ) ;
2
2
const kuberControllers = { } ;
3
3
4
- kuberControllers . getData = async ( req , res , next ) => {
4
+ kuberControllers . getPods = async ( req , res , next ) => {
5
5
try {
6
6
const kc = new k8s . KubeConfig ( ) ;
7
7
kc . loadFromDefault ( ) ;
@@ -11,7 +11,10 @@ kuberControllers.getData = async (req, res, next) => {
11
11
const main = async ( ) => {
12
12
try {
13
13
const podRes = await k8sApi . listNamespacedPod ( 'default' ) ;
14
- console . log ( 'Pod: ' , podRes . body ) ;
14
+ //console.log('Pods: ', podRes.body.items[0]);
15
+ for ( let i = 0 ; i < podRes . body . items . length ; i ++ ) {
16
+ console . log ( 'Pod: ' , podRes . body . items [ i ] . metadata . name ) ;
17
+ }
15
18
} catch ( err ) {
16
19
console . error ( err ) ;
17
20
}
@@ -23,4 +26,119 @@ kuberControllers.getData = async (req, res, next) => {
23
26
}
24
27
}
25
28
29
+ kuberControllers . getNamespace = async ( req , res , next ) => {
30
+ try {
31
+ const kc = new k8s . KubeConfig ( ) ;
32
+ kc . loadFromDefault ( ) ;
33
+
34
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
35
+
36
+ // const namespace = {
37
+ // metadata: {
38
+ // name: 'test',
39
+ // },
40
+ // };
41
+
42
+ const main = async ( ) => {
43
+ try {
44
+ // const createNamespaceRes = await k8sApi.createNamespace(namespace);
45
+ // console.log('New namespace created: ', createNamespaceRes.body);
46
+
47
+ const readNamespaceRes = await k8sApi . readNamespace ( namespace . metadata . name ) ;
48
+ console . log ( 'Namespcace: ' , readNamespaceRes . body ) ;
49
+
50
+ } catch ( err ) {
51
+ console . error ( err ) ;
52
+ }
53
+ } ;
54
+
55
+ main ( ) ;
56
+ } catch ( err ) {
57
+ return next ( err ) ;
58
+ }
59
+ }
60
+
61
+ kuberControllers . getResources = async ( req , res , next ) => {
62
+
63
+ try {
64
+ const kc = new k8s . KubeConfig ( ) ;
65
+ kc . loadFromDefault ( ) ;
66
+
67
+ const appsV1Api = kc . makeApiClient ( k8s . AppsV1Api ) ;
68
+ const coreV1Api = kc . makeApiClient ( k8s . CoreV1Api ) ;
69
+
70
+ const deploymentsRes = await appsV1Api . listNamespacedDeployment ( 'default' ) ;
71
+ //console.log('Deployments: ', deploymentsRes.body.items);
72
+
73
+ let deployments = [ ] ;
74
+ for ( const deployment of deploymentsRes . body . items ) {
75
+ //console.log('Deployment: ', deployment);
76
+ //console.log(deployment.spec.selector.matchLabels.app)
77
+ deployments . push ( {
78
+ name : deployment . metadata . name ,
79
+ status : deployment . status . conditions [ 0 ] . status ,
80
+ image : deployment . spec . template . spec . containers [ 0 ] . image ,
81
+ ports : [ ] ,
82
+ services : { } ,
83
+ app : deployment . spec . selector . matchLabels . app ,
84
+ } ) ;
85
+ }
86
+
87
+ // console.log('Deployments: ', deployments);
88
+
89
+ const servicesRes = await coreV1Api . listNamespacedService ( 'default' ) ;
90
+
91
+ for ( const service of servicesRes . body . items ) {
92
+ console . log ( 'Service: ' , service ) ;
93
+ // if (service.spec.selector && service.spec.selector.role && roles.includes(service.spec.selector.role)) {
94
+ // let filteredDeployments = deployments.filter(d => {
95
+ // return d.role === service.spec.selector.role;
96
+ // });
97
+ // if (filteredDeployments) {
98
+
99
+ for ( const d of deployments ) {
100
+ if ( d . app === service . spec . selector ?. app ) {
101
+ d . ports . push ( service . spec . ports [ 0 ] . port ) ;
102
+ d . services [ service . metadata . name ] = [ ] ;
103
+
104
+ const podRes = await coreV1Api . listNamespacedPod ( 'default' ) ;
105
+ for ( let pod of podRes . body . items ) {
106
+ //console.log('Pod: ', pod);
107
+ if ( pod . metadata . labels . app === d . app ) {
108
+ d . services [ service . metadata . name ] . push ( pod . metadata . name ) ;
109
+ }
110
+ }
111
+
112
+ }
113
+
114
+ }
115
+
116
+ // const podRes = await coreV1Api.listNamespacedPod('default');
117
+ // //console.log('Pods: ', podRes.body.items[0]);
118
+ // for (let pod of podRes.body.items) {
119
+ // //console.log('Pod: ', pod);
120
+ // for (let d of deployments) {
121
+ // if (pod.metadata.labels.app === d.app) {
122
+ // d.services[service.metadata.name].push(pod.metadata.name);
123
+ // }
124
+ // }
125
+ // }
126
+
127
+ // }
128
+ // }
129
+
130
+
131
+ }
132
+ console . log ( 'final deployments: ' , deployments ) ;
133
+ for ( let d of deployments ) {
134
+ console . log ( 'pods: ' , d . services ) ;
135
+ }
136
+
137
+
138
+ } catch ( err ) {
139
+ console . log ( 'Error: ' , err )
140
+ return next ( err ) ;
141
+ }
142
+ }
143
+
26
144
module . exports = kuberControllers ;
0 commit comments