kubernetes-helm

How to enforce that the directory name must match the chart name?


In 2016, this used to be the default behavior in Helm:

fix(helm): produce error if package name is inconsistent

In 2018, this enforcement was removed:

remove dirname constraint on helm package

We would like to reintroduce this requirement in our Helm charts codebase, as a best practice, to prevent chart name collisions.

What would be the most native way to accomplish that?

I would probably write a git pre-commit hook if there is no native way (e.g. via some helm lint flag).


Solution

  • Use the following script with pre-commit:

    #!/usr/bin/env bash
    #
    # Check that the directory name matches the chart name in Chart.yaml.
    #
    # Examples:
    #   - foo/Chart.yaml with "name: hey-foo" fails the check.
    #   - foo/Chart.yaml with "name: foo" passes the check.
    #
    # Usage: $0 [path/to/chart/Chart.yaml ...]
    
    for chart in "$@"; do
        dirname="$(basename "$(dirname "$chart")")"
    
        # Remove trailing slash.
        dirname="${dirname%/}"
    
        # Fetch chart name from Chart.yaml.
        chart_name="$(yq e '.name' "$chart")"
    
        if [[ $dirname != "$chart_name" ]]; then
            echo "error: directory name '${dirname}' does not match chart name '${chart_name}'"
            exit 1
        fi
    done
    

    Edit: Blog post: https://www.perrotta.dev/2024/11/pre-commit-additional-dependencies-in-golang/