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
namespaceset - MTA-02: mandatory labels
- MTA-03:
namewell formed - MTA-04: Non-empty
annotationsandlabels - MTA-05: String vlaues for
annotationsandlabels
Applies to: Deployment, Job, CronJob
- WRK-01: containers set resource requests and limits
- WRK-02:
volumeMountmatchesvolume - WRK-03:
volumesare mounted - WRK-04: container names do not contain invalid characters
- WRK-05:
envvalues 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
backoffLimitset
- 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
envFrommatches aConfigMaporSecretin the manifests - CMB-02: volume from matches
ConfigMaporSecret - CMB-03:
Serviceselector matches aDeployment - CMB-04:
HorizontalPodAutoscalerscaling target matches aDeployment - CMB-05:
Serviceport matches container port - CMB-06: HPA managed deployment does not set replicas
- CMB-07:
Deploymentconnected toServicesets 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