<p>While I was studying Helm charts, I saw the following template function.</p>
<pre><code>_helper.tpl

{{/*
Expand the name of the chart.
*/}}
{{- define &quot;tpl.name&quot; -}}
{{- .Chart.Name | trunc 63 | trimSuffix &quot;-&quot; }}
{{- end }}
</code></pre>
<p>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.</p>
<p>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 '-'.</p>
<p>I have a couple of questions regarding this:</p>
<ol>
<li><p>When I deploy with Deployment, I get a random uuid at the end of the name, is it safe to use <code>trimSuffix &quot;-&quot;</code> 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 '-'?</p>
<p>Example) If it creates something named <code>queue-93h---</code>, you cannot simply remove the last '-' to comply with K8S's naming policy.</p>
</li>
<li><p>I understand that the reason for using <code>trimSuffix &quot;-&quot;</code> is to remove the '-' that will be appended to the uuid when deployed with Deployment, but I don't need to use <code>trimSuffix &quot;-&quot;</code> when using a Statefulset that doesn't have a uuid appended?</p>
</li>
</ol>
<p>Thank you.</p>

Masuri
<p>Most types of <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/names/" rel="nofollow noreferrer">Object Names and IDs</a> 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.</p>
<p>That is, if your specification says</p>
<pre class="lang-yaml prettyprint-override"><code>metadata:
  name: foo
</code></pre>
<p>then if it was a Deployment I'd expect to see Pod names like <code>foo-12345-abcde</code>, and if it was a StatefulSet then Pods like <code>foo-0</code>.  The <code>-12345</code>, <code>-abcde</code>, and <code>-0</code> suffixes are added by Kubernetes itself, and you can't control them from within your Helm chart.</p>
<p>The default templates you quote are defined in <a href="https://github.com/helm/helm/blob/main/pkg/chartutil/create.go" rel="nofollow noreferrer"><code>pkg/chartutil/create.go</code> in the Helm source</a> and in particular reviewing its history can be informative.  Prior to Helm 2.2, in 2017, this template cut off at only 24 characters (<a href="https://github.com/helm/helm/commit/b0d4cacd5410ede24c68af4864f3241da3e54abf" rel="nofollow noreferrer">commit b0d4cac</a>)!  With that it becomes much more likely that <code>printf &quot;%s-%s&quot; .Release.Name .Chart.Name</code> goes over the limit and the <code>trunc 24</code> will have an effect; if the release name is exactly <code>twenty-three-characters</code> then after <code>trunc</code> you will get a string ending with the single hyphen added &quot;between&quot; the release and chart names, and the <code>trimSuffix</code> needs to strip out that single specific character.</p>
<p>As more name templates were added (suppressing a duplicated name in <a href="https://github.com/helm/helm/commit/c864ba179ee97ac111529fa8e6285e0a3e40760c" rel="nofollow noreferrer">commit c864ba1</a>, supporting <code>fullnameOverride</code> in <a href="https://github.com/helm/helm/commit/f47e1619b099669d6bfa2c704412367556a7f7e4" rel="nofollow noreferrer">commit f47e161</a>, 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 <code>trimSuffix</code> call, for example if a <code>.Values.fullnameOverride</code> is provided then there are other rules it needs to obey.</p>
<p>In the specific example you quote, <code>.Chart.Name</code> is exactly the value of the <code>name:</code> field in the <code>Chart.yaml</code> file, and as the chart author you can pick something valid there; you don't especially need the <code>trunc</code> or <code>trimSuffix</code> calls.  I'd probably still use the generated helper templates if they were appropriate though.</p>
