@@ -22,6 +22,21 @@ type KubernetesObjectResponseBody =
2222/** Kubernetes API verbs. */
2323type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'list' | 'replace' ;
2424
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+
2540/**
2641 * Valid Content-Type header values for patch operations. See
2742 * https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/
@@ -74,13 +89,13 @@ export class KubernetesObjectApi extends ApisApi {
7489 * @param options Optional headers to use in the request.
7590 * @return Promise containing the request response and [[KubernetesObject]].
7691 */
77- public async create (
78- spec : KubernetesObject ,
92+ public async create < T extends KubernetesObject | KubernetesObject > (
93+ spec : T ,
7994 pretty ?: string ,
8095 dryRun ?: string ,
8196 fieldManager ?: string ,
8297 options : { headers : { [ name : string ] : string } } = { headers : { } } ,
83- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
98+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
8499 // verify required parameter 'spec' is not null or undefined
85100 if ( spec === null || spec === undefined ) {
86101 throw new Error ( 'Required parameter spec was null or undefined when calling create.' ) ;
@@ -218,14 +233,14 @@ export class KubernetesObjectApi extends ApisApi {
218233 * @param options Optional headers to use in the request.
219234 * @return Promise containing the request response and [[KubernetesObject]].
220235 */
221- public async patch (
222- spec : KubernetesObject ,
236+ public async patch < T extends KubernetesObject | KubernetesObject > (
237+ spec : T ,
223238 pretty ?: string ,
224239 dryRun ?: string ,
225240 fieldManager ?: string ,
226241 force ?: boolean ,
227242 options : { headers : { [ name : string ] : string } } = { headers : { } } ,
228- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
243+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
229244 // verify required parameter 'spec' is not null or undefined
230245 if ( spec === null || spec === undefined ) {
231246 throw new Error ( 'Required parameter spec was null or undefined when calling patch.' ) ;
@@ -275,17 +290,24 @@ export class KubernetesObjectApi extends ApisApi {
275290 * @param options Optional headers to use in the request.
276291 * @return Promise containing the request response and [[KubernetesObject]].
277292 */
278- public async read (
279- spec : KubernetesObject ,
293+ public async read < T extends KubernetesObject | KubernetesObject > (
294+ spec : KubernetesObjectHeader < T > ,
280295 pretty ?: string ,
281296 exact ?: boolean ,
282297 exportt ?: boolean ,
283298 options : { headers : { [ name : string ] : string } } = { headers : { } } ,
284- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
299+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
285300 // verify required parameter 'spec' is not null or undefined
286301 if ( spec === null || spec === undefined ) {
287302 throw new Error ( 'Required parameter spec was null or undefined when calling read.' ) ;
288303 }
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+ throw new Error ( 'Required parameter spec.apiVersion was null or undefined when calling read.' ) ;
310+ }
289311
290312 const localVarPath = await this . specUriPath ( spec , 'read' ) ;
291313 const localVarQueryParameters : any = { } ;
@@ -331,7 +353,7 @@ export class KubernetesObjectApi extends ApisApi {
331353 * @param options Optional headers to use in the request.
332354 * @return Promise containing the request response and [[KubernetesListObject<KubernetesObject>]].
333355 */
334- public async list (
356+ public async list < T extends KubernetesObject | KubernetesObject > (
335357 apiVersion : string ,
336358 kind : string ,
337359 namespace ?: string ,
@@ -343,7 +365,7 @@ export class KubernetesObjectApi extends ApisApi {
343365 limit ?: number ,
344366 continueToken ?: string ,
345367 options : { headers : { [ name : string ] : string } } = { headers : { } } ,
346- ) : Promise < { body : KubernetesListObject < KubernetesObject > ; response : http . IncomingMessage } > {
368+ ) : Promise < { body : KubernetesListObject < T > ; response : http . IncomingMessage } > {
347369 // verify required parameters 'apiVersion', 'kind' is not null or undefined
348370 if ( apiVersion === null || apiVersion === undefined ) {
349371 throw new Error ( 'Required parameter apiVersion was null or undefined when calling list.' ) ;
@@ -418,13 +440,13 @@ export class KubernetesObjectApi extends ApisApi {
418440 * @param options Optional headers to use in the request.
419441 * @return Promise containing the request response and [[KubernetesObject]].
420442 */
421- public async replace (
422- spec : KubernetesObject ,
443+ public async replace < T extends KubernetesObject | KubernetesObject > (
444+ spec : T ,
423445 pretty ?: string ,
424446 dryRun ?: string ,
425447 fieldManager ?: string ,
426448 options : { headers : { [ name : string ] : string } } = { headers : { } } ,
427- ) : Promise < { body : KubernetesObject ; response : http . IncomingMessage } > {
449+ ) : Promise < { body : T ; response : http . IncomingMessage } > {
428450 // verify required parameter 'spec' is not null or undefined
429451 if ( spec === null || spec === undefined ) {
430452 throw new Error ( 'Required parameter spec was null or undefined when calling replace.' ) ;
@@ -477,7 +499,7 @@ export class KubernetesObjectApi extends ApisApi {
477499 *
478500 * @param spec Kubernetes resource spec which must define kind and apiVersion properties.
479501 * @param action API action, see [[K8sApiAction]].
480- * @return tail of resource-specific URI
502+ * @return tail of resource-specific URIDeploym
481503 */
482504 protected async specUriPath ( spec : KubernetesObject , action : KubernetesApiAction ) : Promise < string > {
483505 if ( ! spec . kind ) {
@@ -592,12 +614,36 @@ export class KubernetesObjectApi extends ApisApi {
592614 }
593615 }
594616
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+
595641 /**
596642 * Standard Kubernetes request wrapped in a Promise.
597643 */
598644 protected async requestPromise < T extends KubernetesObjectResponseBody = KubernetesObject > (
599645 requestOptions : request . Options ,
600- tipe : string = 'KubernetesObject' ,
646+ type ? : string ,
601647 ) : Promise < { body : T ; response : http . IncomingMessage } > {
602648 let authenticationPromise = Promise . resolve ( ) ;
603649 if ( this . authentications . BearerToken . apiKey ) {
@@ -616,11 +662,15 @@ export class KubernetesObjectApi extends ApisApi {
616662 await interceptorPromise ;
617663
618664 return new Promise < { body : T ; response : http . IncomingMessage } > ( ( resolve , reject ) => {
619- request ( requestOptions , ( error , response , body ) => {
665+ request ( requestOptions , async ( error , response , body ) => {
620666 if ( error ) {
621667 reject ( error ) ;
622668 } 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 ) ;
624674 if ( response . statusCode && response . statusCode >= 200 && response . statusCode <= 299 ) {
625675 resolve ( { response, body } ) ;
626676 } else {
0 commit comments