Ensuring Effective Helm Charts with Linting, Testing, and Diff Checks 🚀

Hamdi KHELIL - Oct 1 - - Dev Community

When deploying applications to Kubernetes, using Helm charts is a great way to simplify the process. But how do you make sure your Helm charts are high-quality and won’t cause issues down the line? Don’t worry! In this guide, we’ll show you how to:

  • Use Helm Chart-Testing for linting and validation 🕵️‍♀️
  • Perform Unit Testing with the Helm Unit Test plugin 🔧
  • Use Helm Diff to check changes before installing or upgrading 🚦

By following these steps, you’ll catch potential issues early and ensure smooth deployments. We’ll also build a fully tested NGINX Helm chart at the end!

1. Linting and Validation with Helm Chart-Testing 🛠️

Linting helps ensure your Helm charts follow best practices. For this, we use Helm Chart-Testing (ct), a more robust tool than the basic helm lint.

Install Helm Chart-Testing

You can install it via Homebrew:

brew install helm chart-testing
Enter fullscreen mode Exit fullscreen mode

Or via Docker:

docker pull quay.io/helmpack/chart-testing
Enter fullscreen mode Exit fullscreen mode

Running Helm Chart-Testing

Once installed, you can lint your chart:

ct lint --charts ./charts/
Enter fullscreen mode Exit fullscreen mode

This performs validation checks and ensures your chart adheres to best practices before moving to the next step.

2. Unit Testing with Helm Unit Test Plugin 🔧

The Helm Unit Test plugin helps test how your templates behave under different configurations. It’s an easy way to verify that your charts work as expected.

Install Helm Unit Test Plugin

First, install the plugin:

helm plugin install https://github.com/quintush/helm-unittest
Enter fullscreen mode Exit fullscreen mode

Writing Unit Tests

Create a tests/unittest.yaml file to define tests. Here's an example:

suite: Test suite for NGINX chart
templates:
  - deployment.yaml

tests:
  - it: Should render a valid Deployment resource
    set:
      replicaCount: 2
    asserts:
      - isKind:
          of: Deployment
      - equal:
          path: metadata.name
          value: nginx-deployment
Enter fullscreen mode Exit fullscreen mode

Running Unit Tests

Run the unit tests:

helm unittest ./charts/nginx
Enter fullscreen mode Exit fullscreen mode

3. Validating Changes with Helm Diff 🔍

Before applying any changes to your Kubernetes cluster, it’s important to see what will change. The Helm Diff plugin lets you compare the existing state with the new deployment.

Install Helm Diff Plugin

Install the plugin with:

helm plugin install https://github.com/databus23/helm-diff
Enter fullscreen mode Exit fullscreen mode

Using Helm Diff

Check the changes that will occur before upgrading:

helm diff upgrade nginx ./charts/nginx --values ./values.yaml
Enter fullscreen mode Exit fullscreen mode

4. Example: NGINX Deployment Helm Chart 🚀

Here’s how you can apply everything we’ve discussed to deploy an NGINX server with Helm.

Step 1: Create an NGINX Helm Chart

Create a basic NGINX chart:

helm create nginx
Enter fullscreen mode Exit fullscreen mode

Step 2: Customize the deployment.yaml Template

Edit the nginx/templates/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "nginx.fullname" . }}
  labels:
    app: {{ include "nginx.name" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ include "nginx.name" . }}
  template:
    metadata:
      labels:
        app: {{ include "nginx.name" . }}
    spec:
      containers:
      - name: nginx
        image: "nginx:stable"
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Step 3: Write Unit Tests

Add unit tests in tests/unittest.yaml:

suite: Test suite for NGINX chart
templates:
  - deployment.yaml

tests:
  - it: Should render an NGINX deployment with 3 replicas
    set:
      replicaCount: 3
    asserts:
      - isKind:
          of: Deployment
      - equal:
          path: metadata.name
          value: nginx
      - equal:
          path: spec.replicas
          value: 3
Enter fullscreen mode Exit fullscreen mode

Step 4: Run Lint, Unit Tests, and Helm Diff

  • Lint: Run Helm Chart-Testing to check your chart:
  ct lint --charts ./nginx/
Enter fullscreen mode Exit fullscreen mode
  • Unit Test: Ensure your templates behave as expected:
  helm unittest ./nginx/
Enter fullscreen mode Exit fullscreen mode
  • Diff: Check the changes before upgrading:
  helm diff upgrade nginx ./nginx --values ./nginx/values.yaml
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy the Helm Chart

Finally, deploy your NGINX Helm chart:

helm install nginx ./nginx --values ./nginx/values.yaml
Enter fullscreen mode Exit fullscreen mode

5. Example GitHub Action for Helm Linting, Testing, and Diffing 🛠️

Now that you’ve learned the basics, let’s automate the whole process using GitHub Actions! Below is an example workflow that automates linting, testing, and diff checks for your Helm charts. This ensures your charts are always validated before merging to the main branch.

name: Helm Chart CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  helm-chart-testing:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Helm
      run: |
        curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

    - name: Install Helm plugins (Unit Test, Diff)
      run: |
        helm plugin install https://github.com/quintush/helm-unittest
        helm plugin install https://github.com/databus23/helm-diff

    - name: Run Helm Chart-Testing Lint
      run: |
        ct lint --charts ./nginx/

    - name: Run Helm Unit Tests
      run: |
        helm unittest ./nginx/

    - name: Check for Changes with Helm Diff
      run: |
        helm diff upgrade nginx ./nginx --values ./nginx/values.yaml
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Trigger: The action runs on every push or pull request to the main branch.
  2. Helm Setup: Installs Helm and necessary plugins for unit testing and diff checks.
  3. Linting: Runs Helm Chart-Testing lint on the chart.
  4. Unit Testing: Runs Helm Unit Test to ensure templates behave as expected.
  5. Diff Check: Compares the current Helm release with the proposed changes using Helm Diff.

Conclusion 🎉

By combining Helm Chart-Testing, Helm Unit Test, and Helm Diff, you can ensure that your Helm charts are written effectively and behave as expected before deployment. With the example GitHub Action, you can automate this process and keep your CI/CD pipeline smooth.

Explore these awesome tools on GitHub:

Integrate them into your CI/CD pipeline to ensure smoother Kubernetes deployments. Happy charting! 🚀

. . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player