From 204a8fbbd738b79fa30249376bf69b25b1e67547 Mon Sep 17 00:00:00 2001 From: Soule BA Date: Wed, 2 Feb 2022 13:34:54 +0100 Subject: [PATCH] Add test case on hpa metrics for flux build/diff If implemented will detect any change on how ssa dry-run return diffs on hpa metrics Signed-off-by: Soule BA --- cmd/flux/diff_kustomization_test.go | 6 +++ .../delete-service/hpa.yaml | 24 +++++++++ .../build-kustomization/podinfo-result.yaml | 24 +++++++++ .../podinfo-without-service-result.yaml | 24 +++++++++ .../build-kustomization/podinfo/hpa.yaml | 24 +++++++++ .../diff-with-drifted-hpa.golden | 52 +++++++++++++++++++ cmd/flux/testdata/diff-kustomization/hpa.yaml | 48 +++++++++++++++++ internal/build/build.go | 2 +- 8 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 cmd/flux/testdata/diff-kustomization/diff-with-drifted-hpa.golden create mode 100644 cmd/flux/testdata/diff-kustomization/hpa.yaml diff --git a/cmd/flux/diff_kustomization_test.go b/cmd/flux/diff_kustomization_test.go index e2f62e75..285d887c 100644 --- a/cmd/flux/diff_kustomization_test.go +++ b/cmd/flux/diff_kustomization_test.go @@ -79,6 +79,12 @@ func TestDiffKustomization(t *testing.T) { objectFile: "./testdata/diff-kustomization/value-sops-secret.yaml", assert: assertGoldenFile("./testdata/diff-kustomization/diff-with-drifted-value-sops-secret.golden"), }, + { + name: "diff with a drifted hpa object", + args: "diff kustomization podinfo --path ./testdata/build-kustomization/podinfo", + objectFile: "./testdata/diff-kustomization/hpa.yaml", + assert: assertGoldenFile("./testdata/diff-kustomization/diff-with-drifted-hpa.golden"), + }, } tmpl := map[string]string{ diff --git a/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml b/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml index 6bd46a2c..3b1a21ee 100644 --- a/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml +++ b/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml @@ -3,6 +3,23 @@ kind: HorizontalPodAutoscaler metadata: name: podinfo spec: + behavior: + scaleDown: + policies: + - periodSeconds: 15 + type: Percent + value: 99 + selectPolicy: Max + stabilizationWindowSeconds: 60 + scaleUp: + policies: + - periodSeconds: 15 + type: Pods + value: 5 + - periodSeconds: 15 + type: Percent + value: 100 + selectPolicy: Max scaleTargetRef: apiVersion: apps/v1 kind: Deployment @@ -18,3 +35,10 @@ spec: # scale up if usage is above # 99% of the requested CPU (100m) averageUtilization: 99 + - type: Pods + pods: + metric: + name: podinfo_http_requests_total + target: + averageValue: "2" + type: AverageValue diff --git a/cmd/flux/testdata/build-kustomization/podinfo-result.yaml b/cmd/flux/testdata/build-kustomization/podinfo-result.yaml index ce66ff0f..37946f27 100644 --- a/cmd/flux/testdata/build-kustomization/podinfo-result.yaml +++ b/cmd/flux/testdata/build-kustomization/podinfo-result.yaml @@ -86,6 +86,23 @@ metadata: name: podinfo namespace: default spec: + behavior: + scaleDown: + policies: + - periodSeconds: 15 + type: Percent + value: 99 + selectPolicy: Max + stabilizationWindowSeconds: 60 + scaleUp: + policies: + - periodSeconds: 15 + type: Pods + value: 5 + - periodSeconds: 15 + type: Percent + value: 100 + selectPolicy: Max maxReplicas: 4 metrics: - resource: @@ -94,6 +111,13 @@ spec: averageUtilization: 99 type: Utilization type: Resource + - pods: + metric: + name: podinfo_http_requests_total + target: + averageValue: "2" + type: AverageValue + type: Pods minReplicas: 2 scaleTargetRef: apiVersion: apps/v1 diff --git a/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml b/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml index 943381c8..9dc451ff 100644 --- a/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml +++ b/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml @@ -86,6 +86,23 @@ metadata: name: podinfo namespace: default spec: + behavior: + scaleDown: + policies: + - periodSeconds: 15 + type: Percent + value: 99 + selectPolicy: Max + stabilizationWindowSeconds: 60 + scaleUp: + policies: + - periodSeconds: 15 + type: Pods + value: 5 + - periodSeconds: 15 + type: Percent + value: 100 + selectPolicy: Max maxReplicas: 4 metrics: - resource: @@ -94,6 +111,13 @@ spec: averageUtilization: 99 type: Utilization type: Resource + - pods: + metric: + name: podinfo_http_requests_total + target: + averageValue: "2" + type: AverageValue + type: Pods minReplicas: 2 scaleTargetRef: apiVersion: apps/v1 diff --git a/cmd/flux/testdata/build-kustomization/podinfo/hpa.yaml b/cmd/flux/testdata/build-kustomization/podinfo/hpa.yaml index 6bd46a2c..3b1a21ee 100644 --- a/cmd/flux/testdata/build-kustomization/podinfo/hpa.yaml +++ b/cmd/flux/testdata/build-kustomization/podinfo/hpa.yaml @@ -3,6 +3,23 @@ kind: HorizontalPodAutoscaler metadata: name: podinfo spec: + behavior: + scaleDown: + policies: + - periodSeconds: 15 + type: Percent + value: 99 + selectPolicy: Max + stabilizationWindowSeconds: 60 + scaleUp: + policies: + - periodSeconds: 15 + type: Pods + value: 5 + - periodSeconds: 15 + type: Percent + value: 100 + selectPolicy: Max scaleTargetRef: apiVersion: apps/v1 kind: Deployment @@ -18,3 +35,10 @@ spec: # scale up if usage is above # 99% of the requested CPU (100m) averageUtilization: 99 + - type: Pods + pods: + metric: + name: podinfo_http_requests_total + target: + averageValue: "2" + type: AverageValue diff --git a/cmd/flux/testdata/diff-kustomization/diff-with-drifted-hpa.golden b/cmd/flux/testdata/diff-kustomization/diff-with-drifted-hpa.golden new file mode 100644 index 00000000..e0711a0c --- /dev/null +++ b/cmd/flux/testdata/diff-kustomization/diff-with-drifted-hpa.golden @@ -0,0 +1,52 @@ +► Deployment/default/podinfo created +► HorizontalPodAutoscaler/default/podinfo drifted + +spec.behavior.scaleDown.policies.0.periodSeconds + ± value change + - 16 + + 15 + +spec.behavior.scaleUp.policies + - two list entries removed: + two list entries added: + - type: Pods - type: Pods + periodSeconds: 16 periodSeconds: 15 + value: 5 value: 5 + - type: Percent - type: Percent + periodSeconds: 16 periodSeconds: 15 + value: 100 value: 100 + +spec.metrics + - two list entries removed: + - type: Pods + pods: + metric: + name: podinfo_http_requests_total + target: + type: AverageValue + averageValue: 3 + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 98 + + + two list entries added: + - type: Pods + pods: + metric: + name: podinfo_http_requests_total + target: + type: AverageValue + averageValue: 2 + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 99 + + +► Service/default/podinfo created +► Secret/default/podinfo-token-77t89m9b67 created +► Secret/default/db-user-pass-bkbd782d2c created diff --git a/cmd/flux/testdata/diff-kustomization/hpa.yaml b/cmd/flux/testdata/diff-kustomization/hpa.yaml new file mode 100644 index 00000000..52237a8e --- /dev/null +++ b/cmd/flux/testdata/diff-kustomization/hpa.yaml @@ -0,0 +1,48 @@ +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + labels: + kustomize.toolkit.fluxcd.io/name: podinfo + kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }} + name: podinfo + namespace: default +spec: + behavior: + scaleDown: + policies: + - periodSeconds: 16 + type: Percent + value: 99 + selectPolicy: Max + stabilizationWindowSeconds: 60 + scaleUp: + policies: + - periodSeconds: 16 + type: Pods + value: 5 + - periodSeconds: 16 + type: Percent + value: 100 + selectPolicy: Max + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: podinfo + minReplicas: 2 + maxReplicas: 4 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + # scale up if usage is above + # 99% of the requested CPU (100m) + averageUtilization: 98 + - type: Pods + pods: + metric: + name: podinfo_http_requests_total + target: + averageValue: "3" + type: AverageValue diff --git a/internal/build/build.go b/internal/build/build.go index 416c2b0b..22dc0113 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -266,7 +266,7 @@ func trimSopsData(res *resource.Resource) error { data, err := base64.StdEncoding.DecodeString(v) if err != nil { if _, ok := err.(base64.CorruptInputError); ok { - return fmt.Errorf("failed to decode secret data: %w", err) + return fmt.Errorf("failed to decode secret %s data: %w", res.GetName(), err) } }