Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoscaling/V2 for HorizontalPodAutoscaler, servicesGeneral support and serviceAccount name fix #79

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
212 changes: 117 additions & 95 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/samples/whoami/app-deployment.values.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
generic:
podSecurityContext:
runAsNonRoot: true

services:
app-web:
type: ClusterIP
Expand All @@ -10,6 +14,8 @@ services:
deployments:
app:
securityContext:
# mergeWithGeneric will merge generic.podSecurityContext.runAsNonRoot into here (and mergeWithGeneric itself will be removed).
mergeWithGeneric: true
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
Expand Down
48 changes: 41 additions & 7 deletions templates/helpers/_app.tpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
{{- define "helpers.app.name" -}}
{{- if and .Values.parentChart .Values.parentChart.name -}}
{{- .Values.parentChart.name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- include "helpers.app.release.name" .context -}}
{{- end -}}
{{- end -}}

{{/*
Chart release name
*/}}
{{- define "helpers.app.release.name" -}}
{{- default .Release.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "helpers.app.chart" -}}
{{- if and .Values.parentChart .Values.parentChart.name .Values.parentChart.version -}}
{{- printf "%s-%s" .Values.parentChart.name .Values.parentChart.version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}

{{/*
Create chart version as used by the chart label.
*/}}
{{- define "helpers.app.chart.version" -}}
{{- if .Chart.AppVersion }}
{{ .Chart.AppVersion | quote }}
{{- else -}}
{{- if and .Values.parentChart .Values.parentChart.version -}}
{{ .Values.parentChart.version | quote }}
{{- else -}}
{{ .Chart.version | quote }}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
Expand All @@ -19,31 +49,35 @@ If release name contains chart name it will be used as a full name.
{{- if .context.Values.releasePrefix -}}
{{- printf "%s-%s" .context.Values.releasePrefix .name | trunc 63 | trimAll "-" -}}
{{- else -}}
{{- printf "%s-%s" (include "helpers.app.name" .context) .name | trunc 63 | trimSuffix "-" -}}
{{- printf "%s-%s" (include "helpers.app.release.name" .context) .name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- else -}}
{{- include "helpers.app.name" .context -}}
{{- end -}}
{{- end -}}


{{- define "helpers.app.labels" -}}
{{ include "helpers.app.selectorLabels" . }}
helm.sh/chart: {{ include "helpers.app.chart" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
{{- with .Values.generic.labels }}
{{ include "helpers.tplvalues.render" (dict "value" . "context" $) }}
{{- end }}
app.kubernetes.io/version: {{ include "helpers.app.chart.version" . }}
{{ include "helpers.app.genericLabels" $ }}
{{- end }}


{{- define "helpers.app.selectorLabels" -}}
app.kubernetes.io/name: {{ include "helpers.app.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{ include "helpers.app.genericSelectorLabels" $ }}
{{- end }}

{{- define "helpers.app.genericLabels" -}}
{{- with .Values.generic.labels }}
{{ include "helpers.tplvalues.render" (dict "value" . "context" .) }}
{{- end }}
{{- end }}

{{- define "helpers.app.genericSelectorLabels" -}}
{{- with .Values.generic.extraSelectorLabels }}
{{ include "helpers.tplvalues.render" (dict "value" . "context" .) }}
Expand Down
69 changes: 57 additions & 12 deletions templates/helpers/_pod.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
{{- $name := .name -}}
{{- with .value -}}
{{- if .serviceAccountName }}
serviceAccountName: {{- include "helpers.tplvalues.render" (dict "value" .serviceAccountName "context" $) | nindent 2 }}
serviceAccountName: {{- include "helpers.app.fullname" (dict "name" .serviceAccountName "context" $) | nindent 2 }}
{{- else if $.Values.generic.serviceAccountName }}
serviceAccountName: {{- include "helpers.tplvalues.render" (dict "value" $.Values.generic.serviceAccountName "context" $) | nindent 2 }}
serviceAccountName: {{- include "helpers.app.fullname" (dict "name" $.Values.generic.serviceAccountName "context" $) | nindent 2 }}
{{- end }}
{{- if .hostAliases }}
hostAliases: {{- include "helpers.tplvalues.render" (dict "value" .hostAliases "context" $) | nindent 2 }}
Expand Down Expand Up @@ -55,9 +55,24 @@ tolerations:
{{- toYaml $combined | nindent 2 }}
{{- end }}

{{- if and .securityContext .securityContext.mergeWithGeneric $.Values.generic.podSecurityContext }}
{{- $podSecurityContext := merge (omit .securityContext "mergeWithGeneric") (omit $.Values.generic.podSecurityContext "mergeWithGeneric") -}}
{{- with $podSecurityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 2 }}
{{- end }}
{{- else }}
{{- if .securityContext }}
{{- with .securityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 2 }}
{{- end }}
{{- else if $.Values.generic.podSecurityContext }}
{{- with $.Values.generic.podSecurityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 2 }}
{{- end }}
{{- end }}
{{- end -}}


{{ if or $.Values.imagePullSecrets $.Values.generic.extraImagePullSecrets .extraImagePullSecrets .imagePullSecrets }}
imagePullSecrets:
{{- range $sName, $v := $.Values.imagePullSecrets }}
Expand All @@ -82,9 +97,22 @@ initContainers:
{{- $imageTag := $.Values.defaultImageTag }}{{ with .imageTag }}{{ $imageTag = include "helpers.tplvalues.render" ( dict "value" . "context" $) }}{{ end }}
image: {{ $image }}:{{ $imageTag }}
imagePullPolicy: {{ .imagePullPolicy | default $.Values.defaultImagePullPolicy }}
{{- if and .securityContext .securityContext.mergeWithGeneric $.Values.generic.containerSecurityContext }}
{{- $containerSecurityContext := merge (omit .securityContext "mergeWithGeneric") (omit $.Values.generic.containerSecurityContext "mergeWithGeneric") -}}
{{- with $containerSecurityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
{{- else }}
{{- if .securityContext }}
{{- with .securityContext }}
securityContext: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
{{- else if $.Values.generic.containerSecurityContext }}
{{- with $.Values.generic.containerSecurityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
{{- end }}
{{- end -}}
{{- if $.Values.diagnosticMode.enabled }}
args: {{- include "helpers.tplvalues.render" ( dict "value" $.Values.diagnosticMode.args "context" $) | nindent 2 }}
{{- else if .args }}
Expand Down Expand Up @@ -132,9 +160,22 @@ containers:
{{- $imageTag := $.Values.defaultImageTag }}{{ with .imageTag }}{{ $imageTag = include "helpers.tplvalues.render" ( dict "value" . "context" $) }}{{ end }}
image: {{ $image }}:{{ $imageTag }}
imagePullPolicy: {{ .imagePullPolicy | default $.Values.defaultImagePullPolicy }}
{{- if and .securityContext .securityContext.mergeWithGeneric $.Values.generic.containerSecurityContext }}
{{- $containerSecurityContext := merge (omit .securityContext "mergeWithGeneric") (omit $.Values.generic.containerSecurityContext "mergeWithGeneric") -}}
{{- with $containerSecurityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
{{- else }}
{{- if .securityContext }}
{{- with .securityContext }}
securityContext: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
{{- else if $.Values.generic.containerSecurityContext }}
{{- with $.Values.generic.containerSecurityContext }}
securityContext: {{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}
{{- end }}
{{- end }}
{{- end -}}
{{- if $.Values.diagnosticMode.enabled }}
args: {{- include "helpers.tplvalues.render" ( dict "value" $.Values.diagnosticMode.args "context" $) | nindent 2 }}
{{- else if .args }}
Expand All @@ -154,17 +195,21 @@ containers:
{{- with .ports }}
ports: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 2 }}
{{- end }}
{{- with .lifecycle }}
lifecycle: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
{{- $lifecycle := merge (default (dict) .lifecycle) (default (dict) $.Values.generic.lifecycle) -}}
{{- $startupProbe := merge (default (dict) .startupProbe) (default (dict) $.Values.generic.startupProbe) -}}
{{- $livenessProbe := merge (default (dict) .livenessProbe) (default (dict) $.Values.generic.livenessProbe) -}}
{{- $readinessProbe := merge (default (dict) .readinessProbe) (default (dict) $.Values.generic.readinessProbe) -}}
{{- with $lifecycle }}
lifecycle: {{- include "helpers.tplvalues.render" ( dict "value" $lifecycle "context" $) | nindent 4 }}
{{- end }}
{{- with .startupProbe }}
startupProbe: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
{{- with $startupProbe }}
startupProbe: {{- include "helpers.tplvalues.render" ( dict "value" $startupProbe "context" $) | nindent 4 }}
{{- end }}
{{- with .livenessProbe }}
livenessProbe: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
{{- with $livenessProbe }}
livenessProbe: {{- include "helpers.tplvalues.render" ( dict "value" $livenessProbe "context" $) | nindent 4 }}
{{- end }}
{{- with .readinessProbe }}
readinessProbe: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
{{- with $readinessProbe }}
readinessProbe: {{- include "helpers.tplvalues.render" ( dict "value" $readinessProbe "context" $) | nindent 4 }}
{{- end }}
{{- with .resources }}
resources: {{- include "helpers.tplvalues.render" ( dict "value" . "context" $) | nindent 4 }}
Expand Down
14 changes: 13 additions & 1 deletion templates/hpa.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- range $name, $hpa := .Values.hpas }}
---
kind: HorizontalPodAutoscaler
apiVersion: {{ .apiVersion | default "autoscaling/v2beta1" }}
apiVersion: {{ .apiVersion | default "autoscaling/v2" }}
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $name "context" $) }}
namespace: {{ $.Release.Namespace | quote }}
Expand All @@ -24,13 +24,25 @@ spec:
- type: Resource
resource:
name: cpu
{{- if eq .apiVersion "autoscaling/v2beta1" }}
targetAverageUtilization: {{ .targetCPU }}
{{- else }}
target:
type: Utilization
averageUtilization: {{ .targetCPU }}
{{- end }}
{{- end }}
{{- if not (empty .targetMemory) }}
- type: Resource
resource:
name: memory
{{- if eq .apiVersion "autoscaling/v2beta1" }}
targetAverageUtilization: {{ .targetMemory }}
{{- else }}
target:
type: Utilization
averageUtilization: {{ .targetMemory }}
{{- end }}
{{- end }}
{{- if .metrics }}
{{- toYaml .metrics | nindent 4 }}
Expand Down
41 changes: 21 additions & 20 deletions templates/serviceaccount.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ metadata:
{{- with $general.annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
{{- with .annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}

{{- if .role }}
{{- if .role.rules }}
{{- if .roles }}
{{- range $role := .roles }}
{{- if $role.rules }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $val.role.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $role.name "context" $) }}
namespace: {{ $.Release.Namespace | quote }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
Expand All @@ -39,12 +40,12 @@ metadata:
{{- with $general.annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
{{- with .annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
rules:
{{ include "helpers.tplvalues.render" ( dict "value" $val.role.rules "context" $ )}}
{{ include "helpers.tplvalues.render" ( dict "value" $role.rules "context" $ )}}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $val.role.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $role.name "context" $) }}
namespace: {{ $.Release.Namespace | quote }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
Expand All @@ -57,7 +58,7 @@ metadata:
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "helpers.app.fullname" (dict "name" $val.role.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $role.name "context" $) }}
subjects:
- kind: ServiceAccount
name: {{ include "helpers.app.fullname" (dict "name" $name "context" $) }}
Expand All @@ -67,7 +68,7 @@ subjects:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $val.role.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $role.name "context" $) }}
namespace: {{ $.Release.Namespace | quote }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
Expand All @@ -79,21 +80,23 @@ metadata:
{{- with .annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ $val.role.name }}
kind: {{ if $role.clusterScope }}ClusterRole{{ else }}Role{{ end }}
name: {{ $role.name }}
subjects:
- kind: ServiceAccount
name: {{ include "helpers.app.fullname" (dict "name" $name "context" $) }}
namespace: {{ $.Release.Namespace | quote }}
{{- end }}
{{- end }}
{{- if .clusterRole }}
{{- if .clusterRole.rules }}
{{- end }}
{{- if .clusterRoles }}
{{- range $clusterRole := .clusterRoles }}
{{- if $clusterRole.rules }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $val.clusterRole.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $clusterRole.name "context" $) }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
{{- with $general.labels }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
Expand All @@ -103,12 +106,12 @@ metadata:
{{- with $general.annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
{{- with .annotations }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
rules:
{{ include "helpers.tplvalues.render" ( dict "value" $val.clusterRole.rules "context" $ )}}
{{ include "helpers.tplvalues.render" ( dict "value" $clusterRole.rules "context" $ )}}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $val.clusterRole.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $clusterRole.name "context" $) }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
{{- with $general.labels }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
Expand All @@ -120,7 +123,7 @@ metadata:
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{ include "helpers.app.fullname" (dict "name" $val.clusterRole.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $clusterRole.name "context" $) }}
subjects:
- kind: ServiceAccount
name: {{ include "helpers.app.fullname" (dict "name" $name "context" $) }}
Expand All @@ -130,7 +133,7 @@ subjects:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: {{ include "helpers.app.fullname" (dict "name" $val.clusterRole.name "context" $) }}
name: {{ include "helpers.app.fullname" (dict "name" $clusterRole.name "context" $) }}
labels:
{{- include "helpers.app.labels" $ | nindent 4 }}
{{- with $general.labels }}{{- include "helpers.tplvalues.render" (dict "value" . "context" $) | nindent 4 }}{{- end }}
Expand All @@ -142,7 +145,7 @@ metadata:
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: {{ $val.clusterRole.name }}
name: {{ $clusterRole.name }}
subjects:
- kind: ServiceAccount
name: {{ include "helpers.app.fullname" (dict "name" $name "context" $) }}
Expand All @@ -151,6 +154,4 @@ subjects:
{{- end }}
{{- end }}
{{- end }}



{{- end }}
Loading