Skip to content

Commit

Permalink
Replace replace jsonnet with go-jsonnet
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Feder <[email protected]>
  • Loading branch information
matofeder committed Jan 31, 2024
1 parent 8423700 commit bf6f140
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
apiVersion: v2
name: dnation-kubernetes-jsonnet-translator
type: application
version: 1.0.1
appVersion: 1.0.1
version: 1.0.2
appVersion: 1.0.2
description: dNation Translator is a simple container for translating jsonnet content stored in k8s configmaps to **grafana dashboards** or **prometheus rules**.
keywords:
- jsonnet
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb

FROM python:3.8 AS python-builder
RUN apt-get update

RUN apt-get install -y golang-go
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

Expand All @@ -15,7 +15,7 @@ RUN pip3 install /app

FROM python:3.8-slim

LABEL Version="1.0.1"
LABEL Version="1.0.2"
LABEL Vendor="dNation"
LABEL Description="Container generates json resources from jsonnet resources (grafana dashboards, prometheus rules)"

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ kubectl apply -f examples/prom-rule-jsonnet.yaml
kubectl describe prometheusrule prometheus-rules-generated
```

More examples can be found in [examples](examples) folder.
More examples can be found in this folder.
8 changes: 7 additions & 1 deletion helpers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ Create kind cluster
kind create cluster --config helpers/kind_cluster_config.yaml --image kindest/node:v1.20.2
```

Create prometheus rule CRD
- optional, if you do not create prometheus rule CRD you will see some 404 errors in translator logs
```bash
kubectl apply -f https://raw.githubusercontent.com/prometheus-community/helm-charts/main/charts/kube-prometheus-stack/charts/crds/crds/crd-prometheusrules.yaml
```

Translator can run without installation to cluster (but there has to be accessible cluster config).
Install [jsonnet bundler](https://github.com/jsonnet-bundler/jsonnet-bundler) and run:
```
Expand All @@ -34,7 +40,7 @@ Generate example grafana dashboard
```
kubectl apply -f examples/grafana-jsonnet.yaml
# see results
kubectl desribe cm grafana-dashboards-generated-example-dashboard
kubectl describe cm grafana-dashboards-generated-example-dashboard
```
If everything was installed correctly, described config map should contain grafana dashboard in JSON.

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


install_requires = [
"jsonnet==0.16.*",
"gojsonnet==0.17.*",
"kubernetes==12.0.*",
"patool==1.12",
"urllib3==1.25.*",
Expand All @@ -31,7 +31,7 @@
description="Generates json resources from jsonnet resources",
author="dnation",
author_email="[email protected]",
version="0.3.0",
version="1.0.2",
python_requires=">=3.7",
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
30 changes: 20 additions & 10 deletions translator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#

import os
import _jsonnet
import json
import utils
import logger
Expand Down Expand Up @@ -142,11 +141,14 @@ def evaluate_jsonnet_build_annotations(annotations):
Returns:
dict: Valid jsonnet build arguments with evaluated values.
"""
# FIXME: quick and dirty workaround for issue https://github.com/google/go-jsonnet/issues/484
import _gojsonnet

evaluated_args = {}
for key, value in annotations.items():
try:
evaluated_arg = ast.literal_eval(value)
_jsonnet.evaluate_snippet("dummy", "{}", **{key: evaluated_arg})
_gojsonnet.evaluate_snippet("dummy", "{}", **{key: evaluated_arg})
evaluated_args[key] = evaluated_arg
except TypeError as e:
log.error(f"Build argument from annotations {key} is invalid, error: {e}")
Expand Down Expand Up @@ -482,6 +484,9 @@ def process_cm_data(data, ext_libs=[], user_args={}):
Raises:
JsonnetConfigMapError: Raised if jsonnet evaluation fails.
"""
# FIXME: quick and dirty workaround for issue https://github.com/google/go-jsonnet/issues/484
import _gojsonnet

libsonnet_folder = "./libsonnets"
jsons = []

Expand All @@ -495,7 +500,7 @@ def process_cm_data(data, ext_libs=[], user_args={}):

try:
jsonnet_code = data[dataKey]
json_ = _jsonnet.evaluate_snippet(
json_ = _gojsonnet.evaluate_snippet(
dataKey, jsonnet_code, jpathdir=ext_libs, **user_args
)
except RuntimeError as e:
Expand Down Expand Up @@ -537,6 +542,9 @@ def process_cm_binary_data(name, data, main_jsonnet, ext_libs=[], user_args={}):
JsonnetConfigMapError: Raised if jsonnet evaluation fails or
wrong archive format is provided.
"""
# FIXME: quick and dirty workaround for issue https://github.com/google/go-jsonnet/issues/484
import _gojsonnet

tmp_folder_name = f"jsonnet_archive_{name}"
tmp_file_name = f"generated_from_archive_{name}.json"

Expand All @@ -562,7 +570,7 @@ def process_cm_binary_data(name, data, main_jsonnet, ext_libs=[], user_args={}):

jsonnet_filepath = os.path.join(tmp_folder_name, main_jsonnet)
try:
json_ = _jsonnet.evaluate_file(
json_ = _gojsonnet.evaluate_file(
jsonnet_filepath, jpathdir=ext_libs, **user_args
)
except RuntimeError as e:
Expand Down Expand Up @@ -700,6 +708,11 @@ def watch_loop(args_, label_selector):
Return:
None
"""
try:
config.load_kube_config()
except config.config_exception.ConfigException:
config.load_incluster_config()

initial_run = True
while True:
try:
Expand Down Expand Up @@ -795,7 +808,9 @@ def watch_for_changes(args_):
dashboard_proc.daemon = True
dashboard_proc.start()

rule_proc = Process(target=watch_loop, args=(args_, args_.jsonnet_rules_selector))
rule_proc = Process(
target=watch_loop, args=(args_, args_.jsonnet_rules_selector)
)
rule_proc.daemon = True
rule_proc.start()

Expand Down Expand Up @@ -836,11 +851,6 @@ def main(args_):

install_dependencies(args.libsonnet)

try:
config.load_kube_config()
except config.config_exception.ConfigException:
config.load_incluster_config()

if args.delete_resources:
delete_generated_resources(args)
else:
Expand Down

0 comments on commit bf6f140

Please sign in to comment.