Эх сурвалжийг харах

common: scale down workloads based on middleware provided metadata

Stavros kois 1 жил өмнө
parent
commit
f004903659

+ 69 - 0
library/common-test/tests/cronjob/spec_test.yaml

@@ -28,6 +28,7 @@ tests:
             concurrencyPolicy: Forbid
             failedJobsHistoryLimit: 1
             successfulJobsHistoryLimit: 3
+            suspend: false
             startingDeadlineSeconds:
       - documentIndex: *cronJobDoc
         isSubset:
@@ -83,3 +84,71 @@ tests:
             parallelism: 5
             ttlSecondsAfterFinished: 100
             activeDeadlineSeconds: 100
+
+  - it: should set suspend to true
+    set:
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: CronJob
+          schedule: "*/5 * * * *"
+          suspend: true
+          podSpec:
+            restartPolicy: Never
+    asserts:
+      - documentIndex: *cronJobDoc
+        isSubset:
+          path: spec
+          content:
+            schedule: "*/5 * * * *"
+            timeZone: UTC
+            suspend: true
+
+  - it: should set suspend to true on ixChartContext - isStopped (true)
+    set:
+      global:
+        ixChartContext:
+          storageClassName: some-storage-class
+          isStopped: true
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: CronJob
+          schedule: "*/5 * * * *"
+          suspend: false
+          podSpec:
+            restartPolicy: Never
+    asserts:
+      - documentIndex: *cronJobDoc
+        isSubset:
+          path: spec
+          content:
+            schedule: "*/5 * * * *"
+            timeZone: UTC
+            suspend: true
+
+  - it: should not set suspend to true on ixChartContext - isStopped (false)
+    set:
+      global:
+        ixChartContext:
+          storageClassName: some-storage-class
+          isStopped: false
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: CronJob
+          schedule: "*/5 * * * *"
+          suspend: false
+          podSpec:
+            restartPolicy: Never
+    asserts:
+      - documentIndex: *cronJobDoc
+        isSubset:
+          path: spec
+          content:
+            schedule: "*/5 * * * *"
+            timeZone: UTC
+            suspend: false

+ 40 - 0
library/common-test/tests/deployment/spec_test.yaml

@@ -94,3 +94,43 @@ tests:
               type: RollingUpdate
               rollingUpdate:
                 maxUnavailable: 0
+
+  - it: should set replicas to 0 on ixChartContext - isStopped (true)
+    set:
+      global:
+        ixChartContext:
+          storageClassName: some-storage-class
+          isStopped: true
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: Deployment
+          replicas: 2
+          podSpec: {}
+    asserts:
+      - documentIndex: *deploymentDoc
+        isSubset:
+          path: spec
+          content:
+            replicas: 0
+
+  - it: should not set replicas to 0 on ixChartContext - isStopped (false)
+    set:
+      global:
+        ixChartContext:
+          storageClassName: some-storage-class
+          isStopped: false
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: Deployment
+          replicas: 2
+          podSpec: {}
+    asserts:
+      - documentIndex: *deploymentDoc
+        isSubset:
+          path: spec
+          content:
+            replicas: 2

+ 42 - 0
library/common-test/tests/job/spec_test.yaml

@@ -54,3 +54,45 @@ tests:
             parallelism: 5
             ttlSecondsAfterFinished: 100
             activeDeadlineSeconds: 100
+
+  - it: should set parallelism to 0 on ixChartContext - isStopped (true)
+    set:
+      global:
+        ixChartContext:
+          storageClassName: some-storage-class
+          isStopped: true
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: Job
+          parallelism: 3
+          podSpec:
+            restartPolicy: Never
+    asserts:
+      - documentIndex: *jobDoc
+        isSubset:
+          path: spec
+          content:
+            parallelism: 0
+
+  - it: should not set parallelism to 0 on ixChartContext - isStopped (false)
+    set:
+      global:
+        ixChartContext:
+          storageClassName: some-storage-class
+          isStopped: false
+      workload:
+        workload-name:
+          enabled: true
+          primary: true
+          type: Job
+          parallelism: 3
+          podSpec:
+            restartPolicy: Never
+    asserts:
+      - documentIndex: *jobDoc
+        isSubset:
+          path: spec
+          content:
+            parallelism: 3

