Tip 1 ๐’‰ญ

Tip 1 ๐’‰ญ

Flexibility Helm Dependencies

ยท

2 min read

Today, I've been dealing with Helm dependencies, which are located in Chart.yaml.

To put you in context, imagine a parent Chart that hosts 3 child Charts of the same application as dependencies. Each child Chart has its own deployment configurations, configmap, ingress, etc.

dependencies:
- name: application-1
  version: "v1.2.3-feature-index"
  repository: "https://example.com/charts"
- name: application-2
  version: "3.2.1-master"
  repository: "https://another.example.com/charts"
- name: application-3
  version: "3.2.1-feature-reindex"
  repository: "https://another.example.com/charts"

This approach was chosen because the application will not be modified frequently, and it needs to be replicated in different environments. By including everything in a single Chart, we achieve flexibility by only installing one Chart, and any changes propagate to all deployment environments.

Everything seems great until you ask yourself: when developers make changes and need to test them beforehand, how do you tell Helm to use a different subchart as a dependency?

This is where my approach started. My first attempt was to reference the desired version in the Values.yaml file... but surprise! It threw an error because the Chart.yaml file doesn't treat it as an ingress or deployment template. So, I had to discard that idea.

After thinking about how to make it flexible without "hardcoding" the desired version, I came up with an idea: what if, during compilation, the user selects the exact versions they want through the Jenkins interface, and those parameters are then replaced using regex?

That's what I did. Before compiling the parent Chart, I use Groovy to modify it with regex, replacing all instances of "master" (the default) with the desired version.

dependencies:
- name: application-1
  version: "v0.0.0-master"
  repository: "https://example.com/charts"
- name: application-2
  version: "v0.0.0-master"
  repository: "https://another.example.com/charts"
- name: application-3
  version: "v0.0.0-master"
  repository: "https://another.example.com/charts"
// Groovy code

def yamlFilePath = "path/to/Chart.yaml"

// Reads the current contents of the YAML file
def yamlContent = readFile yamlFilePath

// Replace the versions in the YAML template
def modifiedYaml = yamlContent
                        .replaceAll(/name: application-1\n[ \t]+version: "[^"]+"/, "name: application-1\n    version: \"${selectedVersionApplication1}\"")
                        .replaceAll(/name: application-2\n[ \t]+version: "[^"]+"/, "name: application-2\n    version: \"${selectedVersionApplication2}\"")
                        .replaceAll(/name: application-3\n[ \t]+version: "[^"]+"/, "name: application-3\n    version: \"${selectedVersionApplication3}\"")

// Write the modified YAML to the file
writeFile file: yamlFilePath, text: modifiedYaml

In summary:

in this post we have seen how to use regex in Groovy to modify the versions of a Helm Chart before compilation. This technique allows us flexibility when testing changes in different versions without needing to modify the source code.

I hope you found this post useful. If you have any questions, comments or improvements, feel free to leave them below!!!!

GG WP!

Day 1 complete.

Pin by Yani QF on Guts is Berserk | Berserk, Fighting drawing, Dark fantasy  art

ย