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..ca10103 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,37 @@ 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 + } + + YQ="/usr/bin/yq" + YQ_VERSION=${YQ_VERSION:-v4.34.2} + ARCH="$(uname -m)" + YQ_ARCH="amd64" # set a reasonable default + if [[ -n "${ARCH}" ]]; then + case "${ARCH}" in + x86_64) YQ_ARCH=amd64;; + aarch64) YQ_ARCH=arm64;; + esac + fi + YQ_INSTALL="https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_${YQ_ARCH}" + + info "installing yq version: $YQ_VERSION and arch: $YQ_ARCH" + wget "${YQ_INSTALL}" -O "${YQ}" || { + fail "failed to install yq" + rm -rf "${YQ}" # cleanup in case of fail + return 1 + } + chmod +x "${YQ}" + ok "yq was installed successfully" + fi +} + containerruntime() { set -x echo start install container runtime as docker @@ -243,6 +273,7 @@ main() { prerequisites) linuxHeader ebpf + yq_install return $? ;; containerruntime)