From b3628594af1ef855ef9208ab34037f4cb72fba40 Mon Sep 17 00:00:00 2001 From: HumbertoPeluso Date: Tue, 21 Sep 2021 16:18:42 -0300 Subject: [PATCH] automatically add loki to Grafana's datasource --- vkpr/loki/install/src/unix/formula/formula.sh | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/vkpr/loki/install/src/unix/formula/formula.sh b/vkpr/loki/install/src/unix/formula/formula.sh index 790f6c84..0f0b3908 100755 --- a/vkpr/loki/install/src/unix/formula/formula.sh +++ b/vkpr/loki/install/src/unix/formula/formula.sh @@ -5,6 +5,7 @@ runFormula() { addRepLoki installLoki + existGrafana } addRepLoki(){ @@ -14,7 +15,40 @@ addRepLoki(){ } installLoki(){ - $VKPR_HELM upgrade --install vkpr-loki-stack -f $VKPR_EXTERNAL_LOKI_VALUES grafana/loki-stack + $VKPR_HELM upgrade --install --wait --timeout 5m vkpr-loki-stack -f $VKPR_EXTERNAL_LOKI_VALUES grafana/loki-stack + } + + existGrafana(){ + check_pod_name "vkpr-prometheus-stack-grafana" + if [[ $POD_EXISTS ]]; then + echoColor "yellow" "Adding Loki to Grafana's datasource..." + local LOGINGRAFANA="$($VKPR_KUBECTL get secret vkpr-prometheus-stack-grafana -o yaml | $VKPR_YQ eval '.data.admin-user' - | base64 -d):$($VKPR_KUBECTL get secret vkpr-prometheus-stack-grafana -o yaml | $VKPR_YQ eval '.data.admin-password' - | base64 -d)" + local TOKEN_API_GRAFANA=$(curl -k -X POST -H "Host: grafana.localhost" -H "Content-Type: application/json" -d '{"name": "apikeycurl","role": "Admin"}' http://$LOGINGRAFANA@127.0.0.1:8000/api/auth/keys | $VKPR_JQ --raw-output '.key') + if [[ $TOKEN_API_GRAFANA == "" ]]; then + echoColor "red" "Api Token can only be request once or ingress is not installed." + fi + + curl -K -X -H "Host: grafana.localhost" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN_API_GRAFANA" -d ' + { + "name":"loki", + "type":"loki", + "url":"vkpr-loki-stack.default.svs.cluster.local:3100", + "access":"proxy", + "basicAuth":false, + "editable": true + }' http://127.0.0.1:8000/api/datasources + + fi + } + + check_pod_name(){ + for pod in $($VKPR_KUBECTL get pods | awk 'NR>1{print $1}'); do + if [[ "$pod" == "$1"* ]]; then + POD_EXISTS=true # pod name found a match, then returns True + return + fi + done + POD_EXISTS=false }