diff --git a/library/common-test/Chart.yaml b/library/common-test/Chart.yaml index 4693fd66..981e9361 100644 --- a/library/common-test/Chart.yaml +++ b/library/common-test/Chart.yaml @@ -3,7 +3,7 @@ appVersion: "" dependencies: - name: common repository: file://../common - version: ~13.0.0 + version: ~13.1.0 deprecated: false description: Helper chart to test different use cases of the common library home: https://github.com/truecharts/apps/tree/master/charts/library/common-test diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index b502b1b4..3e72d33d 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 13.0.1 +version: 13.1.0 diff --git a/library/common/templates/class/_podDisruptionBudget.tpl b/library/common/templates/class/_podDisruptionBudget.tpl new file mode 100644 index 00000000..97596a1c --- /dev/null +++ b/library/common/templates/class/_podDisruptionBudget.tpl @@ -0,0 +1,54 @@ +{{/* poddisruptionbudget Class */}} +{{/* Call this template: +{{ include "tc.v1.common.class.podDisruptionBudget" (dict "rootCtx" $ "objectData" $objectData) }} + +rootCtx: The root context of the chart. +objectData: + name: The name of the podDisruptionBudget. + labels: The labels of the podDisruptionBudget. + annotations: The annotations of the podDisruptionBudget. + data: The data of the podDisruptionBudget. + namespace: The namespace of the podDisruptionBudget. (Optional) +*/}} + +{{- define "tc.v1.common.class.podDisruptionBudget" -}} + + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} +--- +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ $objectData.name }} + namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Pod Disruption Budget") }} + {{- $labels := (mustMerge ($objectData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} + labels: + {{- . | nindent 4 }} + {{- end -}} + {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "tc.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }} + annotations: + {{- . | nindent 4 }} + {{- end -}} +data: + selector: + matchLabels: + {{- if $objectData.customLabels -}} + {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $objectData.customLabels) | trim) }} + {{- . | nindent 6 }} + {{- end -}} + {{- else -}} + {{- $selectedPod := fromJson (include "tc.v1.common.lib.helpers.getSelectedPodValues" (dict "rootCtx" $rootCtx "objectData" $objectData)) }} + {{- include "tc.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $ "objectType" "pod" "objectName" $selectedPod.shortName) | nindent 6 }} + {{- end -}} + {{- if hasKey "minAvailable" $objectData }} + minAvailable: {{ $objectData.minAvailable }} + {{- end -}} + {{- if hasKey "maxUnavailable" $objectData }} + maxUnavailable: {{ $objectData.maxUnavailable }} + {{- end -}} + {{- with $objectData.unhealthyPodEvictionPolicy }} + unhealthyPodEvictionPolicy: {{ . }} + {{- end -}} +{{- end -}} diff --git a/library/common/templates/lib/podDistruptionBudget/_validation.tpl b/library/common/templates/lib/podDistruptionBudget/_validation.tpl new file mode 100644 index 00000000..1c7dc90c --- /dev/null +++ b/library/common/templates/lib/podDistruptionBudget/_validation.tpl @@ -0,0 +1,30 @@ +{{/* Metadata Validation */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.podDisruptionBudget.validation" (dict "objectData" $objectData "caller" $caller) -}} +objectData: + labels: The labels of the configmap. + annotations: The annotations of the configmap. + data: The data of the configmap. +*/}} + +{{- define "tc.v1.common.lib.podDisruptionBudget.validation" -}} + {{- $objectData := .objectData -}} + {{- $caller := .caller -}} + + {{- with $objectData.unhealthyPodEvictionPolicy -}} + {{- $policies := (list "IfHealthyBudget" "AlwaysAllow") -}} + {{- if not (mustHas . $policies) -}} + {{- fail (printf "Pod Disruption Budget - Expected to be one of [%s], but got [%s]" (join ", " $policies) .) -}} + {{- end -}} + {{- end -}} + + {{- $keys := (list "minAvailable" "maxUnavailable") -}} + {{- range $key := $keys -}} + {{- if hasKey $key $objectData -}} + {{- if kindIs "invalid" (get $objectData $key) -}} + {{- fail (printf "Pod Disruption Budget - Expected the defined key [%v] in to not be empty" $key $key) -}} + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}} diff --git a/library/common/templates/loader/_apply.tpl b/library/common/templates/loader/_apply.tpl index 14d3c364..bb353ac5 100644 --- a/library/common/templates/loader/_apply.tpl +++ b/library/common/templates/loader/_apply.tpl @@ -52,6 +52,9 @@ {{/* Render Networkpolicy(s) */}} {{- include "tc.v1.common.spawner.networkpolicy" . | nindent 0 -}} + {{/* Render podDisruptionBudget(s) */}} + {{- include "tc.v1.common.spawner.podDisruptionBudget" . | nindent 0 -}} + {{/* Render Prometheus Metrics(s) */}} {{- include "tc.v1.common.spawner.metrics" . | nindent 0 -}} diff --git a/library/common/templates/spawner/_podDisruptionBudget.tpl b/library/common/templates/spawner/_podDisruptionBudget.tpl new file mode 100644 index 00000000..7098d2da --- /dev/null +++ b/library/common/templates/spawner/_podDisruptionBudget.tpl @@ -0,0 +1,58 @@ +{{/* poddisruptionbudget Spawwner */}} +{{/* Call this template: +{{ include "tc.v1.common.spawner.podDisruptionBudget" $ -}} +*/}} + +{{- define "tc.v1.common.spawner.podDisruptionBudget" -}} + + {{- range $name, $poddisruptionbudget := .Values.podDisruptionBudget -}} + + {{- $enabled := false -}} + {{- if hasKey $poddisruptionbudget "enabled" -}} + {{- if not (kindIs "invalid" $poddisruptionbudget.enabled) -}} + {{- $enabled = $poddisruptionbudget.enabled -}} + {{- else -}} + {{- fail (printf "Pod Disruption Budget - Expected the defined key [enabled] in to not be empty" $name) -}} + {{- end -}} + {{- end -}} + + {{- if kindIs "string" $enabled -}} + {{- $enabled = tpl $enabled $ -}} + + {{/* After tpl it becomes a string, not a bool */}} + {{- if eq $enabled "true" -}} + {{- $enabled = true -}} + {{- else if eq $enabled "false" -}} + {{- $enabled = false -}} + {{- end -}} + {{- end -}} + + {{- if $enabled -}} + + {{/* Create a copy of the poddisruptionbudget */}} + {{- $objectData := (mustDeepCopy $poddisruptionbudget) -}} + + {{- $objectName := (printf "%s-%s" (include "tc.v1.common.lib.chart.names.fullname" $) $name) -}} + {{- if hasKey $objectData "expandObjectName" -}} + {{- if not $objectData.expandObjectName -}} + {{- $objectName = $name -}} + {{- end -}} + {{- end -}} + + {{/* Perform validations */}} + {{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName) -}} + {{- include "tc.v1.common.lib.podDisruptionBudget.validation" (dict "objectData" $objectData) -}} + {{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "Pod Distruption Budget") -}} + + {{/* Set the name of the poddisruptionbudget */}} + {{- $_ := set $objectData "name" $objectName -}} + {{- $_ := set $objectData "shortName" $name -}} + + {{/* Call class to create the object */}} + {{- include "tc.v1.common.class.podDisruptionBudget" (dict "rootCtx" $ "objectData" $objectData) -}} + + {{- end -}} + + {{- end -}} + +{{- end -}} diff --git a/library/common/values.yaml b/library/common/values.yaml index b9827397..38470a76 100644 --- a/library/common/values.yaml +++ b/library/common/values.yaml @@ -524,6 +524,16 @@ route: type: PathPrefix value: / +podDisruptionBudget: + main: + enabled: false + # -- Custom Selector Labels + # customLabels: + # customKey: customValue + targetSelector: main + minAvailable: 1 + maxUnavailable: 1 + metrics: main: enabled: false