Skip to content

Commit

Permalink
feat: new checks to require memory and CPU resources limits and requests
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm committed Jun 19, 2023
1 parent 50195db commit 69acd30
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 1 deletion.
48 changes: 48 additions & 0 deletions internal/builtins/general/M-404_memory_requests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2023 Undistro Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

id: M-404
slug: memory-requests
severity: Medium
message: "Memory requests not specified"
match:
resources:
- group: ""
version: v1
resource: pods
- group: apps
version: v1
resource: deployments
- group: apps
version: v1
resource: daemonsets
- group: apps
version: v1
resource: statefulsets
- group: apps
version: v1
resource: replicasets
- group: batch
version: v1
resource: cronjobs
- group: batch
version: v1
resource: jobs
validations:
- expression: >
allContainers.all(container,
has(container.resources) &&
has(container.resources.requests) &&
has(container.resources.requests.memory)
)
79 changes: 79 additions & 0 deletions internal/builtins/general/M-404_memory_requests_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2023 Undistro Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: "resources not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
- name: "resources requests not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 128Mi
- name: "memory requests not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
cpu: 10m
- name: "ok"
pass: true
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
cpu: 10m
memory: 64Mi
48 changes: 48 additions & 0 deletions internal/builtins/general/M-405_cpu_requests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2023 Undistro Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

id: M-405
slug: cpu-requests
severity: Medium
message: "CPU requests not specified"
match:
resources:
- group: ""
version: v1
resource: pods
- group: apps
version: v1
resource: deployments
- group: apps
version: v1
resource: daemonsets
- group: apps
version: v1
resource: statefulsets
- group: apps
version: v1
resource: replicasets
- group: batch
version: v1
resource: cronjobs
- group: batch
version: v1
resource: jobs
validations:
- expression: >
allContainers.all(container,
has(container.resources) &&
has(container.resources.requests) &&
has(container.resources.requests.cpu)
)
79 changes: 79 additions & 0 deletions internal/builtins/general/M-405_cpu_requests_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2023 Undistro Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: "resources not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
- name: "resources requests not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 128Mi
- name: "cpu requests not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
memory: 64Mi
- name: "ok"
pass: true
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
cpu: 10m
memory: 64Mi
48 changes: 48 additions & 0 deletions internal/builtins/general/M-406_memory_limit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2023 Undistro Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

id: M-406
slug: memory-limit
severity: Medium
message: "Memory not limited"
match:
resources:
- group: ""
version: v1
resource: pods
- group: apps
version: v1
resource: deployments
- group: apps
version: v1
resource: daemonsets
- group: apps
version: v1
resource: statefulsets
- group: apps
version: v1
resource: replicasets
- group: batch
version: v1
resource: cronjobs
- group: batch
version: v1
resource: jobs
validations:
- expression: >
allContainers.all(container,
has(container.resources) &&
has(container.resources.limits) &&
has(container.resources.limits.memory)
)
78 changes: 78 additions & 0 deletions internal/builtins/general/M-406_memory_limit_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2023 Undistro Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: "resources not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
- name: "resources limits not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
memory: 64Mi
- name: "memory limits not set"
pass: false
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
cpu: 500m
- name: "ok"
pass: true
input: |
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 128Mi
Loading

0 comments on commit 69acd30

Please sign in to comment.