Possible to do a "dry run" validation of files?

8/20/2015

Before creating an object in Kubernetes (Service, ReplicationController, etc.), I'd like to test that the JSON or YAML specification of the object is valid. But I don't want to actually create the object.

Is there some to do a "dry run" that would be equivalent to running kubectl create --validate=true -f file.json, but would just let me know that it passes validation, and not actually create it?

Ideally, it would be great if I could do this via API, and not require the use of kubectl. But I could make it work if it required me to use kubectl.

Thanks.

-- JonM
kubernetes
validation

4 Answers

11/12/2019

The use of --dry-run and --validate only seem to partially solve the issue.

client-side validation is not exhaustive. it primarily ensures the fields names and types in the yaml file are valid. full validation is always done by the server, and can always impose additional restrictions/constraints over client-side validation.

Source - kubectl --validate flag pass when yaml file is wrong #64830

Given this you cannot do a full set of validations except to hand it off completely to the server for vetting.

-- slm
Source: StackOverflow

3/7/2018

This works for me (kubernetes 1.7 and 1.9):

kubectl apply --validate=true --dry-run=true --filename=task.yaml
-- David Watzke
Source: StackOverflow

8/21/2015

Some kubectl commands support a --dry-run flag (like kubectl run, kubectl expose, and kubectl rolling-update).

There is an issue open to add the --dry-run flag to more commands.

-- CJ Cullen
Source: StackOverflow

5/21/2020

There is a tool called kubeval which validates configs against the expected schema, and does not require connection to a cluster to operate, making it a good choice for applications such as CI.

-- Andrew Ring
Source: StackOverflow