Search code examples
kuberneteskubernetes-helm

Do I need to use the trimSuffix "-" in a StatefulSet?


While I was studying Helm charts, I saw the following template function.

_helper.tpl

{{/*
Expand the name of the chart.
*/}}
{{- define "tpl.name" -}}
{{- .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}

This is code that truncates the value of .Chart-Name to 63 digits when it becomes more than 63 digits and removes the '-' value at the end of name.

As far as I understand, The reason for truncating the value by 63 characters is that the maximum length of a name in k8s is 63 characters. The reason for removing the last '-' is that k8s naming convention does not allow names ending in '-'.

I have a couple of questions regarding this:

  1. When I deploy with Deployment, I get a random uuid at the end of the name, is it safe to use trimSuffix "-" to remove only the last '-'? If it creates a random uuid like below, shouldn't it remove all the '-'s after it, not just the last '-'?

    Example) If it creates something named queue-93h---, you cannot simply remove the last '-' to comply with K8S's naming policy.

  2. I understand that the reason for using trimSuffix "-" is to remove the '-' that will be appended to the uuid when deployed with Deployment, but I don't need to use trimSuffix "-" when using a Statefulset that doesn't have a uuid appended?

Thank you.


Solution

  • Most types of Object Names and IDs must start and end with alphanumeric characters, and not hyphens. With both Deployments and StatefulSets, Kubernetes will append things to the object names when it creates the corresponding Pods, and you need to use the same rules in both cases.

    That is, if your specification says

    metadata:
      name: foo
    

    then if it was a Deployment I'd expect to see Pod names like foo-12345-abcde, and if it was a StatefulSet then Pods like foo-0. The -12345, -abcde, and -0 suffixes are added by Kubernetes itself, and you can't control them from within your Helm chart.

    The default templates you quote are defined in pkg/chartutil/create.go in the Helm source and in particular reviewing its history can be informative. Prior to Helm 2.2, in 2017, this template cut off at only 24 characters (commit b0d4cac)! With that it becomes much more likely that printf "%s-%s" .Release.Name .Chart.Name goes over the limit and the trunc 24 will have an effect; if the release name is exactly twenty-three-characters then after trunc you will get a string ending with the single hyphen added "between" the release and chart names, and the trimSuffix needs to strip out that single specific character.

    As more name templates were added (suppressing a duplicated name in commit c864ba1, supporting fullnameOverride in commit f47e161, both in Helm 2.8) the same pattern has gotten copied. I suspect most of the places this is used don't actually need the trimSuffix call, for example if a .Values.fullnameOverride is provided then there are other rules it needs to obey.

    In the specific example you quote, .Chart.Name is exactly the value of the name: field in the Chart.yaml file, and as the chart author you can pick something valid there; you don't especially need the trunc or trimSuffix calls. I'd probably still use the generated helper templates if they were appropriate though.