Search code examples
kubernetes-helmgo-templates

Padding integers with zeros in Helm value


I have a need to construct Kubernetes objects with name containing three digits padded with leading zeros.

For example with an iteration, I am trying to create StatefulSets with names

"XXXX-001"
"XXXX-002"
...

I have tried

printf "%s has **%3d** dogs." .Name .NumberDogs.

but the %3d isn't padding this to 3 digits.

And I tried to look into Helm Template Function List, but could not find anything which can help my scenario.


Solution

  • The printf template function uses the Go fmt package internally, and its documentation has the rules for constructing the % strings. %3d should request a string that is (at least) three characters wide, but the default padding is with spaces; you need to include a 0 modifier as well to get zero padding.

    printf "%03d dogs" .NumberDogs