+ 1 - 1
library/common/Chart.yaml

@@ -2,7 +2,7 @@ apiVersion: v2
 name: common
 description: A library chart for iX Official Catalog
 type: library
-version: 1.2.0
+version: 1.2.1
 appVersion: v1
 annotations:
   title: Common Library Chart

+ 12 - 0
library/common/templates/helpers/_isStopped.tpl

@@ -0,0 +1,12 @@
+{{- define "ix.v1.common.helper.isStopped" -}}
+  {{- $rootCtx := . -}}
+
+  {{- $stop := "" -}}
+  {{- with $rootCtx.Values.global.ixChartContext -}}
+    {{- if .isStopped -}}
+      {{- $stop = true -}}
+    {{- end -}}
+  {{- end -}}
+
+  {{- $stop -}}
+{{- end -}}

+ 5 - 0
library/common/templates/lib/workload/_cronjobSpec.tpl

@@ -14,12 +14,17 @@ objectData:
 {{- define "ix.v1.common.lib.workload.cronjobSpec" -}}
   {{- $objectData := .objectData -}}
   {{- $rootCtx := .rootCtx -}}
+  {{- $suspend := $objectData.suspend | default false -}}
+  {{- if (include "ix.v1.common.helper.isStopped" $rootCtx) -}}
+    {{- $suspend = true -}}
+  {{- end }}
 timeZone: {{ (tpl ($objectData.timezone | default $rootCtx.Values.TZ) $rootCtx) | quote }}
 schedule: {{ (tpl $objectData.schedule $rootCtx) | quote }}
 concurrencyPolicy: {{ $objectData.concurrencyPolicy | default "Forbid" }}
 failedJobsHistoryLimit: {{ $objectData.failedJobsHistoryLimit | default 1 }}
 successfulJobsHistoryLimit: {{ $objectData.successfulJobsHistoryLimit | default 3 }}
 startingDeadlineSeconds: {{ $objectData.startingDeadlineSeconds | default nil }}
+suspend: {{ $suspend }}
 jobTemplate:
   spec:
     {{- include "ix.v1.common.lib.workload.jobSpec" (dict "rootCtx" $rootCtx "objectData" $objectData) | nindent 4 }}

+ 8 - 1
library/common/templates/lib/workload/_deployementSpec.tpl

@@ -11,7 +11,14 @@ objectData:
   {{- $objectData := .objectData -}}
   {{- $rootCtx := .rootCtx -}}
   {{- $strategy := $objectData.strategy | default "Recreate" }}
-replicas: {{ $objectData.replicas | default 1 }}
+  {{- $replicas := 1 -}}
+  {{- if hasKey $objectData "replicas" -}}
+    {{- $replicas = $objectData.replicas -}}
+  {{- end -}}
+  {{- if (include "ix.v1.common.helper.isStopped" $rootCtx) -}}
+    {{- $replicas = 0 -}}
+  {{- end }}
+replicas: {{ $replicas }}
 revisionHistoryLimit: {{ $objectData.revisionHistoryLimit | default 3 }}
 strategy:
   type: {{ $strategy }}

+ 8 - 1
library/common/templates/lib/workload/_jobSpec.tpl

@@ -13,10 +13,17 @@ objectData:
 {{- define "ix.v1.common.lib.workload.jobSpec" -}}
   {{- $objectData := .objectData -}}
   {{- $rootCtx := .rootCtx -}}
+  {{- $parallelism := 1 -}}
+  {{- if hasKey $objectData "parallelism" -}}
+    {{- $parallelism = $objectData.parallelism -}}
+  {{- end -}}
+  {{- if (include "ix.v1.common.helper.isStopped" $rootCtx) -}}
+    {{- $parallelism = 0 -}}
+  {{- end }}
 backoffLimit: {{ $objectData.backoffLimit | default 5 }}
 completionMode: {{ $objectData.completionMode | default "NonIndexed" }}
 completions: {{ $objectData.completions | default nil }}
-parallelism: {{ $objectData.parallelism | default 1 }}
+parallelism: {{ $parallelism }}
 ttlSecondsAfterFinished: {{ $objectData.ttlSecondsAfterFinished | default 120 }}
   {{- with $objectData.activeDeadlineSeconds }}
 activeDeadlineSeconds: {{ . }}