Skip to content

Modify rule S6892: Add examples for LimitRange #4017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rules/S6864/kubernetes/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ node failure.

=== Code examples

To avoid potential issues, either specify a memory limit for each container in a pod specification or create a resource of a kind, `LimitRange`, that sets a default memory limit for all containers in all pod specifications belonging to the same namespace.
To avoid potential issues, either specify a memory limit for each container in a pod specification or create a resource of a kind `LimitRange`, that sets a default memory limit for all containers in all pod specifications belonging to the same namespace.

==== Noncompliant code example

Expand Down
40 changes: 38 additions & 2 deletions rules/S6892/kubernetes/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ By setting a CPU request, Kubernetes will make sure that the container will get

=== Code examples

To avoid potential issues specify a CPU request for each container with `resources.requests.cpu`.
To avoid potential issues, either specify a CPU request for each container with `resources.requests.cpu` or create a resource of a kind `LimitRange` that sets a default CPU request for all containers in all pod specifications in a namespace.

==== Noncompliant code example

Expand All @@ -53,6 +53,18 @@ spec:
image: nginx
----

[source,yaml,diff-id=2,diff-type=noncompliant]
----
apiVersion: v1
kind: Pod
metadata:
name: example
spec:
containers:
- name: web # Noncompliant
image: nginx
----

==== Compliant solution

[source,yaml,diff-id=1,diff-type=compliant]
Expand All @@ -70,11 +82,35 @@ spec:
cpu: 0.5
----

[source,yaml,diff-id=2,diff-type=compliant]
----
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-request-range
namespace: default-cpu-example
spec:
limits:
- defaultRequest:
cpu: 0.5
type: Container
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-ns-compliant
namespace: default-cpu-example
spec:
containers:
- name: nginx-ns-compliant
image: nginx
----

=== How does this work?

A request can be set through the property `resources.requests.cpu` of a
container. Alternatively, a default request for a namespace can be set with
`LimitRange`.
`LimitRange` through the property `spec.limits[].defaultRequest.cpu`.

== Resources

Expand Down