|
1 | 1 | # https://helm.sh/docs/topics/charts/#the-chartyaml-file |
| 2 | +import os |
| 3 | + |
| 4 | +import yaml |
| 5 | + |
| 6 | +from projspec.proj.base import ParseFailed, ProjectSpec |
| 7 | +from projspec.utils import AttrDict |
| 8 | + |
| 9 | + |
| 10 | +class HelmChart(ProjectSpec): |
| 11 | + """A Kubernetes application packaged as a Helm chart. |
| 12 | +
|
| 13 | + A Helm chart is a directory tree containing a ``Chart.yaml`` manifest, |
| 14 | + a ``templates/`` directory of Kubernetes resource manifests, and an |
| 15 | + optional ``values.yaml`` file with default configuration values. |
| 16 | + Dependency charts may be declared in ``Chart.yaml`` under the |
| 17 | + ``dependencies`` key; pinned versions are recorded in ``Chart.lock``. |
| 18 | + """ |
| 19 | + |
| 20 | + spec_doc = "https://helm.sh/docs/topics/charts/#the-chartyaml-file" |
| 21 | + |
| 22 | + def match(self) -> bool: |
| 23 | + return "Chart.yaml" in self.proj.basenames |
| 24 | + |
| 25 | + def parse(self) -> None: |
| 26 | + from projspec.artifact.base import FileArtifact |
| 27 | + from projspec.artifact.deployment import HelmDeployment |
| 28 | + from projspec.artifact.process import Process |
| 29 | + from projspec.content.metadata import DescriptiveMetadata |
| 30 | + |
| 31 | + # ------------------------------------------------------------------ # |
| 32 | + # Chart.yaml — required by the Helm spec |
| 33 | + # ------------------------------------------------------------------ # |
| 34 | + try: |
| 35 | + with self.proj.fs.open(self.proj.basenames["Chart.yaml"], "rt") as f: |
| 36 | + chart = yaml.safe_load(f) |
| 37 | + except (OSError, yaml.YAMLError) as exc: |
| 38 | + raise ParseFailed(f"Could not read Chart.yaml: {exc}") from exc |
| 39 | + |
| 40 | + if not isinstance(chart, dict): |
| 41 | + raise ParseFailed("Chart.yaml did not parse to a mapping") |
| 42 | + |
| 43 | + name = chart.get("name", "") |
| 44 | + version = chart.get("version", "") |
| 45 | + |
| 46 | + # ------------------------------------------------------------------ # |
| 47 | + # Contents |
| 48 | + # ------------------------------------------------------------------ # |
| 49 | + meta: dict[str, str] = {} |
| 50 | + for key in ( |
| 51 | + "name", |
| 52 | + "version", |
| 53 | + "appVersion", |
| 54 | + "description", |
| 55 | + "type", |
| 56 | + "home", |
| 57 | + "icon", |
| 58 | + ): |
| 59 | + val = chart.get(key) |
| 60 | + if val is not None: |
| 61 | + meta[key] = str(val) |
| 62 | + |
| 63 | + keywords = chart.get("keywords", []) |
| 64 | + if keywords: |
| 65 | + meta["keywords"] = ", ".join(keywords) |
| 66 | + |
| 67 | + maintainers = chart.get("maintainers", []) |
| 68 | + if maintainers: |
| 69 | + # Each entry: {name, email, url} — flatten to a readable string |
| 70 | + meta["maintainers"] = ", ".join( |
| 71 | + m.get("name", "") for m in maintainers if isinstance(m, dict) |
| 72 | + ) |
| 73 | + |
| 74 | + self._contents = AttrDict( |
| 75 | + descriptive_metadata=DescriptiveMetadata(proj=self.proj, meta=meta) |
| 76 | + ) |
| 77 | + |
| 78 | + # ------------------------------------------------------------------ # |
| 79 | + # Artifacts |
| 80 | + # ------------------------------------------------------------------ # |
| 81 | + arts = AttrDict() |
| 82 | + |
| 83 | + # helm package . → produces <name>-<version>.tgz |
| 84 | + if name and version: |
| 85 | + arts["packaged_chart"] = FileArtifact( |
| 86 | + proj=self.proj, |
| 87 | + cmd=["helm", "package", "."], |
| 88 | + fn=f"{self.proj.url}/{name}-{version}.tgz", |
| 89 | + ) |
| 90 | + |
| 91 | + # helm dependency update → populates charts/ and writes Chart.lock |
| 92 | + arts["chart_lock"] = FileArtifact( |
| 93 | + proj=self.proj, |
| 94 | + cmd=["helm", "dependency", "update", "."], |
| 95 | + fn=f"{self.proj.url}/Chart.lock", |
| 96 | + ) |
| 97 | + |
| 98 | + # helm install / upgrade → deploys to the active k8s cluster |
| 99 | + release = name or "release" |
| 100 | + arts["release"] = HelmDeployment( |
| 101 | + proj=self.proj, |
| 102 | + release=release, |
| 103 | + ) |
| 104 | + |
| 105 | + # helm lint — validates chart structure and values |
| 106 | + arts["lint"] = Process( |
| 107 | + proj=self.proj, |
| 108 | + cmd=["helm", "lint", "."], |
| 109 | + ) |
| 110 | + |
| 111 | + self._artifacts = arts |
| 112 | + |
| 113 | + @staticmethod |
| 114 | + def _create(path: str) -> None: |
| 115 | + """Scaffold a minimal but valid Helm chart directory.""" |
| 116 | + name = os.path.basename(path) |
| 117 | + |
| 118 | + # Chart.yaml — required manifest |
| 119 | + with open(f"{path}/Chart.yaml", "wt") as f: |
| 120 | + f.write( |
| 121 | + f"apiVersion: v2\n" |
| 122 | + f"name: {name}\n" |
| 123 | + f"description: A Helm chart for {name}\n" |
| 124 | + f"type: application\n" |
| 125 | + f"version: 0.1.0\n" |
| 126 | + f'appVersion: "1.0.0"\n' |
| 127 | + ) |
| 128 | + |
| 129 | + # values.yaml — default configuration values |
| 130 | + with open(f"{path}/values.yaml", "wt") as f: |
| 131 | + f.write( |
| 132 | + "replicaCount: 1\n" |
| 133 | + "\n" |
| 134 | + "image:\n" |
| 135 | + f" repository: {name}\n" |
| 136 | + " tag: latest\n" |
| 137 | + " pullPolicy: IfNotPresent\n" |
| 138 | + "\n" |
| 139 | + "service:\n" |
| 140 | + " type: ClusterIP\n" |
| 141 | + " port: 80\n" |
| 142 | + ) |
| 143 | + |
| 144 | + # templates/ directory with a minimal Deployment manifest |
| 145 | + os.makedirs(f"{path}/templates", exist_ok=True) |
| 146 | + with open(f"{path}/templates/deployment.yaml", "wt") as f: |
| 147 | + f.write( |
| 148 | + "apiVersion: apps/v1\n" |
| 149 | + "kind: Deployment\n" |
| 150 | + "metadata:\n" |
| 151 | + f" name: {name}\n" |
| 152 | + "spec:\n" |
| 153 | + " replicas: {{ .Values.replicaCount }}\n" |
| 154 | + " selector:\n" |
| 155 | + " matchLabels:\n" |
| 156 | + f" app: {name}\n" |
| 157 | + " template:\n" |
| 158 | + " metadata:\n" |
| 159 | + " labels:\n" |
| 160 | + f" app: {name}\n" |
| 161 | + " spec:\n" |
| 162 | + " containers:\n" |
| 163 | + f" - name: {name}\n" |
| 164 | + ' image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"\n' |
| 165 | + " imagePullPolicy: {{ .Values.image.pullPolicy }}\n" |
| 166 | + " ports:\n" |
| 167 | + " - containerPort: {{ .Values.service.port }}\n" |
| 168 | + ) |
0 commit comments