Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse uuid in helm charts

I am writing helm charts and it creates one deployment and one statefulset component.

Now I want to generate uuid and send the value to both k8s components.

I a using uuid function to generate the uuid. But need help how I can send this value to both components.

Here is my chart folder structure --

  • projectdir
    • chart1
      • templates
        • statefulset.yaml
    • chart2
      • templates
        • deployment.yaml
    • helperchart
      • templates
        • _helpers.tpl

I have to write the logic to generate the uuid in _helpers.tpl.

like image 493
srr Avatar asked Nov 28 '25 03:11

srr


1 Answers

Edit: It seems defining it in the _helpers.tpl does not work - thank you for pointing it out.

I have lookup it up a bit, and it seems currently the only way to achieve that is to put both of the manifests, separated by --- to the same file under the templates/. See the following example, where the UUID is defined in the first line and then used in the both Deployment and the StatefulSet:

{{- $mySharedUuid := uuidv4 -}}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "uuid-test.fullname" . }}-1
  labels:
    {{- include "uuid-test.labels" . | nindent 4 }}
  annotations:
    my-uuid: {{ $mySharedUuid }}
spec:
...
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "uuid-test.fullname" . }}-2
  labels:
    {{- include "uuid-test.labels" . | nindent 4 }}
  annotations:
    my-uuid: {{ $mySharedUuid }}
spec:
...

After templating, the output is:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: uuid-test-app-1
  labels:
    helm.sh/chart: uuid-test-0.1.0
    app.kubernetes.io/name: uuid-test
    app.kubernetes.io/instance: uuid-test-app
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
  annotations:
    my-uuid: fe0346f5-a963-4ca1-ada0-af17405f3155
spec:
...
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: uuid-test-app-2
  labels:
    helm.sh/chart: uuid-test-0.1.0
    app.kubernetes.io/name: uuid-test
    app.kubernetes.io/instance: uuid-test-app
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
  annotations:
    my-uuid: fe0346f5-a963-4ca1-ada0-af17405f3155
spec:
...

See the same issue: https://github.com/helm/helm/issues/6456

Note that this approach will still cause the UUID to be regenerated when you do a helm upgrade. To circumvent that, you would need to use another workaround along with this one.

like image 132
Utku Özdemir Avatar answered Dec 02 '25 00:12

Utku Özdemir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!