kubernetes-validate

raw JSON →
1.35.0 verified Mon Apr 27 auth: no python

Validates Kubernetes resource definitions (YAML/JSON) against the schemas from the Kubernetes OpenAPI specification. Current version 1.35.0 supports Kubernetes 1.35. Supports Python >=3.7. Released approximately twice a year to match Kubernetes minor versions.

pip install kubernetes-validate
error ModuleNotFoundError: No module named 'kubernetes_validate'
cause Package name has an underscore, but pip installation uses 'kubernetes-validate' (hyphen).
fix
Run: pip install kubernetes-validate
error KeyError: 'apiVersion'
cause The input resource dict does not contain 'apiVersion', 'kind', or 'metadata'. The validate function requires these keys.
fix
Ensure the resource dictionary has the standard Kubernetes fields, e.g., from yaml.safe_load of a valid manifest.
error kubernetes_validate.utils.validation_failures object has no attribute '__iter__'
cause validation_failures is a class, not a function. The correct usage is to instantiate it or use the validate function directly.
fix
Use 'from kubernetes_validate import validate' and call 'validate(...)' which returns a list of failures.
breaking kubernetes-validate v1.29.0 removed support for Kubernetes v1.20 and v1.21. If you rely on those versions, pin to v1.28.1 or earlier.
fix Pin version: pip install 'kubernetes-validate<1.29'
breaking From v1.29.0, the JSON schemas changed completely to use a newer schema version, requiring the 'referencing' library. Old schema files may not be compatible.
fix Update any custom schema files to the new format or use the bundled schemas.
gotcha The validate function returns an empty list (not None) on success. Checking 'if not failures:' works, but comparing to None will fail.
fix Use 'if failures:' or 'if len(failures) > 0' to detect validation errors.

Validates a Kubernetes YAML resource against the schema for a specific API version and Kubernetes version.

from kubernetes_validate import validate
import yaml

with open('deployment.yaml') as f:
    resource = yaml.safe_load(f)
failures = validate(resource, 'apps/v1', 'Deployment', '1.35')
if failures:
    for f in failures:
        print(f)
else:
    print('Resource is valid')