-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(query): Extend memory_requests_not_defined k8s rule to cover furt…
…her resource kinds (#4944) * fix(query): Extend memory_requests_not_defined k8s rule to cover further resource kinds * add searchLine attribute to query
- Loading branch information
Showing
7 changed files
with
73 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,26 @@ | ||
package Cx | ||
|
||
import data.generic.common as common_lib | ||
import data.generic.k8s as k8sLib | ||
|
||
types := {"initContainers", "containers"} | ||
|
||
CxPolicy[result] { | ||
document := input.document[i] | ||
spec := document.spec | ||
specInfo := k8sLib.getSpecInfo(document) | ||
container := specInfo.spec[types[t]][c] | ||
|
||
containers := spec[types[t]][c] | ||
|
||
requests := containers.resources.requests | ||
resources := object.get(container, "resources", {}) | ||
requests := object.get(resources, "requests", {}) | ||
not common_lib.valid_key(requests, "memory") | ||
|
||
metadata := document.metadata | ||
|
||
result := { | ||
"documentId": document.id, | ||
"searchKey": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources.requests", [metadata.name, types[t], containers.name]), | ||
"issueType": "MissingAttribute", | ||
"keyExpectedValue": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources.requests.memory is defined", [metadata.name, types[t], containers.name]), | ||
"keyActualValue": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources.requests.memory is undefined", [metadata.name, types[t], containers.name]), | ||
} | ||
} | ||
|
||
CxPolicy[result] { | ||
document := input.document[i] | ||
spec := document.spec | ||
|
||
containers := spec[types[t]][c] | ||
|
||
not common_lib.valid_key(containers, "resources") | ||
metadata := document.metadata | ||
|
||
result := { | ||
"documentId": document.id, | ||
"searchKey": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}", [metadata.name, types[t], containers.name]), | ||
"issueType": "MissingAttribute", | ||
"keyExpectedValue": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources are defined", [metadata.name, types[t], containers.name]), | ||
"keyActualValue": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources are undefined", [metadata.name, types[t], containers.name]), | ||
} | ||
} | ||
|
||
CxPolicy[result] { | ||
document := input.document[i] | ||
spec := document.spec | ||
|
||
containers := spec[types[t]][c] | ||
|
||
resources := containers.resources | ||
not common_lib.valid_key(resources, "requests") | ||
|
||
metadata := document.metadata | ||
|
||
result := { | ||
"documentId": document.id, | ||
"searchKey": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources", [metadata.name, types[t], containers.name]), | ||
"searchKey": sprintf("metadata.name={{%s}}.%s.%s.name={{%s}}", [metadata.name, specInfo.path, types[t], container.name]), | ||
"issueType": "MissingAttribute", | ||
"keyExpectedValue": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources.requests are defined", [metadata.name, types[t], containers.name]), | ||
"keyActualValue": sprintf("metadata.name={{%s}}.spec.%s.name={{%s}}.resources.requests are undefined", [metadata.name, types[t], containers.name]), | ||
"keyExpectedValue": sprintf("metadata.name={{%s}}.%s.%s.name={{%s}}.resources.requests.memory is defined", [metadata.name, specInfo.path, types[t], container.name]), | ||
"keyActualValue": sprintf("metadata.name={{%s}}.%s.%s.name={{%s}}.resources.requests.memory is undefined", [metadata.name, specInfo.path, types[t], container.name]), | ||
"searchLine": common_lib.build_search_line(split(specInfo.path, "."), [types[t], c, "resources", "requests"]) | ||
} | ||
} |
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
assets/queries/k8s/memory_requests_not_defined/test/negative2.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: test-deployment-ctr-neg | ||
labels: | ||
app: test-neg | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: test-ctr-neg | ||
template: | ||
metadata: | ||
labels: | ||
app: test-ctr-neg | ||
spec: | ||
containers: | ||
- name: pause | ||
image: k8s.gcr.io/pause | ||
resources: | ||
limits: | ||
cpu: 0.5 | ||
memory: 512Mi | ||
requests: | ||
cpu: 0.5 | ||
memory: 512Mi |
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
assets/queries/k8s/memory_requests_not_defined/test/positive2.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: test-deployment2 | ||
labels: | ||
app: test2 | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: test2 | ||
template: | ||
metadata: | ||
labels: | ||
app: test2 | ||
spec: | ||
containers: | ||
- name: pause | ||
image: k8s.gcr.io/pause | ||
resources: | ||
limits: | ||
cpu: 0.5 | ||
memory: 512Mi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters