Skip to content

Commit 077e20b

Browse files
committed
feat: issue-49 adding if statements for the cpu and memory with tests,
changing to use include instead of template
1 parent b39f7b3 commit 077e20b

File tree

3 files changed

+115
-6
lines changed

3 files changed

+115
-6
lines changed

charts/k8s-service/templates/horizontalpodautoscaler.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@
22
apiVersion: autoscaling/v2beta2
33
kind: HorizontalPodAutoscaler
44
metadata:
5-
name: {{ template "k8s-service.fullname" . }}
5+
name: {{ include "k8s-service.fullname" . }}
66
namespace: {{ $.Release.Namespace }}
77
spec:
88
scaleTargetRef:
99
apiVersion: apps/v1
1010
kind: Deployment
11-
name: {{ template "k8s-service.fullname" . }}
11+
name: {{ include "k8s-service.fullname" . }}
1212
minReplicas: {{ .Values.horizontalPodAutoscaler.minReplicas }}
1313
maxReplicas: {{ .Values.horizontalPodAutoscaler.maxReplicas }}
1414
metrics:
15+
{{ if .Values.horizontalPodAutoscaler.avgCpuUtilization }}
1516
- type: Resource
1617
resource:
1718
name: cpu
1819
target:
1920
type: Utilization
2021
averageUtilization: {{ .Values.horizontalPodAutoscaler.avgCpuUtilization }}
22+
{{- end }}
23+
{{ if .Values.horizontalPodAutoscaler.avgMemoryUtilization }}
2124
- type: Resource
2225
resource:
2326
name: memory
2427
target:
2528
type: Utilization
2629
averageUtilization: {{ .Values.horizontalPodAutoscaler.avgMemoryUtilization }}
30+
{{- end }}
2731
{{- end }}

charts/k8s-service/values.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ horizontalPodAutoscaler:
419419
enabled: false
420420
minReplicas: 1
421421
maxReplicas: 10
422-
avgCpuUtilization: 50
423-
avgMemoryUtilization: 50
424422

425423

426424
#----------------------------------------------------------------------------------------------------------------------

test/k8s_service_horizontal_pod_autoscaler_template_test.go

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
)
1818

1919
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
20-
// Autoscaler resource
21-
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscaler(t *testing.T) {
20+
// Autoscaler resource with both metrics
21+
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithAllMetrics(t *testing.T) {
2222
t.Parallel()
2323
minReplicas := "20"
2424
maxReplicas := "30"
@@ -81,3 +81,110 @@ func TestK8SServiceHorizontalPodAutoscalerCreateFalse(t *testing.T) {
8181
assert.NoError(t, err)
8282
assert.Equal(t, 0, len(rendered))
8383
}
84+
85+
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
86+
// Autoscaler resource with the cpu metric
87+
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithCpuMetric(t *testing.T) {
88+
t.Parallel()
89+
minReplicas := "20"
90+
maxReplicas := "30"
91+
avgCpuUtil := "55"
92+
93+
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
94+
require.NoError(t, err)
95+
96+
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
97+
// We then use SetValues to override all the defaults.
98+
options := &helm.Options{
99+
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
100+
SetValues: map[string]string{
101+
"horizontalPodAutoscaler.enabled": "true",
102+
"horizontalPodAutoscaler.minReplicas": minReplicas,
103+
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
104+
"horizontalPodAutoscaler.avgCpuUtilization": avgCpuUtil,
105+
},
106+
}
107+
out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/horizontalpodautoscaler.yaml"})
108+
109+
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
110+
rendered := map[string]interface{}{}
111+
err = yaml.Unmarshal([]byte(out), &rendered)
112+
assert.NoError(t, err)
113+
assert.NotEqual(t, 0, len(rendered))
114+
min, err := strconv.ParseFloat(minReplicas, 64)
115+
max, err := strconv.ParseFloat(maxReplicas, 64)
116+
avgCpu, err := strconv.ParseFloat(avgCpuUtil, 64)
117+
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
118+
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
119+
assert.Equal(t, avgCpu, rendered["spec"].(map[string]interface{})["metrics"].([]interface{})[0].(map[string]interface{})["resource"].(map[string]interface{})["target"].(map[string]interface{})["averageUtilization"])
120+
}
121+
122+
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
123+
// Autoscaler resource with the memory metric
124+
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithMemoryMetric(t *testing.T) {
125+
t.Parallel()
126+
minReplicas := "20"
127+
maxReplicas := "30"
128+
avgMemoryUtil := "65"
129+
130+
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
131+
require.NoError(t, err)
132+
133+
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
134+
// We then use SetValues to override all the defaults.
135+
options := &helm.Options{
136+
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
137+
SetValues: map[string]string{
138+
"horizontalPodAutoscaler.enabled": "true",
139+
"horizontalPodAutoscaler.minReplicas": minReplicas,
140+
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
141+
"horizontalPodAutoscaler.avgMemoryUtilization": avgMemoryUtil,
142+
},
143+
}
144+
out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/horizontalpodautoscaler.yaml"})
145+
146+
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
147+
rendered := map[string]interface{}{}
148+
err = yaml.Unmarshal([]byte(out), &rendered)
149+
assert.NoError(t, err)
150+
assert.NotEqual(t, 0, len(rendered))
151+
min, err := strconv.ParseFloat(minReplicas, 64)
152+
max, err := strconv.ParseFloat(maxReplicas, 64)
153+
avgMem, err := strconv.ParseFloat(avgMemoryUtil, 64)
154+
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
155+
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
156+
assert.Equal(t, avgMem, rendered["spec"].(map[string]interface{})["metrics"].([]interface{})[0].(map[string]interface{})["resource"].(map[string]interface{})["target"].(map[string]interface{})["averageUtilization"])
157+
}
158+
159+
// Test that setting horizontalPodAutoscaler.enabled = true will cause the helm template to render the Horizontal Pod
160+
// Autoscaler resource with the no metrics
161+
func TestK8SServiceHorizontalPodAutoscalerCreateTrueCreatesHorizontalPodAutoscalerWithNoMetrics(t *testing.T) {
162+
t.Parallel()
163+
minReplicas := "20"
164+
maxReplicas := "30"
165+
166+
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
167+
require.NoError(t, err)
168+
169+
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
170+
// We then use SetValues to override all the defaults.
171+
options := &helm.Options{
172+
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
173+
SetValues: map[string]string{
174+
"horizontalPodAutoscaler.enabled": "true",
175+
"horizontalPodAutoscaler.minReplicas": minReplicas,
176+
"horizontalPodAutoscaler.maxReplicas": maxReplicas,
177+
},
178+
}
179+
out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/horizontalpodautoscaler.yaml"})
180+
181+
// We take the output and render it to a map to validate it has created a Horizontal Pod Autoscaler output or not
182+
rendered := map[string]interface{}{}
183+
err = yaml.Unmarshal([]byte(out), &rendered)
184+
assert.NoError(t, err)
185+
assert.NotEqual(t, 0, len(rendered))
186+
min, err := strconv.ParseFloat(minReplicas, 64)
187+
max, err := strconv.ParseFloat(maxReplicas, 64)
188+
assert.Equal(t, min, rendered["spec"].(map[string]interface{})["minReplicas"])
189+
assert.Equal(t, max, rendered["spec"].(map[string]interface{})["maxReplicas"])
190+
}

0 commit comments

Comments
 (0)