From 3a8f3743c5afeab41adf67b455b31ac86007660a Mon Sep 17 00:00:00 2001 From: Pete Wall Date: Mon, 28 Oct 2024 18:06:04 -0500 Subject: [PATCH] Add platform tests Signed-off-by: Pete Wall --- .github/workflows/platform-test.yml | 79 +++++++++++++++++++ charts/k8s-monitoring-test/.helmignore | 28 ++----- .../templates/configmap.yaml | 3 +- .../k8s-monitoring-test/templates/secret.yaml | 2 + .../templates/tests/test-pod.yaml | 3 + .../tests/platform/remote-config/.envrc | 5 ++ .../grafana-cloud-credentials.yaml | 13 +++ .../platform/remote-config/test-manifest.yaml | 5 ++ .../platform/remote-config/test-values.yaml | 8 ++ .../tests/platform/remote-config/values.yaml | 18 +++++ scripts/run-integration-test.sh | 19 ++--- 11 files changed, 146 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/platform-test.yml create mode 100644 charts/k8s-monitoring/tests/platform/remote-config/.envrc create mode 100644 charts/k8s-monitoring/tests/platform/remote-config/grafana-cloud-credentials.yaml create mode 100644 charts/k8s-monitoring/tests/platform/remote-config/test-manifest.yaml create mode 100644 charts/k8s-monitoring/tests/platform/remote-config/test-values.yaml create mode 100644 charts/k8s-monitoring/tests/platform/remote-config/values.yaml diff --git a/.github/workflows/platform-test.yml b/.github/workflows/platform-test.yml new file mode 100644 index 0000000000..acba075ea4 --- /dev/null +++ b/.github/workflows/platform-test.yml @@ -0,0 +1,79 @@ +--- +name: Platform Test +# yamllint disable-line rule:truthy +on: + push: + branches: ["main"] + paths: + - 'charts/**' + - '!charts/k8s-monitoring-v1/**' + pull_request: + paths: + - 'charts/**' + - '!charts/k8s-monitoring-v1/**' + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + list-tests: + name: List tests + runs-on: ubuntu-latest + outputs: + tests: ${{ steps.list_tests.outputs.tests }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: List tests + id: list_tests + run: | + contains(github.event.pull_request.labels.*.name, 'example-label') + if [ "${{ github.event_name }}" == "pull_request" ] && [ "${{contains(github.event.pull_request.labels.*.name, 'run-platform-tests')}}" != "true" ]; then + echo "Skipping platform tests" + exit 0 + fi + + tests=$(ls charts/k8s-monitoring/tests/platform) + echo "Tests: ${tests}" + echo "tests=$(echo "${tests}" | jq --raw-input --slurp --compact-output 'split("\n") | map(select(. != ""))')" >> "${GITHUB_OUTPUT}" + + run-tests: + name: Platform Test + needs: list-tests + runs-on: ubuntu-latest + strategy: + matrix: + test: ${{ fromJson(needs.list-tests.outputs.tests) }} + fail-fast: false + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Helm + uses: azure/setup-helm@v4 + + - name: Check for cluster config + id: check-cluster-config + run: | + if [ -f "charts/k8s-monitoring/tests/integration/${{ matrix.test }}/cluster.yaml" ]; then + echo "has-cluster-config=true" >> "${GITHUB_OUTPUT}" + else + echo "has-cluster-config=false" >> "${GITHUB_OUTPUT}" + fi + + - name: Create kind cluster + if: ${{ steps.check-cluster-config.outputs.has-cluster-config == 'false' }} + uses: helm/kind-action@v1 + + - name: Create kind cluster with special config + if: ${{ steps.check-cluster-config.outputs.has-cluster-config == 'true' }} + uses: helm/kind-action@v1 + with: + config: charts/k8s-monitoring/tests/integration/${{ matrix.test }}/cluster.yaml + + - name: Run test + env: + CREATE_CLUSTER: "false" + DEPLOY_GRAFANA: "false" + run: ./scripts/run-integration-test.sh "charts/k8s-monitoring/tests/platform/${{ matrix.test }}" diff --git a/charts/k8s-monitoring-test/.helmignore b/charts/k8s-monitoring-test/.helmignore index 0e8a0eb36f..c68bdc700b 100644 --- a/charts/k8s-monitoring-test/.helmignore +++ b/charts/k8s-monitoring-test/.helmignore @@ -1,23 +1,5 @@ -# Patterns to ignore when building packages. -# This supports shell glob matching, relative path matching, and -# negation (prefixed with !). Only one pattern per line. -.DS_Store -# Common VCS dirs -.git/ -.gitignore -.bzr/ -.bzrignore -.hg/ -.hgignore -.svn/ -# Common backup files -*.swp -*.bak -*.tmp -*.orig -*~ -# Various IDEs -.project -.idea/ -*.tmproj -.vscode/ +docs +schema-mods +Makefile +README.md +README.md.gotmpl diff --git a/charts/k8s-monitoring-test/templates/configmap.yaml b/charts/k8s-monitoring-test/templates/configmap.yaml index c0b7d6cded..7a2db08a4a 100644 --- a/charts/k8s-monitoring-test/templates/configmap.yaml +++ b/charts/k8s-monitoring-test/templates/configmap.yaml @@ -8,7 +8,6 @@ metadata: data: queries.json: |- { - "queries": - {{- index $test "queries" | toPrettyJson | nindent 6 }} + "queries": {{ index $test "queries" | toPrettyJson | nindent 6 }} } {{- end }} diff --git a/charts/k8s-monitoring-test/templates/secret.yaml b/charts/k8s-monitoring-test/templates/secret.yaml index 2db5123884..790c6c7833 100644 --- a/charts/k8s-monitoring-test/templates/secret.yaml +++ b/charts/k8s-monitoring-test/templates/secret.yaml @@ -1,4 +1,5 @@ {{- range $i, $test := .Values.tests }} +{{- if hasKey $test "env" }} --- apiVersion: v1 kind: Secret @@ -10,3 +11,4 @@ stringData: {{ $key }}: {{ $value | quote }} {{- end }} {{- end }} +{{- end }} diff --git a/charts/k8s-monitoring-test/templates/tests/test-pod.yaml b/charts/k8s-monitoring-test/templates/tests/test-pod.yaml index ecd103b1d7..40815717cb 100644 --- a/charts/k8s-monitoring-test/templates/tests/test-pod.yaml +++ b/charts/k8s-monitoring-test/templates/tests/test-pod.yaml @@ -63,8 +63,11 @@ spec: done exit 1 envFrom: +{{ (index $test "envFrom") | toYaml | indent 8 }} +{{- if hasKey $test "env" }} - secretRef: name: {{ include "k8s-monitoring-test.fullname" $ }}-{{ $i }} +{{- end }} volumeMounts: - name: queries mountPath: /etc/test diff --git a/charts/k8s-monitoring/tests/platform/remote-config/.envrc b/charts/k8s-monitoring/tests/platform/remote-config/.envrc new file mode 100644 index 0000000000..1e08cc1de5 --- /dev/null +++ b/charts/k8s-monitoring/tests/platform/remote-config/.envrc @@ -0,0 +1,5 @@ +export GRAFANA_CLOUD_FLEET_MGMT_USER=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Fleet Management/username") +export GRAFANA_CLOUD_FLEET_MGMT_TOKEN=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Fleet Management/password") +export GRAFANA_CLOUD_METRICS_USERNAME=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Prometheus/username") +export GRAFANA_CLOUD_LOGS_USERNAME=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Loki/username") +export GRAFANA_CLOUD_RW_POLICY_TOKEN=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Loki/password") diff --git a/charts/k8s-monitoring/tests/platform/remote-config/grafana-cloud-credentials.yaml b/charts/k8s-monitoring/tests/platform/remote-config/grafana-cloud-credentials.yaml new file mode 100644 index 0000000000..4e20c82580 --- /dev/null +++ b/charts/k8s-monitoring/tests/platform/remote-config/grafana-cloud-credentials.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: v1 +kind: Secret +metadata: + name: grafana-cloud-credentials +stringData: + GRAFANA_CLOUD_FLEET_MGMT_USER: "$GRAFANA_CLOUD_FLEET_MGMT_USER" + GRAFANA_CLOUD_FLEET_MGMT_TOKEN: "$GRAFANA_CLOUD_FLEET_MGMT_TOKEN" + PROMETHEUS_URL: "https://prometheus-prod-13-prod-us-east-0.grafana.net/api/prom/api/v1/query" + PROMETHEUS_USER: "$GRAFANA_CLOUD_METRICS_USERNAME" + PROMETHEUS_PASS: "$GRAFANA_CLOUD_RW_POLICY_TOKEN" + LOKI_USER: "$GRAFANA_CLOUD_LOGS_USERNAME" + LOKI_PASS: "$GRAFANA_CLOUD_RW_POLICY_TOKEN" diff --git a/charts/k8s-monitoring/tests/platform/remote-config/test-manifest.yaml b/charts/k8s-monitoring/tests/platform/remote-config/test-manifest.yaml new file mode 100644 index 0000000000..2c2274f37e --- /dev/null +++ b/charts/k8s-monitoring/tests/platform/remote-config/test-manifest.yaml @@ -0,0 +1,5 @@ +--- +prerequisites: + - type: manifest + name: grafana-cloud-credentials + file: charts/k8s-monitoring/tests/platform/remote-config/grafana-cloud-credentials.yaml diff --git a/charts/k8s-monitoring/tests/platform/remote-config/test-values.yaml b/charts/k8s-monitoring/tests/platform/remote-config/test-values.yaml new file mode 100644 index 0000000000..b6d9271aea --- /dev/null +++ b/charts/k8s-monitoring/tests/platform/remote-config/test-values.yaml @@ -0,0 +1,8 @@ +--- +tests: + - envFrom: + - secretRef: + name: grafana-cloud-credentials + queries: + - query: alloy_build_info{cluster="remote-config-platform-test"} + type: promql diff --git a/charts/k8s-monitoring/tests/platform/remote-config/values.yaml b/charts/k8s-monitoring/tests/platform/remote-config/values.yaml new file mode 100644 index 0000000000..402a123350 --- /dev/null +++ b/charts/k8s-monitoring/tests/platform/remote-config/values.yaml @@ -0,0 +1,18 @@ +--- +cluster: + name: remote-config-platform-test + +alloy-metrics: + enabled: true + remoteConfig: + enabled: true + url: https://fleet-management-prod-008.grafana.net + auth: + type: basic + usernameFrom: env("GRAFANA_CLOUD_FLEET_MGMT_USER") + passwordFrom: env("GRAFANA_CLOUD_FLEET_MGMT_TOKEN") + alloy: + stabilityLevel: public-preview + envFrom: + - secretRef: + name: grafana-cloud-credentials diff --git a/scripts/run-integration-test.sh b/scripts/run-integration-test.sh index 6837abe8b3..1df40b2ded 100755 --- a/scripts/run-integration-test.sh +++ b/scripts/run-integration-test.sh @@ -59,11 +59,6 @@ cleanup() { } trap cleanup EXIT -runAndEcho() { - echo "$@" - eval "$@" -} - if [ "${CREATE_CLUSTER}" == "true" ]; then echo "Creating cluster..." if [ ! -f "${clusterConfig}" ]; then @@ -91,9 +86,9 @@ for ((i=0; i temp-values.yaml - runAndEcho helm upgrade --install "${prereqName}" "${namespaceArg}" --create-namespace --repo "${prereqRepo}" "${prereqChart}" -f temp-values.yaml --hide-notes --wait + helm upgrade --install "${prereqName}" ${namespaceArg} --create-namespace --repo "${prereqRepo}" "${prereqChart}" -f temp-values.yaml --hide-notes --wait rm temp-values.yaml else - runAndEcho helm upgrade --install "${prereqName}" "${namespaceArg}" --create-namespace --repo "${prereqRepo}" "${prereqChart}" --hide-notes --wait + helm upgrade --install "${prereqName}" ${namespaceArg} --create-namespace --repo "${prereqRepo}" "${prereqChart}" --hide-notes --wait fi else echo "Unknown prerequisite type: ${prereqType}" @@ -124,6 +119,6 @@ helm upgrade --install k8smon "${PARENT_DIR}/charts/k8s-monitoring" -f "${values if [ -f "${testValuesFile}" ]; then echo "Deploying test chart..." - runAndEcho helm upgrade --install k8smon-test "${PARENT_DIR}/charts/k8s-monitoring-test" -f "${testValuesFile}" --wait - runAndEcho helm test k8smon-test --logs + helm upgrade --install k8smon-test "${PARENT_DIR}/charts/k8s-monitoring-test" -f "${testValuesFile}" --wait + helm test k8smon-test --logs fi