<p>I would like to implement functionality (or even better reuse existing libraries/APIs!) that would intercept a kubectl command to create an object and perform some pre-creation validation tasks on it before allowing kubectl command to proceed.</p>

<p>e.g. 
check various values in the yaml against external DB for example
check a label conforms to the internal naming convention
and so on..</p>

<p>Is there an accepted pattern or existing tools etc?
Any guidance appreciated</p>

user1843591
<p>The way to do this is by creating a <a href="https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#validatingadmissionwebhook" rel="nofollow noreferrer">ValidatingAdmissionWebhook</a>. It's not for the faint of heart and even a brief example would be an overkill as a SO answer. A few pointers to start:</p>

<p><a href="https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#validatingadmissionwebhook" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#validatingadmissionwebhook</a></p>

<p><a href="https://banzaicloud.com/blog/k8s-admission-webhooks/" rel="nofollow noreferrer">https://banzaicloud.com/blog/k8s-admission-webhooks/</a></p>

<p><a href="https://container-solutions.com/a-gentle-intro-to-validation-admission-webhooks-in-kubernetes/" rel="nofollow noreferrer">https://container-solutions.com/a-gentle-intro-to-validation-admission-webhooks-in-kubernetes/</a></p>

<p>I hope this helps :-)</p>
