From 74c9efa148ac6591a1dcf8f95c7fdac35aa603d0 Mon Sep 17 00:00:00 2001 From: Nikhita Raghunath Date: Mon, 8 Jan 2018 14:44:48 +0530 Subject: [PATCH] Add CustomResourceValidation example in sample-controller - Mention the schema in the example CRD. - Update README and mention about feature gates. --- staging/src/k8s.io/sample-controller/README.md | 16 ++++++++++++++++ .../artifacts/examples/crd.yaml | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/staging/src/k8s.io/sample-controller/README.md b/staging/src/k8s.io/sample-controller/README.md index d2aaf2b34e2..95ba305a7bf 100644 --- a/staging/src/k8s.io/sample-controller/README.md +++ b/staging/src/k8s.io/sample-controller/README.md @@ -73,6 +73,22 @@ type User struct { } ``` +## Validation + +To validate custom resources, use the [`CustomResourceValidation`](https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/#validation) feature. + +This feature is beta and enabled by default in v1.9. If you are using v1.8, enable the feature using +the `CustomResourceValidation` feature gate on the [kube-apiserver](https://kubernetes.io/docs/admin/kube-apiserver): + +```sh +--feature-gates=CustomResourceValidation=true +``` + +### Example + +The schema in the [example CRD](./artifacts/examples/crd.yaml) applies the following validation on the custom resource: +`spec.replicas` must be an integer and must have a minimum value of 1 and a maximum value of 10. + ## Cleanup You can clean up the created CustomResourceDefinition with: diff --git a/staging/src/k8s.io/sample-controller/artifacts/examples/crd.yaml b/staging/src/k8s.io/sample-controller/artifacts/examples/crd.yaml index 4a457068dcd..36469161c6a 100644 --- a/staging/src/k8s.io/sample-controller/artifacts/examples/crd.yaml +++ b/staging/src/k8s.io/sample-controller/artifacts/examples/crd.yaml @@ -9,3 +9,12 @@ spec: kind: Foo plural: foos scope: Namespaced + validation: + openAPIV3Schema: + properties: + spec: + properties: + replicas: + type: integer + minimum: 1 + maximum: 10