From 06ca69d4cb484596657a558d94c6e93dce5e95cc Mon Sep 17 00:00:00 2001 From: Maryam Tahhan Date: Mon, 22 Apr 2024 10:27:21 -0400 Subject: [PATCH] Prometheus: automate kepler grafana dashboard setup Signed-off-by: Maryam Tahhan --- lib/prometheus.sh | 43 +++++++++++++++++++++++++++++++++++++++++-- main.sh | 6 +++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/prometheus.sh b/lib/prometheus.sh index be7d03c..a5e6cc6 100755 --- a/lib/prometheus.sh +++ b/lib/prometheus.sh @@ -21,11 +21,13 @@ declare -r PROMETHEUS_OPERATOR_VERSION=${PROMETHEUS_OPERATOR_VERSION:-v0.11.0} declare -r PROMETHEUS_REPLICAS=${PROMETHEUS_REPLICAS:-1} -# constants - # constants declare -r KUBE_PROM_DIR="$PROJECT_ROOT/tmp/kube-prometheus" declare -r MONITORING_NS="monitoring" +declare -r DASHBOARD_DIR="$KUBE_PROM_DIR/grafana-dashboards" +declare KEPLER_EXPORTER_GRAFANA_DASHBOARD_JSON + +KEPLER_EXPORTER_GRAFANA_DASHBOARD_JSON=$( curl -fsSL https://raw.githubusercontent.com/sustainable-computing-io/kepler/main/grafana-dashboards/Kepler-Exporter.json | sed '1 ! s/^/ /' ) deploy_prometheus_operator() { @@ -42,6 +44,8 @@ deploy_prometheus_operator() { mv kube-prometheus/manifests/prometheus-prometheus.yaml.tmp \ kube-prometheus/manifests/prometheus-prometheus.yaml + _setup_dashboard + _run_yq _load_prometheus_operator_images_to_local_registry kubectl create -f kube-prometheus/manifests/setup kubectl wait \ @@ -58,8 +62,10 @@ deploy_prometheus_operator() { is_set "$GRAFANA_ENABLE" && { find kube-prometheus/manifests -name 'grafana-*.yaml' -type f \ -exec kubectl create -f {} \; + ok "Grafana deployed" } + ok "Prometheus deployed" rm -rf kube-prometheus ) wait_for_pods_in_namespace "$MONITORING_NS" @@ -84,6 +90,7 @@ _load_prometheus_operator_images_to_local_registry() { # TODO: fix this by passing in the registry information to deploy_prometheus_operator # from main + header "Load prometheus operator images to local registry" local registry if [[ "$CLUSTER_PROVIDER" == "kind" ]]; then registry="localhost:${REGISTRY_PORT}" @@ -106,3 +113,35 @@ _load_prometheus_operator_images_to_local_registry() { done done } + +_setup_dashboard(){ + if [ -f "$DASHBOARD_DIR/grafana-dashboards/kepler-exporter-configmap.yaml" ]; then + return 0 + else + header "Setup Dashboard" + mkdir -p "$DASHBOARD_DIR/grafana-dashboards/" + cat - > "$DASHBOARD_DIR/grafana-dashboards/kepler-exporter-configmap.yaml" << EOF +apiVersion: v1 +data: + kepler-exporter.json: |- + $KEPLER_EXPORTER_GRAFANA_DASHBOARD_JSON +kind: ConfigMap +metadata: + labels: + app.kubernetes.io/component: grafana + app.kubernetes.io/name: grafana + app.kubernetes.io/part-of: kube-prometheus + app.kubernetes.io/version: 9.5.3 + name: grafana-dashboard-kepler-exporter + namespace: monitoring +EOF + fi +} + +_run_yq(){ + f="$DASHBOARD_DIR/grafana-dashboards/kepler-exporter-configmap.yaml" \ + yq -i e '.items += [load(env(f))]' "$KUBE_PROM_DIR"/manifests/grafana-dashboardDefinitions.yaml; + yq -i e '.spec.template.spec.containers.0.volumeMounts += [ {"mountPath": "/grafana-dashboard-definitions/0/kepler-exporter", "name": "grafana-dashboard-kepler-exporter", "readOnly": false} ]' "$KUBE_PROM_DIR"/manifests/grafana-deployment.yaml + yq -i e '.spec.template.spec.volumes += [ {"configMap": {"name": "grafana-dashboard-kepler-exporter"}, "name": "grafana-dashboard-kepler-exporter"} ]' "$KUBE_PROM_DIR"/manifests/grafana-deployment.yaml; + ok "Dashboard setup complete" +} diff --git a/main.sh b/main.sh index 67d8494..0627d5c 100755 --- a/main.sh +++ b/main.sh @@ -45,7 +45,7 @@ declare -r PROJECT_ROOT CALLER_PROJECT_ROOT declare -r CTR_CMD=${CTR_CMD:-docker} declare -r CLUSTER_PROVIDER=${CLUSTER_PROVIDER:-kind} declare -r KUBECONFIG_ROOT_DIR=${KUBECONFIG_ROOT_DIR:-$PROJECT_ROOT/.kube} -declare -r KEPLER_KUBECONFIG=${KEPLER_KUBECONFIG:-config-kepler} +declare -r KEPLER_KUBECONFIG=${KEPLER_KUBECONFIG:-config} declare -r REGISTRY_PORT=${REGISTRY_PORT:-5001} @@ -60,7 +60,7 @@ source "$PROJECT_ROOT/lib/utils.sh" cluster_up() { "${CLUSTER_PROVIDER}_up" - info "Coping $CLUSTER_PROVIDER kubeconfig to $KUBECONFIG_ROOT_DIR/$KEPLER_KUBECONFIG" + info "Copying $CLUSTER_PROVIDER kubeconfig to $KUBECONFIG_ROOT_DIR/$KEPLER_KUBECONFIG" local kubeconfig kubeconfig="$("${CLUSTER_PROVIDER}"_kubeconfig)" @@ -73,7 +73,7 @@ cluster_up() { KUBECONFIG=$kubeconfig kubectl config view --merge --flatten >all-in-one-kubeconfig.yaml mv -f all-in-one-kubeconfig.yaml "${KUBECONFIG_ROOT_DIR}/config" - export KUBECONFIG="${KUBECONFIG_ROOT_DIR}/config" + export KUBECONFIG="${KUBECONFIG_ROOT_DIR}/$KEPLER_KUBECONFIG" if is_set "$PROMETHEUS_ENABLE" || is_set "$GRAFANA_ENABLE"; then source "$PROJECT_ROOT/lib/prometheus.sh"