| 1 | # Running Configuration Functions using kustomize CLI |
| 2 | |
| 3 | Configuration functions can be implemented using any toolchain and invoked using |
| 4 | any container workflow orchestrator including Tekton, Cloud Build, or run |
| 5 | directly using `docker run`. |
| 6 | |
| 7 | Run `config help docs-fn-spec` to see the Configuration Functions Specification. |
| 8 | |
| 9 | `kustomize fn run` is an example orchestrator for invoking Configuration |
| 10 | Functions. This document describes how to implement and invoke an example |
| 11 | function. |
| 12 | |
| 13 | ## Example Function Implementation |
| 14 | |
| 15 | Following is an example for implementing an nginx abstraction using a |
| 16 | configuration function. |
| 17 | |
| 18 | ### `nginx-template.sh` |
| 19 | |
| 20 | `nginx-template.sh` is a simple bash script which uses a _heredoc_ as a |
| 21 | templating solution for generating Resources from the functionConfig input |
| 22 | fields. |
| 23 | |
| 24 | The script wraps itself using `config run wrap -- $0` which will: |
| 25 | |
| 26 | 1. Parse the `ResourceList.functionConfig` (provided to the container stdin) |
| 27 | into env vars |
| 28 | 2. Merge the stdout into the original list of Resources |
| 29 | 3. Defaults filenames for newly generated Resources (if they are not set as |
| 30 | annotations) to `config/NAME_KIND.yaml` |
| 31 | 4. Format the output |
| 32 | |
| 33 | ```bash |
| 34 | #!/bin/bash |
| 35 | # script must run wrapped by "kustomize fn run wrap" |
| 36 | # for parsing input the functionConfig into env vars |
| 37 | if [ -z ${WRAPPED} ]; then |
| 38 | export WRAPPED=true |
| 39 | config run wrap -- $0 |
| 40 | exit $? |
| 41 | fi |
| 42 | |
| 43 | cat <<End-of-message |
| 44 | apiVersion: v1 |
| 45 | kind: Service |
| 46 | metadata: |
| 47 | name: ${NAME} |
| 48 | labels: |
| 49 | app: nginx |
| 50 | instance: ${NAME} |
| 51 | spec: |
| 52 | ports: |
| 53 | - port: 80 |
| 54 | targetPort: 80 |
| 55 | name: http |
| 56 | selector: |
| 57 | app: nginx |
| 58 | instance: ${NAME} |
| 59 | --- |
| 60 | apiVersion: apps/v1 |
| 61 | kind: Deployment |
| 62 | metadata: |
| 63 | name: ${NAME} |
| 64 | labels: |
| 65 | app: nginx |
| 66 | instance: ${NAME} |
| 67 | spec: |
| 68 | replicas: ${REPLICAS} |
| 69 | selector: |
| 70 | matchLabels: |
| 71 | app: nginx |
| 72 | instance: ${NAME} |
| 73 | template: |
| 74 | metadata: |
| 75 | labels: |
| 76 | app: nginx |
| 77 | instance: ${NAME} |
| 78 | spec: |
| 79 | containers: |
| 80 | - name: nginx |
| 81 | image: nginx:1.7.9 |
| 82 | ports: |
| 83 | - containerPort: 80 |
| 84 | End-of-message |
| 85 | ``` |
| 86 | |
| 87 | ### Dockerfile |
| 88 | |
| 89 | `Dockerfile` installs `kustomize fn` and copies the script into the container |
| 90 | image. |
| 91 | |
| 92 | ``` |
| 93 | FROM public.ecr.aws/docker/library/golang:1.24.6-bullseye |
| 94 | RUN go get sigs.k8s.io/kustomize/cmd/config |
| 95 | RUN mv /go/bin/config /usr/bin/config |
| 96 | COPY nginx-template.sh /usr/bin/nginx-template.sh |
| 97 | CMD ["nginx-template.sh] |
| 98 | ``` |
| 99 | |
| 100 | ## Example Function Usage |
| 101 | |
| 102 | Following is an example of running the `kustomize fn run` using the preceding |
| 103 | API. |
| 104 | |
| 105 | When run by `kustomize fn run`, functions are run in containers with the |
| 106 | following environment: |
| 107 | |
| 108 | - Network: `none` |
| 109 | - User: `nobody` |
| 110 | - Security Options: `no-new-privileges` |
| 111 | - Volumes: the volume containing the `functionConfig` yaml is mounted under |
| 112 | `/local` as `ro` |
| 113 | |
| 114 | ### Input |
| 115 | |
| 116 | `dir/nginx.yaml` contains a reference to the Function. The contents of |
| 117 | `nginx.yaml` are passed to the Function through the |
| 118 | `ResourceList.functionConfig` field. |
| 119 | |
| 120 | ```yaml |
| 121 | apiVersion: example.com/v1beta1 |
| 122 | kind: Nginx |
| 123 | metadata: |
| 124 | name: my-instance |
| 125 | annotations: |
| 126 | config.kubernetes.io/local-config: "true" |
| 127 | config.kubernetes.io/function: | |
| 128 | container: |
| 129 | image: gcr.io/example-functions/nginx-template:v1.0.0 |
| 130 | spec: |
| 131 | replicas: 5 |
| 132 | ``` |
| 133 | |
| 134 | - `annotations[config.kubernetes.io/function].container.image`: the image to use |
| 135 | for this API |
| 136 | - `annotations[config.kubernetes.io/local-config]`: mark this as not a Resource |
| 137 | that should be applied |
| 138 | |
| 139 | ### Output |
| 140 | |
| 141 | The function is invoked using byrunning `kustomize fn run dir/`. |
| 142 | |
| 143 | `dir/my-instance_deployment.yaml` contains the Deployment: |
| 144 | |
| 145 | ```yaml |
| 146 | apiVersion: apps/v1 |
| 147 | kind: Deployment |
| 148 | metadata: |
| 149 | name: my-instance |
| 150 | labels: |
| 151 | app: nginx |
| 152 | instance: my-instance |
| 153 | spec: |
| 154 | replicas: 5 |
| 155 | selector: |
| 156 | matchLabels: |
| 157 | app: nginx |
| 158 | instance: my-instance |
| 159 | template: |
| 160 | metadata: |
| 161 | labels: |
| 162 | app: nginx |
| 163 | instance: my-instance |
| 164 | spec: |
| 165 | containers: |
| 166 | - name: nginx |
| 167 | image: nginx:1.7.9 |
| 168 | ports: |
| 169 | - containerPort: 80 |
| 170 | ``` |
| 171 | |
| 172 | `dir/my-instance_service.yaml` contains the Service: |
| 173 | |
| 174 | ```yaml |
| 175 | apiVersion: v1 |
| 176 | kind: Service |
| 177 | metadata: |
| 178 | name: my-instance |
| 179 | labels: |
| 180 | app: nginx |
| 181 | instance: my-instance |
| 182 | spec: |
| 183 | ports: |
| 184 | - port: 80 |
| 185 | targetPort: 80 |
| 186 | name: http |
| 187 | selector: |
| 188 | app: nginx |
| 189 | instance: my-instance |
| 190 | ``` |