#!/bin/bash

set -ex

# export time in milliseconds
# export OTEL_METRIC_EXPORT_INTERVAL=500

# use http instead of https (needed because of https://github.com/open-telemetry/opentelemetry-go/issues/4834)
# export OTEL_EXPORTER_OTLP_INSECURE="true"

# https://github.com/grafana/docker-otel-lgtm/tree/main/examples

# docker run -p 3001:3000 -p 4317:4317 -p 4318:4318 \
#     -v ./provisioning/dashboards:/otel-lgtm/grafana/conf/provisioning/dashboards \
#     -v ../dashboards_out:/kubernetes-mixin/dashboards_out \
#     --rm -ti grafana/otel-lgtm

cp ../prometheus_alerts.yaml provisioning/prometheus/
cp ../prometheus_rules.yaml provisioning/prometheus/

# set up 1-node k3d cluster
k3d cluster create kubernetes-mixin \
    -v "$PWD"/provisioning:/kubernetes-mixin/provisioning \
    -v "$PWD"/../dashboards_out:/kubernetes-mixin/dashboards_out

# run grafana, prometheus
kubectl apply -f lgtm.yaml
# kubectl port-forward service/lgtm 3000:3000 4317:4317 4318:4318 9090:9090

# scrape kube-state-metrics, node_exporter, cAdvisor, kubelet, kube-proxy, kube-apiserver, kube-controller-manager, kube-scheduler... write to prometheus
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
helm repo update
helm upgrade --install otel-collector-deployment open-telemetry/opentelemetry-collector \
    -n default \
    -f otel-collector-deployment.values.yaml

# install kube-state-metrics
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm upgrade --install kube-state-metrics prometheus-community/kube-state-metrics \
    -n default

# install node_exporter
helm upgrade --install prometheus-node-exporter prometheus-community/prometheus-node-exporter \
    -n default

# TODO OATs:
# https://github.com/grafana/oats

# test metrics in prometheus
# test recording rules in prometheus
# test alerting rules in prometheus
# e2e test dashboards?
