Generate YAML Schema for Kubernetes Configuration
Learn how to convert Kubernetes resource JSON into YAML Schema definitions. Covers Deployment, Service, ConfigMap schemas, resource limits, labels/selectors, and custom resource definitions (CRDs).
Detailed Explanation
Kubernetes Configuration to YAML Schema
Kubernetes resources are defined in YAML and follow strict structural rules. Converting a sample Kubernetes resource JSON to YAML Schema creates a validation layer for custom resources and Helm chart values.
Example Kubernetes Deployment JSON
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "web-app",
"namespace": "production",
"labels": {
"app": "web-app",
"env": "production"
}
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": {
"app": "web-app"
}
},
"template": {
"spec": {
"containers": [
{
"name": "web",
"image": "myapp:2.0.0",
"ports": [
{ "containerPort": 8080 }
],
"resources": {
"requests": {
"cpu": "100m",
"memory": "128Mi"
},
"limits": {
"cpu": "500m",
"memory": "512Mi"
}
}
}
]
}
}
}
}
Generated YAML Schema (Simplified)
type: object
properties:
apiVersion:
type: string
const: "apps/v1"
kind:
type: string
const: Deployment
metadata:
type: object
properties:
name:
type: string
pattern: "^[a-z0-9][a-z0-9-]*[a-z0-9]$"
maxLength: 253
namespace:
type: string
labels:
type: object
patternProperties:
"^[a-zA-Z0-9][a-zA-Z0-9._-]*$":
type: string
required:
- name
spec:
type: object
properties:
replicas:
type: integer
minimum: 0
default: 1
selector:
type: object
properties:
matchLabels:
type: object
patternProperties:
".*":
type: string
required:
- matchLabels
template:
type: object
properties:
spec:
$ref: "#/definitions/PodSpec"
required:
- replicas
- selector
- template
required:
- apiVersion
- kind
- metadata
- spec
definitions:
PodSpec:
type: object
properties:
containers:
type: array
items:
$ref: "#/definitions/Container"
minItems: 1
Container:
type: object
properties:
name:
type: string
image:
type: string
pattern: "^[a-z0-9][a-z0-9._/-]*:[a-zA-Z0-9._-]+$"
ports:
type: array
items:
type: object
properties:
containerPort:
type: integer
minimum: 1
maximum: 65535
resources:
$ref: "#/definitions/ResourceRequirements"
required:
- name
- image
ResourceRequirements:
type: object
properties:
requests:
type: object
properties:
cpu:
type: string
pattern: "^\\d+m?$"
memory:
type: string
pattern: "^\\d+(Ki|Mi|Gi|Ti)?$"
limits:
type: object
properties:
cpu:
type: string
memory:
type: string
Helm values.yaml Schema
Helm 3 supports values.schema.json for chart value validation. Converting your default values to a YAML schema creates this validation automatically:
# values.schema.yaml
type: object
properties:
replicaCount:
type: integer
minimum: 1
image:
type: object
properties:
repository:
type: string
tag:
type: string
pullPolicy:
type: string
enum: [Always, IfNotPresent, Never]
Custom Resource Definitions (CRDs)
Kubernetes CRDs use OpenAPI v3 schema to validate custom resources. The same JSON-to-YAML-Schema conversion produces schemas suitable for CRD spec.validation.openAPIV3Schema.
Use Case
Platform engineering teams create internal Kubernetes templates and Helm charts that must follow organizational standards. A YAML schema generated from a reference deployment catches common errors like missing resource limits, invalid label formats, or incorrect API versions during local development and CI checks.