Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #66 from tumido/fix-dereference-jsonschema
Browse files Browse the repository at this point in the history
fix: dereference jsonschema from a template
  • Loading branch information
tumido authored Jun 8, 2023
2 parents b93f15f + d844883 commit c0009f1
Show file tree
Hide file tree
Showing 8 changed files with 4,662 additions and 74 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- "charts/**"

jobs:
check-readme:
check-metadata:
runs-on: ubuntu-latest
env:
GO111MODULE: on
Expand All @@ -27,7 +27,8 @@ jobs:

- name: Run pre-commit
uses: pre-commit/action@646c83fcd040023954eafda54b4db0192ce70507 # renovate: tag=v3.0.0

with:
extra_args: --show-diff-on-failure
test:
runs-on: ubuntu-latest
steps:
Expand Down
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ repos:
- --template-files=./_templates.gotmpl
# A base filename makes it relative to each chart directory found
- --template-files=README.md.gotmpl
- repo: local
hooks:
- id: jsonschema-dereference
name: jsonschema-dereference
entry: python .pre-commit/jsonschema-dereference.py
additional_dependencies: [jsonref, Jinja2, pyyaml]
language: python
types_or: [yaml, json]
49 changes: 49 additions & 0 deletions .pre-commit/jsonschema-dereference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json
from typing import List, Dict, Any
from pathlib import Path

import jsonref
import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
from jinja2 import Template

JSONSCHEMA_TEMPLATE_NAME = "values.schema.tmpl.json"
JSONSCHEMA_NAME = "values.schema.json"
CHART_LOCK = "Chart.lock"

def parse_chart_lock(chart_dir: Path):
"""Open and load Chart.yaml file."""
with open(chart_dir / CHART_LOCK, "r") as f:
return yaml.load(f, Loader=Loader)

def template_schema(chart_dir: Path, lock: Dict[str, Any]):
"""Load values.schema.tmpl.json and template it via Jinja2."""
with open(chart_dir / JSONSCHEMA_TEMPLATE_NAME, "r") as f:
schema_template = Template(f.read())

return json.loads(schema_template.render(lock))

def dereference_and_save(chart_dir: Path, schema_template: Any):
"""Take schema containing $refs and dereference them."""
schema = jsonref.replace_refs(schema_template)
with open(chart_dir / JSONSCHEMA_NAME, "w") as f:
json.dump(schema, f, indent=4, sort_keys=True)

if __name__ == '__main__':
charts = [p.parent for p in Path(".").rglob(CHART_LOCK)]

errors: List[BaseException] = []
for chart in charts:
try:
lock = parse_chart_lock(chart)
schema_template = template_schema(chart, lock)

dereference_and_save(chart, schema_template)
except BaseException as e:
print(f"Could not process schema for '{chart}': {e}")
errors.append(e)
if errors:
exit(1)
3 changes: 3 additions & 0 deletions charts/backstage/.helmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
.idea/
*.tmproj
.vscode/
# Templates files
README.md.gotmpl
values.schema.tmpll.json
2 changes: 1 addition & 1 deletion charts/backstage/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ sources:
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 2.0.1
version: 2.0.2
2 changes: 1 addition & 1 deletion charts/backstage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Janus-IDP Backstage Helm Chart

[![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/janus-idp&style=flat-square)](https://artifacthub.io/packages/search?repo=janus-idp)
![Version: 2.0.1](https://img.shields.io/badge/Version-2.0.1-informational?style=flat-square)
![Version: 2.0.2](https://img.shields.io/badge/Version-2.0.2-informational?style=flat-square)
![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)

A Helm chart for deploying a Backstage application
Expand Down
Loading

0 comments on commit c0009f1

Please sign in to comment.