github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/reference/cli/fn/eval/README.md (about) 1 --- 2 title: "`eval`" 3 linkTitle: "eval" 4 type: docs 5 description: > 6 Execute function on resources 7 --- 8 9 <!--mdtogo:Short 10 Execute function on resources 11 --> 12 13 `eval` executes a function on resources in a directory. Functions are packaged 14 as container images. 15 16 If the function fails (i.e. exits with non-zero status code), `eval` will abort 17 and the local filesystem is left intact. 18 19 Refer to the [Imperative Function Execution] for detailed overview. 20 21 ### Synopsis 22 23 <!--mdtogo:Long--> 24 25 ``` 26 kpt fn eval [DIR|-] [flags] [-- fn-args] 27 ``` 28 29 #### Args 30 31 ``` 32 DIR|-: 33 Path to the local directory containing resources. Defaults to the current 34 working directory. Using '-' as the directory path will cause `eval` to 35 read resources from `stdin` and write the output to `stdout`. When resources are 36 read from `stdin`, they must be in one of the following input formats: 37 38 1. Multi object YAML where resources are separated by `---`. 39 40 2. KRM Function Specification wire format where resources are wrapped in an object 41 of kind ResourceList. 42 43 If the output is written to `stdout`, resources are written in multi object YAML 44 format where resources are separated by `---`. 45 ``` 46 47 ``` 48 fn-args: 49 function arguments to be provided as input to the function. These must be 50 provided in the `key=value` format and come after the separator `--`. 51 ``` 52 53 #### Flags 54 55 ``` 56 --as-current-user: 57 Use the `uid` and `gid` of the kpt process for container function execution. 58 By default, container function is executed as `nobody` user. You may want to use 59 this flag to run higher privilege operations such as mounting the local filesystem. 60 61 --env, e: 62 List of local environment variables to be exported to the container function. 63 By default, none of local environment variables are made available to the 64 container running the function. The value can be in `key=value` format or only 65 the key of an already exported environment variable. 66 67 --exec: 68 Path to the local executable binary to execute as a function. Quotes are needed 69 if the executable requires arguments. `eval` executes only one function, so do 70 not use `--image` flag with this flag. This is useful for testing function locally 71 during development. It enables faster dev iterations by avoiding the function to 72 be published as container image. 73 74 --fn-config: 75 Path to the file containing `functionConfig` for the function. 76 77 --image, i: 78 Container image of the function to execute e.g. `gcr.io/kpt-fn/set-namespace:v0.1`. 79 For convenience, if full image path is not specified, `gcr.io/kpt-fn/` is added as default prefix. 80 e.g. instead of passing `gcr.io/kpt-fn/set-namespace:v0.1` you can pass `set-namespace:v0.1`. 81 `eval` executes only one function, so do not use `--exec` flag with this flag. 82 83 --image-pull-policy: 84 If the image should be pulled before rendering the package(s). It can be set 85 to one of always, ifNotPresent, never. If unspecified, always will be the 86 default. 87 If using always, kpt will ensure the function images to run are up-to-date 88 with the remote container registry. This can be useful for tags like v1. 89 If using ifNotPresent, kpt will only pull the image when it can't find it in 90 the local cache. 91 If using never, kpt will only use images from the local cache. 92 93 --include-meta-resources, m: 94 (DEPRECATED) include-meta-resources is no longer necessary because meta 95 resources are included by default with kpt version v1.0.0-beta.15+. 96 97 --match-api-version: 98 Select resources matching the given apiVersion. 99 100 --match-kind 101 Select resources matching the given kind. 102 103 --match-name: 104 Select resources matching the given name. 105 106 --match-namespace: 107 Select resources matching the given namespace. 108 109 --mount: 110 List of storage options to enable reading from the local filesytem. By default, 111 container functions can not access the local filesystem. It accepts the same options 112 as specified on the [Docker Volumes] for `docker run`. All volumes are mounted 113 readonly by default. Specify `rw=true` to mount volumes in read-write mode. 114 115 --network: 116 If enabled, container functions are allowed to access network. 117 By default it is disabled. 118 119 --output, o: 120 If specified, the output resources are written to provided location, 121 if not specified, resources are modified in-place. 122 Allowed values: stdout|unwrap|<OUT_DIR_PATH> 123 1. stdout: output resources are wrapped in ResourceList and written to stdout. 124 2. unwrap: output resources are written to stdout, in multi-object yaml format. 125 3. OUT_DIR_PATH: output resources are written to provided directory. 126 The provided directory must not already exist. 127 128 --type, t; 129 Specify the function type. Accept value `mutator` (default), `validator`. 130 If used with `--save`, this flag will save the evaluated function to the corresponding 131 Kptfile section: `.pipeline.mutators` if type is `mutator`; `.pipeline.validators` if type 132 is `validator`. 133 134 --results-dir: 135 Path to a directory to write structured results. Directory will be created if 136 it doesn't exist. Structured results emitted by the functions are aggregated and saved 137 to `results.yaml` file in the specified directory. 138 If not specified, no result files are written to the local filesystem. 139 140 --save, s: 141 Save the function image and fn-config to Kptfile. Require ` + "`" + `--image` + "`" + `. 142 143 ``` 144 145 #### Environment Variables 146 147 ``` 148 KPT_FN_RUNTIME: 149 The runtime to run kpt functions. It must be one of "docker", "podman" and "nerdctl". 150 ``` 151 152 <!--mdtogo--> 153 154 ## Examples 155 156 <!--mdtogo:Examples--> 157 158 ```shell 159 # execute container my-fn on the resources in DIR directory and 160 # write output back to DIR 161 $ kpt fn eval DIR -i gcr.io/example.com/my-fn 162 ``` 163 164 ```shell 165 # execute container my-fn on the resources in DIR directory with 166 # `functionConfig` my-fn-config 167 $ kpt fn eval DIR -i gcr.io/example.com/my-fn --fn-config my-fn-config 168 ``` 169 170 ```shell 171 # execute container my-fn with an input ConfigMap containing `data: {foo: bar}` 172 $ kpt fn eval DIR -i gcr.io/example.com/my-fn:v1.0.0 -- foo=bar 173 ``` 174 175 ```shell 176 # execute container my-fn and save it to Kptfile `pipeline.mutators` (Default) list. 177 $ kpt fn eval DIR -s -i gcr.io/example.com/my-fn:v1.0.0 -- foo=bar 178 ``` 179 180 ```shell 181 # execute container my-fn and save it to Kptfile `pipeline.validators` list. 182 $ kpt fn eval DIR -s -t validator -i gcr.io/example.com/my-fn:v1.0.0 -- foo=bar 183 ``` 184 185 ```shell 186 # execute executable my-fn on the resources in DIR directory and 187 # write output back to DIR 188 $ kpt fn eval DIR --exec ./my-fn 189 ``` 190 191 ```shell 192 # execute executable my-fn with arguments on the resources in DIR directory and 193 # write output back to DIR 194 $ kpt fn eval DIR --exec "./my-fn arg1 arg2" 195 ``` 196 197 ```shell 198 # execute container my-fn on the resources in DIR directory, 199 # save structured results in /tmp/my-results dir and write output back to DIR 200 $ kpt fn eval DIR -i gcr.io/example.com/my-fn --results-dir /tmp/my-results-dir 201 ``` 202 203 ```shell 204 # execute container my-fn on the resources in DIR directory with network access enabled, 205 # and write output back to DIR 206 $ kpt fn eval DIR -i gcr.io/example.com/my-fn --network 207 ``` 208 209 ```shell 210 # execute container my-fn on the resource in DIR and export KUBECONFIG 211 # and foo environment variable 212 $ kpt fn eval DIR -i gcr.io/example.com/my-fn --env KUBECONFIG -e foo=bar 213 ``` 214 215 ```shell 216 # execute kubeval function by mounting schema from a local directory on wordpress package 217 $ kpt fn eval -i gcr.io/kpt-fn/kubeval:v0.1 \ 218 --mount type=bind,src="/path/to/schema-dir",dst=/schema-dir \ 219 --as-current-user wordpress -- additional_schema_locations=/schema-dir 220 ``` 221 222 ```shell 223 # chaining functions using the unix pipe to set namespace and set labels on 224 # wordpress package 225 $ kpt fn source wordpress \ 226 | kpt fn eval - -i gcr.io/kpt-fn/set-namespace:v0.1 -- namespace=mywordpress \ 227 | kpt fn eval - -i gcr.io/kpt-fn/set-labels:v0.1 -- label_name=color label_value=orange \ 228 | kpt fn sink wordpress 229 ``` 230 231 ```shell 232 # execute container 'set-namespace' on the resources in current directory and write 233 # the output resources to another directory 234 $ kpt fn eval -i gcr.io/kpt-fn/set-namespace:v0.1 -o path/to/dir -- namespace=mywordpress 235 ``` 236 237 ```shell 238 # execute container 'set-namespace' on the resources in current directory and write 239 # the output resources to stdout which are piped to 'kubectl apply' 240 $ kpt fn eval -i gcr.io/kpt-fn/set-namespace:v0.1 -o unwrap -- namespace=mywordpress \ 241 | kubectl apply -f - 242 ``` 243 244 ```shell 245 # execute container 'set-namespace' on the resources in current directory and write 246 # the wrapped output resources to stdout which are passed to 'set-annotations' function 247 # and the output resources after setting namespace and annotation is written to another directory 248 $ kpt fn eval -i gcr.io/kpt-fn/set-namespace:v0.1 -o stdout -- namespace=staging \ 249 | kpt fn eval - -i gcr.io/kpt-fn/set-annotations:v0.1.3 -o path/to/dir -- foo=bar 250 ``` 251 252 ```shell 253 # execute container 'set-namespace' on the resources with 'name' foo and 'kind' Deployment 254 # in current directory 255 kpt fn eval -i set-namespace:v0.1 --by-kind Deployment --by-name foo -- namespace=staging 256 ``` 257 258 ```shell 259 # execute container my-fn with podman on the resources in DIR directory and 260 # write output back to DIR 261 $ KPT_FN_RUNTIME=podman kpt fn eval DIR -i gcr.io/example.com/my-fn 262 ``` 263 264 <!--mdtogo--> 265 266 [docker volumes]: https://docs.docker.com/storage/volumes/ 267 [imperative function execution]: /book/04-using-functions/02-imperative-function-execution 268 [function specification]: /book/05-developing-functions/01-functions-specification