Skip to content

Commit b871e44

Browse files
committed
rename testClusterConnection to testContextConnection
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
1 parent cb91806 commit b871e44

File tree

6 files changed

+20
-76
lines changed

6 files changed

+20
-76
lines changed

src/commands/cluster/tasks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ClusterCommandTasks {
4848

4949
localConfig.clusterContextMapping[cluster] = context;
5050
}
51-
if (!(await self.parent.getK8().testClusterConnection(context, cluster))) {
51+
if (!(await self.parent.getK8().testContextConnection(context))) {
5252
subTask.title = `${subTask.title} - ${chalk.red('Cluster connection failed')}`;
5353
throw new SoloError(`${ErrorMessages.INVALID_CONTEXT_FOR_CLUSTER_DETAILED(context, cluster)}`);
5454
}
@@ -322,7 +322,7 @@ export class ClusterCommandTasks {
322322
}
323323
}
324324

325-
const connectionValid = await this.parent.getK8().testClusterConnection(selectedContext, selectedCluster);
325+
const connectionValid = await this.parent.getK8().testContextConnection(selectedContext);
326326
if (!connectionValid) {
327327
throw new SoloError(ErrorMessages.INVALID_CONTEXT_FOR_CLUSTER(selectedContext, selectedCluster));
328328
}

