github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/book/05-developing-functions/04-developing-in-Starlark.md (about) 1 You can write the function in Starlark script. 2 3 ?> Starlark SDK is in *experimental* stage. 4 5 [Starlark] is a python-like language designed for use in configuration files that has several desirable properties: 6 * deterministic evaluation 7 * hermetic execution 8 * simplicity. 9 10 Current Starlark SDK is driven by [`gcr.io/kpt-fn/starlark:v0.4`] which contains the interpreter and accepts 11 a `StarlarkRun` object as its `FunctionConfig`. You should place your starlark script in the `source` field 12 of the `StarlarkRun` object. 13 14 ## Quickstart 15 16 Let's write a starlark function which add annotation "managed-by=kpt" only to `Deployment` resources. 17 18 ### Get the "get-started" example 19 20 ```shell 21 kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-sdk.git/starlark/get-started@master set-annotation 22 cd set-annotation 23 ``` 24 25 ### Update the `FunctionConfig` 26 ```yaml 27 # starlark-fn-config.yaml 28 apiVersion: fn.kpt.dev/v1alpha1 29 kind: StarlarkRun 30 metadata: 31 name: set-annotation 32 # EDIT THE SOURCE! 33 # This should be your starlark script which preloads the `ResourceList` to `ctx.resource_list` 34 source: | 35 for resource in ctx.resource_list["items"]: 36 if resource.get("kind") == "Deployment": 37 resource["metadata"]["annotations"]["managed-by"] = "kpt" 38 ``` 39 In the `source` field, the `ResourceList` from STDIN is loaded to `ctx.resource_list` as a dict. 40 You can manipulate KRM resource as operating on a dict. 41 42 ### Test and Run 43 44 Run the starlark script via `kpt` 45 ```shell 46 # `starlark:v0.4` is the short form of gcr.io/kpt-fn/starlark:v0.4 catalog function. 47 kpt fn eval ./data --image starlark:v0.4 --fn-config starlark-fn-config.yaml 48 49 # Verify that the annotation is added to the `Deployment` resource and the other resource `Service` 50 # does not have this annotation. 51 cat ./data/resources.yaml | grep annotations -A1 -B5 52 ``` 53 54 ?> Refer to the [Functions Catalog](https://catalog.kpt.dev/starlark/v0.4/) for 55 details on how to use this function. 56 57 [`gcr.io/kpt-fn/starlark:v0.4`]: https://catalog.kpt.dev/starlark/v0.4/ 58 [Starlark]: https://github.com/bazelbuild/starlark#starlark