TExcelTAD
Home › Kubernetes Packaging: Helm & Kustomize › Validating with kubeconform

Validating with kubeconform

helm lint checks that a chart renders; kustomize build checks that the overlays assemble. Neither checks that the result is a valid Kubernetes object: that replicas is a number, that containerPort is spelled right, that an HTTPRoute's backendRefs has the fields Gateway API requires. Without a cluster, that's the job of kubeconform, a fast validator that checks manifests against JSON schemas generated from the Kubernetes OpenAPI specification.

What it catches

bash · try it yourself
cat > broken.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: linkly
spec:
  replicas: "2"
  selector: {matchLabels: {app: linkly}}
  template:
    metadata: {labels: {app: linkly}}
    spec:
      containers:
        - name: linkly
          image: ghcr.io/acme/linkly:1.4.0
          imagePullPolice: Always
          ports: [{containerPort: 8080}]
---
apiVersion: v1
kind: Service
metadata:
  name: linkly
spec:
  ports: [{port: "80", targetPort: http}]
  selector: {app: linkly}
---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: linkly
spec: {}
EOF
kubeconform -n 1 -summary broken.yaml 2>&1 | sed -E 's/ - at /\n    at /g; s/problem validating schema.*json#.//'
echo "--- with -strict:"
kubeconform -n 1 -strict broken.yaml 2>&1 | grep -o "at '[^']*': [^-]*" | sort -u
broken.yaml - Deployment linkly is invalid:
    at '/spec/replicas': got string, want null or integer
broken.yaml - Service linkly is invalid:
    at '/spec/ports/0/port': got string, want integer
broken.yaml - HorizontalPodAutoscaler linkly failed validation: could not find schema for HorizontalPodAutoscaler
Summary: 3 resources found in 1 file - Valid: 0, Invalid: 2, Errors: 1, Skipped: 0
--- with -strict:
at '/spec/ports/0/port': got string, want integer
at '/spec/replicas': got string, want null or integer
at '/spec/template/spec/containers/0': additional properties 'imagePullPolice' not allowed

Read it from the bottom up:

  • replicas: "2" and the Service's port: "80" are strings where integers are required: invalid in both modes. (-n 1 runs a single worker: kubeconform validates in parallel by default and may print results in any order. The sed shortens the long messages.)
  • imagePullPolice is an unknown field, a typo of imagePullPolicy. Only -strict reports it; without it, the object passes and the setting silently doesn't apply. Always use -strict.
  • Not everything is in the schemas: an invalid Service type such as LoadBalancr passes, because the OpenAPI spec doesn't list the allowed values. The API server would still reject it.
  • autoscaling/v2beta2 HorizontalPodAutoscaler was removed in Kubernetes 1.26, so there is no schema for it: kubeconform reports an error (no schema), which is how it catches manifests that target removed APIs.

Which Kubernetes version?

Schemas are versioned like Kubernetes. kubeconform defaults to the master schemas; this course's image ships offline schemas for Kubernetes 1.34.12, the newest version published in the community schema repository it uses, and its kubeconform wrapper selects them. For clusters on 1.35 to 1.37 that's close enough for structure checks, but an API added after 1.34 would be reported as missing a schema. In your own CI, pin -kubernetes-version to your clusters' version (or the closest published one) and update it when you upgrade clusters.

Custom resources

Gateway API, cert-manager, Prometheus Operator, Argo CD and Flux objects aren't in the Kubernetes schemas. kubeconform takes extra -schema-location templates, and the community CRDs-catalog publishes JSON schemas for popular CRDs. The image's wrapper adds a local copy of the catalog for the projects listed in the course intro:

bash · try it yourself
cat > routes.yaml <<'EOF'
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: linkly
spec:
  parentRefs: [{name: public, namespace: gateway-system}]
  hostnames: [lnk.example.com]
  rules:
    - backendRefs: [{name: linkly, port: 80}]
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: linkly-admin
spec:
  parentRefs: [{name: public, namespace: gateway-system}]
  rules:
    - matches: [{path: {type: Prefix, value: /admin}}]
      backendRefs: [{name: linkly-admin}]
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: linkly
spec:
  selector: {matchLabels: {app: linkly}}
  endpoints: [{port: http, path: /metrics}]
