Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
port forward
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 20, 2024
1 parent 646163b commit 33ce9a2
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 95 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# starship-js
# starshipjs

<p align="center">
<img src="https://user-images.githubusercontent.com/545047/188804067-28e67e5e-0214-4449-ab04-2e0c564a6885.svg" width="80"><br />
<img src="https://user-images.githubusercontent.com/10805402/242348990-c141d6cd-e1c9-413f-af68-283de029c3a4.png" width="80"><br />
Starship TypeScript Utilties
</p>

Expand All @@ -12,6 +12,9 @@
<a href="https://github.com/cosmology-tech/starshipjs/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-BSD%203--Clause%20Clear-blue.svg"></a>
</p>

Universal interchain development environment in k8s. The vision of this project is to have a single easy to use developer environment with full testing support for multichain use cases

Utilities for [Starship](https://github.com/cosmology-tech/starship) 🚀

## install

Expand All @@ -20,7 +23,7 @@ npm install starshipjs
```
## Table of contents

- [starship-js](#starship-js)
- [starshipjs](#starshipjs)
- [Install](#install)
- [Table of contents](#table-of-contents)
- [Developing](#developing)
Expand Down
49 changes: 39 additions & 10 deletions ci/utils/__tests__/__snapshots__/client.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`StarshipClient setup 1`] = `
"Call: setup()
Call: setupHelm()
Call: exec(["helm","repo","add","starship","https://cosmology-tech.github.io/starship/"])
"Call: setConfig({"chains":[{"name":"osmosis-1","type":"osmosis","image":"pyramation/osmosis:v16.1.0","numValidators":1,"ports":{"rest":1317,"rpc":26657,"faucet":8007}},{"name":"cosmos-2","type":"cosmos","numValidators":1,"ports":{"rest":1313,"rpc":26653,"faucet":8003}}],"relayers":[{"name":"osmos-cosmos","type":"hermes","replicas":1,"chains":["osmosis-1","cosmos-2"]}],"registry":{"enabled":true,"ports":{"rest":8081,"grpc":9091}}})
Call: setup()
helm repo add starship https://cosmology-tech.github.io/starship/
Call: exec(["helm","repo","update"])
helm repo update
Call: exec(["helm","search","repo","starship/devnet","--version","v0.1.38"])
helm search repo starship/devnet --version v0.1.38
Call: install()
Call: log("Installing the helm chart. This is going to take a while.....")
Installing the helm chart. This is going to take a while.....
Call: exec(["helm","install","-f","../../__fixtures__/config.yaml","osmojs","starship/devnet","--version","v0.1.38"])
helm install -f ../../__fixtures__/config.yaml osmojs starship/devnet --version v0.1.38
Call: log("Run \\"kubectl get pods\\" to check the status of the cluster")
Run "kubectl get pods" to check the status of the cluster
Call: startPortForward()
Attempting to stop any existing port-forwards...
Trying to stop all port-forward, if any....
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
Starting new port forwarding...
kubectl port-forward pods/osmosis-1-genesis-0 26657:26657 > /dev/null 2>&1 &
Forwarded osmosis-1: local 26657 -> target (host) 26657
kubectl port-forward pods/osmosis-1-genesis-0 1317:1317 > /dev/null 2>&1 &
Forwarded osmosis-1: local 1317 -> target (host) 1317
kubectl port-forward pods/osmosis-1-genesis-0 8007:8000 > /dev/null 2>&1 &
Forwarded osmosis-1: local 8007 -> target (host) 8000
kubectl port-forward pods/cosmos-2-genesis-0 26653:26657 > /dev/null 2>&1 &
Forwarded cosmos-2: local 26653 -> target (host) 26657
kubectl port-forward pods/cosmos-2-genesis-0 1313:1317 > /dev/null 2>&1 &
Forwarded cosmos-2: local 1313 -> target (host) 1317
kubectl port-forward pods/cosmos-2-genesis-0 8003:8000 > /dev/null 2>&1 &
Forwarded cosmos-2: local 8003 -> target (host) 8000
kubectl port-forward service/registry 8081:8080 > /dev/null 2>&1 &
Forwarded registry on port 8081 to target port 8080
kubectl port-forward service/registry 9091:9090 > /dev/null 2>&1 &
Forwarded registry on port 9091 to target port 9090
Call: stop()
pkill -f port-forward
helm delete osmojs
Call: teardown()
Call: removeHelm()
Call: exec(["helm","repo","remove","starship"])
helm repo remove starship"
`;

Expand All @@ -27,5 +44,17 @@ exports[`StarshipClient setup 2`] = `
helm repo update
helm search repo starship/devnet --version v0.1.38
helm install -f ../../__fixtures__/config.yaml osmojs starship/devnet --version v0.1.38
ps -ef | grep -i 'kubectl port-forward' | grep -v 'grep' | awk '{print $2}'
sleep 2
kubectl port-forward pods/osmosis-1-genesis-0 26657:26657 > /dev/null 2>&1 &
kubectl port-forward pods/osmosis-1-genesis-0 1317:1317 > /dev/null 2>&1 &
kubectl port-forward pods/osmosis-1-genesis-0 8007:8000 > /dev/null 2>&1 &
kubectl port-forward pods/cosmos-2-genesis-0 26653:26657 > /dev/null 2>&1 &
kubectl port-forward pods/cosmos-2-genesis-0 1313:1317 > /dev/null 2>&1 &
kubectl port-forward pods/cosmos-2-genesis-0 8003:8000 > /dev/null 2>&1 &
kubectl port-forward service/registry 8081:8080 > /dev/null 2>&1 &
kubectl port-forward service/registry 9091:9090 > /dev/null 2>&1 &
pkill -f port-forward
helm delete osmojs
helm repo remove starship"
`;
10 changes: 10 additions & 0 deletions ci/utils/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { createClient, expectClient } from '../test-utils/client';
import { config } from '../test-utils/config';

describe('StarshipClient', () => {
it('setup', () => {
const { client, ctx } = createClient();

client.dependencies.forEach(dep => dep.installed = true);

client.setConfig(config.config);

// helm
client.setup();
client.install();

client.startPortForward();

client.stop();

// remove helm chart
client.teardown();

expectClient(ctx, -1);
Expand Down
5 changes: 3 additions & 2 deletions ci/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
"@chain-registry/types": "^0.25.5",
"@types/js-yaml": "^4.0.9",
"chalk": "^4.1.0",
"deepmerge": "^4.3.1",
"js-yaml": "^4.1.0",
"shelljs": "^0.8.5",
"semver": "7.6.0"
"semver": "7.6.0",
"shelljs": "^0.8.5"
},
"devDependencies": {
"@types/shelljs": "^0.8.15"
Expand Down
129 changes: 77 additions & 52 deletions ci/utils/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import chalk from 'chalk';
import * as os from 'os';
import * as yaml from 'js-yaml';
import * as shell from 'shelljs';
import { StarshipConfig } from './config';
import { Chain, StarshipConfig } from './config';
import { readFileSync } from 'fs';
import { dependencies as defaultDependencies, Dependency } from "./deps";
import { readAndParsePackageJson } from './package';

import { Ports } from './config';
export interface StarshipContext {
helmName: string;
helmFile: string;
Expand All @@ -19,12 +19,40 @@ export interface StarshipContext {
curdir?: string;
};

export interface PodPorts {
registry: Ports,
explorer: Ports,
chains: {
defaultPorts: Ports,
[chainName: string]: Ports
}
}

// TODO talk to Anmol about moving these into yaml, if not already possible?
const defaultPorts: PodPorts = {
explorer: {
rest: 8080
},
registry: {
grpc: 9090,
rest: 8080
},
chains: {
defaultPorts: {
rpc: 26657,
rest: 1317,
exposer: 8081,
faucet: 8000
}
}
};
export interface StarshipClientI {
ctx: StarshipContext;
version: string;
dependencies: Dependency[];
depsChecked: boolean;
config: StarshipConfig;
podPorts: PodPorts
};

export class StarshipClient implements StarshipClientI{
Expand All @@ -33,6 +61,7 @@ export class StarshipClient implements StarshipClientI{
dependencies: Dependency[] = defaultDependencies;
depsChecked: boolean = false;
config: StarshipConfig;
podPorts: PodPorts = defaultPorts;

constructor(ctx: StarshipContext) {
this.ctx = ctx;
Expand Down Expand Up @@ -113,11 +142,15 @@ export class StarshipClient implements StarshipClientI{
this.removeHelm();
}

private loadConfig(): void {
public loadConfig(): void {
const fileContents = readFileSync(this.ctx.helmFile, 'utf8');
this.config = yaml.load(fileContents) as StarshipConfig;
}

public setConfig(config: StarshipConfig): void {
this.config = config;
}

// TODO do we need this here?
public test(): void {
this.exec([
Expand Down Expand Up @@ -214,81 +247,74 @@ export class StarshipClient implements StarshipClientI{
this.exec(['helm', 'delete', this.ctx.helmName]);
}

public startPortForward(): void {
this.stopPortForward();
this.log(chalk.magenta(`Port forwarding for config ${this.ctx.helmFile}`));
this.log(chalk.magenta("Port forwarding all chains"));

if (!this.config.chains.length) {
this.log(chalk.red("No chains to port-forward."));
return;
}

this.config.chains.forEach(chain => {
const { rpc, rest, exposer, faucet } = chain.ports;
const forwardPort = (localPort: number, targetPort: number) => {
if (localPort !== undefined && targetPort !== undefined) {
this.exec([
"kubectl", "port-forward",
`pods/${chain.name}-genesis-0`,
`${localPort}:${targetPort}`,
">", "/dev/null",
"2>&1", "&"
]);
}
};

if (rpc) forwardPort(rpc, 26657);
if (rest) forwardPort(rest, 1317);
if (exposer) forwardPort(exposer, 8081);
if (faucet) forwardPort(faucet, 8000);

this.log(chalk.yellow(`chains: forwarded ${chain.name} lcd to http://localhost:${rest}, rpc to http://localhost:${rpc}, faucet to http://localhost:${faucet}`));
});

