<p>I am trying to write a kubernetes crd validation schema. I have an array (vc) of structures and one of the fields in those structures is required (<code>name</code> field).</p>

<p>I tried looking through various examples but it doesn't generate error when <code>name</code> is not there. Any suggestions what is wrong ?</p>

<pre><code>vc:
  type: array
  items:
    type: object
    properties:
      name:
        type: string
      address:
        type: string
    required:
    - name
</code></pre>

<p>If you are on v1.8, you will need to enable the <code>CustomResourceValidation</code> feature gate for using the validation feature. This can be done by using the following flag on kube-apiserver:</p>

<pre><code>--feature-gates=CustomResourceValidation=true
</code></pre>

<p>Here is an example of it working (I tested this on v1.12, but this should work on earlier versions as well):</p>

<p>The CRD:</p>

<pre><code>apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: foos.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
  version: v1
  scope: Namespaced
  names:
    plural: foos
    singular: foo
    kind: Foo
  validation:
    openAPIV3Schema:
      properties:
        spec:
          properties:
            vc:
              type: array
              items:
                type: object
                properties:
                  name:
                    type: string
                  address:
                    type: string
                required:
                - name
</code></pre>

<p>The custom resource:</p>

<pre><code>apiVersion: "stable.example.com/v1"
kind: Foo
metadata:
  name: new-foo
spec:
  vc:
  - address: "bar"
</code></pre>

<ol>
<li>Create the CRD.</li>
</ol>

<p><code>kubectl create -f crd.yaml</code>
<code>customresourcedefinition.apiextensions.k8s.io/foos.stable.example.com created</code></p>

<ol start="2">
<li>Get the CRD and check if the validation field exists in the output. If it doesn't, you probably don't have the feature gate turned on.</li>
</ol>

<p><code>kubectl get crd foos.stable.example.com -oyaml</code></p>

<ol start="3">
<li>Try to create the custom resource. This should fail with:</li>
</ol>

<p><code>kubectl create -f cr-validation.yaml</code></p>

<p><code>The Foo "new-foo" is invalid: []: Invalid value: map[string]interface {}{"metadata":map[string]interface {}{"creationTimestamp":"2018-11-18T19:45:23Z", "generation":1, "uid":"7d7f8f0b-eb6a-11e8-b861-54e1ad9de0be", "name":"new-foo", "namespace":"default"}, "spec":map[string]interface {}{"vc":[]interface {}{map[string]interface {}{"address":"bar"}}}, "apiVersion":"stable.example.com/v1", "kind":"Foo"}: validation failure list:
spec.vc.name in body is required</code></p>
