Skip to content

Commit

Permalink
add dynamic helm chart version lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
usrbinkat committed Jan 31, 2024
1 parent 2ac92e9 commit 5dd1c89
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
8 changes: 7 additions & 1 deletion __main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pulumi
import pulumi_kubernetes as k8s
from typing import Optional
from src.lib.helm_chart_versions import get_latest_helm_chart_version

import os

Expand Down Expand Up @@ -166,13 +167,18 @@ def __init__(self, name: str, k8s_provider: Optional[k8s.Provider] = None, opts:
# Choose the appropriate Helm values
cilium_helm_values = cilium_helm_values_kind if kubernetes_distribution == 'kind' else cilium_helm_values_talos

# Fetch the latest version of the Cilium helm chart
cilium_chart_url = "https://raw.githubusercontent.com/cilium/charts/master/index.yaml"
cilium_chart_name = "cilium"
cilium_latest_version = get_latest_helm_chart_version(cilium_chart_url, cilium_chart_name)

# Deploy Cilium using Helm chart
cilium_helm_release = k8s.helm.v3.Release(
"cilium-release",
chart="cilium",
name="cilium",
repository_opts={"repo": "https://helm.cilium.io/"},
version="1.14.5",
version=cilium_latest_version,
values=cilium_helm_values,
namespace=cilium_helm_values["namespace"],
wait_for_jobs=True,
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pulumi>=3
pulumi_kubernetes>=4.4.0
pulumi_kubernetes>=4.4.0beautifulsoup4
pyyaml
packaging

Empty file added src/lib/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions src/lib/helm_chart_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import requests
import logging
import yaml
from packaging.version import parse as parse_version, InvalidVersion, Version

# Set up basic logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def is_stable_version(version_str):
"""Check if the version string is a valid and stable semantic version."""
try:
parsed_version = parse_version(version_str)
# Check if it's a stable version (no pre-release or dev metadata)
return isinstance(parsed_version, Version) and not parsed_version.is_prerelease and not parsed_version.is_devrelease
except InvalidVersion:
return False

def get_latest_helm_chart_version(url, chart_name):
try:
logging.info(f"Fetching URL: {url}")
response = requests.get(url)
response.raise_for_status()

# Parse the YAML content
index = yaml.safe_load(response.content)
if chart_name in index['entries']:
chart_versions = index['entries'][chart_name]
# Filter out non-stable versions and sort
stable_versions = [v for v in chart_versions if is_stable_version(v['version'])]
if not stable_versions:
logging.info(f"No stable versions found for chart '{chart_name}'.")
return "No stable version found"
latest_chart = max(stable_versions, key=lambda x: parse_version(x['version']))
return latest_chart['version']
else:
logging.info(f"No chart named '{chart_name}' found in repository.")
return "Chart not found"

except requests.RequestException as e:
logging.error(f"Error fetching data: {e}")
return f"Error fetching data: {e}"

# Example usage
url = "https://raw.githubusercontent.com/cilium/charts/master/index.yaml"
chart = "cilium"
latest_version = get_latest_helm_chart_version(url, chart)
print(f"The latest version of {chart} is: {latest_version}")

0 comments on commit 5dd1c89

Please sign in to comment.