<p>I created an <code>CRD</code> like this:</p>

<pre><code>import v1 "k8s.io/api/core/v1"

type ApplicationSpec struct {
    Name string `json:"name"`

    PodSpec v1.PodSpec `json:"podSpec"`

    ...
}
</code></pre>

<p>notice that I reused <code>PodSpec</code> from core apigroup in my CRD </p>

<p>To avoid user apply invalid yaml files, I decided to add validation logic in my CRD controller, for simple fields like <code>Name</code>, it's easy to check it's correctness using regex, while for complex and native kind like <code>PodSpec</code>, since k8s already have validation logic for that, I feel the right way is reuse that in my controller, but how can I do that?</p>

<p>You can reuse the <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/validation/validation.go#L3189-L3264" rel="nofollow noreferrer">upstream <code>ValidatePodSpec</code> directly</a>.</p>

<p>You would need to import a couple packages first:</p>

<pre class="lang-golang prettyprint-override"><code>import (
    "k8s.io/kubernetes/pkg/apis/core/validation"
    "k8s.io/apimachinery/pkg/util/validation/field"
)
</code></pre>

<p>Then make use of <code>ValidatePodSpec</code> in your controller method:</p>

<pre class="lang-golang prettyprint-override"><code>errs := validation.ValidatePodSpec(instance.podSpec, field.NewPath("podSpec"))
</code></pre>
