diff --git a/charts/library/common-test/tests/controller/field_revisionhistorylimit_test.yaml b/charts/library/common-test/tests/controller/field_revisionhistorylimit_test.yaml new file mode 100644 index 00000000..f1031f00 --- /dev/null +++ b/charts/library/common-test/tests/controller/field_revisionhistorylimit_test.yaml @@ -0,0 +1,130 @@ +--- +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: controller revisionHistoryLimit +templates: + - common.yaml +values: + - ../_values/controllers_main_default_container.yaml +tests: + # Deployment + - it: default should pass for Deployment + set: + controllers.main.type: deployment + asserts: + - documentIndex: 0 + isKind: + of: Deployment + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 3 + + - it: custom revisionHistoryLimit should pass for Deployment + set: + controllers.main: + type: deployment + revisionHistoryLimit: 8 + asserts: + - documentIndex: 0 + isKind: + of: Deployment + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 8 + + - it: explicit revisionHistoryLimit 0 should pass for Deployment + set: + controllers.main: + type: deployment + revisionHistoryLimit: 0 + asserts: + - documentIndex: 0 + isKind: + of: Deployment + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 0 + + # StatefulSet + - it: default should pass for StatefulSet + set: + controllers.main.type: statefulset + asserts: + - documentIndex: 0 + isKind: + of: StatefulSet + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 3 + + - it: custom revisionHistoryLimit should pass for StatefulSet + set: + controllers.main: + type: statefulset + revisionHistoryLimit: 8 + asserts: + - documentIndex: 0 + isKind: + of: StatefulSet + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 8 + + - it: explicit revisionHistoryLimit 0 should pass for StatefulSet + set: + controllers.main: + type: statefulset + revisionHistoryLimit: 0 + asserts: + - documentIndex: 0 + isKind: + of: StatefulSet + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 0 + + # DaemonSet + - it: default should pass for DaemonSet + set: + controllers.main.type: daemonset + asserts: + - documentIndex: 0 + isKind: + of: DaemonSet + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 3 + + - it: custom revisionHistoryLimit should pass for DaemonSet + set: + controllers.main: + type: daemonset + revisionHistoryLimit: 8 + asserts: + - documentIndex: 0 + isKind: + of: DaemonSet + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 8 + + - it: explicit revisionHistoryLimit 0 should pass for DaemonSet + set: + controllers.main: + type: daemonset + revisionHistoryLimit: 0 + asserts: + - documentIndex: 0 + isKind: + of: DaemonSet + - documentIndex: 0 + equal: + path: spec.revisionHistoryLimit + value: 0 diff --git a/charts/library/common-test/tests/serviceMonitor/servicemonitor_test.yaml b/charts/library/common-test/tests/serviceMonitor/servicemonitor_test.yaml index b0aa224d..9b7f26f7 100644 --- a/charts/library/common-test/tests/serviceMonitor/servicemonitor_test.yaml +++ b/charts/library/common-test/tests/serviceMonitor/servicemonitor_test.yaml @@ -52,6 +52,16 @@ tests: equal: path: metadata.name value: RELEASE-NAME + - documentIndex: *ServiceMonitorDocument + equal: + path: spec.namespaceSelector + value: + matchNames: + - NAMESPACE + - documentIndex: *ServiceMonitorDocument + equal: + path: spec.jobLabel + value: RELEASE-NAME - documentIndex: *ServiceMonitorDocument equal: path: spec.selector.matchLabels diff --git a/charts/library/common/Chart.yaml b/charts/library/common/Chart.yaml index 6647635f..87227157 100644 --- a/charts/library/common/Chart.yaml +++ b/charts/library/common/Chart.yaml @@ -3,7 +3,7 @@ apiVersion: v2 name: common description: Function library for Helm charts type: library -version: 3.0.4 +version: 3.1.0 kubeVersion: ">=1.22.0-0" keywords: - common @@ -16,4 +16,10 @@ annotations: artifacthub.io/changes: |- - kind: fixed description: |- - Improved fix for preventing potential incorrect controller references + Fixed `default` function overriding `0` values + - kind: fixed + description: |- + Fixed missing `volumeName` field in persistence schema + - kind: added + description: |- + Add jobLabel and namespaceSelector to serviceMonitor diff --git a/charts/library/common/README.md b/charts/library/common/README.md index 973435ed..c7322b5a 100644 --- a/charts/library/common/README.md +++ b/charts/library/common/README.md @@ -1,6 +1,6 @@ # common -![Version: 3.0.4](https://img.shields.io/badge/Version-3.0.4-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) +![Version: 3.1.0](https://img.shields.io/badge/Version-3.1.0-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) Function library for Helm charts @@ -27,7 +27,7 @@ Include this chart as a dependency in your `Chart.yaml` e.g. # Chart.yaml dependencies: - name: common - version: 3.0.4 + version: 3.1.0 repository: https://bjw-s.github.io/helm-charts/ ``` @@ -50,4 +50,4 @@ These values will be validated by a JSON schema which can be found [here](https: - Join the home-operations [Discord](https://discord.gg/home-operations) community ---------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.12.0](https://github.com/norwoodj/helm-docs/releases/v1.12.0) +Autogenerated from chart metadata using [helm-docs v1.13.1](https://github.com/norwoodj/helm-docs/releases/v1.13.1) diff --git a/charts/library/common/schemas/persistence.json b/charts/library/common/schemas/persistence.json index 240c8e89..50f2f919 100644 --- a/charts/library/common/schemas/persistence.json +++ b/charts/library/common/schemas/persistence.json @@ -51,6 +51,9 @@ "storageClass": { "type": "string" }, + "volumeName": { + "type": "string" + }, "advancedMounts": {"$ref": "#/advancedMounts"}, "globalMounts": {"$ref": "#/globalMounts"} }, diff --git a/charts/library/common/templates/classes/_cronjob.tpl b/charts/library/common/templates/classes/_cronjob.tpl index 91d0d7aa..110838c8 100644 --- a/charts/library/common/templates/classes/_cronjob.tpl +++ b/charts/library/common/templates/classes/_cronjob.tpl @@ -36,13 +36,13 @@ metadata: spec: suspend: {{ default false $cronJobSettings.suspend }} concurrencyPolicy: {{ default "Forbid" $cronJobSettings.concurrencyPolicy }} - startingDeadlineSeconds: {{ default 30 $cronJobSettings.startingDeadlineSeconds }} + startingDeadlineSeconds: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $cronJobSettings.startingDeadlineSeconds "default" 30) }} {{- with $timeZone }} timeZone: {{ . }} {{- end }} schedule: {{ $cronJobSettings.schedule | quote }} - successfulJobsHistoryLimit: {{ default 1 $cronJobSettings.successfulJobsHistory }} - failedJobsHistoryLimit: {{ default 1 $cronJobSettings.failedJobsHistory }} + successfulJobsHistoryLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $cronJobSettings.successfulJobsHistory "default" 1) }} + failedJobsHistoryLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $cronJobSettings.failedJobsHistory "default" 1) }} jobTemplate: spec: {{- with $cronJobSettings.ttlSecondsAfterFinished }} @@ -51,7 +51,7 @@ spec: {{- with $cronJobSettings.parallelism }} parallelism: {{ . }} {{- end }} - backoffLimit: {{ default 6 $cronJobSettings.backoffLimit }} + backoffLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $cronJobSettings.backoffLimit "default" 6) }} template: metadata: {{- with (include "bjw-s.common.lib.pod.metadata.annotations" (dict "rootContext" $rootContext "controllerObject" $cronjobObject)) }} diff --git a/charts/library/common/templates/classes/_daemonset.tpl b/charts/library/common/templates/classes/_daemonset.tpl index 0ce6edd2..a53823a7 100644 --- a/charts/library/common/templates/classes/_daemonset.tpl +++ b/charts/library/common/templates/classes/_daemonset.tpl @@ -27,7 +27,7 @@ metadata: annotations: {{- toYaml . | nindent 4 -}} {{- end }} spec: - revisionHistoryLimit: {{ default 3 $daemonsetObject.revisionHistoryLimit }} + revisionHistoryLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $daemonsetObject.revisionHistoryLimit "default" 3) }} selector: matchLabels: app.kubernetes.io/component: {{ $daemonsetObject.identifier }} diff --git a/charts/library/common/templates/classes/_deployment.tpl b/charts/library/common/templates/classes/_deployment.tpl index b39f9dff..cd8109ea 100644 --- a/charts/library/common/templates/classes/_deployment.tpl +++ b/charts/library/common/templates/classes/_deployment.tpl @@ -27,7 +27,7 @@ metadata: annotations: {{- toYaml . | nindent 4 -}} {{- end }} spec: - revisionHistoryLimit: {{ default 3 $deploymentObject.revisionHistoryLimit }} + revisionHistoryLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $deploymentObject.revisionHistoryLimit "default" 3) }} {{- if hasKey $deploymentObject "replicas" }} {{- if not (eq $deploymentObject.replicas nil) }} replicas: {{ $deploymentObject.replicas }} diff --git a/charts/library/common/templates/classes/_job.tpl b/charts/library/common/templates/classes/_job.tpl index 0ba51539..99cc1d2d 100644 --- a/charts/library/common/templates/classes/_job.tpl +++ b/charts/library/common/templates/classes/_job.tpl @@ -42,7 +42,7 @@ spec: {{- with $jobSettings.completionMode }} completionMode: {{ . }} {{- end }} - backoffLimit: {{ default 6 $jobSettings.backoffLimit }} + backoffLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $jobSettings.backoffLimit "default" 6) }} template: metadata: {{- with (include "bjw-s.common.lib.pod.metadata.annotations" (dict "rootContext" $rootContext "controllerObject" $jobObject)) }} diff --git a/charts/library/common/templates/classes/_route.tpl b/charts/library/common/templates/classes/_route.tpl index 123bc5fe..9c07fa7c 100644 --- a/charts/library/common/templates/classes/_route.tpl +++ b/charts/library/common/templates/classes/_route.tpl @@ -64,7 +64,7 @@ spec: name: {{ default .name $service.name }} namespace: {{ default $rootContext.Release.Namespace .namespace }} port: {{ default .port $servicePrimaryPort.port }} - weight: {{ default 1 .weight }} + weight: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" .weight "default" 1) }} {{- end }} {{- if or (eq $routeKind "HTTPRoute") (eq $routeKind "GRPCRoute") }} {{- with .matches }} diff --git a/charts/library/common/templates/classes/_serviceMonitor.tpl b/charts/library/common/templates/classes/_serviceMonitor.tpl index b1418608..dc3b9e85 100644 --- a/charts/library/common/templates/classes/_serviceMonitor.tpl +++ b/charts/library/common/templates/classes/_serviceMonitor.tpl @@ -21,6 +21,10 @@ metadata: annotations: {{- toYaml . | nindent 4 -}} {{- end }} spec: + jobLabel: "{{ $serviceMonitorObject.name }}" + namespaceSelector: + matchNames: + - {{ $rootContext.Release.Namespace }} selector: {{- if $serviceMonitorObject.selector -}} {{- tpl ($serviceMonitorObject.selector | toYaml) $rootContext | nindent 4}} diff --git a/charts/library/common/templates/classes/_statefulset.tpl b/charts/library/common/templates/classes/_statefulset.tpl index c244aca5..0b7cdcc3 100644 --- a/charts/library/common/templates/classes/_statefulset.tpl +++ b/charts/library/common/templates/classes/_statefulset.tpl @@ -27,7 +27,7 @@ metadata: annotations: {{- toYaml . | nindent 4 -}} {{- end }} spec: - revisionHistoryLimit: {{ default 3 $statefulsetObject.revisionHistoryLimit }} + revisionHistoryLimit: {{ include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $statefulsetObject.revisionHistoryLimit "default" 3) }} replicas: {{ $statefulsetObject.replicas }} podManagementPolicy: {{ dig "statefulset" "podManagementPolicy" "OrderedReady" $statefulsetObject }} updateStrategy: diff --git a/charts/library/common/templates/lib/chart/_defaultKeepNonNullValue.tpl b/charts/library/common/templates/lib/chart/_defaultKeepNonNullValue.tpl new file mode 100644 index 00000000..1dc5ba18 --- /dev/null +++ b/charts/library/common/templates/lib/chart/_defaultKeepNonNullValue.tpl @@ -0,0 +1,13 @@ +{{/* +Return the value or the specified default if the given value is null. +*/}} +{{- define "bjw-s.common.lib.defaultKeepNonNullValue" -}} + {{- $value := .value -}} + {{- $default := required "default value is required" .default -}} + + {{- if eq nil $value -}} + {{- $default | toYaml -}} + {{- else -}} + {{- $value | toYaml -}} + {{- end -}} +{{- end }} diff --git a/charts/library/common/templates/lib/container/fields/_probes.tpl b/charts/library/common/templates/lib/container/fields/_probes.tpl index 02034da4..9fe4023b 100644 --- a/charts/library/common/templates/lib/container/fields/_probes.tpl +++ b/charts/library/common/templates/lib/container/fields/_probes.tpl @@ -39,10 +39,10 @@ Probes used by the container. {{- $probeType = $probeValues.type | default "TCP" -}} {{- end -}} - {{- $_ := set $probeDefinition "initialDelaySeconds" (default 0 $probeSpec.initialDelaySeconds) -}} - {{- $_ := set $probeDefinition "failureThreshold" (default 3 $probeSpec.failureThreshold) -}} - {{- $_ := set $probeDefinition "timeoutSeconds" (default 1 $probeSpec.timeoutSeconds) -}} - {{- $_ := set $probeDefinition "periodSeconds" (default 10 $probeSpec.periodSeconds) -}} + {{- $_ := set $probeDefinition "initialDelaySeconds" (include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $probeSpec.initialDelaySeconds "default" 0) | int) -}} + {{- $_ := set $probeDefinition "failureThreshold" (include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $probeSpec.failureThreshold "default" 3) | int) -}} + {{- $_ := set $probeDefinition "timeoutSeconds" (include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $probeSpec.timeoutSeconds "default" 1) | int) -}} + {{- $_ := set $probeDefinition "periodSeconds" (include "bjw-s.common.lib.defaultKeepNonNullValue" (dict "value" $probeSpec.periodSeconds "default" 10) | int) -}} {{- $probeHeader := "" -}} {{- if or ( eq $probeType "HTTPS" ) ( eq $probeType "HTTP" ) -}} diff --git a/charts/library/common/values.schema.json b/charts/library/common/values.schema.json index 9357760c..e9d7fadb 100644 --- a/charts/library/common/values.schema.json +++ b/charts/library/common/values.schema.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema", - "$id": "https://raw.githubusercontent.com/bjw-s/helm-charts/common-3.0.4/charts/library/common/values.schema.json", + "$id": "https://raw.githubusercontent.com/bjw-s/helm-charts/common-3.1.0/charts/library/common/values.schema.json", "type": "object", "properties": {