Skip to content

Commit d48164c

Browse files
committed
feat: cache context results in k8
Signed-off-by: Ivo Yankov <[email protected]>
1 parent 1083862 commit d48164c

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

src/commands/context/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class ContextCommandTasks {
4141
let clusterAliases = Templates.parseClusterAliases(argv[flags.clusterName.name])
4242
let contextName = argv[flags.context.name]
4343

44-
const kubeContexts = await this.parent.getK8().getKubeConfig().getContexts()
44+
const kubeContexts = await this.parent.getK8().getContexts()
4545

4646
if (isQuiet) {
4747
const currentCluster = (await this.parent.getK8().getKubeConfig().getCurrentCluster())

src/core/config/local_config_data.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export interface Deployment {
2121

2222
// an alias for the cluster, provided during the configuration
2323
// of the deployment, must be unique
24-
export type Deployments = Record<string, Deployment>;
24+
export type Deployments = Record<string, Deployment>
2525

2626
export interface LocalConfigData {
27-
userEmailAddress: string;
28-
deployments: Deployments;
29-
currentDeploymentName: string;
27+
userEmailAddress: string
28+
deployments: Deployments
29+
currentDeploymentName: string
3030
}

src/core/k8.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { flags } from '../commands/index.js'
2323
import { SoloError, IllegalArgumentError, MissingArgumentError } from './errors.js'
2424
import * as tar from 'tar'
2525
import { v4 as uuid4 } from 'uuid'
26-
import { type V1Lease, V1ObjectMeta, V1Secret, Config } from '@kubernetes/client-node'
26+
import { type V1Lease, V1ObjectMeta, V1Secret, type Context } from '@kubernetes/client-node'
2727
import { sleep } from './helpers.js'
2828
import { type ConfigManager, constants } from './index.js'
2929
import * as stream from 'node:stream'
@@ -44,6 +44,8 @@ interface TDirectoryData {directory: boolean; owner: string; group: string; size
4444
* For parallel execution, create separate instances by invoking clone()
4545
*/
4646
export class K8 {
47+
private _cachedContexts: Context[]
48+
4749
static PodReadyCondition = new Map<string, string>()
4850
.set(constants.POD_CONDITION_READY, constants.POD_CONDITION_STATUS_TRUE)
4951
private kubeConfig!: k8s.KubeConfig
@@ -320,15 +322,24 @@ export class K8 {
320322
* Get a list of contexts
321323
* @returns a list of context names
322324
*/
323-
getContexts () {
325+
getContextNames () : string[] {
324326
const contexts: string[] = []
325-
for (const context of this.kubeConfig.getContexts()) {
327+
328+
for (const context of this.getContexts()) {
326329
contexts.push(context.name)
327330
}
328331

329332
return contexts
330333
}
331334

335+
getContexts () :Context[] {
336+
if (!this._cachedContexts) {
337+
this._cachedContexts = this.kubeConfig.getContexts()
338+
}
339+
340+
return this._cachedContexts
341+
}
342+
332343
/**
333344
* List files and directories in a container
334345
*

test/e2e/integration/core/k8_e2e.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ describe('K8', () => {
131131
expect(namespaces).to.contain(constants.DEFAULT_NAMESPACE)
132132
}).timeout(defaultTimeout)
133133

134+
it('should be able to list context names', () => {
135+
const contexts = k8.getContextNames()
136+
expect(contexts).not.to.have.lengthOf(0)
137+
}).timeout(defaultTimeout)
138+
134139
it('should be able to list contexts', () => {
135140
const contexts = k8.getContexts()
136141
expect(contexts).not.to.have.lengthOf(0)

test/unit/commands/context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ describe('ContextCommandTasks unit tests', () => {
4545
const getBaseCommandOpts = () => {
4646
const loggerStub = sinon.createStubInstance(SoloLogger)
4747
const k8Stub = sinon.createStubInstance(K8)
48-
const kubeConfigStub = sinon.createStubInstance(KubeConfig)
49-
kubeConfigStub.getContexts.returns([
48+
k8Stub.getContexts.returns([
5049
{ cluster: 'cluster-1', user: 'user-1', name: 'context-1', namespace: 'deployment-1' },
5150
{ cluster: 'cluster-2', user: 'user-2', name: 'context-2', namespace: 'deployment-2' },
5251
{ cluster: 'cluster-3', user: 'user-3', name: 'context-3', namespace: 'deployment-3' },
5352
])
53+
const kubeConfigStub = sinon.createStubInstance(KubeConfig)
5454
kubeConfigStub.getCurrentContext.returns('context-3')
5555
kubeConfigStub.getCurrentContext.returns('context-3')
5656
kubeConfigStub.getCurrentCluster.returns({

0 commit comments

Comments
 (0)