Skip to content

Commit

Permalink
Added check for cpu resources which are template params (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
garethahealy authored Jul 10, 2020
1 parent 726e987 commit 7bc36e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,6 @@ items:
resources:
requests:
memory: ${PARAM}
cpu: 1
cpu: ${PARAM}
limits:
memory: ${PARAM}
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ items:
resources:
requests:
memory: ${PARAM}
cpu: 1
cpu: ${PARAM}
limits:
memory: ${PARAM}
triggers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ warn[msg] {

container := input.spec.template.spec.containers[_]

not isResourceCpuRequestsContainsDollar with input as container
not isResourceCpuRequestsUnitsValid with input as container

msg := sprintf("%s/%s container '%s' cpu resources for requests (%s) has an incorrect unit. See: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes", [input.kind, input.metadata.name, container.name, container.resources.requests.cpu])
}

isResourceCpuRequestsContainsDollar {
not isResourceCpuRequestsAnCore
startswith(input.resources.requests.cpu, "$")
}

isResourceCpuRequestsAnCore {
is_number(input.resources.requests.cpu)
to_number(input.resources.requests.cpu)
Expand All @@ -128,10 +134,10 @@ isResourceCpuRequestsUnitsValid {
not isResourceCpuRequestsAnCore

# 'cpu' can be a quoted number, which is why we concat an empty string[] to match whole cpu cores
cpuLimitsUnit := array.concat(regex.find_n("[A-Za-z]+", input.resources.requests.cpu, 1), [""])[0]
cpuRequestsUnit := array.concat(regex.find_n("[A-Za-z]+", input.resources.requests.cpu, 1), [""])[0]

units := ["m", ""]
cpuLimitsUnit == units[_]
cpuRequestsUnit == units[_]
}

warn[msg] {
Expand Down

0 comments on commit 7bc36e8

Please sign in to comment.