diff --git a/lib/prometheus.sh b/lib/prometheus.sh index a5e6cc6..e42301a 100755 --- a/lib/prometheus.sh +++ b/lib/prometheus.sh @@ -44,8 +44,6 @@ 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 \ @@ -60,9 +58,12 @@ deploy_prometheus_operator() { -exec kubectl create -f {} \; is_set "$GRAFANA_ENABLE" && { - find kube-prometheus/manifests -name 'grafana-*.yaml' -type f \ - -exec kubectl create -f {} \; - ok "Grafana deployed" + # Double check yq is available + command -v yq >/dev/null 2>&1 && { + _setup_dashboard_configmap + _add_dashboard_to_grafana + } + _deploy_grafana } ok "Prometheus deployed" @@ -114,11 +115,11 @@ _load_prometheus_operator_images_to_local_registry() { done } -_setup_dashboard(){ +_setup_dashboard_configmap(){ if [ -f "$DASHBOARD_DIR/grafana-dashboards/kepler-exporter-configmap.yaml" ]; then return 0 else - header "Setup Dashboard" + header "Create Dashboard Configmap" mkdir -p "$DASHBOARD_DIR/grafana-dashboards/" cat - > "$DASHBOARD_DIR/grafana-dashboards/kepler-exporter-configmap.yaml" << EOF apiVersion: v1 @@ -136,12 +137,21 @@ metadata: namespace: monitoring EOF fi + ok "Create Dashboard Configmap" } -_run_yq(){ +_add_dashboard_to_grafana() { + header "Edit Kepler Grafana Dashboard " 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" } + +_deploy_grafana() { + header "Deploy Grafana" + find kube-prometheus/manifests -name 'grafana-*.yaml' -type f \ + -exec kubectl create -f {} \; + ok "Grafana deployed" +} diff --git a/main.sh b/main.sh index 82beeb8..dcb09e4 100755 --- a/main.sh +++ b/main.sh @@ -172,7 +172,6 @@ ebpf() { cd /tmp/elfutils-source/elfutils-0.189 ./configure --disable-debuginfod make install - mkdir -p /tmp/libbpf-source cd /tmp/libbpf-source yumdownloader --source libbpf @@ -188,6 +187,32 @@ ebpf() { fi } +yq_install() { + # Will only install yq if GRAFANA_ENABLE option is set to true + if is_set "$GRAFANA_ENABLE"; then + command -v yq >/dev/null 2>&1 && { + ok "yq is already installed" + return 0 + } + + GOOS="$(go env GOOS)" + GOARCH="$(go env GOARCH)" + YQ_VERSION=${YQ_VERSION:-v4.34.2} + YQ_INSTALL="https://github.com/mikefarah/yq/releases/download/$YQ_VERSION/yq_${GOOS}_${GOARCH}" + LOCAL_BIN="/usr/bin" + + info "installing yq with version: $YQ_VERSION" + mkdir -p "$LOCAL_BIN" + curl -sSLo "$LOCAL_BIN/yq" "$YQ_INSTALL" || { + fail "failed to install yq" + return 1 + } + + chmod +x "$LOCAL_BIN/yq" + ok "yq was installed successfully" + fi +} + containerruntime() { set -x echo start install container runtime as docker @@ -243,6 +268,7 @@ main() { prerequisites) linuxHeader ebpf + yq_install return $? ;; containerruntime)