tl;dr: Test Kubernetes manifests for known production issues before deploying to production.
This repo contains Open Policy Agent policies for testing
Kubernetes manifests with conftest. The policies close the gap
between kubectl apply --dry-run --validate
or kubeval
, and the
Kubernetes API. These policies identify semantic validation issues
such as Deployment
selector not matching template labels and higher
level best practices.
$ git clone git@github.com:ahawkins/opa-kubernetes.git
$ conftest test -p opa-kubernetes/policy your_manifests/*.yml
$ conftest test -p opa-kubernetes/policy -n combined your_manifests/*.yml
Goals:
- Identify manifest that may be rejected by the Kubernetes API
- Identify functional issues in manifests not identified by existing tools
- Enforce best practices
- MTA-01: no
namespace
set - MTA-02: mandatory labels
- MTA-03:
name
well formed - MTA-04: Non-empty
annotations
andlabels
- MTA-05: String vlaues for
annotations
andlabels
Applies to: Deployment
, Job
, CronJob
- WRK-01: containers set resource requests and limits
- WRK-02:
volumeMount
matchesvolume
- WRK-03:
volumes
are mounted - WRK-04: container names do not contain invalid characters
- WRK-05:
env
values are explicit strings
- DPL-01: containers set liveness and readiness probes
- DPL-02: selector matches template labels
- DPL-03: liveness and readiness probes match container port
- DPL-04: reusing liveness and readiness probe
- JOB-01: explicit
backoffLimit
set
- CFG-01: values are String type
- SEC-01: base64 encoded secrets contain valid Base64 encoded keys
- HPA-01: Less minimum than maximum replicas
- CMB-01: container
envFrom
matches aConfigMap
orSecret
in the manifests - CMB-02: volume from matches
ConfigMap
orSecret
- CMB-03:
Service
selector matches aDeployment
- CMB-04:
HorizontalPodAutoscaler
scaling target matches aDeployment
- CMB-05:
Service
port matches container port - CMB-06: HPA managed deployment does not set replicas
- CMB-07:
Deployment
connected toService
sets a readiness probe
Applies to: Deployment
, Job
, CronJob
- DOG-01: Unified Service Tagging
- DOG-02: APM setup
- DOG-03: Autodiscovery Setup
Applies to sandbox
environments
Applies to the staging
environment
- STG-01: VPN only access
Add a new acceptance test in test/
for the rule. Tests take valid
data then modify them with yq
to break the rule. Tests assert that
the relevant conftest test
command exists non-zero and outputs the
rule number.
$ task test:acceptance