Using Helm helper files to manage shared properties across multiple micro-services

In Helm we use Charts. In Cloud services, a service is typically separated to multiple micro-services.
In Helm lingo then, a micro-service could be considered as a Chart.

Despite being separate to multiple micro-services, it is likely that some properties would be the same across some of them. You would typically resolve this by either duplicating the properties in each micro-service’s ConfigMap (not recommended), or by using a “common ConfigMap” (works well).

But what to do if you want to switch over to using Helm? In Helm you can’t use a common ConfigMap across charts since a Chart can only manage its own “resources” – you cannot use the same common ConfigMap in multiple Charts as Helm will complain the resource already exists, and creating another Chart just for the common ConfigMap seems a bit much (but doable) IMO.

To the rescue come Helm helper (.tpl) files, also called “Partials”. These files are basically “includes” that can be embedded into existing files while a Chart is being installed. Lets see an example.

Lets assume you want to add some common properties to your ConfigMap, to do so create a .tpl file. Make sure the filename starts with an underscore.


{{- define "my-common-configuration" -}}
# Evironment
clusterEnv: {{ index | quote }}
namespace: {{ index | quote }}

Also create a YAML file to store the values (assuming you need different common properties for different environments…).


  # env refers to the environment properties belong to during build-time. Default is "development".
  env: development

    development: staging
    pre_production: preprod

    development: dev-images
    pre_production: preprod-images

Next, in the micro-service’s (Chart) ConfigMap, include the helper file.
Note the use of include to load the configuration data.

apiVersion: v1
kind: ConfigMap
  name: my-configmap-name
  namespace: {{ index }}
  log_level: {{ index | quote }}
{{ include "my-common-configuration" . | indent 2 }}

The generated result would look like this:

apiVersion: v1
kind: ConfigMap
  name: my-configmap-name
  namespace: dev
  log_level: debug
  namespace: staging
  registry: dev-images

You can repeat this in each micro-service that needs the common properties.
And of course, remember to specify the properties in your Deployment…

- name: clusterEnv
       name: my-configmap-name
       key: clusterEnv

But wait, what do I actually do with these files?
The .tpl file should be placed in your Chart’s templates folder and the values YAML file at the root of the Chart directory.

Since the files are shared across multiple micro-services, and each micro-service is likely its own repository, stored them in some Helm or DevOps repository. Configure your pipeline so that these files will get picked and placed in the right location during job execution.

If you’re wondering how to determine which value gets picked from the values YAML file, see my previous blog post, Upgrading to Helm.

Additional reading material

Upgrading to Helm

Helm, the Package Manager for Kubernetes is a wonderful addition to anyone doing DevOps operations with Kubernetes.

With Helm you can:

  • Manage development and production properties
  • Validate and test configuration resources prior to deployment
  • Group deployment resources into Releases
  • Rollback failed deployments

Learn about the Helm basics in the Helm documentation.

Manage development and production properties

You can manage the values.yaml file in a way that will allow a simpler access to environment properties. For example:

  env: development

name: myMicroService
  development: "0.0.1"
  production: "0.0.2"
  development: debug
  production: info

How to choose the environment to use? When running commands, such as helm lint, helm template, helm install or helm upgrade, you can pass the env variable. By passing it the YAML parser will know which property to apply to the template:

sh """
    helm template . --set "env=$ENV"
  • $ENV refers to an environment variable set to either “development” or “production” during the lifespan of a pipeline execution.

Note that this also requires modifying the Kubernetes resource template files. For example, in the ConfigMap.template.yaml file:

log_level: {{ index | quote }}

Learn more about Go templating commands in the Sprig library.

Validate and test configuration resources prior to deployment

helm template

To generate the actual Kubernetes configuration resources based on your resource template files before executing the pipeline, use helm template.

helm lint

To test the syntax of your configuration resources in your pipeline, use helm lint:

lintResults = sh(script: "cd config/helm/${microServiceName};helm lint -f common-values.yaml || true", returnStdout:true)
if ((lintResults.contains("[ERROR]")) || (lintResults.contains("[WARNING]"))) {
   currentBuild.result = "FAILURE"
   env.shouldBuild = "false"

   slackSend (
       color: '#F01717',
       message: "*$JOB_NAME*: <$BUILD_URL|Build #$BUILD_NUMBER>, validation stage failed.\n\n${lintResults}"

Note: the file common-values.yaml refers to an exteral values.yaml-like file containing properties that are used by multiple Charts.

Rollback failed deployments

If helm install/upgrade failed, you can revert back to the previous successful release. E.g.:

helm rollback <release name> <revision number> --force --wait --timeout 180"

To get the revision number, run

helm history <release name>