technosophos / common-chart

A base Helm chart with shared definitions
https://technosophos.github.io/common-chart/
Other
97 stars 16 forks source link

Simplify unique name configurations for multiple deployments #6

Closed kivagant-ba closed 5 years ago

kivagant-ba commented 5 years ago

Originally you suggest the following syntax to extend default names for different resources:

{{- define "my.fullname" -}}
  {{ template "common.fullname" . }}-my-stuff
{{- end -}}

But this is inconvenient. When you need to refer to different configuration values for different resources, you have to use something like in the DataDog's example:

{{- if .Values.my-chart-app-my-stuff.env }}
{{ toYaml .Values.my-chart-app-my-stuff.env | indent 10 }}
{{- end }}

As you see, the code can't be reused in templates. But actually there's a trick that may help to make it better. Let's change `common.fullname' a little bit:

{{- define "common.fullname"}}
  {{- $global := default (dict) .Values.global -}}
  {{- $base := default (printf "%s-%s" .Release.Name .Chart.Name) .Values.fullnameOverride -}}
  {{- if (.appName) -}}
    {{- $base = .appName -}}
  {{- else -}}
  {{- end -}}
  {{- $gpre := default "" $global.fullnamePrefix -}}
  {{- $pre := default "" .Values.fullnamePrefix -}}
  {{- $suf := default "" .Values.fullnameSuffix -}}
  {{- $gsuf := default "" $global.fullnameSuffix -}}
  {{- $name := print $gpre $pre $base $suf $gsuf -}}
  {{- $name | lower | trunc 54 | trimSuffix "-" -}}
{{- end -}}

Now it checks if .appName is set in global scope. And now we can set the name differently in different deployments/services/etc:

{{- $_ := set . "appName" "my-chart-app-my-stuff" -}}
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: {{ template "common.fullname" . }}

And in the same time in values.yaml we can set the structure:


resources:
  my-chart-app-my-stuff:
    requests:
      memory: 512Mi
    limits:
      memory: 512Mi

And use it in the same deployment using the .appName:

# ...
          resources:
{{ index .Values "resources" .appName | toYaml | trim | indent 12 }}

So the key difference is that .Values.fullnameOverride can be used for the entire chart but .appName can be set for a specific resource individually which makes templates more clear, reusable and flexible.

P.S.: I know about https://github.com/helm/charts/tree/master/incubator/common but I guess it's better to discuss the chart here than in that repo with tons of issues.

kivagant-ba commented 5 years ago

I also found that .Values.fullnameOverride also can be overridden, so actually the syntax may look like this:

{{- $_ := set .Values "fullnameOverride" "my-chart-app-my-stuff" -}}
{{- template "common.deployment" (list . "my.deployment") -}}
{{- define "my.deployment" -}}
## Define overrides for your Deployment resource here, e.g.
spec:
  replicas: {{ index .Values "replicas" .Values.fullnameOverride }}
{{- end -}}

Works fine for me. Maybe the trick can be added to README of https://github.com/helm/charts/tree/master/incubator/common but it isn't so critical.