src/commands/deployment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class DeploymentCommand extends BaseCommand {
9696
subTasks.push({
9797
title: `Testing connection to cluster: ${chalk.cyan(cluster)}`,
9898
task: async (_, task) => {
99-
if (!(await self.k8.testClusterConnection(context, cluster))) {
99+
if (!(await self.k8.testContextConnection(context))) {
100100
task.title = `${task.title} - ${chalk.red('Cluster connection failed')}`;
101101

102102
throw new SoloError(`Cluster connection failed for: ${cluster}`);

src/core/k8.ts

+3-43
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*/
44
import * as k8s from '@kubernetes/client-node';
5+
import {type Context, type V1Lease, V1ObjectMeta, V1Secret, type V1Pod} from '@kubernetes/client-node';
56
import fs from 'fs';
67
import net from 'net';
78
import os from 'os';
89
import path from 'path';
910
import {Flags as flags} from '../commands/flags.js';
10-
import {SoloError, IllegalArgumentError, MissingArgumentError} from './errors.js';
11+
import {IllegalArgumentError, MissingArgumentError, SoloError} from './errors.js';
1112
import * as tar from 'tar';
1213
import {v4 as uuid4} from 'uuid';
13-
import {type V1Lease, V1ObjectMeta, V1Secret, type Context, type V1Pod} from '@kubernetes/client-node';
1414
import * as stream from 'node:stream';
1515
import type * as http from 'node:http';
1616
import type * as WebSocket from 'ws';
@@ -26,7 +26,6 @@ import {Duration} from './time/duration.js';
2626
import {inject, injectable} from 'tsyringe-neo';
2727
import {patchInject} from './container_helper.js';
2828
import type {Namespace} from './config/remote/types.js';
29-
import type {Cluster} from '@kubernetes/client-node/dist/config_types.js';
3029
import type TDirectoryData from './kube/t_directory_data.js';
3130

3231
/**
@@ -235,22 +234,6 @@ export class K8 {
235234
return result.body.items;
236235
}
237236

238-
/**
239-
* Get host IP of a podName
240-
* @param podNameName - name of the podName
241-
* @returns podName IP
242-
*/
243-
public async getPodIP(podNameName: string) {
244-
const pod = await this.getPodByName(podNameName);
245-
if (pod && pod.status && pod.status.podIP) {
246-
this.logger.debug(`Found pod IP for ${podNameName}: ${pod.status.podIP}`);
247-
return pod.status.podIP;
248-
}
249-
250-
this.logger.debug(`Unable to find pod IP for ${podNameName}`);
251-
throw new SoloError(`unable to find host IP of podName: ${podNameName}`);
252-
}
253-
254237
/**
255238
* Get a svc by name
256239
* @param name - svc name
@@ -872,29 +855,6 @@ export class K8 {
872855
}
873856
}
874857

875-
/**
876-
* to test the connection to a pod within the network
877-
* @param host - the host of the target connection
878-
* @param port - the port of the target connection
879-
*/
880-
public testSocketConnection(host: string, port: number) {
881-
const self = this;
882-
883-
return new Promise<boolean>((resolve, reject) => {
884-
const s = new net.Socket();
885-
s.on('error', e => {
886-
s.destroy();
887-
reject(new SoloError(`failed to connect to '${host}:${port}': ${e.message}`, e));
888-
});
889-
890-
s.connect(port, host, () => {
891-
self.logger.debug(`Connection test successful: ${host}:${port}`);
892-
s.destroy();
893-
resolve(true);
894-
});
895-
});
896-
}
897-
898858
/**
899859
* Stop the port forwarder server
900860
*
@@ -1179,7 +1139,7 @@ export class K8 {
11791139

11801140
// --------------------------------------- Utility Methods --------------------------------------- //
11811141

1182-
public async testClusterConnection(context: string, cluster: string): Promise<boolean> {
1142+
public async testContextConnection(context: string): Promise<boolean> {
11831143
this.kubeConfig.setCurrentContext(context);
11841144

11851145
const tempKubeClient = this.kubeConfig.makeApiClient(k8s.CoreV1Api);

test/e2e/integration/core/account_manager.test.ts

-9
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,9 @@ e2eTestSuite(namespace, argv, undefined, undefined, undefined, undefined, undefi
5757
// ports should be opened
5858
// @ts-expect-error - TS2341: Property _portForwards is private and only accessible within class AccountManager
5959
accountManager._portForwards.push(await k8.portForward(podName, localPort, podPort));
60-
const status = await k8.testSocketConnection(localHost, localPort);
61-
expect(status, 'test connection status should be true').to.be.ok;
6260

6361
// ports should be closed
6462
await accountManager.close();
65-
try {
66-
await k8.testSocketConnection(localHost, localPort);
67-
} catch (e) {
68-
expect(e.message, 'expect failed test connection').to.include(
69-
`failed to connect to '${localHost}:${localPort}'`,
70-
);
71-
}
7263
expect(
7364
// @ts-expect-error - TS2341: Property _portForwards is private and only accessible within class AccountManager
7465
accountManager._portForwards,

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

-7
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,6 @@ describe('K8', () => {
164164
expect(pods).to.have.lengthOf(1);
165165
}).timeout(defaultTimeout);
166166

167-
it('should be able to detect pod IP of a pod', async () => {
168-
const pods = await k8.getPodsByLabel([`app=${podLabelValue}`]);
169-
const podName = pods[0].metadata.name;
170-
await expect(k8.getPodIP(podName)).to.eventually.not.be.null;
171-
await expect(k8.getPodIP('INVALID')).to.be.rejectedWith(SoloError);
172-
}).timeout(defaultTimeout);
173-
174167
it('should be able to check if a path is directory inside a container', async () => {
175168
const pods = await k8.getPodsByLabel([`app=${podLabelValue}`]);
176169
const podName = pods[0].metadata.name;

test/unit/commands/cluster.test.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('ClusterCommand unit tests', () => {
146146
// @ts-expect-error - TS2344: Type CommandFlag does not satisfy the constraint string | number | symbol
147147
stubbedFlags: Record<CommandFlag, any>[] = [],
148148
opts: any = {
149-
testClusterConnectionError: false,
149+
testContextConnectionError: false,
150150
},
151151
) => {
152152
const loggerStub = sandbox.createStubInstance(SoloLogger);
@@ -156,10 +156,10 @@ describe('ClusterCommand unit tests', () => {
156156
k8Stub.isPrometheusInstalled.returns(new Promise<boolean>(() => true));
157157
k8Stub.isCertManagerInstalled.returns(new Promise<boolean>(() => true));
158158

159-
if (opts.testClusterConnectionError) {
160-
k8Stub.testClusterConnection.resolves(false);
159+
if (opts.testContextConnectionError) {
160+
k8Stub.testContextConnection.resolves(false);
161161
} else {
162-
k8Stub.testClusterConnection.resolves(true);
162+
k8Stub.testContextConnection.resolves(true);
163163
}
164164

165165
const kubeConfigClusterObject = {
@@ -465,7 +465,7 @@ describe('ClusterCommand unit tests', () => {
465465

466466
it('throws error when context is invalid', async () => {
467467
const opts = getBaseCommandOpts(sandbox, {}, [[flags.context, 'invalid-context']], {
468-
testClusterConnectionError: true,
468+
testContextConnectionError: true,
469469
});
470470

471471
try {
@@ -545,8 +545,8 @@ describe('ClusterCommand unit tests', () => {
545545
await runSubTasks(subTasks);
546546
expect(contextPromptStub).not.called;
547547
expect(command.getK8().setCurrentContext).to.have.been.calledWith('context-2');
548-
expect(command.getK8().testClusterConnection).calledOnce;
549-
expect(command.getK8().testClusterConnection).calledWith('context-2', 'cluster-2');
548+
expect(command.getK8().testContextConnection).calledOnce;
549+
expect(command.getK8().testContextConnection).calledWith('context-2', 'cluster-2');
550550
});
551551

552552
it('should prompt for context when reading unknown cluster', async () => {
@@ -564,8 +564,8 @@ describe('ClusterCommand unit tests', () => {
564564
await runSubTasks(subTasks);
565565
expect(contextPromptStub).calledOnce;
566566
expect(command.getK8().setCurrentContext).to.have.been.calledWith('prompted-context');
567-
expect(command.getK8().testClusterConnection).calledOnce;
568-
expect(command.getK8().testClusterConnection).calledWith('prompted-context', 'cluster-4');
567+
expect(command.getK8().testContextConnection).calledOnce;
568+
expect(command.getK8().testContextConnection).calledWith('prompted-context', 'cluster-4');
569569
});
570570

571571
it('should throw error for invalid prompted context', async () => {
@@ -575,7 +575,7 @@ describe('ClusterCommand unit tests', () => {
575575
'cluster-4': 'deployment',
576576
},
577577
});
578-
const opts = getBaseCommandOpts(sandbox, remoteConfig, [], {testClusterConnectionError: true});
578+
const opts = getBaseCommandOpts(sandbox, remoteConfig, [], {testContextConnectionError: true});
579579
command = await runReadClustersFromRemoteConfigTask(opts);
580580
expect(taskStub.newListr).calledOnce;
581581
const subTasks = taskStub.newListr.firstCall.firstArg;
@@ -586,8 +586,8 @@ describe('ClusterCommand unit tests', () => {
586586
} catch (e) {
587587
expect(e.message).to.eq(ErrorMessages.INVALID_CONTEXT_FOR_CLUSTER_DETAILED('prompted-context', 'cluster-4'));
588588
expect(contextPromptStub).calledOnce;
589-
expect(command.getK8().testClusterConnection).calledOnce;
590-
expect(command.getK8().testClusterConnection).calledWith('prompted-context', 'cluster-4');
589+
expect(command.getK8().testContextConnection).calledOnce;
590+
expect(command.getK8().testContextConnection).calledWith('prompted-context', 'cluster-4');
591591
}
592592
});
593593

@@ -615,8 +615,8 @@ describe('ClusterCommand unit tests', () => {
615615
} catch (e) {
616616
expect(e.message).to.eq(ErrorMessages.REMOTE_CONFIGS_DO_NOT_MATCH('cluster-3', 'cluster-4'));
617617
expect(contextPromptStub).calledOnce;
618-
expect(command.getK8().testClusterConnection).calledOnce;
619-
expect(command.getK8().testClusterConnection).calledWith('prompted-context', 'cluster-4');
618+
expect(command.getK8().testContextConnection).calledOnce;
619+
expect(command.getK8().testContextConnection).calledWith('prompted-context', 'cluster-4');
620620
}
621621
});
622622
});

0 commit comments

Comments
 (0)