-
Notifications
You must be signed in to change notification settings - Fork 23
Project typescript support #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
stevendborrelli
wants to merge
11
commits into
crossplane:main
Choose a base branch
from
stevendborrelli:project-typescript-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d4bc108
add typescript support
stevendborrelli d425585
coderabbit fixes
stevendborrelli cc2f9c0
fix schema generation
stevendborrelli d623d71
fix package
stevendborrelli cd39b5a
address review and lint issues
stevendborrelli 593d04b
fix test
stevendborrelli 17dea57
add testing guide
stevendborrelli 39c575d
fix all lint issues
stevendborrelli 64163e7
add render and timeout sections
stevendborrelli cd33c32
improve ts detection
stevendborrelli db9412d
update for ts 7
stevendborrelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Crossplane Composition Function | ||
|
|
||
| This is a [Crossplane](https://crossplane.io) composition function written in TypeScript. | ||
|
|
||
| ## Development | ||
|
|
||
| Install dependencies: | ||
|
|
||
| ```shell | ||
| npm install | ||
| ``` | ||
|
|
||
| Build the function: | ||
|
|
||
| ```shell | ||
| npm run build | ||
| ``` | ||
|
|
||
| Run locally (for testing): | ||
|
|
||
| ```shell | ||
| npm run local | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| Test your function using `crossplane resource render`: | ||
|
|
||
| ```shell | ||
| crossplane resource render xr.yaml composition.yaml functions.yaml | ||
| ``` | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [Composition Functions documentation](https://docs.crossplane.io/latest/concepts/composition-functions/) | ||
| - [TypeScript Function SDK](https://github.com/crossplane/function-sdk-typescript) |
26 changes: 26 additions & 0 deletions
26
cmd/crossplane/function/templates/typescript/package.json.tmpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "function", | ||
| "version": "0.1.0", | ||
| "description": "A Crossplane composition function.", | ||
| "license": "Apache-2.0", | ||
| "type": "module", | ||
| "main": "dist/main.js", | ||
| "scripts": { | ||
| "build": "tsgo", | ||
| "local": "node dist/main.js --insecure --debug" | ||
| }, | ||
| "dependencies": { | ||
| "@crossplane-org/function-sdk-typescript": "^0.5.0", | ||
| "@types/node": "^26.0.0", | ||
| "commander": "^15.0.0", | ||
| {{- if .HasSchemas }} | ||
| "crossplane-models": "file:{{ .SchemasPath }}", | ||
| {{- end }} | ||
| "kubernetes-models": "^4.5.1", | ||
| "pino": "^10.3.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@typescript/native-preview": "^7.0.0-dev.20260627.1", | ||
| "typescript": "^6.0.0" | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
cmd/crossplane/function/templates/typescript/src/function.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { | ||
| type RunFunctionRequest, | ||
| type RunFunctionResponse, | ||
| type FunctionHandler, | ||
| type Logger, | ||
| to, | ||
| normal, | ||
| getObservedCompositeResource, | ||
| getDesiredComposedResources, | ||
| setDesiredComposedResources, | ||
| } from '@crossplane-org/function-sdk-typescript'; | ||
|
|
||
| /** | ||
| * Function is a Crossplane composition function. | ||
| */ | ||
| export class Function implements FunctionHandler { | ||
| async RunFunction(req: RunFunctionRequest, logger?: Logger): Promise<RunFunctionResponse> { | ||
| let rsp = to(req); | ||
|
|
||
| // Get the observed composite resource (XR). | ||
| const observedComposite = getObservedCompositeResource(req); | ||
| logger?.debug({ observedComposite }, 'Observed composite resource'); | ||
|
|
||
| // Get the desired composed resources from previous functions in the pipeline. | ||
| const desiredComposed = getDesiredComposedResources(req); | ||
| logger?.debug({ desiredComposed }, 'Desired composed resources'); | ||
|
|
||
| // TODO: Add your function logic here. | ||
| // Use desiredComposed to add, modify, or remove composed resources. | ||
| // Example: | ||
| // desiredComposed['my-resource'] = { resource: { ... } }; | ||
|
|
||
| // Update the response with the desired composed resources. | ||
| rsp = setDesiredComposedResources(rsp, desiredComposed); | ||
|
|
||
| normal(rsp, 'Function completed successfully'); | ||
| return rsp; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { Command, type OptionValues } from 'commander'; | ||
| import { | ||
| newGrpcServer, | ||
| startServer, | ||
| FunctionRunner, | ||
| type ServerOptions, | ||
| } from '@crossplane-org/function-sdk-typescript'; | ||
| import { pino } from 'pino'; | ||
| import { Function } from './function.js'; | ||
|
|
||
| const defaultAddress = '0.0.0.0:9443'; | ||
| const defaultTlsServerCertsDir = '/tls/server'; | ||
|
|
||
| const program = new Command('function') | ||
| .option('--address <address>', 'Address at which to listen for gRPC connections', defaultAddress) | ||
| .option('-d, --debug', 'Emit debug logs.', false) | ||
| .option('--insecure', 'Run without mTLS credentials.', false) | ||
| .option( | ||
| '--tls-server-certs-dir <directory>', | ||
| 'Serve using mTLS certificates in this directory.', | ||
| defaultTlsServerCertsDir | ||
| ); | ||
|
|
||
| function parseArgs(args: OptionValues): ServerOptions { | ||
| return { | ||
| address: typeof args.address === 'string' ? args.address : defaultAddress, | ||
| debug: Boolean(args.debug), | ||
| insecure: Boolean(args.insecure), | ||
| tlsServerCertsDir: | ||
| typeof args.tlsServerCertsDir === 'string' | ||
| ? args.tlsServerCertsDir | ||
| : defaultTlsServerCertsDir, | ||
| }; | ||
| } | ||
|
|
||
| function main() { | ||
| program.parse(process.argv); | ||
| const args = program.opts(); | ||
| const opts = parseArgs(args); | ||
|
|
||
| const logger = pino({ | ||
| level: opts?.debug ? 'debug' : 'info', | ||
| formatters: { | ||
| level: (label: string) => { | ||
| return { severity: label.toUpperCase() }; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| logger.debug({ options: opts }, 'Starting function'); | ||
|
|
||
| try { | ||
| const fn = new Function(); | ||
| const fnRunner = new FunctionRunner(fn, logger); | ||
| const server = newGrpcServer(fnRunner, logger); | ||
| startServer(server, opts, logger); | ||
|
|
||
| process.on('SIGINT', () => { | ||
| logger.info('Shutting down gracefully...'); | ||
| server.tryShutdown((err: Error | undefined) => { | ||
| if (err) { | ||
| logger.error(err, 'Error during shutdown'); | ||
| process.exit(1); | ||
| } | ||
| logger.info('Server shut down successfully'); | ||
| process.exit(0); | ||
| }); | ||
| }); | ||
| } catch (err) { | ||
| logger.error(err); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| main(); |
21 changes: 21 additions & 0 deletions
21
cmd/crossplane/function/templates/typescript/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "exclude": ["node_modules", "dist"], | ||
| "compilerOptions": { | ||
| "rootDir": "./src", | ||
| "outDir": "./dist", | ||
| "module": "nodenext", | ||
| "target": "esnext", | ||
| "types": ["node"], | ||
| "sourceMap": true, | ||
| "declaration": true, | ||
| "declarationMap": true, | ||
| "noUncheckedIndexedAccess": true, | ||
| "exactOptionalPropertyTypes": true, | ||
| "strict": true, | ||
| "verbatimModuleSyntax": true, | ||
| "isolatedModules": true, | ||
| "noUncheckedSideEffectImports": true, | ||
| "moduleDetection": "force", | ||
| "skipLibCheck": true | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.