Skip to content

Commit 154548f

Browse files
committed
fix(cozy-lib): reject a negative minReplicaCount and cover the annotations parameter
cozy-lib.keda.scaledObject only checked max >= min, so an all-negative or mixed-sign pair (min -5, max -3) passed the ordering guard and rendered a ScaledObject with a nonsensical floor. Reject minReplicaCount < 0 before the ordering check. Also add a cozy-lib-tests case for the documented annotations parameter, which had no caller and no coverage. Follow-up from cozystack#3954 (cozystack#4187). Signed-off-by: Alexey Artamonov <[email protected]>
1 parent 1f2ff31 commit 154548f

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎packages/library/cozy-lib/templates/_keda.tpl‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ Parameters:
8989
{{- end -}}
9090
{{- $min := int .minReplicaCount -}}
9191
{{- $max := int .maxReplicaCount -}}
92+
{{- /* A replica floor cannot be negative; reject it before the ordering check so an
93+
all-negative or mixed-sign pair (e.g. min -5, max -3, which satisfies max >= min)
94+
cannot render a ScaledObject with a nonsensical bound. A negative max with a
95+
non-negative min is already caught by the ordering check below. */ -}}
96+
{{- if lt $min 0 -}}
97+
{{- fail "cozy-lib.keda.scaledObject: minReplicaCount must be >= 0" -}}
98+
{{- end -}}
9299
{{- if lt $max $min -}}
93100
{{- fail "cozy-lib.keda.scaledObject: maxReplicaCount must be >= minReplicaCount" -}}
94101
{{- end -}}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
{{- if or .Values.omit .Values.badRef .Values.badMin -}}
1+
{{- if or .Values.omit .Values.badRef .Values.badMin .Values.negMin .Values.wantAnnotations -}}
22
{{- /* Build a fully-valid argument dict, then either drop exactly one required
33
field named by .Values.omit, replace scaleTargetRef with a non-map
4-
(.Values.badRef), or set a non-numeric minReplicaCount (.Values.badMin), to
5-
exercise each fail-closed guard in isolation. */ -}}
4+
(.Values.badRef), set a non-numeric minReplicaCount (.Values.badMin), set a
5+
negative minReplicaCount (.Values.negMin), or attach an extra annotation
6+
(.Values.wantAnnotations), to exercise each guard / merge path in isolation. */ -}}
67
{{- $ref := dict "apiVersion" "postgresql.cnpg.io/v1" "kind" "Cluster" "name" "db" -}}
78
{{- if eq .Values.omit "kind" }}{{- $_ := unset $ref "kind" }}{{- end -}}
89
{{- if eq .Values.omit "apiVersion" }}{{- $_ := unset $ref "apiVersion" }}{{- end -}}
910
{{- $arg := dict "name" "db" "scaleTargetRef" $ref "minReplicaCount" 2 "maxReplicaCount" 6 "serverAddress" "http://vmselect.example.svc:8481/select/0/prometheus" "query" "sum(x) + 150" "threshold" "150" -}}
1011
{{- if .Values.omit }}{{- $arg = unset $arg .Values.omit -}}{{- end -}}
1112
{{- if .Values.badRef }}{{- $arg = set $arg "scaleTargetRef" "not-a-map" -}}{{- end -}}
1213
{{- if .Values.badMin }}{{- $arg = set $arg "minReplicaCount" "two" -}}{{- end -}}
14+
{{- if .Values.negMin }}{{- $arg = set $arg "minReplicaCount" -1 -}}{{- end -}}
15+
{{- if .Values.wantAnnotations }}{{- $arg = set $arg "annotations" (dict "cozystack.io/example" "hi") -}}{{- end -}}
1316
{{ include "cozy-lib.keda.scaledObject" $arg }}
1417
{{- end -}}

‎packages/tests/cozy-lib-tests/tests/keda_scaledobject_test.yaml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,17 @@ tests:
203203
set: {badMin: true}
204204
asserts:
205205
- failedTemplate: {errorMessage: "cozy-lib.keda.scaledObject: minReplicaCount is required and must be a number"}
206+
207+
- it: fails closed when minReplicaCount is negative
208+
templates: [templates/tests/keda-scaledobject-arg.yaml]
209+
set: {negMin: true}
210+
asserts:
211+
- failedTemplate: {errorMessage: "cozy-lib.keda.scaledObject: minReplicaCount must be >= 0"}
212+
213+
- it: merges a caller-supplied annotation onto the ScaledObject
214+
templates: [templates/tests/keda-scaledobject-arg.yaml]
215+
set: {wantAnnotations: true}
216+
asserts:
217+
- equal:
218+
path: metadata.annotations["cozystack.io/example"]
219+
value: hi

0 commit comments

Comments
 (0)