Skip to content

Commit

Permalink
Address requirements gathered during PR discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
Hennadii Mykhailiuta committed Apr 30, 2024
1 parent 2168f13 commit 1e6ca30
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
1 change: 1 addition & 0 deletions charts/shared/common-res/Makefile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include ../../../Makefile
NAMESPACE=default
100 changes: 62 additions & 38 deletions charts/shared/common-res/templates/functions/podspec.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
{{/* vim: set filetype=mustache: */}}
{{/* Renders a value for initContainers, containers, and ephemeralContainers.
Input dict:
{
value: [map] - template
root: [map] - root context
}
*/}}
{{- define "common-res.containersSpec" -}}
{{- $root := .root -}}
{{- $value := .value -}}
{{/* Render list "as-is", while dictionary needs to be converted to list */}}
{{- if kindIs "slice" $value -}}
{{- include "common-gitops.tplvalues.render" (dict "value" $value "context" $root) | nindent 2 -}}
{{- else if kindIs "map" $value -}}
{{- $containers := $value -}}
{{- range $name := keys $containers | sortAlpha -}}
{{- $container := get $containers $name -}}
- name: {{ $name }}
{{- include "common-gitops.tplvalues.render" (dict "value" $container "context" $root) | nindent 2 -}}
{{- end -}}
{{- else -}}
{{- fail "value for initContainers is not list nor dictionary" -}}
{{- end -}}
{{- end -}}

{{/* vim: set filetype=mustache: */}}
{{/* Renders a value for podSpec.
Accepts dict:
{
root: [map] - root context
kind: [string] - resource kind name, e.g. "Deployment"
name: [string] - item id, e.g. "argocd"
}
*/}}
{{- define "common-res.podSpec" -}}
{{- $kindObj := (get .root.Values .kind) -}}
{{- $kind := .kind -}}
Expand All @@ -6,54 +41,43 @@
{{- $item := (get $kindObj.items .name) -}}
{{- with mergeOverwrite ((.root.Values.global).podSpec)
($kindObj.podSpec)
($item.podSpec) }}
($item.podSpec) -}}
metadata:
{{- with .name }}
{{- with .name -}}
name: {{ include "common-gitops.tplvalues.render" (dict "value" . "context" $root) }}
{{- end -}}
{{- end }}
{{- include "common-gitops.labels" (dict "root" $root "name" $name "kind" $kind) | trim | nindent 2 }}
{{- include "common-gitops.annotations" (dict "root" $root "name" $name "kind" $kind) | trim | nindent 2 }}
{{- with .spec }}
spec:
{{- include "common-gitops.tplvalues.render" (dict
"value" (omit . "initContainers" "containers")
"value" (omit .
"containers"
"ephemeralContainers"
"initContainers"
"serviceAccountName"
"dnsPolicy"
"terminationGracePeriodSeconds"
"image"
)
"context" $root
"trimEmpty" "true") | nindent 2 -}}
{{ with .initContainers }}
"trimEmpty" "true") | nindent 2 }}
serviceAccountName: {{ include "common-gitops.names.itemFullname" (dict "root" $root "name" $name "override" .serviceAccountName) }}
{{- with .initContainers }}
initContainers:
{{- if kindIs "slice" . -}}
{{/* Standard list-like syntax */}}
{{ include "common-gitops.tplvalues.render" (dict "value" . "context" $root) | nindent 4 -}}
{{- else if kindIs "map" . -}}
{{/* Dict-like syntax */}}
{{- $containers := . -}}
{{ range $name := keys $containers | sortAlpha -}}
{{ $container := get $containers $name }}
- name: {{ $name -}}
{{ include "common-gitops.tplvalues.render" (dict "value" $container "context" $root) | nindent 4 -}}
{{ end }}
{{- else }}
{{- fail "value for initContainers is not list nor dictionary" }}
{{ end }}
{{ end }}
{{ with .containers }}
{{- include "common-res.containersSpec" (dict "value" . "root" $root) | nindent 2 }}
{{- end }}
{{- with .ephemeralContainers }}
ephemeralContainers:
{{- include "common-res.containersSpec" (dict "value" . "root" $root) | nindent 2 }}
{{- end }}
{{- with .containers }}
containers:
{{- if kindIs "slice" . }}
{{/* Standard list-like syntax */}}
{{ include "common-gitops.tplvalues.render" (dict "value" . "context" $root) | nindent 4 -}}
{{- else if kindIs "map" . -}}
{{/* Dict-like syntax */}}
{{- $containers := . -}}
{{- $names := keys $containers | sortAlpha -}}
{{ range $name := $names -}}
{{ $container := get $containers $name }}
- name: {{ $name -}}
{{ include "common-gitops.tplvalues.render" (dict "value" $container "context" $root) | nindent 4 -}}
{{ end }}
{{- else }}
{{- fail "value for containers is not list nor dictionary" }}
{{ end }}
{{ end }}
{{- include "common-res.containersSpec" (dict "value" . "root" $root) | nindent 2 }}
{{- end }}
dnsPolicy: {{ include "common-gitops.tplvalues.render" (dict "value" .dnsPolicy "context" $root "trimEmpty" true) | default "ClusterFirst" }}
terminationGracePeriodSeconds: {{ include "common-gitops.tplvalues.render" (dict "value" .terminationGracePeriodSeconds "context" $root "trimEmpty" true) | default 30 }}
restartPolicy: {{ include "common-gitops.tplvalues.render" (dict "value" .restartPolicy "context" $root "trimEmpty" true) | default "Always" }}
{{- end -}}
{{- end -}}
{{- end -}}
16 changes: 10 additions & 6 deletions charts/shared/common-res/values-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ Deployment:
runAsGroup: 3000
fsGroup: 2000
initContainers:
test:
test-init:
command: ['/bin/echo', 'init']
image: busybox
containers:
test:
test-dict:
command: ['/bin/echo', 'run']
image: busybox
test-list:
podSpec:
spec:
Expand All @@ -109,11 +111,13 @@ Deployment:
runAsGroup: 3000
fsGroup: 2000
initContainers:
- name: test
- name: test-init
command: ['/bin/echo', 'init']
image: busybox
containers:
- name: test
- name: test-list
command: ['/bin/echo', 'run']
image: busybox
_:
replicas: 1
revisionHistoryLimit: 3
Expand Down Expand Up @@ -171,7 +175,7 @@ Deployment:
readinessProbe:
httpGet:
port: 3000
scheme: HTTPused
scheme: HTTP
path: "/"
initialDelaySeconds: 10
periodSeconds: 10
Expand Down Expand Up @@ -241,6 +245,7 @@ Job:
command: ["sh", "-c"]
args:
- sleep 500
restartPolicy: OnFailure

CronJob:
enabled: true
Expand Down Expand Up @@ -276,7 +281,6 @@ ConfigMap:
items:
_:
name: test
namespace: test
enabled: true
data:
config.yaml: |
Expand Down

0 comments on commit 1e6ca30

Please sign in to comment.