Skip to content

Commit

Permalink
feat(schema): improve volumes implementation (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh authored Jun 20, 2024
1 parent 4cfd7b8 commit 52c81ac
Show file tree
Hide file tree
Showing 40 changed files with 1,120 additions and 822 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ to accelerate the adoption of blockchain technology.
```js
import hardhat from '@chainfile/eip-155-31337/hardhat.json';

const testcontainers = new ChainfileTestcontainers(hardhat);
const testcontainers = new CFTestcontainers(hardhat);

beforeAll(async () => {
await testcontainers.start();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"turbo": "^2.0.4",
"typescript": "5.4.5"
},
"packageManager": "pnpm@9.3.0",
"packageManager": "pnpm@9.4.0",
"engines": {
"node": "^20 <21"
}
Expand Down
14 changes: 7 additions & 7 deletions packages/chainfile-agent/src/routers/_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ function getChainfile(): Chainfile {
return chainfile;
}

function getValues(): Record<string, string> {
const values = process.env.CHAINFILE_VALUES;
if (values !== undefined) {
return JSON.parse(values);
function getParams(): Record<string, string> {
const params = process.env.CHAINFILE_PARAMS;
if (params !== undefined) {
return JSON.parse(params);
}

throw new Error('CHAINFILE_VALUES is not defined, cannot start @chainfile/agent.');
throw new Error('CHAINFILE_PARAMS is not defined, cannot start @chainfile/agent.');
}

const chainfile = getChainfile();
const values = getValues();
const params = getParams();

export const createContext = async () => {
return {
chainfile: chainfile,
values: values,
params: params,
};
};

Expand Down
4 changes: 2 additions & 2 deletions packages/chainfile-agent/src/routers/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chainfile: Chainfile = {
$schema: 'https://chainfile.org/schema.json',
caip2: 'bip122:0f9188f13cb7b2c71f2a335e3a4fc328',
name: 'Bitcoin Regtest',
values: {
params: {
rpc_user: 'agent',
rpc_password: 'agent',
},
Expand Down Expand Up @@ -59,7 +59,7 @@ const chainfile: Chainfile = {

const caller = createCaller({
chainfile: chainfile,
values: {},
params: {},
});

it('should getChainfile', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/chainfile-agent/src/routers/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const agentRouter = router({
$schema: z.string().optional(),
caip2: z.string(),
name: z.string(),
values: z.any().optional(),
params: z.any().optional(),
volumes: z.any().optional(),
containers: z.any(),
}),
)
Expand Down
16 changes: 8 additions & 8 deletions packages/chainfile-agent/src/routers/probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EndpointHttpAuthorization,
EndpointHttpJsonRpc,
EndpointHttpRest,
ValueReference,
ParamReference,
} from '@chainfile/schema';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
Expand All @@ -29,7 +29,7 @@ const probeProcedure = publicProcedure
.use((opts) => {
return opts.next({
ctx: {
probes: new Probes(opts.ctx.chainfile, opts.ctx.values) as any,
probes: new Probes(opts.ctx.chainfile, opts.ctx.params) as any,
},
});
});
Expand Down Expand Up @@ -68,7 +68,7 @@ type ProbeFunction = () => Promise<ProbeResponse>;
class Probes {
constructor(
private readonly chainfile: Chainfile,
private readonly values: Record<string, string>,
private readonly params: Record<string, string>,
private readonly ajv: Ajv = new Ajv(),
) {
addFormats(this.ajv);
Expand Down Expand Up @@ -253,16 +253,16 @@ class Probes {
}
}

private resolve(value: string | ValueReference): string {
if (typeof value === 'string') {
return value;
private resolve(param: string | ParamReference): string {
if (typeof param === 'string') {
return param;
}

return this.values[value.$value] ?? '';
return this.params[param.$param] ?? '';
}
}

// TODO(fuxingloh): Probes (Liveness, Readiness, Startup) currently only allow a single probe per endpoint
// TODO(?): Probes (Liveness, Readiness, Startup) currently only allow a single probe per endpoint
// We should allow multiple endpoints per container where some conditions can only be checked by through calling
// multiple endpoints.
// For example, a container with 3 conditions required for it to be liveness:
Expand Down
2 changes: 1 addition & 1 deletion packages/chainfile-agent/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const server = http.createServer(
}),
);

/** The sum of the ASCII values for the string "@chainfile/agent" is 1569. */
/** The sum of the ASCII for the string "@chainfile/agent" is 1569. */
server.listen(1569);
35 changes: 18 additions & 17 deletions packages/chainfile-cdk8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

This package contains the CDK8s application that deploys the Chainfile application to a Kubernetes cluster.

## Contributing Guidelines
## Local Development

You need to install `kind` to run the tests. You can install it with the following command:
You can test kubernetes locally with Kubernetes-in-Docker (kind).
Kind literally runs Kubernetes in Docker containers where each container is a node in the cluster.
Allowing you to test complex Kubernetes configurations locally.
To install `kind` on macOS and set up a cluster, run the following commands:

```shell
```bash
brew install kind
kind create cluster --name cdk8s
```

### Setting up a kind cluster for local development
To synth and deploy to the local cluster, run the following commands:

```shell
kind create cluster --config kind.k8s.yaml
```bash
turbo synth
kubectl apply --context kind-cdk8s -f [file_name].k8s.yaml
```

Optionally, you can install the Kubernetes Dashboard to monitor the cluster for better visibility:
Other Add-Ons:

<details>
<summary>Metrics Server</summary>

```shell
# Add the Kubernetes Dashboard Helm repository
helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboard/
# Install the Kubernetes Dashboard
helm upgrade --install kubernetes-dashboard kubernetes-dashboard/kubernetes-dashboard --create-namespace --namespace kubernetes-dashboard
# Get the token to access the Kubernetes Dashboard
kubectl create serviceaccount dashboard
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=default:dashboard
kubectl create token dashboard
# Start the proxy and access the Kubernetes Dashboard on https://localhost:8443
kubectl -n kubernetes-dashboard port-forward svc/kubernetes-dashboard-kong-proxy 8443:443
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability-1.21+.yaml
```

</details>
Loading

0 comments on commit 52c81ac

Please sign in to comment.