|
| 1 | +# Crossplane Function JS |
| 2 | + |
| 3 | +A JavaScript function for Crossplane that enables executing inline JavaScript/TypeScript code in compositions. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Crossplane Function JS is a specialized Crossplane function that allows you to write JavaScript/TypeScript code directly in your compositions. It consists of a Go gRPC server that receives requests from Crossplane and manages Node.js servers that execute the JavaScript/TypeScript code. |
| 8 | + |
| 9 | +The architecture is designed for robustness and isolation: |
| 10 | + |
| 11 | +- The Go server acts as a proxy between Crossplane and the Node.js servers |
| 12 | +- Each unique piece of inline code gets its own isolated Node.js server instance |
| 13 | +- Node.js servers are lazy instantiated based on the hash of the inline code |
| 14 | +- Unused Node.js servers are garbage collected to conserve resources |
| 15 | + |
| 16 | +## Architecture |
| 17 | + |
| 18 | +```mermaid |
| 19 | +graph TD |
| 20 | + A[Crossplane] -->|gRPC Requests| B[Go gRPC Server] |
| 21 | + B -->|Manages| C[Process Manager] |
| 22 | + C -->|Creates/Manages| D[Node.js Server 1] |
| 23 | + C -->|Creates/Manages| E[Node.js Server 2] |
| 24 | + C -->|Creates/Manages| F[Node.js Server N] |
| 25 | + D -->|Executes| G[Inline Code 1] |
| 26 | + E -->|Executes| H[Inline Code 2] |
| 27 | + F -->|Executes| I[Inline Code N] |
| 28 | + G -->|Returns Result| D |
| 29 | + H -->|Returns Result| E |
| 30 | + I -->|Returns Result| F |
| 31 | + D -->|Returns Composition| B |
| 32 | + E -->|Returns Composition| B |
| 33 | + F -->|Returns Composition| B |
| 34 | + B -->|Returns Composition| A |
| 35 | +``` |
| 36 | + |
| 37 | +### Components |
| 38 | + |
| 39 | +1. **Go gRPC Server**: Receives requests from Crossplane, manages Node.js processes, and forwards requests. |
| 40 | +2. **Process Manager**: Manages the lifecycle of Node.js processes, including creation, health checking, and garbage collection. |
| 41 | +3. **Node.js Servers**: Execute JavaScript/TypeScript code from compositions. |
| 42 | + |
| 43 | +## Features |
| 44 | + |
| 45 | +- **Inline Code Execution**: Write JavaScript/TypeScript code directly in your compositions |
| 46 | +- **Code Isolation**: Each piece of inline code runs in its own Node.js server for robustness |
| 47 | +- **Optional Dependencies**: Specify npm dependencies for your inline code (recommended for development only) |
| 48 | +- **Optional Inline yarn.lock**: Include a yarn.lock file in your composition for dependency version locking |
| 49 | +- **CLI Tool**: Generate composition manifests with inline code from source files |
| 50 | + |
| 51 | +## Installation |
| 52 | + |
| 53 | +### Prerequisites |
| 54 | + |
| 55 | +- Kubernetes cluster with Crossplane installed |
| 56 | +- Helm 3+ |
| 57 | + |
| 58 | +### Installing the Helm Chart |
| 59 | + |
| 60 | +```bash |
| 61 | +# Install the chart |
| 62 | +helm install crossplane-function-js oci://ghcr.io/socialgouv/helm/crossplane-function-js --version 0.0.2 |
| 63 | +``` |
| 64 | + |
| 65 | +For more details on chart configuration options, see the [chart documentation](charts/crossplane-function-js/README.md). |
| 66 | + |
| 67 | +## Development |
| 68 | + |
| 69 | +### Prerequisites |
| 70 | + |
| 71 | +- [direnv](https://direnv.net/) |
| 72 | +- [Nix](https://nixos.org/download.html) |
| 73 | + |
| 74 | +### Setting Up the Development Environment |
| 75 | + |
| 76 | +1. Clone the repository: |
| 77 | + |
| 78 | + ```bash |
| 79 | + git clone https://github.com/socialgouv/crossplane-function-js.git |
| 80 | + cd crossplane-function-js |
| 81 | + ``` |
| 82 | + |
| 83 | +2. Allow direnv: |
| 84 | + |
| 85 | + ```bash |
| 86 | + direnv allow |
| 87 | + ``` |
| 88 | + |
| 89 | +3. Install dependencies: |
| 90 | + ```bash |
| 91 | + yarn |
| 92 | + ``` |
| 93 | + |
| 94 | +## Usage |
| 95 | + |
| 96 | +### Creating a Composition with Inline Code |
| 97 | + |
| 98 | +Here's an example of a composition that uses inline JavaScript code: |
| 99 | + |
| 100 | +```yaml |
| 101 | +apiVersion: apiextensions.crossplane.io/v1 |
| 102 | +kind: Composition |
| 103 | +metadata: |
| 104 | + name: example-composition |
| 105 | +spec: |
| 106 | + compositeTypeRef: |
| 107 | + apiVersion: example.org/v1alpha1 |
| 108 | + kind: XExample |
| 109 | + pipeline: |
| 110 | + - step: transform-with-js |
| 111 | + functionRef: |
| 112 | + name: function-xfuncjs |
| 113 | + input: |
| 114 | + spec: |
| 115 | + source: |
| 116 | + inline: | |
| 117 | + export default async function(input) { |
| 118 | + // Your JavaScript/TypeScript code here |
| 119 | + const composite = input.observed.composite.resource; |
| 120 | + |
| 121 | + // Transform the input |
| 122 | + const transformed = { |
| 123 | + // Your transformed resources |
| 124 | + }; |
| 125 | + |
| 126 | + return { |
| 127 | + resources: transformed |
| 128 | + }; |
| 129 | + } |
| 130 | +``` |
| 131 | +
|
| 132 | +### Using the CLI to Generate Compositions |
| 133 | +
|
| 134 | +The CLI tool can be used to generate composition manifests from source files: |
| 135 | +
|
| 136 | +1. Create a directory structure for your functions: |
| 137 | +
|
| 138 | + ``` |
| 139 | + functions/ |
| 140 | + ├── example1/ |
| 141 | + │ ├── composition.fn.ts |
| 142 | + │ ├── package.json (optional) |
| 143 | + │ └── composition.yaml (optional) |
| 144 | + └── example2/ |
| 145 | + ├── composition.fn.ts |
| 146 | + └── package.json |
| 147 | + ``` |
| 148 | + |
| 149 | +2. Run the CLI tool: |
| 150 | + |
| 151 | + ```bash |
| 152 | + npx @crossplane-js/cli compo |
| 153 | + ``` |
| 154 | + |
| 155 | +3. This will generate composition manifests in the `manifests/` directory. |
| 156 | + |
| 157 | +#### Customizing Compositions |
| 158 | + |
| 159 | +By default, the CLI uses a template for generating compositions. However, you can provide your own template by creating a `composition.yaml` file in the same directory as your `composition.fn.ts` file. |
| 160 | + |
| 161 | +If you create a custom template, you should maintain the following placeholders if you want to preserve the substitution functionality: |
| 162 | + |
| 163 | +- `__FUNCTION_NAME__`: Will be replaced with the name of the function directory |
| 164 | +- `__FUNCTION_CODE__`: Will be replaced with the content of the `composition.fn.ts` file |
| 165 | +- `__DEPENDENCIES__`: Will be replaced with dependencies from package.json |
| 166 | +- `__YARN_LOCK__`: Will be replaced with the content of yarn.lock |
| 167 | + |
| 168 | +#### Dependencies and yarn.lock Handling |
| 169 | + |
| 170 | +The CLI handles dependencies and yarn.lock files as follows: |
| 171 | + |
| 172 | +- **Dependencies**: By default, the `__DEPENDENCIES__` placeholder will be replaced with dependencies from the `package.json` file in the same directory as the `composition.fn.ts` file. If no `package.json` exists in that directory, it will use dependencies from the `package.json` in the parent directory (the one containing the `functions` directory). |
| 173 | + |
| 174 | +- **yarn.lock**: Similarly, the `__YARN_LOCK__` placeholder will be replaced with the content of the `yarn.lock` file in the same directory as the `composition.fn.ts` file. If no `yarn.lock` exists in that directory, it will use the `yarn.lock` from the parent directory. |
| 175 | + |
| 176 | +This approach allows you to have function-specific dependencies or share dependencies across all functions. |
| 177 | + |
| 178 | +## Testing |
| 179 | + |
| 180 | +The project includes end-to-end tests that use a Kind cluster to verify functionality: |
| 181 | + |
| 182 | +```bash |
| 183 | +# Set up the test environment |
| 184 | +task setup-test-env |
| 185 | + |
| 186 | +# Run the end-to-end tests |
| 187 | +task e2e-test |
| 188 | + |
| 189 | +# Clean up the test environment |
| 190 | +task clean-test-env |
| 191 | +``` |
| 192 | + |
| 193 | +The tests: |
| 194 | + |
| 195 | +1. Set up a Kind cluster with a local registry |
| 196 | +2. Install Crossplane |
| 197 | +3. Deploy the XFuncJS server |
| 198 | +4. Apply test compositions and resources |
| 199 | +5. Verify that the resources are created correctly |
| 200 | + |
| 201 | +## Contributing |
| 202 | + |
| 203 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 204 | + |
| 205 | +1. Fork the repository |
| 206 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 207 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 208 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 209 | +5. Open a Pull Request |
0 commit comments