---
apiVersion: linkerd.io/v1alpha2
kind: ServiceProfile
metadata:
  name: linkly.links.svc.cluster.local
spec: {}
EOF
kubeconform -n 1 -strict -summary routes.yaml 2>&1 | sed -E 's/ - at /\n    at /g; s/problem validating schema.*json#.//'
echo "--- skipping kinds without a schema:"
kubeconform -n 1 -strict -summary -ignore-missing-schemas routes.yaml 2>&1 | tail -1
routes.yaml - HTTPRoute linkly-admin is invalid:
    at '/spec/rules/0/matches/0/path/type': value must be one of 'Exact', 'PathPrefix', 'RegularExpression'
routes.yaml - ServiceProfile linkly.links.svc.cluster.local failed validation: could not find schema for ServiceProfile
Summary: 4 resources found in 1 file - Valid: 2, Invalid: 1, Errors: 1, Skipped: 0
--- skipping kinds without a schema:
Summary: 4 resources found in 1 file - Valid: 2, Invalid: 1, Errors: 0, Skipped: 1

The admin route has a real mistake, found offline: Gateway API's path match type is PathPrefix, not Prefix. It has a second one that the schema can't see: a backendRefs entry for a Service needs a port, a rule Gateway API enforces with CEL validation rules inside the CRD, which JSON-schema validators don't evaluate. The API server would reject it. The Linkerd object has no schema in the catalog. -ignore-missing-schemas turns that into a skip, but say so in the pipeline: a skipped object is an unvalidated object. For CRDs not in the catalog, generate a schema from the CRD itself (the kubeconform project ships openapi2jsonschema.py for that) and add a -schema-location pointing at it.

The schema location templates the wrapper uses look like this (useful when you set up CI yourself):

bash
kubeconform -strict -summary \
  -schema-location default \
  -schema-location 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json' \
  rendered.yaml

Output formats

For CI, -output json, junit or tap feed test reports; -summary gives the one-line count. JSON output lists only the failing objects unless you add -verbose. Exit code 1 means at least one invalid or errored object:

bash · try it yourself
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: ok\ndata: {a: "1"}\n---\napiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: bad\ndata: {a: 1}\n' > cms.yaml
kubeconform -n 1 -strict -output json cms.yaml | jq -c '.resources[] | {name, status, msg: (.msg | .[0:60])}'
echo "exit code: ${PIPESTATUS[0]}"
kubeconform -n 1 -strict -output tap cms.yaml | cut -c1-80
{"name":"bad","status":"statusInvalid","msg":"problem validating schema. Check JSON formatting: jsonschema"}
exit code: 1
TAP version 13
ok 1 - cms.yaml (v1/ConfigMap//ok)
not ok 2 - cms.yaml (v1/ConfigMap//bad): problem validating schema. Check JSON f
1..2

(data: {a: 1} is invalid because ConfigMap values must be strings, the same trap as in Values Files & Overrides.)

What kubeconform doesn't do

It checks structure, not policy or cluster state. It won't tell you that the image tag is latest, that there's no memory limit, that a Service selects no Pods, or that the referenced Gateway doesn't exist. Those need policy tools (kube-linter, Polaris, Kyverno CLI, Conftest; see the Toolkit) and, finally, a server-side dry run (kubectl apply --dry-run=server, helm upgrade --dry-run=server) against a real cluster with its admission webhooks.

Test yourself

Which flag makes kubeconform report a misspelt field such as containerport?

  1. -summary
  2. -strict
  3. -ignore-missing-schemas
  4. -verbose

How does kubeconform validate an HTTPRoute?

  1. It can't
  2. With an extra -schema-location pointing at Gateway API JSON schemas (e.g. from the CRDs-catalog)
  3. By asking the cluster
  4. HTTPRoute is in the core schemas

Why is -ignore-missing-schemas risky?

  1. It slows validation
  2. Objects without schemas are silently skipped, i.e. not validated at all
  3. It ignores invalid objects
  4. It disables -strict