Skip to content

Commit bb63943

Browse files
author
David Dooling
committed
Address PR #446 reviewer comments
Remove simple static values and methods. Add cache for API resource response.
1 parent d292a4d commit bb63943

File tree

2 files changed

+177
-74
lines changed

2 files changed

+177
-74
lines changed

src/object.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,15 @@ export class KubernetesObjectApi extends ApisApi {
5151
*/
5252
public static makeApiClient(kc: KubeConfig): KubernetesObjectApi {
5353
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);
6055
return client;
6156
}
6257

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-
7758
/** 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> = {};
7963

8064
/**
8165
* Create any Kubernetes resource.
@@ -385,6 +369,17 @@ export class KubernetesObjectApi extends ApisApi {
385369
return this.requestPromise(localVarRequestOptions);
386370
}
387371

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+
388383
/**
389384
* Use spec information to construct resource URI path. If any required information in not provided, an Error is
390385
* 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 {
416411
parts.push('namespaces', encodeURIComponent(String(spec.metadata.namespace)));
417412
}
418413
parts.push(resource.name);
419-
if (KubernetesObjectApi.appendName(action)) {
414+
if (action !== 'create') {
420415
if (!spec.metadata.name) {
421416
throw new Error('Required spec property name is not set');
422417
}
@@ -457,6 +452,9 @@ export class KubernetesObjectApi extends ApisApi {
457452
* Get metadata from Kubernetes API for resources described by `kind` and `apiVersion`. If it is unable to find the
458453
* resource `kind` under the provided `apiVersion`, `undefined` is returned.
459454
*
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+
*
460458
* @param apiVersion Kubernetes API version, e.g., 'v1' or 'apps/v1'.
461459
* @param kind Kubernetes resource kind, e.g., 'Pod' or 'Namespace'.
462460
* @return Promise of the resource metadata or `undefined` if the resource is not found.
@@ -471,6 +469,13 @@ export class KubernetesObjectApi extends ApisApi {
471469
throw new Error('Required parameter kind was null or undefined when calling resource');
472470
}
473471

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+
474479
const localVarPath = this.apiVersionPath(apiVersion);
475480
const localVarQueryParameters: any = {};
476481
const localVarHeaderParams = this.generateHeaders({});
@@ -489,8 +494,8 @@ export class KubernetesObjectApi extends ApisApi {
489494
localVarRequestOptions,
490495
'V1APIResourceList',
491496
);
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);
494499
} catch (e) {
495500
e.message = `Failed to fetch resource metadata for ${apiVersion}/${kind}: ${e.message}`;
496501
throw e;

0 commit comments

Comments
 (0)