| 1 | # KRM Functions Specification |
| 2 | |
| 3 | _apiVersion: v1_ |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | This document specifies a standard for client-side functions that operate on |
| 8 | Kubernetes declarative configurations referred to as _KRM Functions_. This |
| 9 | standard enables creating small, interoperable, and language-independent |
| 10 | executable programs packaged as containers that can be chained together as part |
| 11 | of a configuration management pipeline. The end result of such a pipeline are |
| 12 | fully rendered configurations that can then be applied to a control plane (e.g. |
| 13 | Using ‘kubectl apply’ for Kubernetes control plane). As such, although this |
| 14 | document references Kubernetes Resource Model and API conventions, it is |
| 15 | completely decoupled from Kubernetes API machinery and does not depend on any |
| 16 | in-cluster components. |
| 17 | |
| 18 | This document references terms described in [Kubernetes API Conventions][1]. |
| 19 | |
| 20 | The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", |
| 21 | "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be |
| 22 | interpreted as described in [RFC 2119][2]. |
| 23 | |
| 24 | ## Use Cases |
| 25 | |
| 26 | KRM functions enable shift-left practices (client-side) through: |
| 27 | |
| 28 | - Pre-commit / delivery validation and linting of configuration |
| 29 | - e.g. Fail if any containers don't have CPU / Memory limits |
| 30 | - Implementation of abstractions as client actuated APIs |
| 31 | - e.g. Create a client-side _"CRD"_ for generating configuration checked into |
| 32 | git |
| 33 | - Injection of cross-cutting configuration |
| 34 | - e.g. T-Shirt size containers by annotating resources with `small`, `medium`, |
| 35 | `large` and inject the cpu and memory resources into containers accordingly. |
| 36 | - e.g. Inject `init` and `side-car` containers into resources based off of |
| 37 | resource type, annotations, etc. |
| 38 | |
| 39 | Performing these on the client rather than the server enables: |
| 40 | |
| 41 | - Configuration to be reviewed prior to being sent to the API server |
| 42 | - Configuration to be validated as part of the CI/CD pipeline |
| 43 | - Configuration for resources to validated holistically rather than individually |
| 44 | per-resource |
| 45 | - e.g. ensure the `Service.selector` and `Deployment.spec.template` labels |
| 46 | match. |
| 47 | - e.g. MutatingWebHooks are scoped to a single resource instance at a time. |
| 48 | - Low-level tweaks to the output of high-level abstractions |
| 49 | - e.g. add an `init container` to a client _"CRD"_ resource after it was |
| 50 | generated. |
| 51 | - Composition and layering of multiple functions together |
| 52 | - Compose generation, injection, validation together |
| 53 | |
| 54 | ## Definitions |
| 55 | |
| 56 | - **function:** A containerized program conforming to the spec described in this |
| 57 | document. |
| 58 | - **orchestrator:** A program that invokes the function container, passing |
| 59 | arguments and processing its output. |
| 60 | |
| 61 | ## Interface |
| 62 | |
| 63 | The inter-process communication between the orchestrator and a function works as |
| 64 | follows: |
| 65 | |
| 66 | 1. Orchestrator runs the function container and provides the input on `stdin`. |
| 67 | The input is a Kubernetes object of kind `ResourceList` as described below. |
| 68 | 2. Function reads the input from `stdin`, performs computations, and provides |
| 69 | the output as a `ResourceList` to `stdout`. The function MAY also emit |
| 70 | non-structured error message on `stderr`. |
| 71 | 3. Orchestrator uses the `stdout`, `stderr`, and the exit code of the function |
| 72 | as it sees fit following to the semantics described below. |
| 73 | |
| 74 | ### Schema |
| 75 | |
| 76 | A function MUST accept input from `stdin` and MUST output to `stdout` a |
| 77 | Kubernetes object of kind `ResourceList` with the following OpenAPI schema: |
| 78 | |
| 79 | ```yaml |
| 80 | swagger: "2.0" |
| 81 | info: |
| 82 | title: KRM Functions Specification (ResourceList) |
| 83 | version: v1 |
| 84 | definitions: |
| 85 | ResourceList: |
| 86 | type: object |
| 87 | description: ResourceList is the input/output wire format for KRM functions. |
| 88 | x-kubernetes-group-version-kind: |
| 89 | - group: config.kubernetes.io |
| 90 | kind: ResourceList |
| 91 | version: v1 |
| 92 | - group: config.kubernetes.io |
| 93 | kind: ResourceList |
| 94 | version: v1beta1 |
| 95 | required: |
| 96 | - items |
| 97 | properties: |
| 98 | apiVersion: |
| 99 | description: apiVersion of ResourceList |
| 100 | type: string |
| 101 | kind: |
| 102 | description: kind of ResourceList i.e. `ResourceList` |
| 103 | type: string |
| 104 | items: |
| 105 | type: array |
| 106 | description: | |
| 107 | [input/output] |
| 108 | Items is a list of Kubernetes objects: |
| 109 | https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds). |
| 110 | |
| 111 | A function will read this field in the input ResourceList and populate |
| 112 | this field in the output ResourceList. |
| 113 | items: |
| 114 | type: object |
| 115 | functionConfig: |
| 116 | type: object |
| 117 | description: | |
| 118 | [input] |
| 119 | FunctionConfig is an optional Kubernetes object for passing arguments to a |
| 120 | function invocation. |
| 121 | results: |
| 122 | type: array |
| 123 | description: | |
| 124 | [output] |
| 125 | Results is an optional list that can be used by function to emit results |
| 126 | for observability and debugging purposes. |
| 127 | items: |
| 128 | "$ref": "#/definitions/Result" |
| 129 | Result: |
| 130 | type: object |
| 131 | required: |
| 132 | - message |
| 133 | properties: |
| 134 | message: |
| 135 | type: string |
| 136 | description: Message is a human readable message. |
| 137 | severity: |
| 138 | type: string |
| 139 | enum: |
| 140 | - error |
| 141 | - warning |
| 142 | - info |
| 143 | default: error |
| 144 | description: | |
| 145 | Severity is the severity of a result: |
| 146 | |
| 147 | "error": indicates an error result. |
| 148 | "warning": indicates a warning result. |
| 149 | "info": indicates an informational result. |
| 150 | resourceRef: |
| 151 | type: object |
| 152 | description: | |
| 153 | ResourceRef is the metadata for referencing a Kubernetes object |
| 154 | associated with a result. |
| 155 | required: |
| 156 | - apiVersion |
| 157 | - kind |
| 158 | - name |
| 159 | properties: |
| 160 | apiVersion: |
| 161 | description: |
| 162 | APIVersion refers to the `apiVersion` field of the object |
| 163 | manifest. |
| 164 | type: string |
| 165 | kind: |
| 166 | description: Kind refers to the `kind` field of the object. |
| 167 | type: string |
| 168 | namespace: |
| 169 | description: |
| 170 | Namespace refers to the `metadata.namespace` field of the object |
| 171 | manifest. |
| 172 | type: string |
| 173 | name: |
| 174 | description: |
| 175 | Name refers to the `metadata.name` field of the object manifest. |
| 176 | type: string |
| 177 | field: |
| 178 | type: object |
| 179 | description: | |
| 180 | Field is the reference to a field in the object. |
| 181 | If defined, `ResourceRef` must also be provided. |
| 182 | required: |
| 183 | - path |
| 184 | properties: |
| 185 | path: |
| 186 | type: string |
| 187 | description: | |
| 188 | Path is the JSON path of the field |
| 189 | e.g. `spec.template.spec.containers[3].resources.limits.cpu` |
| 190 | currentValue: |
| 191 | description: | |
| 192 | CurrrentValue is the current value of the field. |
| 193 | Can be any value - string, number, boolean, array or object. |
| 194 | proposedValue: |
| 195 | description: | |
| 196 | PropposedValue is the proposed value of the field to fix an issue. |
| 197 | Can be any value - string, number, boolean, array or object. |
| 198 | file: |
| 199 | type: object |
| 200 | description: File references a file containing the resource. |
| 201 | required: |
| 202 | - path |
| 203 | properties: |
| 204 | path: |
| 205 | type: string |
| 206 | description: | |
| 207 | Path is the OS agnostic, slash-delimited, relative path. |
| 208 | e.g. `some-dir/some-file.yaml`. |
| 209 | index: |
| 210 | type: number |
| 211 | default: 0 |
| 212 | description: Index of the object in a multi-object YAML file. |
| 213 | tags: |
| 214 | type: object |
| 215 | additionalProperties: |
| 216 | type: string |
| 217 | description: | |
| 218 | Tags is an unstructured key value map stored with a result that may be set |
| 219 | by external tools to store and retrieve arbitrary metadata. |
| 220 | paths: {} |
| 221 | ``` |
| 222 | |
| 223 | #### Examples |
| 224 | |
| 225 | The following is an example input, where the custom resource of kind |
| 226 | `FulfillmentCenter` is provided as `functionConfig`. The function will operate |
| 227 | on one resource of kind `Service`. |
| 228 | |
| 229 | ```yaml |
| 230 | apiVersion: config.kubernetes.io/v1 |
| 231 | kind: ResourceList |
| 232 | functionConfig: |
| 233 | apiVersion: foo-corp.com/v1 |
| 234 | kind: FulfillmentCenter |
| 235 | metadata: |
| 236 | name: staging |
| 237 | spec: |
| 238 | address: "100 Main St." |
| 239 | items: |
| 240 | - apiVersion: v1 |
| 241 | kind: Service |
| 242 | metadata: |
| 243 | name: wordpress |
| 244 | labels: |
| 245 | app: wordpress |
| 246 | annotations: |
| 247 | internal.config.kubernetes.io/index: "0" |
| 248 | internal.config.kubernetes.io/path: "service.yaml" |
| 249 | spec: # Example comment |
| 250 | type: LoadBalancer |
| 251 | selector: |
| 252 | app: wordpress |
| 253 | tier: frontend |
| 254 | ports: |
| 255 | - protocol: TCP |
| 256 | port: 80 |
| 257 | ``` |
| 258 | |
| 259 | The following is an example output containing one result representing a |
| 260 | validation error: |
| 261 | |
| 262 | ```yaml |
| 263 | apiVersion: config.kubernetes.io/v1 |
| 264 | kind: ResourceList |
| 265 | items: |
| 266 | - apiVersion: v1 |
| 267 | kind: Service |
| 268 | metadata: |
| 269 | name: wordpress |
| 270 | labels: |
| 271 | app: wordpress |
| 272 | annotations: |
| 273 | internal.config.kubernetes.io/index: "0" |
| 274 | internal.config.kubernetes.io/path: "service.yaml" |
| 275 | spec: # Example comment |
| 276 | type: LoadBalancer |
| 277 | selector: |
| 278 | app: wordpress |
| 279 | tier: frontend |
| 280 | ports: |
| 281 | - protocol: TCP |
| 282 | port: 80 |
| 283 | results: |
| 284 | - message: "Invalid type. Expected: integer, given: string" |
| 285 | severity: error |
| 286 | resourceRef: |
| 287 | apiVersion: v1 |
| 288 | kind: Service |
| 289 | name: wordpress |
| 290 | field: |
| 291 | path: spec.ports.0.port |
| 292 | file: |
| 293 | path: service.yaml |
| 294 | ``` |
| 295 | |
| 296 | ### Serialization Format |
| 297 | |
| 298 | A function MUST support YAML as a serialization format for the input and output. |
| 299 | A function MUST use utf8 encoding (as YAML is a superset of JSON, JSON will also |
| 300 | be supported by any conforming function). |
| 301 | |
| 302 | ### Containerization |
| 303 | |
| 304 | A function MUST be implemented as a container. |
| 305 | |
| 306 | A function container MUST be capable of running as a non-root user `nobody` if |
| 307 | it does not require access to host filesystem. |
| 308 | |
| 309 | ### stderr |
| 310 | |
| 311 | Any non-structured error messages MUST be emitted to `stderr`. `stdout` is |
| 312 | reserved for `ResourceList` as described above. |
| 313 | |
| 314 | ### Exit Code |
| 315 | |
| 316 | An exit code of zero indicates function execution was successful. A non-zero |
| 317 | exit code indicates a failure. |
| 318 | |
| 319 | ### Operations |
| 320 | |
| 321 | A function MAY Create, Update, or Delete any number of items in the `items` |
| 322 | field and output the resultant list in the corresponding `items` field of the |
| 323 | output. |
| 324 | |
| 325 | A function SHOULD preserve comments when input serialization format is YAML. |
| 326 | This allows for human authoring of configuration to coexist with changes made by |
| 327 | functions. |
| 328 | |
| 329 | ### Internal Annotations |
| 330 | |
| 331 | For orchestration purposes, the orchestrator will use a set of annotations, |
| 332 | referred to as _internal annotations_, on resources in `Resources.items`. These |
| 333 | annotations are not persisted to resource manifests on the filesystem: The |
| 334 | orchestrator sets this annotation when reading files from the local filesystem |
| 335 | and removes the annotation when writing the output of functions back to the |
| 336 | filesystem. |
| 337 | |
| 338 | Annotation prefix `internal.config.kubernetes.io` is reserved for use for |
| 339 | internal annotations. In general, a function MUST NOT modify these annotations with |
| 340 | the exception of the specific annotations listed below. This enables orchestrators to add additional internal annotations, without requiring changes to existing functions. |
| 341 | |
| 342 | #### `internal.config.kubernetes.io/path` |
| 343 | |
| 344 | Records the slash-delimited, OS-agnostic, relative file path to a resource. The |
| 345 | path is relative to a fix location on the filesystem. Different orchestrator |
| 346 | implementations can choose different fixed points. |
| 347 | |
| 348 | A function SHOULD NOT modify these annotations. |
| 349 | |
| 350 | Example: |
| 351 | |
| 352 | ```yaml |
| 353 | metadata: |
| 354 | annotations: |
| 355 | internal.config.kubernetes.io/path: "relative/file/path.yaml" |
| 356 | ``` |
| 357 | |
| 358 | #### `internal.config.kubernetes.io/index` |
| 359 | |
| 360 | Records the index of a Resource in file. In a multi-object YAML file, resources |
| 361 | are separated by three dashes (`---`), and the index represents the position of |
| 362 | the Resource starting from zero. When this annotation is not specified, it |
| 363 | implies a value of `0`. |
| 364 | |
| 365 | A function SHOULD NOT modify these annotations. |
| 366 | |
| 367 | Example: |
| 368 | |
| 369 | ```yaml |
| 370 | metadata: |
| 371 | annotations: |
| 372 | internal.config.kubernetes.io/path: "relative/file/path.yaml" |
| 373 | internal.config.kubernetes.io/index: 2 |
| 374 | ``` |
| 375 | |
| 376 | This represents the third resource in the file. |
| 377 | |
| 378 | [1]: |
| 379 | https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md |
| 380 | [2]: https://tools.ietf.org/html/rfc2119 |
| 381 | [3]: |
| 382 | https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds |