From 7f042c84b9222ff92a4157bba0fc27da808b282e Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Sat, 15 Jul 2023 20:26:39 +0200 Subject: [PATCH] feat: implement poddisruptionbudget object creation (#490) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** poddisruptionbudget objects are rather easy to implement and offer another unique sellingpoint by being standardised. **โš™๏ธ Type of change** - [x] โš™๏ธ Feature/App addition - [ ] ๐Ÿช› Bugfix - [ ] โš ๏ธ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] ๐Ÿ”ƒ Refactor of current code **๐Ÿงช How Has This Been Tested?** **๐Ÿ“ƒ Notes:** **โœ”๏ธ Checklist:** - [ ] โš–๏ธ My code follows the style guidelines of this project - [ ] ๐Ÿ‘€ I have performed a self-review of my own code - [ ] #๏ธโƒฃ I have commented my code, particularly in hard-to-understand areas - [ ] ๐Ÿ“„ I have made corresponding changes to the documentation - [ ] โš ๏ธ My changes generate no new warnings - [ ] ๐Ÿงช I have added tests to this description that prove my fix is effective or that my feature works - [ ] โฌ†๏ธ I increased versions for any altered app according to semantic versioning **โž• App addition** If this PR is an app addition please make sure you have done the following. - [ ] ๐Ÿชž I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] ๐Ÿ–ผ๏ธ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --------- Co-authored-by: Stavros kois --- library/common-test/Chart.yaml | 2 +- library/common/Chart.yaml | 2 +- .../templates/class/_podDisruptionBudget.tpl | 54 +++++++++++++++++ .../lib/podDistruptionBudget/_validation.tpl | 30 ++++++++++ library/common/templates/loader/_apply.tpl | 3 + .../spawner/_podDisruptionBudget.tpl | 58 +++++++++++++++++++ library/common/values.yaml | 10 ++++ 7 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 library/common/templates/class/_podDisruptionBudget.tpl create mode 100644 library/common/templates/lib/podDistruptionBudget/_validation.tpl create mode 100644 library/common/templates/spawner/_podDisruptionBudget.tpl 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