Skip to content

Commit

Permalink
add: policies, resources and new actions
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliagranato committed Jan 24, 2024
1 parent cac091e commit 8927ae8
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 174 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/kyverno-test-policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: kyverno-policy-test
# This workflow is triggered on push to any branch and pull request to the main branch.
# It runs on ubuntu-latest.
# It uses the community action kyverno/action-install-cli@v0.2.0
# It runs the following steps:
on:
push:
branches:
- '*'
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install Kyverno CLI
uses: kyverno/action-install-cli@v0.2.0
with:
release: 'v1.11.0'
- name: Check install
run: kyverno version
- name: Test new resources against existing policies
run: kyverno apply policies/ -r resources/

44 changes: 21 additions & 23 deletions .github/workflows/kyverno.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
name: kyverno-policy-test
name: Kyverno Policy Check

on:
push:
branches:
- '*'
pull_request:
branches:
- main

push:
branches:
- main

jobs:
test:
test-kyverno-policies:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install Kyverno CLI
uses: kyverno/action-install-cli@v0.2.0
with:
release: 'v1.11.0'
- name: Check install
run: kyverno version
- name: Test new resources against existing policies
run: kyverno apply policies/ -r resources/
- name: Test pre-defined cases
run: kyverno test tests/
- name: Checkout Repository
uses: actions/checkout@v2

- name: Validate YAML Indentation
run: |
if ! yamllint policies/; then
echo "YAML indentation validation failed."
exit 1
fi
- name: Simulate Apply Kyverno Policy
run: |
kubectl apply -f policies/require-resources-limits.yml --dry-run=client -o yaml
36 changes: 26 additions & 10 deletions policies/require-resources-limits.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
apiVersion: kyverno.io/v1

Check warning on line 1 in policies/require-resources-limits.yml

View workflow job for this annotation

GitHub Actions / test-kyverno-policies

1:1 [document-start] missing document start "---"
kind: ClusterPolicy
metadata:
name: require-resources-limits
name: require-requests-limits
annotations:
policies.kyverno.io/title: Require Limits and Requests
policies.kyverno.io/category: Best Practices, EKS Best Practices
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Pod
policies.kyverno.io/minversion: 1.6.0
policies.kyverno.io/description: >-
As application workloads share cluster resources, it is important to limit resources

Check failure on line 12 in policies/require-resources-limits.yml

View workflow job for this annotation

GitHub Actions / test-kyverno-policies

12:81 [line-length] line too long (90 > 80 characters)
requested and consumed by each Pod. It is recommended to require resource requests and

Check failure on line 13 in policies/require-resources-limits.yml

View workflow job for this annotation

GitHub Actions / test-kyverno-policies

13:81 [line-length] line too long (92 > 80 characters)
limits per Pod, especially for memory and CPU. If a Namespace level request or limit is specified,

Check failure on line 14 in policies/require-resources-limits.yml

View workflow job for this annotation

GitHub Actions / test-kyverno-policies

14:81 [line-length] line too long (104 > 80 characters)
defaults will automatically be applied to each Pod based on the LimitRange configuration.

Check failure on line 15 in policies/require-resources-limits.yml

View workflow job for this annotation

GitHub Actions / test-kyverno-policies

15:81 [line-length] line too long (95 > 80 characters)
This policy validates that all containers have something specified for memory and CPU

Check failure on line 16 in policies/require-resources-limits.yml

View workflow job for this annotation

GitHub Actions / test-kyverno-policies

16:81 [line-length] line too long (91 > 80 characters)
requests and memory limits.
spec:
validationFailureAction: Enforce
validationFailureAction: audit
background: true
rules:
- name: validate-resources-limits
- name: validate-resources
match:
resources:
kinds:
- Pod
any:
- resources:
kinds:
- Pod
validate:
message: "Pod {{request.object.metadata.name}} does not have resource limits set"
message: "CPU and memory resource requests and limits are required."
pattern:
spec:
containers:
- name: "*"
resources:
limits:
- resources:
requests:
memory: "?*"
cpu: "?*"
limits:
memory: "?*"
18 changes: 18 additions & 0 deletions resources/require-resources-limits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx
namespace: default
spec:
containers:
- image: nginx
name: nginx
resources:
requests:
memory: "?*"
cpu: "?*"
limits:
memory: "?*"
cpu: "?*"
24 changes: 0 additions & 24 deletions tests/kyverno-test.yaml

This file was deleted.

31 changes: 0 additions & 31 deletions tests/require_labels.yaml

This file was deleted.

86 changes: 0 additions & 86 deletions tests/resource.yaml

This file was deleted.

0 comments on commit 8927ae8

Please sign in to comment.