Skip to content

Commit

Permalink
feat: add hpa
Browse files Browse the repository at this point in the history
  • Loading branch information
soniqua committed Jul 8, 2024
1 parent 2a99a81 commit 5f19291
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
33 changes: 33 additions & 0 deletions charts/snyk-broker/templates/broker_hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{- $autoscaling := .Values.autoscaling | default dict -}}
{{- $cpuType := ternary "Utilization" "Value" ( eq $autoscaling.cpu.type "Utilization" ) -}}
{{- $memoryType := ternary "Utilization" "Value" ( eq $autoscaling.memory.type "Utilization" ) -}}
{{- if ( and $autoscaling.enabled .Values.highAvailabilityMode.enabled ) -}}
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2
metadata:
name: "{{ .Values.scmType}}-broker{{if not .Values.disableSuffixes }}-{{ .Release.Name }}{{ end }}"
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: "{{ .Values.scmType}}-broker{{if not .Values.disableSuffixes }}-{{ .Release.Name }}{{ end }}"
minReplicas: {{ $autoscaling.minReplicas | default 2 }}
maxReplicas: {{ $autoscaling.maxReplicas | default 4 }}
metrics:
{{- if $autoscaling.cpu.enabled }}
- type: Resource
resource:
name: cpu
target:
type: {{ $autoscaling.cpu.type }}
{{ printf "average%s" $cpuType }}: {{ $autoscaling.cpu.value }}
{{- end -}}
{{- if $autoscaling.memory.enabled }}
- type: Resource
resource:
name: memory
target:
type: {{ $autoscaling.memory.type }}
{{ printf "average%s" $memoryType }}: {{ $autoscaling.memory.value }}
{{- end -}}
{{- end -}}
78 changes: 78 additions & 0 deletions charts/snyk-broker/tests/broker_autoscaling_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
suite: test broker hpa
chart:
version: 0.0.0
templates:
- broker_hpa.yaml
values:
- ./fixtures/default_values.yaml

tests:
- it: does not render if autoscaling enabled but ha mode is off
set:
autoscaling.enabled: true
asserts:
- containsDocument:
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2
name: github-com-broker-RELEASE-NAME
not: true
- it: does not render if autoscaling off and ha mode is on
set:
autoscaling.enabled: false
highAvailabilityMode.enabled: true
asserts:
- containsDocument:
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2
name: github-com-broker-RELEASE-NAME
not: true
- it: renders if autoscaling enabled and ha is on
set:
autoscaling.enabled: true
highAvailabilityMode.enabled: true
asserts:
- containsDocument:
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2
name: github-com-broker-RELEASE-NAME
- it: targets the correct deployment
set:
autoscaling.enabled: true
highAvailabilityMode.enabled: true
asserts:
- equal:
path: spec.scaleTargetRef.name
value: github-com-broker-RELEASE-NAME
- it: overrides cpu to value correctly
set:
autoscaling.enabled: true
highAvailabilityMode.enabled: true
autoscaling.cpu.type: "AverageValue"
autoscaling.cpu.value: 100m
asserts:
- contains:
path: spec.metrics
content:
type: Resource
resource:
name: cpu
target:
averageValue: 100m
type: AverageValue
- it: overrides memory to value correctly
set:
autoscaling.enabled: true
highAvailabilityMode.enabled: true
autoscaling.memory.enabled: true
autoscaling.memory.type: "AverageValue"
autoscaling.memory.value: 256Mi
asserts:
- contains:
path: spec.metrics
content:
type: Resource
resource:
name: memory
target:
averageValue: 256Mi
type: AverageValue

0 comments on commit 5f19291

Please sign in to comment.