@@ -51,31 +51,15 @@ export class KubernetesObjectApi extends ApisApi {
51
51
*/
52
52
public static makeApiClient ( kc : KubeConfig ) : KubernetesObjectApi {
53
53
const client = kc . makeApiClient ( KubernetesObjectApi ) ;
54
- if ( kc . currentContext ) {
55
- const currentContext = kc . getContextObject ( kc . currentContext ) ;
56
- if ( currentContext && currentContext . namespace ) {
57
- client . defaultNamespace = currentContext . namespace ;
58
- }
59
- }
54
+ client . setDefaultNamespace ( kc ) ;
60
55
return client ;
61
56
}
62
57
63
- /**
64
- * Return whether the name of the resource should be appended to the API URI path. When creating resources, it is
65
- * not appended.
66
- *
67
- * @param action API action, see [[K8sApiAction]]
68
- * @return true if name should be appended to URI
69
- */
70
- protected static appendName ( action : KubernetesApiAction ) : boolean {
71
- return action !== 'create' ;
72
- }
73
-
74
- /** Default default namespace, so to speak. */
75
- private static readonly defaultDefaultNamespace = 'default' ;
76
-
77
58
/** Initialize the default namespace. May be overwritten by context. */
78
- protected defaultNamespace : string = KubernetesObjectApi . defaultDefaultNamespace ;
59
+ protected defaultNamespace : string = 'default' ;
60
+
61
+ /** Cache resource API response. */
62
+ protected apiVersionResourceCache : Record < string , V1APIResourceList > = { } ;
79
63
80
64
/**
81
65
* Create any Kubernetes resource.
@@ -385,6 +369,17 @@ export class KubernetesObjectApi extends ApisApi {
385
369
return this . requestPromise ( localVarRequestOptions ) ;
386
370
}
387
371
372
+ /** Set default namespace from current context, if available. */
373
+ protected setDefaultNamespace ( kc : KubeConfig ) : string {
374
+ if ( kc . currentContext ) {
375
+ const currentContext = kc . getContextObject ( kc . currentContext ) ;
376
+ if ( currentContext && currentContext . namespace ) {
377
+ this . defaultNamespace = currentContext . namespace ;
378
+ }
379
+ }
380
+ return this . defaultNamespace ;
381
+ }
382
+
388
383
/**
389
384
* Use spec information to construct resource URI path. If any required information in not provided, an Error is
390
385
* thrown. If an `apiVersion` is not provided, 'v1' is used. If a `metadata.namespace` is not provided for a
@@ -416,7 +411,7 @@ export class KubernetesObjectApi extends ApisApi {
416
411
parts . push ( 'namespaces' , encodeURIComponent ( String ( spec . metadata . namespace ) ) ) ;
417
412
}
418
413
parts . push ( resource . name ) ;
419
- if ( KubernetesObjectApi . appendName ( action ) ) {
414
+ if ( action !== 'create' ) {
420
415
if ( ! spec . metadata . name ) {
421
416
throw new Error ( 'Required spec property name is not set' ) ;
422
417
}
@@ -457,6 +452,9 @@ export class KubernetesObjectApi extends ApisApi {
457
452
* Get metadata from Kubernetes API for resources described by `kind` and `apiVersion`. If it is unable to find the
458
453
* resource `kind` under the provided `apiVersion`, `undefined` is returned.
459
454
*
455
+ * This method caches responses from the Kubernetes API to use for future requests. If the cache for apiVersion
456
+ * exists but the kind is not found the request is attempted again.
457
+ *
460
458
* @param apiVersion Kubernetes API version, e.g., 'v1' or 'apps/v1'.
461
459
* @param kind Kubernetes resource kind, e.g., 'Pod' or 'Namespace'.
462
460
* @return Promise of the resource metadata or `undefined` if the resource is not found.
@@ -471,6 +469,13 @@ export class KubernetesObjectApi extends ApisApi {
471
469
throw new Error ( 'Required parameter kind was null or undefined when calling resource' ) ;
472
470
}
473
471
472
+ if ( this . apiVersionResourceCache [ apiVersion ] ) {
473
+ const resource = this . apiVersionResourceCache [ apiVersion ] . resources . find ( ( r ) => r . kind === kind ) ;
474
+ if ( resource ) {
475
+ return resource ;
476
+ }
477
+ }
478
+
474
479
const localVarPath = this . apiVersionPath ( apiVersion ) ;
475
480
const localVarQueryParameters : any = { } ;
476
481
const localVarHeaderParams = this . generateHeaders ( { } ) ;
@@ -489,8 +494,8 @@ export class KubernetesObjectApi extends ApisApi {
489
494
localVarRequestOptions ,
490
495
'V1APIResourceList' ,
491
496
) ;
492
- const apiResourceList = getApiResponse . body ;
493
- return apiResourceList . resources . find ( ( r ) => r . kind === kind ) ;
497
+ this . apiVersionResourceCache [ apiVersion ] = getApiResponse . body ;
498
+ return this . apiVersionResourceCache [ apiVersion ] . resources . find ( ( r ) => r . kind === kind ) ;
494
499
} catch ( e ) {
495
500
e . message = `Failed to fetch resource metadata for ${ apiVersion } /${ kind } : ${ e . message } ` ;
496
501
throw e ;
0 commit comments