Skip to content

Commit

Permalink
Fix numerical resource ranges (#991)
Browse files Browse the repository at this point in the history
* fix numerical resource numbers

* add resource range tests
  • Loading branch information
rbren authored Sep 5, 2023
1 parent c856874 commit 9768776
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func (max resourceMaximum) Validate(path string, data interface{}, errs *[]jsons
}

func parseQuantity(i interface{}) (resource.Quantity, *[]jsonschema.ValError) {
if resNum, ok := i.(float64); ok {
i = fmt.Sprintf("%f", resNum)
}
resStr, ok := i.(string)
if !ok {
return resource.Quantity{}, &[]jsonschema.ValError{
Expand Down
32 changes: 32 additions & 0 deletions test/checks/resourceRange/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
containers:
exclude:
- initContainer
successMessage: Resource limits are within the required range
failureMessage: Resource limits should be within the required range
category: Resources
target: Container
schema:
'$schema': http://json-schema.org/draft-07/schema
type: object
required:
- resources
properties:
resources:
type: object
required:
- limits
properties:
limits:
type: object
required:
- memory
- cpu
properties:
memory:
type: string
resourceMinimum: 100Mi # 104857600 bytes
resourceMaximum: 6G
cpu:
type: string
resourceMinimum: 100m
resourceMaximum: "2"
14 changes: 14 additions & 0 deletions test/checks/resourceRange/failure.cpu-number.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app.kubernetes.io/name: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 250Mi
cpu: 0.05
14 changes: 14 additions & 0 deletions test/checks/resourceRange/failure.mem-number.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app.kubernetes.io/name: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 104857599
cpu: 1
14 changes: 14 additions & 0 deletions test/checks/resourceRange/success.cpu-number.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app.kubernetes.io/name: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 250Mi
cpu: 1.5
14 changes: 14 additions & 0 deletions test/checks/resourceRange/success.mem-number.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app.kubernetes.io/name: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: 104857600
cpu: 1

0 comments on commit 9768776

Please sign in to comment.