Skip to content

Commit

Permalink
Merge pull request #35 from Kuadrant/grafonnet
Browse files Browse the repository at this point in the history
Generate dashboards from grafonnet
  • Loading branch information
david-martin authored Oct 9, 2023
2 parents f05ad77 + 2f3e7ae commit 4c7021f
Show file tree
Hide file tree
Showing 32 changed files with 10,959 additions and 15,121 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Ignore bin directory contents
bin/*

log/
log/
/vendor/
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,28 @@ generate-bundles:

.PHONY: compare-bundles
compare-bundles: generate-bundles
git diff --exit-code
git diff --exit-code

JSONNET_SRC_DIR := src/dashboards
JSONNET_TARGET_DIR := config/examples/dashboards
JSONNET_TARGETS := $(addprefix $(JSONNET_TARGET_DIR)/,$(patsubst %.jsonnet,%.json,$(shell ls $(JSONNET_SRC_DIR)/*.jsonnet | xargs -n 1 basename)))
DASHBOARD_CR_TARGETS := $(addprefix $(JSONNET_TARGET_DIR)/,$(patsubst %.jsonnet,%.yaml,$(shell ls $(JSONNET_SRC_DIR)/*.jsonnet | xargs -n 1 basename)))

$(JSONNET_TARGET_DIR)/%.json: $(JSONNET_SRC_DIR)/%.jsonnet
jsonnet -J vendor $< -o $@

$(JSONNET_TARGET_DIR)/%.yaml: $(JSONNET_TARGET_DIR)/%.json
DASHBOARD_NAME=$(shell echo "$<" | sed -r "s/.+\/(.+)\..+/\1/") envsubst < $(JSONNET_SRC_DIR)/dashboard_cr_template.yaml > $@
cat "$<" | jq -c . >> $@

.PHONY: generate-dashboards
generate-dashboards: ${JSONNET_TARGETS}

.PHONY: generate-dashboard-crs
generate-dashboard-crs: generate-dashboards ${DASHBOARD_CR_TARGETS}

.PHONY: apply-latest-dashboard-crs
apply-latest-dashboard-crs: generate-dashboard-crs
for i in ${DASHBOARD_CR_TARGETS}; do kubectl apply -f $$i; done

print-% : ; @echo $* = $($*)
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,41 @@ gatewayapi_udproute_parent_info{name="<UDPRoute_NAME>",namespace="<NAMESPACE>",p
gatewayapi_udproute_status_parent_info{name="<UDPRoute_NAME>",namespace="<NAMESPACE>",parent_group="<PARENT_GROUP>",parent_kind="<PARENT_KIND>",parent_name="<PARENT_NAME>",parent_namespace="<PARENT_NAMESPACE>"}
```

## Local dashboard development

Dashboards are written in jsonnet, and use the [grafonnet library](https://github.com/grafana/grafonnet).
Resulting dashboard json files are checked in.
To generate dashboards, run `make generate-dashboards`.

Local development can be done using a combination of automatic jsonnet execution
and using the [grafana-operator](https://github.com/grafana-operator/grafana-operator)
to automatically update dashboards in Grafana. This allows for a relatviely fast
development loop where you can change a dashboard jsonnet file, save it, then
see the changes automically in a browser.

To set up the local development environment, run the following:
```shell
./hack/local_dev.sh
```

## Grafonnet Development Guidelines

### Experiment and Learn

Grafonnet is a powerful tool, but it may not cover all scenarios in its documentation. If you encounter issues or roadblocks, don’t be afraid to experiment with different approaches. Learning through trial and error can often lead to better understanding and innovative solutions. Remember, every challenge is an opportunity to learn.

### Use Grafana UI Reference

Grafana’s user interface can be an invaluable reference when working with transformations and overrides in Grafonnet. If you’re unsure about how to implement a specific feature in Grafonnet, try creating it in the Grafana UI first. Then, export the dashboard as JSON. This exported JSON can serve as a reference for how to implement the same feature in Grafonnet.

### Panel Abstraction and Reuse

Consider creating abstract representations of panels that are used repeatedly across your dashboards. This not only helps avoid repetition but also ensures consistency in the layout and design of your panels.

Abstraction can also make your code more readable and maintainable, especially when dealing with complex input patterns. When deciding whether to create a new abstraction or reuse an existing one, keep the “Don’t Repeat Yourself” (DRY) principle in mind. If you find yourself writing similar code for multiple panels, it might be time to consider abstraction.

By following these guidelines, you can navigate Grafonnet development more effectively and efficiently. Remember, the goal is not just to create functional dashboards, but also to write clean, maintainable code that can be understood and modified by others.

## Contributing

Contributions are welcome in the form of bugs, feature requests & pull requests.
Expand Down
Loading

0 comments on commit 4c7021f

Please sign in to comment.