Skip to content

Commit 5898ab4

Browse files
committed
fix: use port forward if load balancer IP is not in supported network interface subnets
Signed-off-by: Lenin Mehedy <[email protected]>
1 parent e9b2e50 commit 5898ab4

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"figlet": "^1.7.0",
3939
"got": "^14.2.0",
4040
"inquirer": "^9.2.15",
41+
"ip": "^2.0.1",
4142
"js-base64": "^3.7.7",
4243
"js-yaml": "^4.1.0",
4344
"listr2": "^8.0.2",

src/commands/node.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export class NodeCommand extends BaseCommand {
498498
for (const nodeId of ctx.config.nodeIds) {
499499
const podName = ctx.config.podNames[nodeId]
500500
subTasks.push({
501-
title: `Node: ${chalk.yellow(nodeId)}`,
501+
title: `Updating node: ${chalk.yellow(nodeId)}`,
502502
task: () =>
503503
self.plaformInstaller.fetchPlatform(podName, config.releaseTag)
504504
})

src/core/account_manager.mjs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
import * as HashgraphProto from '@hashgraph/proto'
1818
import * as Base64 from 'js-base64'
19+
import os from 'os'
1920
import * as constants from './constants.mjs'
2021
import {
2122
AccountCreateTransaction,
@@ -32,6 +33,7 @@ import {
3233
} from '@hashgraph/sdk'
3334
import { FullstackTestingError, MissingArgumentError } from './errors.mjs'
3435
import { Templates } from './templates.mjs'
36+
import ip from 'ip'
3537

3638
const REASON_FAILED_TO_GET_KEYS = 'failed to get keys for accountId'
3739
const REASON_SKIPPED = 'skipped since it does not have a genesis key'
@@ -178,6 +180,35 @@ export class AccountManager {
178180
}
179181
}
180182

183+
shouldUseLocalHostPortForward (serviceObject) {
184+
if (!serviceObject.loadBalancerIp) return true
185+
186+
const loadBalancerIp = serviceObject.loadBalancerIp
187+
const interfaces = os.networkInterfaces()
188+
let usePortForward = true
189+
const loadBalancerIpFormat = ip.isV6Format(loadBalancerIp) ? 'ipv4' : 'ipv6'
190+
191+
// check if serviceIP falls into any subnet of the network interfaces
192+
for (const nic of Object.keys(interfaces)) {
193+
const inf = interfaces[nic]
194+
for (const item of inf) {
195+
if (item.family.toLowerCase() === loadBalancerIpFormat &&
196+
ip.cidrSubnet(item.cidr).contains(loadBalancerIp)) {
197+
usePortForward = false
198+
break
199+
}
200+
}
201+
}
202+
203+
if (usePortForward) {
204+
this.logger.debug('Local network and Load balancer are in different network, using local host port forward')
205+
} else {
206+
this.logger.debug('Local network and Load balancer are in the same network, using load balancer IP port forward')
207+
}
208+
209+
return usePortForward
210+
}
211+
181212
/**
182213
* Returns a node client that can be used to make calls against
183214
* @param namespace the namespace for which the node client resides
@@ -192,7 +223,7 @@ export class AccountManager {
192223
let localPort = constants.LOCAL_NODE_START_PORT
193224

194225
for (const serviceObject of serviceMap.values()) {
195-
const usePortForward = !(serviceObject.loadBalancerIp)
226+
const usePortForward = this.shouldUseLocalHostPortForward(serviceObject)
196227
const host = usePortForward ? '127.0.0.1' : serviceObject.loadBalancerIp
197228
const port = serviceObject.grpcPort
198229
const targetPort = usePortForward ? localPort : port

test/e2e/core/account_manager.test.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ describe('AccountManager', () => {
5353

5454
expect(accountManager._portForwards.length).toStrictEqual(0)
5555
})
56+
57+
it('should be able to load a new client', async () => {
58+
await accountManager.loadNodeClient(configManager.getFlag(flags.namespace))
59+
expect(accountManager._nodeClient).not.toBeNull()
60+
await accountManager.close()
61+
})
5662
})

0 commit comments

Comments
 (0)