if (this.config.registry?.enabled) {
private forwardPort(chain: Chain, localPort: number, externalPort: number): void {
if (localPort !== undefined && externalPort !== undefined) {
this.exec([
"kubectl", "port-forward",
"service/registry",
"8081:8080",
`pods/${chain.name}-genesis-0`,
`${localPort}:${externalPort}`,
">", "/dev/null",
"2>&1", "&"
]);
this.log(chalk.yellow(`Forwarded ${chain.name}: local ${localPort} -> target (host) ${externalPort}`));
}
}

private forwardPortService(serviceName: string, localPort: number, externalPort: number): void {
if (localPort !== undefined && externalPort !== undefined) {
this.exec([
"kubectl", "port-forward",
"service/registry",
"9091:9090",
`service/${serviceName}`,
`${localPort}:${externalPort}`,
">", "/dev/null",
"2>&1", "&"
]);
this.log(chalk.yellow("registry: forwarded registry lcd to grpc http://localhost:8081, to http://localhost:9091"));
this.log(`Forwarded ${serviceName} on port ${localPort} to target port ${externalPort}`);
}
}

public startPortForward(): void {
if (!this.config) {
throw new Error('no config!');
}
this.log("Attempting to stop any existing port-forwards...");
this.stopPortForward();
this.log("Starting new port forwarding...");

this.config.chains.forEach(chain => {
const chainPodPorts = this.podPorts.chains[chain.name] || this.podPorts.chains.defaultPorts;

if (chain.ports.rpc) this.forwardPort(chain, chain.ports.rpc, chainPodPorts.rpc);
if (chain.ports.rest) this.forwardPort(chain, chain.ports.rest, chainPodPorts.rest);
if (chain.ports.exposer) this.forwardPort(chain, chain.ports.exposer, chainPodPorts.exposer);
if (chain.ports.faucet) this.forwardPort(chain, chain.ports.faucet, chainPodPorts.faucet);
});

if (this.config.registry?.enabled) {
this.forwardPortService("registry", this.config.registry.ports.rest, this.podPorts.registry.rest);
this.forwardPortService("registry", this.config.registry.ports.grpc, this.podPorts.registry.grpc);
}

if (this.config.explorer?.enabled) {
this.exec([
"kubectl", "port-forward",
"service/explorer",
"8080:8080",
">", "/dev/null",
"2>&1", "&"
]);
this.log(chalk.green("Open the explorer to get started.... http://localhost:8080"));
this.forwardPortService("explorer", this.config.explorer.ports.rest, this.podPorts.explorer.rest);
}
}

public stopForward(): void {
this.exec(['pkill', '-f', 'port-forward']);
}

// TODO review with Anmol, which stopForward is better...
public stopPortForward(): void {
this.log(chalk.green("Trying to stop all port-forward, if any...."));
const pids = this.exec([
const result = this.exec([
"ps", "-ef",
"|", "grep", "-i", "'kubectl port-forward'",
"|", "grep", "-v", "'grep'",
"|", "awk", "'{print $2}'"
]).split('\n');
]);
const pids = (result || '').split('\n');
pids.forEach(pid => {
if (pid.trim()) {
this.exec([
Expand All @@ -299,7 +325,6 @@ export class StarshipClient implements StarshipClientI{
this.exec(['sleep', '2']);
}


private setupKind(): void {
if (this.ctx.kindCluster) {
this.exec(['kind', 'create', 'cluster', '--name', this.ctx.kindCluster]);
Expand Down
Loading

0 comments on commit 33ce9a2

Please sign in to comment.