Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions kcl/lib/steps/k8s/install_prometheus.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import azure_pipelines.ap.steps
import lib.steps.azure

# Install kube-prometheus-stack via Helm. Caller must run azure.GetCredentials first and check in a values.yaml.
InstallKubePrometheusStack = lambda serviceConnection: str, valuesFile: str, namespace: str = "monitoring", releaseName: str = "prometheus", chartVersion: str = "", waitTimeout: str = "10m" -> steps.Step {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name can be more succinct. e.g. InstallPrometheus. KubePrometheus is not a thing. Stack bears no meaning.

script = """
set -euo pipefail

# Install Helm v3 if missing

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider extracting install helm into a separate step. Imaging there are 10 helm charts, each have their own way to install helm 3.

if ! command -v helm >/dev/null 2>&1; then
echo "==> helm not found; installing"
curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
fi
helm version --short

# Add / refresh prometheus-community repo (idempotent)
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts >/dev/null 2>&1 || true
helm repo update prometheus-community

VERSION_FLAG=""
if [ -n "${chartVersion}" ]; then
VERSION_FLAG="--version ${chartVersion}"
fi

helm upgrade --install "${releaseName}" prometheus-community/kube-prometheus-stack \\
--namespace "${namespace}" --create-namespace \\
--values "$(Pipeline.Workspace)/s/${valuesFile}" \\
--wait --atomic --timeout "${waitTimeout}" \\
\${VERSION_FLAG}

echo "==> kube-prometheus-stack release '${releaseName}' is ready in namespace '${namespace}'"
# Prometheus web service is svc/<releaseName>-kube-prometheus-prometheus on port 9090.
echo "==> Prometheus web service: svc/${releaseName}-kube-prometheus-prometheus in ${namespace} (port 9090)"
"""
azure.AzCli(serviceConnection, "Install kube-prometheus-stack", script)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

}