This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from salemove/rm-yaml
- Loading branch information
Showing
22 changed files
with
28,262 additions
and
158 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
File renamed without changes.
This file contains 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 @@ | ||
node_modules/ |
This file contains 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,15 @@ | ||
.PHONY: render run build | ||
|
||
render: build | ||
@crossplane beta render -r xr.yaml composition.yaml functions.yaml | ||
|
||
run: | ||
$(MAKE) -C ../.. run | ||
|
||
build: composition.yaml node_modules | ||
|
||
composition.yaml: build.mjs src/index.js package.json package-lock.json | ||
@npm run build | ||
|
||
node_modules: | ||
@npm i |
This file contains 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,51 @@ | ||
# Building a function with external dependencies | ||
|
||
This example demonstrates how to package external dependencies into a | ||
single text file and package it into a Crossplane Composition resource. | ||
|
||
The function source code is located in [`src/index.js`](./src/index.js) file, | ||
and it uses NPM package [YAML](https://www.npmjs.com/package/yaml) as an external | ||
dependency. | ||
|
||
Eternal dependencies are bundled into a single file by [esbuild] and then transpiled | ||
with [Babel] to ES5 syntax supported by [Goja]. The resulting source code is used to | ||
build the [`composition.yaml`](./composition.yaml) file. | ||
|
||
By default, the function server also _transpiles_ the input code into ES 5.1 syntax using | ||
[Babel], but since the input code is already ES 5.1, `.spec.source.transpile` is set to `false`. | ||
Setting this flag for large bundles can significantly improve performance. | ||
|
||
[esbuild]: https://esbuild.github.io/ | ||
[Goja]: https://github.com/dop251/goja | ||
[Babel]: https://babeljs.io/ | ||
|
||
## Running this example | ||
|
||
Make sure you have Node.js installed. | ||
|
||
You can run your function locally and test it using `crossplane beta render` | ||
with these example manifests. | ||
|
||
Run the function locally in the background: | ||
```shell | ||
$ make run & | ||
``` | ||
|
||
Then call it with example manifests: | ||
```shell | ||
$ make render | ||
--- | ||
apiVersion: example.crossplane.io/v1 | ||
kind: XR | ||
metadata: | ||
name: example-xr | ||
--- | ||
# ...function results | ||
``` | ||
|
||
Stop the function running in background: | ||
```shell | ||
$ fg | ||
# Press Ctrl-C | ||
^C | ||
``` |
This file contains 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,67 @@ | ||
import process from 'node:process'; | ||
import fs from 'node:fs/promises'; | ||
import * as esbuild from 'esbuild'; | ||
import babel from '@babel/core'; | ||
import YAML from 'yaml'; | ||
|
||
const buildResult = await esbuild.build({ | ||
entryPoints: ['src/index.js'], | ||
bundle: true, | ||
write: false, | ||
platform: "neutral", | ||
sourcemap: "inline", | ||
sourceRoot: "examples/dependencies", | ||
target: "es6" | ||
}); | ||
|
||
if (buildResult.errors.length > 0) { | ||
console.log(buildResult.errors); | ||
process.exit(1); | ||
} | ||
|
||
const transpiled = await babel.transformAsync(buildResult.outputFiles[0].text, { | ||
plugins: [ | ||
["@babel/plugin-transform-modules-commonjs", { loose: true }], | ||
], | ||
ast: false, | ||
babelrc: false, | ||
sourceMaps: 'inline', | ||
inputSourceMap: true, | ||
compact: false, | ||
retainLines: true, | ||
highlightCode: false | ||
}); | ||
|
||
const composition = { | ||
apiVersion: 'apiextensions.crossplane.io/v1', | ||
kind: 'Composition', | ||
metadata: { name: 'function-javascript' }, | ||
spec: { | ||
compositeTypeRef: { | ||
apiVersion: 'example.crossplane.io/v1', | ||
kind: 'XR' | ||
}, | ||
mode: 'Pipeline', | ||
pipeline: [{ | ||
step: 'run-the-template', | ||
functionRef: { | ||
name: 'function-javascript', | ||
}, | ||
input: { | ||
apiVersion: 'javascript.fn.crossplane.io/v1beta1', | ||
kind: 'Input', | ||
metadata: { | ||
annotations: { key: 'value' }, | ||
}, | ||
spec: { | ||
source: { | ||
transpile: false, | ||
inline: transpiled.code | ||
} | ||
} | ||
} | ||
}] | ||
} | ||
}; | ||
|
||
await fs.writeFile('./composition.yaml', YAML.stringify(composition)); |
Oops, something went wrong.