diff --git a/chart/Chart.yaml b/chart/Chart.yaml index 472b594..039c650 100644 --- a/chart/Chart.yaml +++ b/chart/Chart.yaml @@ -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 diff --git a/docker/Dockerfile b/docker/Dockerfile index 086b919..ef0b578 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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" @@ -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)" diff --git a/examples/README.md b/examples/README.md index a434a11..a6ed089 100644 --- a/examples/README.md +++ b/examples/README.md @@ -9,7 +9,7 @@ Prerequisites Download this repo and install kind and translator chart. ```bash -kind create cluster --image kindest/node:v1.19.1 +kind create cluster --image kindest/node:v1.27.3 helm install translator ./chart ``` @@ -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. diff --git a/helpers/README.md b/helpers/README.md index 325f901..159a636 100644 --- a/helpers/README.md +++ b/helpers/README.md @@ -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: ``` @@ -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. diff --git a/setup.py b/setup.py index 36d7b6e..a25a9ae 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ install_requires = [ - "jsonnet==0.16.*", + "gojsonnet==0.17.*", "kubernetes==12.0.*", "patool==1.12", "urllib3==1.25.*", @@ -31,7 +31,7 @@ description="Generates json resources from jsonnet resources", author="dnation", author_email="david.suba@dnation.cloud", - version="0.3.0", + version="1.0.2", python_requires=">=3.7", classifiers=[ "Development Status :: 5 - Production/Stable", diff --git a/translator/main.py b/translator/main.py index 0efb732..13b5fd8 100644 --- a/translator/main.py +++ b/translator/main.py @@ -14,7 +14,6 @@ # import os -import _jsonnet import json import utils import logger @@ -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}") @@ -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 = [] @@ -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: @@ -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" @@ -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: @@ -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: @@ -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() @@ -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: