diff --git a/Makefile b/Makefile index a0c9ff7e6..0f45f3f39 100644 --- a/Makefile +++ b/Makefile @@ -256,7 +256,17 @@ image-registry: ## Build the testdata catalog used for e2e tests and push it to test-e2e: KIND_CLUSTER_NAME := operator-controller-e2e test-e2e: KUSTOMIZE_BUILD_DIR := config/overlays/e2e test-e2e: GO_BUILD_EXTRA_FLAGS := -cover -test-e2e: run image-registry e2e e2e-coverage kind-clean #HELP Run e2e test suite on local kind cluster +test-e2e: run image-registry prometheus e2e e2e-coverage e2e-metrics #HELP Run e2e test suite on local kind cluster + +.PHONY: prometheus +prometheus: PROMETHEUS_NAMESPACE := olmv1-system +prometheus: PROMETHEUS_VERSION := v0.83.0 +prometheus: #HELP Deploy Prometheus into specified namespace + ./hack/test/setup-monitoring.sh $(PROMETHEUS_NAMESPACE) $(PROMETHEUS_VERSION) $(KUSTOMIZE) + +.PHONY: e2e-metrics +e2e-metrics: #HELP Request metrics from prometheus; place in ARTIFACT_PATH if set + curl 127.0.0.1:30900/metrics > $(if $(ARTIFACT_PATH),$(ARTIFACT_PATH),.)/metrics.out .PHONY: extension-developer-e2e extension-developer-e2e: KUSTOMIZE_BUILD_DIR := config/overlays/cert-manager diff --git a/hack/test/setup-monitoring.sh b/hack/test/setup-monitoring.sh new file mode 100755 index 000000000..e4fb764ad --- /dev/null +++ b/hack/test/setup-monitoring.sh @@ -0,0 +1,219 @@ +#!/bin/bash + +set -euo pipefail + +help="setup-monitoring.sh is used to set up prometheus monitoring for e2e testing. + +Usage: + setup-monitoring.sh [PROMETHEUS_NAMESPACE] [PROMETHEUS_VERSION] [KUSTOMIZE] +" + +if [[ "$#" -ne 3 ]]; then + echo "Illegal number of arguments passed" + echo "${help}" + exit 1 +fi + +NAMESPACE=$1 +PROMETHEUS_VERSION=$2 +KUSTOMIZE=$3 + +TMPDIR=$(mktemp -d) +trap 'echo "Cleaning up ${TMPDIR}"; rm -rf "${TMPDIR}"' EXIT +curl -s "https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/refs/tags/${PROMETHEUS_VERSION}/kustomization.yaml" > "${TMPDIR}/kustomization.yaml" +curl -s "https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/refs/tags/${PROMETHEUS_VERSION}/bundle.yaml" > "${TMPDIR}/bundle.yaml" +(cd ${TMPDIR} && ${KUSTOMIZE} edit set namespace ${NAMESPACE}) && kubectl create -k "${TMPDIR}" +kubectl wait --for=condition=Ready pods -n ${NAMESPACE} -l app.kubernetes.io/name=prometheus-operator + +kubectl apply -f - << EOF +apiVersion: v1 +kind: ServiceAccount +metadata: + name: prometheus + namespace: ${NAMESPACE} +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: prometheus +rules: +- apiGroups: [""] + resources: + - nodes + - nodes/metrics + - services + - endpoints + - pods + verbs: ["get", "list", "watch"] +- apiGroups: [""] + resources: + - configmaps + verbs: ["get"] +- apiGroups: + - discovery.k8s.io + resources: + - endpointslices + verbs: ["get", "list", "watch"] +- apiGroups: + - networking.k8s.io + resources: + - ingresses + verbs: ["get", "list", "watch"] +- nonResourceURLs: ["/metrics"] + verbs: ["get"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: prometheus +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: prometheus +subjects: +- kind: ServiceAccount + name: prometheus + namespace: ${NAMESPACE} +EOF + +kubectl apply -f - << EOF +apiVersion: monitoring.coreos.com/v1 +kind: Prometheus +metadata: + name: prometheus + namespace: ${NAMESPACE} +spec: + logLevel: debug + serviceAccountName: prometheus + scrapeTimeout: 30s + scrapeInterval: 1m + securityContext: + runAsNonRoot: true + runAsUser: 65534 + seccompProfile: + type: RuntimeDefault + serviceMonitorSelector: {} +EOF + +kubectl apply -f - << EOF +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: prometheus + namespace: ${NAMESPACE} +spec: + podSelector: + matchLabels: + app.kubernetes.io/name: prometheus + policyTypes: + - Egress + egress: + - {} # Allows all egress traffic for metrics requests +EOF + +# Give the operator time to create the pod +kubectl wait --for=create pods -n ${NAMESPACE} prometheus-prometheus-0 --timeout=60s +kubectl wait --for=condition=Ready pods -n ${NAMESPACE} prometheus-prometheus-0 --timeout=120s + +# Authentication token for the scrape requests +kubectl apply -f - <