@@ -22,6 +22,21 @@ type KubernetesObjectResponseBody =
22
22
/** Kubernetes API verbs. */
23
23
type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'list' | 'replace' ;
24
24
25
+ type KubernetesObjectHeader < T extends KubernetesObject | KubernetesObject > = Pick <
26
+ T ,
27
+ 'apiVersion' | 'kind'
28
+ > & {
29
+ metadata : {
30
+ name : string ;
31
+ namespace : string ;
32
+ } ;
33
+ } ;
34
+
35
+ interface GroupVersion {
36
+ group : string ;
37
+ version : string ;
38
+ }
39
+
25
40
/**
26
41
* Valid Content-Type header values for patch operations. See
27
42
* https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/
@@ -74,13 +89,13 @@ export class KubernetesObjectApi extends ApisApi {
74
89
* @param options Optional headers to use in the request.
75
90
* @return Promise containing the request response and [[KubernetesObject]].
76
91
*/
77
- public async create (
78
- spec : KubernetesObject ,
92
+ public async create < T extends KubernetesObject | KubernetesObject > (
93
+ spec : T ,
79
94
pretty ?: string ,
80
95
dryRun ?: string ,
81
96
fieldManager ?: string ,
82
97
options : { headers : { [ name : string ] : string } } = { headers : { } } ,
83
- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
98
+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
84
99
// verify required parameter 'spec' is not null or undefined
85
100
if ( spec === null || spec === undefined ) {
86
101
throw new Error ( 'Required parameter spec was null or undefined when calling create.' ) ;
@@ -218,14 +233,14 @@ export class KubernetesObjectApi extends ApisApi {
218
233
* @param options Optional headers to use in the request.
219
234
* @return Promise containing the request response and [[KubernetesObject]].
220
235
*/
221
- public async patch (
222
- spec : KubernetesObject ,
236
+ public async patch < T extends KubernetesObject | KubernetesObject > (
237
+ spec : T ,
223
238
pretty ?: string ,
224
239
dryRun ?: string ,
225
240
fieldManager ?: string ,
226
241
force ?: boolean ,
227
242
options : { headers : { [ name : string ] : string } } = { headers : { } } ,
228
- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
243
+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
229
244
// verify required parameter 'spec' is not null or undefined
230
245
if ( spec === null || spec === undefined ) {
231
246
throw new Error ( 'Required parameter spec was null or undefined when calling patch.' ) ;
@@ -275,17 +290,24 @@ export class KubernetesObjectApi extends ApisApi {
275
290
* @param options Optional headers to use in the request.
276
291
* @return Promise containing the request response and [[KubernetesObject]].
277
292
*/
278
- public async read (
279
- spec : KubernetesObject ,
293
+ public async read < T extends KubernetesObject | KubernetesObject > (
294
+ spec : KubernetesObjectHeader < T > ,
280
295
pretty ?: string ,
281
296
exact ?: boolean ,
282
297
exportt ?: boolean ,
283
298
options : { headers : { [ name : string ] : string } } = { headers : { } } ,
284
- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
299
+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
285
300
// verify required parameter 'spec' is not null or undefined
286
301
if ( spec === null || spec === undefined ) {
287
302
throw new Error ( 'Required parameter spec was null or undefined when calling read.' ) ;
288
303
}
304
+ // verify required parameter 'kind' is not null or undefined
305
+ if ( spec . kind === null || spec . kind === undefined ) {
306
+ throw new Error ( 'Required parameter spec.kind was null or undefined when calling read.' ) ;
307
+ }
308
+ if ( ! spec . apiVersion ) {
309
+ spec . apiVersion = 'v1' ;
310
+ }
289
311
290
312
const localVarPath = await this . specUriPath ( spec , 'read' ) ;
291
313
const localVarQueryParameters : any = { } ;
@@ -331,7 +353,7 @@ export class KubernetesObjectApi extends ApisApi {
331
353
* @param options Optional headers to use in the request.
332
354
* @return Promise containing the request response and [[KubernetesListObject<KubernetesObject>]].
333
355
*/
334
- public async list (
356
+ public async list < T extends KubernetesObject | KubernetesObject > (
335
357
apiVersion : string ,
336
358
kind : string ,
337
359
namespace ?: string ,
@@ -343,7 +365,7 @@ export class KubernetesObjectApi extends ApisApi {
343
365
limit ?: number ,
344
366
continueToken ?: string ,
345
367
options : { headers : { [ name : string ] : string } } = { headers : { } } ,
346
- ) : Promise < { body : KubernetesListObject < KubernetesObject > ; response : http . IncomingMessage } > {
368
+ ) : Promise < { body : KubernetesListObject < T > ; response : http . IncomingMessage } > {
347
369
// verify required parameters 'apiVersion', 'kind' is not null or undefined
348
370
if ( apiVersion === null || apiVersion === undefined ) {
349
371
throw new Error ( 'Required parameter apiVersion was null or undefined when calling list.' ) ;
@@ -418,13 +440,13 @@ export class KubernetesObjectApi extends ApisApi {
418
440
* @param options Optional headers to use in the request.
419
441
* @return Promise containing the request response and [[KubernetesObject]].
420
442
*/
421
- public async replace (
422
- spec : KubernetesObject ,
443
+ public async replace < T extends KubernetesObject | KubernetesObject > (
444
+ spec : T ,
423
445
pretty ?: string ,
424
446
dryRun ?: string ,
425
447
fieldManager ?: string ,
426
448
options : { headers : { [ name : string ] : string } } = { headers : { } } ,
427
- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
449
+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
428
450
// verify required parameter 'spec' is not null or undefined
429
451
if ( spec === null || spec === undefined ) {
430
452
throw new Error ( 'Required parameter spec was null or undefined when calling replace.' ) ;
@@ -477,7 +499,7 @@ export class KubernetesObjectApi extends ApisApi {
477
499
*
478
500
* @param spec Kubernetes resource spec which must define kind and apiVersion properties.
479
501
* @param action API action, see [[K8sApiAction]].
480
- * @return tail of resource-specific URI
502
+ * @return tail of resource-specific URIDeploym
481
503
*/
482
504
protected async specUriPath ( spec : KubernetesObject , action : KubernetesApiAction ) : Promise < string > {
483
505
if ( ! spec . kind ) {
@@ -592,12 +614,36 @@ export class KubernetesObjectApi extends ApisApi {
592
614
}
593
615
}
594
616
617
+ protected async getSerializationType ( apiVersion ?: string , kind ?: string ) : Promise < string > {
618
+ if ( apiVersion === undefined || kind === undefined ) {
619
+ return 'KubernetesObject' ;
620
+ }
621
+ // Types are defined in src/gen/api/models with the format "<Version><Kind>".
622
+ // Version and Kind are in PascalCase.
623
+ const gv = this . groupVersion ( apiVersion ) ;
624
+ const version = gv . version . charAt ( 0 ) . toUpperCase ( ) + gv . version . slice ( 1 ) ;
625
+ return `${ version } ${ kind } ` ;
626
+ }
627
+
628
+ protected groupVersion ( apiVersion : string ) : GroupVersion {
629
+ const v = apiVersion . split ( '/' ) ;
630
+ return v . length === 1
631
+ ? {
632
+ group : 'core' ,
633
+ version : apiVersion ,
634
+ }
635
+ : {
636
+ group : v [ 0 ] ,
637
+ version : v [ 1 ] ,
638
+ } ;
639
+ }
640
+
595
641
/**
596
642
* Standard Kubernetes request wrapped in a Promise.
597
643
*/
598
644
protected async requestPromise < T extends KubernetesObjectResponseBody = KubernetesObject > (
599
645
requestOptions : request . Options ,
600
- tipe : string = 'KubernetesObject' ,
646
+ type ? : string ,
601
647
) : Promise < { body : T ; response : http . IncomingMessage } > {
602
648
let authenticationPromise = Promise . resolve ( ) ;
603
649
if ( this . authentications . BearerToken . apiKey ) {
@@ -616,11 +662,15 @@ export class KubernetesObjectApi extends ApisApi {
616
662
await interceptorPromise ;
617
663
618
664
return new Promise < { body : T ; response : http . IncomingMessage } > ( ( resolve , reject ) => {
619
- request ( requestOptions , ( error , response , body ) => {
665
+ request ( requestOptions , async ( error , response , body ) => {
620
666
if ( error ) {
621
667
reject ( error ) ;
622
668
} else {
623
- body = ObjectSerializer . deserialize ( body , tipe ) ;
669
+ // TODO(schrodit): support correct deserialization to KubernetesObject.
670
+ if ( type === undefined ) {
671
+ type = await this . getSerializationType ( body . apiVersion , body . kind ) ;
672
+ }
673
+ body = ObjectSerializer . deserialize ( body , type ) ;
624
674
if ( response . statusCode && response . statusCode >= 200 && response . statusCode <= 299 ) {
625
675
resolve ( { response, body } ) ;
626
676
} else {
0 commit comments