Skip to content

Commit

Permalink
Prometheus: automate kepler grafana dashboard setup
Browse files Browse the repository at this point in the history
Signed-off-by: Maryam Tahhan <[email protected]>
  • Loading branch information
maryamtahhan committed Apr 26, 2024
1 parent 7dafcda commit 06ca69d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
43 changes: 41 additions & 2 deletions lib/prometheus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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 \
Expand All @@ -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"
Expand All @@ -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}"
Expand All @@ -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"
}
6 changes: 3 additions & 3 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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)"

Expand All @@ -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"
Expand Down

0 comments on commit 06ca69d

Please sign in to comment.