File tree 3 files changed +62
-1
lines changed
3 files changed +62
-1
lines changed Original file line number Diff line number Diff line change
1
+ const k8s = require ( '@kubernetes/client-node' ) ;
2
+
3
+ let kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromCluster ( ) ;
5
+
6
+ let k8sApi = new k8s . Core_v1Api ( kc . getCurrentCluster ( ) [ 'server' ] ) ;
7
+ k8sApi . setDefaultAuthentication ( kc ) ;
8
+
9
+ k8sApi . listNamespacedPod ( 'default' )
10
+ . then ( ( res ) => {
11
+ console . log ( res . body ) ;
12
+ } )
13
+ . catch ( ( err ) => {
14
+ console . log ( err ) ;
15
+ } ) ;
16
+
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " @kubernetes/client-node" ,
3
- "version" : " 0.2.1 " ,
3
+ "version" : " 0.4.0 " ,
4
4
"description" : " NodeJS client for kubernetes" ,
5
5
"repository" : {
6
6
"type" : " git" ,
Original file line number Diff line number Diff line change @@ -210,6 +210,51 @@ export class KubeConfig {
210
210
this . users = newUsers ( obj [ 'users' ] ) ;
211
211
this . currentContext = obj [ 'current-context' ] ;
212
212
}
213
+
214
+ public loadFromCluster ( ) {
215
+ const host = process . env . KUBERNETES_SERVICE_HOST ;
216
+ const port = process . env . KUBERNETES_SERVICE_PORT ;
217
+ const clusterName = 'inCluster' ;
218
+ const userName = 'inClusterUser' ;
219
+ const contextName = 'inClusterContext' ;
220
+
221
+ let scheme = 'https' ;
222
+ if ( port === '80' || port === '8080' || port === '8001' ) {
223
+ scheme = 'http' ;
224
+ }
225
+
226
+ this . clusters = [
227
+ {
228
+ name : clusterName ,
229
+ caFile : Config . SERVICEACCOUNT_CA_PATH ,
230
+ caData : null ,
231
+ server : `${ scheme } ://${ host } :${ port } ` ,
232
+ skipTLSVerify : false
233
+ }
234
+ ]
235
+ this . users = [
236
+ {
237
+ name : userName ,
238
+ token : fs . readFileSync ( Config . SERVICEACCOUNT_TOKEN_PATH ) . toString ( ) ,
239
+ // empty defaults, fields are required...
240
+ certData : null ,
241
+ certFile : null ,
242
+ keyData : null ,
243
+ keyFile : null ,
244
+ authProvider : null ,
245
+ username : null ,
246
+ password : null
247
+ }
248
+ ]
249
+ this . contexts = [
250
+ {
251
+ cluster : clusterName ,
252
+ user : userName ,
253
+ name : contextName
254
+ }
255
+ ] ;
256
+ this . currentContext = contextName ;
257
+ }
213
258
}
214
259
215
260
export class Config {
You can’t perform that action at this time.
0 commit comments