|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +: "${GOOGLE_APPLICATION_CREDENTIALS:?Environment variable empty or not defined.}" |
| 22 | + |
| 23 | +function boskosctlwrapper() { |
| 24 | + boskosctl --server-url http://"${BOSKOS_HOST}" --owner-name "secret-store-provider-gcp" "${@}" |
| 25 | +} |
| 26 | + |
| 27 | +cleanup() { |
| 28 | + gcloud beta secrets delete "${SECRET_ID}" --quiet |
| 29 | + # stop boskos heartbeat |
| 30 | + if [ -n "${BOSKOS_HOST:-}" ]; then |
| 31 | + boskosctlwrapper release --name "${GCP_PROJECT}" --target-state dirty |
| 32 | + fi |
| 33 | +} |
| 34 | +trap cleanup EXIT |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +main() { |
| 39 | + echo "starting the secret store csi driver test for gcp provider" |
| 40 | + |
| 41 | + # install boskosctl |
| 42 | + if [[ -z "$(command -v boskosctl)" ]]; then |
| 43 | + echo "installing boskosctl" |
| 44 | + GO111MODULE=on go install sigs.k8s.io/boskos/cmd/boskosctl@master |
| 45 | + echo "'boskosctl' has been installed to $GOPATH/bin, make sure this directory is in your \$PATH" |
| 46 | + fi |
| 47 | + |
| 48 | + echo "testing boskosctl" |
| 49 | + boskosctl --help |
| 50 | + |
| 51 | + # Acquire a project from boskos pool, test will use secret created on this acquired project |
| 52 | + if [ -n "${BOSKOS_HOST:-}" ]; then |
| 53 | + echo "Boskos acquire - ${BOSKOS_HOST}" |
| 54 | + BOSKOS_RESOURCE="$( boskosctlwrapper acquire --type gce-project --state free --target-state busy --timeout 1h )" |
| 55 | + export BOSKOS_RESOURCE |
| 56 | + GCP_PROJECT=$(echo "$BOSKOS_RESOURCE" | jq -r ".name") |
| 57 | + export GCP_PROJECT |
| 58 | + |
| 59 | + # send a heartbeat in the background to keep the lease while using the resource |
| 60 | + echo "Starting Boskos HeartBeat" |
| 61 | + boskosctlwrapper heartbeat --resource "${BOSKOS_RESOURCE}" & |
| 62 | + fi |
| 63 | + |
| 64 | + echo "Using project ${GCP_PROJECT}" |
| 65 | + gcloud config set project "${GCP_PROJECT}" |
| 66 | + |
| 67 | + # create a secret in the aquired project |
| 68 | + SECRET_ID="test-secret-$(openssl rand -hex 4)" |
| 69 | + export SECRET_ID |
| 70 | + export SECRET_VALUE="secret-a" |
| 71 | + echo -n ${SECRET_VALUE} | gcloud beta secrets create "${SECRET_ID}" --data-file=- --ttl=1800s --quiet |
| 72 | + |
| 73 | + SECRET_PROJECT_ID="$(gcloud config get project)" |
| 74 | + export SECRET_PROJECT_ID |
| 75 | + SECRET_PROJECT_NUMBER="$(gcloud projects describe "$SECRET_PROJECT_ID" --format='value(projectNumber)')" |
| 76 | + export SECRET_PROJECT_NUMBER |
| 77 | + |
| 78 | + export SECRET_URI="projects/${SECRET_PROJECT_NUMBER}/secrets/${SECRET_ID}/versions/latest" |
| 79 | + |
| 80 | + # Prow jobs are executed by `k8s-infra-prow-build.svc.id.goog` in test-pods namespace, so grant the access to the secret |
| 81 | + gcloud secrets add-iam-policy-binding "${SECRET_ID}" \ |
| 82 | + --role=roles/secretmanager.secretAccessor \ |
| 83 | + --member=principalSet://iam.googleapis.com/projects/773781448124/locations/global/workloadIdentityPools/k8s-infra-prow-build.svc.id.goog/namespace/test-pods |
| 84 | + |
| 85 | + # wait for permissions to propagate |
| 86 | + sleep 60 |
| 87 | + |
| 88 | + if [[ ${RELEASE:-} == "true" ]]; then |
| 89 | + make e2e-bootstrap e2e-helm-deploy-release e2e-gcp |
| 90 | + else |
| 91 | + make e2e-bootstrap e2e-helm-deploy e2e-gcp |
| 92 | + fi |
| 93 | +} |
| 94 | + |
| 95 | +main |
0 commit comments