Format and Validate Kubernetes YAML Manifests
Format Kubernetes YAML manifests (Deployments, Services, ConfigMaps) with proper indentation. Validate structure and catch common K8s YAML errors before applying to your cluster.
Detailed Explanation
Kubernetes YAML Formatting
Kubernetes uses YAML as its primary configuration language. Every resource — Deployments, Services, ConfigMaps, Ingresses — is defined in YAML manifests. Proper formatting is essential because Kubernetes is strict about YAML structure.
Anatomy of a Kubernetes Manifest
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
namespace: production
labels:
app: web-server
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
app: web-server
template:
metadata:
labels:
app: web-server
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Common Formatting Issues
- Inconsistent indentation — Mixing 2-space and 4-space indentation within a single manifest
- Tab characters — Kubernetes rejects YAML with tabs; only spaces are allowed
- Trailing whitespace — Can cause unexpected diffs in version control
- Missing
---separators — Multi-resource files need document separators between resources - Quoted vs. unquoted values — Resource limits like
"250m"need quotes to avoid being parsed as numbers
Multi-Resource Files
Kubernetes manifests often contain multiple resources in a single file, separated by ---:
apiVersion: v1
kind: Service
metadata:
name: web-server
spec:
selector:
app: web-server
ports:
- port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
# ...
A YAML formatter must handle multi-document files correctly, formatting each document independently while preserving the --- separators.
Best Practices
- Use 2-space indentation (Kubernetes ecosystem standard)
- Always quote string values that look like numbers or booleans
- Use
---to separate multiple resources - Run
kubectl apply --dry-run=clientafter formatting to validate
Use Case
Kubernetes YAML formatting is a daily task for DevOps engineers and platform teams. Before applying manifests to a cluster, formatting catches indentation errors that would cause kubectl to reject the file. Teams often integrate YAML formatting into their CI pipeline alongside kubeval or kubeconform validation.