| 1 | ## Function Basics |
| 2 | |
| 3 | ### Synopsis |
| 4 | |
| 5 | `kustomize fn` enables encapsulating function for manipulating Resource |
| 6 | configuration inside containers, which are run using `run`. |
| 7 | |
| 8 | First fetch the kustomize repository, which contains a collection of example |
| 9 | functions |
| 10 | |
| 11 | git clone https://github.com/kubernetes-sigs/kustomize |
| 12 | cd kustomize/functions/examples/ |
| 13 | |
| 14 | ### Templating -- CockroachDB |
| 15 | |
| 16 | This section demonstrates how to leverage templating based solutions from |
| 17 | `kustomize fn`. The templating function is implemented as a `bash` script |
| 18 | using a `heredoc`. |
| 19 | |
| 20 | #### 1: Generate the Resources |
| 21 | |
| 22 | `cd` into the `kustomize/functions/examples/template-heredoc-cockroachdb/` |
| 23 | directory, and invoke `run` on the `local-resource/` directory. |
| 24 | |
| 25 | cd template-heredoc-cockroachdb/ |
| 26 | |
| 27 | # view the Resources |
| 28 | kustomize cfg tree local-resource/ --name --image --replicas |
| 29 | |
| 30 | # run the function |
| 31 | kustomize fn run local-resource/ |
| 32 | |
| 33 | # view the generated Resources |
| 34 | kustomize cfg tree local-resource/ --name --image --replicas |
| 35 | |
| 36 | `run` generated the directory ` local-resource/config` containing the generated |
| 37 | Resources. |
| 38 | |
| 39 | #### 2. Modify the Generated Resources |
| 40 | |
| 41 | - modify the generated Resources by adding an annotation, sidecar container, etc. |
| 42 | - modify the `local-resource/example-use.yaml` by changing the replicas |
| 43 | |
| 44 | re-run `run`. this will apply the updated replicas to the generated Resources, |
| 45 | but keep the fields that you manually added to the generated Resource configuration. |
| 46 | |
| 47 | # run the function |
| 48 | kustomize fn run local-resource/ |
| 49 | |
| 50 | `run` facilitates a non-destructive *smart templating* approach that allows templating |
| 51 | to be composed with manual modifications directly to the template output, as well as |
| 52 | composition with other functions which may appy validation or injection of values. |
| 53 | |
| 54 | #### 3. Function Implementation |
| 55 | |
| 56 | the function implementation is located under the `image/` directory as a `Dockerfile` |
| 57 | and a `bash` script. |
| 58 | |
| 59 | ### Templating -- Nginx |
| 60 | |
| 61 | The steps in this section are identical to the CockroachDB templating example, |
| 62 | but the function implementation is very different, and implemented as a `go` |
| 63 | program rather than a `bash` script. |
| 64 | |
| 65 | #### 1: Generate the Resources |
| 66 | |
| 67 | `cd` into the `kustomize/functions/examples/template-go-nginx/` |
| 68 | directory, and invoke `run` on the `local-resource/` directory. |
| 69 | |
| 70 | cd template-go-nginx/ |
| 71 | |
| 72 | # view the Resources |
| 73 | kustomize cfg tree local-resource/ --name --image --replicas |
| 74 | |
| 75 | # run the function |
| 76 | kustomize fn run local-resource/ |
| 77 | |
| 78 | # view the generated Resources |
| 79 | kustomize cfg tree local-resource/ --name --image --replicas |
| 80 | |
| 81 | `run` generated the directory ` local-resource/config` containing the generated |
| 82 | Resources. this time it put the configuration in a single file rather than multiple |
| 83 | files. The mapping of Resources to files is controlled by the function itself through |
| 84 | annotations on the generated Resources. |
| 85 | |
| 86 | #### 2. Modify the Generated Resources |
| 87 | |
| 88 | - modify the generated Resources by adding an annotation, sidecar container, etc. |
| 89 | - modify the `local-resource/example-use.yaml` by changing the replicas |
| 90 | |
| 91 | re-run `run`. this will apply the updated replicas to the generated Resources, |
| 92 | but keep the fields that you manually added to the generated Resource configuration. |
| 93 | |
| 94 | # run the function |
| 95 | kustomize fn run local-resource/ |
| 96 | |
| 97 | Just like in the preceding section, the function is implemented using a non-destructive |
| 98 | approach which merges the generated Resources into previously generated instances. |
| 99 | |
| 100 | #### 3. Function Implementation |
| 101 | |
| 102 | the function implementation is located under the `image/` directory as a `Dockerfile` |
| 103 | and a `go` program. |
| 104 | |
| 105 | ### Validation -- resource reservations |
| 106 | |
| 107 | This section uses `run` to perform validation rather than generate Resources. |
| 108 | |
| 109 | #### 1: Run the Validator |
| 110 | |
| 111 | `cd` into the `kustomize/functions/examples/validator-resource-requests` |
| 112 | directory, and invoke `run` on the `local-resource/` directory. |
| 113 | |
| 114 | # run the function |
| 115 | kustomize fn run local-resource/ |
| 116 | cpu-requests missing for a container in Deployment nginx (example-use.yaml [1]) |
| 117 | Error: exit status 1 |
| 118 | Usage: |
| 119 | ... |
| 120 | |
| 121 | #### 2: Fix the validation issue |
| 122 | |
| 123 | The command will fail complaining that the nginx Deployment is missing `cpu-requests`, |
| 124 | and print the name of the file + Resource index. Edit the file and uncomment the resources, |
| 125 | then re-run the functions. |
| 126 | |
| 127 | kustomize fn run local-resource/ |
| 128 | |
| 129 | The validation now passes. |
| 130 | |
| 131 | ### Injection -- resource reservations |
| 132 | |
| 133 | This section uses `run` to perform injection of field values based off annotations |
| 134 | on the Resource. |
| 135 | |
| 136 | #### 1: Run the Injector |
| 137 | |
| 138 | `cd` into the `kustomize/functions/examples/inject-tshirt-sizes` |
| 139 | directory, and invoke `run` on the `local-resource/` directory. |
| 140 | |
| 141 | # print the resources |
| 142 | kustomize cfg tree local-resource --resources --name |
| 143 | local-resource |
| 144 | ├── [example-use.yaml] Validator |
| 145 | └── [example-use.yaml] Deployment nginx |
| 146 | └── spec.template.spec.containers |
| 147 | └── 0 |
| 148 | └── name: nginx |
| 149 | |
| 150 | # run the functions |
| 151 | kustomize fn run local-resource/ |
| 152 | |
| 153 | # print the new resources |
| 154 | kustomize cfg tree local-resource --resources --name |
| 155 | ├── [example-use.yaml] Validator |
| 156 | └── [example-use.yaml] Deployment nginx |
| 157 | └── spec.template.spec.containers |
| 158 | └── 0 |
| 159 | ├── name: nginx |
| 160 | └── resources: {requests: {cpu: 4, memory: 1GiB}} |
| 161 | |
| 162 | #### 2: Change the tshirt-size |
| 163 | |
| 164 | Change the `tshirt-size` annotation from `medium` to `small` and re-run the functions. |
| 165 | |
| 166 | kustomize fn run local-resource/ |
| 167 | kustomize cfg tree local-resource/ |
| 168 | local-resource |
| 169 | ├── [example-use.yaml] Validator |
| 170 | └── [example-use.yaml] Deployment nginx |
| 171 | └── spec.template.spec.containers |
| 172 | └── 0 |
| 173 | ├── name: nginx |
| 174 | └── resources: {requests: {cpu: 200m, memory: 50MiB}} |
| 175 | |
| 176 | The function has applied the reservations for the new tshirt-size |
| 177 | |
| 178 | ### Function Composition |
| 179 | |
| 180 | Functions may be composed together. Try putting the Injection (tshirt-size) and |
| 181 | Validation functions together in the same .yaml file (separated by `---`). Run |
| 182 | `run` and observe that the first function in the file is applied to the Resources, |
| 183 | and then the second function in the file is applied. |