Automating Helm Chart Updates and Downloads with Python

Managing Helm charts efficiently is crucial for Kubernetes administrators and DevOps engineers. Today, I’ll guide you through automating the process of checking for the latest version of a Helm chart and downloading it using Python. Specifically, we’ll focus on the aws-ebs-csi-driver Helm chart as an example, but the principles can be applied to any Helm chart.

Why Automate Helm Chart Management?

Automating Helm chart management, including checking for updates and downloading new versions, streamlines deployment processes, ensures consistency, and reduces manual errors. It’s especially beneficial in environments where maintaining the latest versions of software is critical for security, performance, or feature availability.

Setting Up Your Environment

Before we begin, ensure you have Python and pip installed on your system. You’ll also need the requests library, which can be installed via pip if you haven’t already:

pip install requests

Step 1: Fetching the Latest Chart Version

First, let’s write a function to check the latest version of the aws-ebs-csi-driver Helm chart from Artifact Hub. We’ll use the Artifact Hub API to fetch the version information.

import requests

def get_latest_chart_version_from_artifact_hub():
    api_url = "https://artifacthub.io/api/v1/packages/helm/deliveryhero/aws-ebs-csi-driver"

    try:
        response = requests.get(api_url)
        response.raise_for_status()
        data = response.json()
        return data['version']
    except requests.RequestException as e:
        print(f"Failed to fetch the latest version: {e}")
        return None

This function makes a GET request to the Artifact Hub API and parses the version from the JSON response.

Step 2: Downloading the Chart

Next, we’ll create a function to download the Helm chart using the version number obtained from step 1. The download URL pattern will depend on where the chart is hosted. For our example, we’re assuming the chart is hosted on GitHub.

def download_helm_chart(version):
    download_url = f"https://github.com/kubernetes-sigs/aws-ebs-csi-driver/releases/download/helm-chart-aws-ebs-csi-driver-{version}/aws-ebs-csi-driver-{version}.tgz"

    try:
        with requests.get(download_url, stream=True) as r:
            r.raise_for_status()
            with open(f"aws-ebs-csi-driver-{version}.tgz", 'wb') as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)
        print(f"Successfully downloaded aws-ebs-csi-driver-{version}.tgz")
    except requests.RequestException as e:
        print(f"Error downloading the chart: {e}")

Step 3: Integrating the Functions

Finally, let’s integrate these functions into a main workflow that checks for the latest version and prompts the user to confirm the download.

def main():
    latest_version = get_latest_chart_version_from_artifact_hub()
    if latest_version:
        print(f"Latest version of the aws-ebs-csi-driver Helm chart: {latest_version}")
        confirm = input("Do you want to download this version? (yes/no) ").strip().lower()
        if confirm == 'yes':
            download_helm_chart(latest_version)
        else:
            print("Download canceled.")
    else:
        print("Could not fetch the latest version.")

if __name__ == "__main__":
    main()

Conclusion

Automating Helm chart management not only saves time but also ensures you’re always up-to-date with the latest versions of your Kubernetes deployments. With Python and a few lines of code, we’ve demonstrated how to streamline checking for updates and downloading Helm charts. This approach can be adapted to manage other charts and integrated into larger automation workflows to enhance deployment efficiency and reliability.

Remember, automation is key to modern DevOps practices, and managing Kubernetes resources efficiently plays a critical role in the successful operation of your infrastructure.