|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.27: Server Side Field Validation and OpenAPI V3 move to GA" |
| 4 | +date: 2023-04-24 |
| 5 | +slug: openapi-v3-field-validation-ga |
| 6 | +--- |
| 7 | + |
| 8 | +**Author**: Jeffrey Ying (Google), Antoine Pelisse (Google) |
| 9 | + |
| 10 | +Before Kubernetes v1.8 (!), typos, mis-indentations or minor errors in |
| 11 | +YAMLs could have catastrophic consequences (e.g. a typo like |
| 12 | +forgetting the trailing s in `replica: 1000` could cause an outage, |
| 13 | +because the value would be ignored and missing, forcing a reset of |
| 14 | +replicas back to 1). This was solved back then by fetching the OpenAPI |
| 15 | +v2 in kubectl and using it to verify that fields were correct and |
| 16 | +present before applying. Unfortunately, at that time, Custom Resource |
| 17 | +Definitions didn’t exist, and the code was written under that |
| 18 | +assumption. When CRDs were later introduced, the lack of flexibility |
| 19 | +in the validation code forced some hard decisions in the way CRDs |
| 20 | +exposed their schema, leaving us in a cycle of bad validation causing |
| 21 | +bad OpenAPI and vice-versa. With the new OpenAPI v3 and Server Field |
| 22 | +Validation being GA in 1.27, we’ve now solved both of these problems. |
| 23 | + |
| 24 | +Server Side Field Validation offers resource validation on create, |
| 25 | +update and patch requests to the apiserver and was added to Kubernetes |
| 26 | +in v1.25, beta in v1.26 and is now GA in v1.27. It provides all the |
| 27 | +functionality of kubectl validate on the server side. |
| 28 | + |
| 29 | +[OpenAPI](https://swagger.io/specification/) is a standard, language |
| 30 | +agnostic interface for discovering the set of operations and types |
| 31 | +that a Kubernetes cluster supports. OpenAPI V3 is the latest standard |
| 32 | +of the OpenAPI and is an improvement upon [OpenAPI |
| 33 | +V2](https://kubernetes.io/blog/2016/12/kubernetes-supports-openapi/) |
| 34 | +which has been supported since Kubernetes 1.5. OpenAPI V3 support was |
| 35 | +added in Kubernetes in v1.23, moved to beta in v1.24 and is now GA in |
| 36 | +v1.27. |
| 37 | + |
| 38 | +## OpenAPI V3 |
| 39 | + |
| 40 | +### What does OpenAPI V3 offer over V2 |
| 41 | + |
| 42 | +#### Built-in types |
| 43 | + |
| 44 | +Kubernetes offers certain annotations on fields that are not |
| 45 | +representable in OpenAPI V2, or sometimes not represented in the |
| 46 | +OpenAPI v2 that Kubernetes generate. Most notably, the "default" field |
| 47 | +is published in OpenAPI V3 while omitted in OpenAPI V2. A single type |
| 48 | +that can represent multiple types is also expressed correctly in |
| 49 | +OpenAPI V3 with the oneOf field. This includes proper representations |
| 50 | +for IntOrString and Quantity. |
| 51 | + |
| 52 | +#### Custom Resource Definitions |
| 53 | + |
| 54 | +In Kubernetes, Custom Resource Definitions use a structural OpenAPI V3 |
| 55 | +schema that cannot be represented as OpenAPI V2 without a loss of |
| 56 | +certain fields. Some of these include nullable, default, anyOf, oneOf, |
| 57 | +not, etc. OpenAPI V3 is a completely lossless representation of the |
| 58 | +CustomResourceDefinition structural schema. |
| 59 | + |
| 60 | +### How do I use it? |
| 61 | + |
| 62 | +The OpenAPI V3 root discovery can be found at the `/openapi/v3` |
| 63 | +endpoint of a Kubernetes API server. OpenAPI V3 documents are grouped |
| 64 | +by group-version to reduce the size of the data transported, the |
| 65 | +separate documents can be accessed at |
| 66 | +`/openapi/v3/apis/<group>/<version>` and `/openapi/v3/api/v1` |
| 67 | +representing the legacy group version. Please refer to the [Kubernetes |
| 68 | +API Documentation](/docs/concepts/overview/kubernetes-api/) for more |
| 69 | +information around this endpoint. |
| 70 | + |
| 71 | +Various consumers of the OpenAPI have already been updated to consume |
| 72 | +v3, including the entirety of kubectl, and server side apply. An |
| 73 | +OpenAPI V3 Golang client is available in |
| 74 | +[client-go](https://github.com/kubernetes/client-go/blob/release-1.27/openapi3/root.go). |
| 75 | + |
| 76 | +## Server Side Field Validation |
| 77 | + |
| 78 | +The query parameter `fieldValidation` may be used to indicate the |
| 79 | +level of field validation the server should perform. If the parameter |
| 80 | +is not passed, server side field validation is in `Warn` mode by |
| 81 | +default. |
| 82 | + |
| 83 | +- Strict: Strict field validation, errors on validation failure |
| 84 | +- Warn: Field validation is performed, but errors are exposed as |
| 85 | + warnings rather than failing the request |
| 86 | +- Ignore: No server side field validation is performed |
| 87 | + |
| 88 | +kubectl will skip client side validation and will automatically use |
| 89 | +server side field validation in `Strict` mode. Controllers by default |
| 90 | +use server side field validation in `Warn` mode. |
| 91 | + |
| 92 | +With client side validation, we had to be extra lenient because some |
| 93 | +fields were missing from OpenAPI V2 and we didn’t want to reject |
| 94 | +possibly valid objects. This is all fixed in server side validation. |
| 95 | +Additional documentation may be found |
| 96 | +[here](/docs/reference/using-api/api-concepts/#field-validation) |
| 97 | + |
| 98 | +## What's next? |
| 99 | + |
| 100 | +With Server Side Field Validation and OpenAPI V3 released as GA, we |
| 101 | +introduce more accurate representations of Kubernetes resources. It is |
| 102 | +recommended to use server side field validation over client side, but |
| 103 | +with OpenAPI V3, clients are free to implement their own validation if |
| 104 | +necessary (to “shift things left”) and we guarantee a full lossless |
| 105 | +schema published by OpenAPI. |
| 106 | + |
| 107 | +Some existing efforts will further improve the information available |
| 108 | +through OpenAPI including [CEL validation and |
| 109 | +admission](/docs/reference/using-api/cel/), along with OpenAPI |
| 110 | +annotations on built-in types. |
| 111 | + |
| 112 | +Many other tools can be built for authoring and transforming resources |
| 113 | +using the type information found in the OpenAPI v3. |
| 114 | + |
| 115 | +## How to get involved? |
| 116 | + |
| 117 | +These two features are driven by the SIG API Machinery community, |
| 118 | +available on the slack channel \#sig-api-machinery, through the |
| 119 | +[mailing |
| 120 | +list](https://groups.google.com/g/kubernetes-sig-api-machinery) and we |
| 121 | +meet every other Wednesday at 11:00 AM PT on Zoom. |
| 122 | + |
| 123 | +We offer a huge thanks to all the contributors who helped design, |
| 124 | +implement, and review these two features. |
| 125 | + |
| 126 | +- Alexander Zielenski |
| 127 | +- Antoine Pelisse |
| 128 | +- Daniel Smith |
| 129 | +- David Eads |
| 130 | +- Jeffrey Ying |
| 131 | +- Jordan Liggitt |
| 132 | +- Kevin Delgado |
| 133 | +- Sean Sullivan |
0 commit comments