Skip to content

Commit 7f754f6

Browse files
authored
fix setup.secret.sh script (GoogleContainerTools#6014)
1 parent c208c95 commit 7f754f6

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ docs/themes
1717
docs/package-lock.json
1818
pkg/skaffold/output/debug.test
1919
cmd/skaffold/app/cmd/statik/statik.go
20+
secrets/keys.json

deploy/setup-secret.sh

+23-7
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,33 @@ while getopts "p:" opt; do
2828
esac
2929
done
3030

31+
function download_existing_key() {
32+
# Download a valid key created within the past two weeks.
33+
KEY_IDS=$(gcloud iam service-accounts keys list --iam-account=metrics-writer@k8s-skaffold.iam.gserviceaccount.com --project=k8s-skaffold --managed-by=user --filter="validAfterTime>-P2W" --format="value(name)")
34+
while read -r KEY_ID
35+
do
36+
if gsutil cp gs://${BUCKET_ID}/${KEY_ID}.json ${KEY_FILE}; then
37+
echo "Downloaded existing key to ${KEY_FILE}"
38+
return 0
39+
fi
40+
done <<< "$KEY_IDS"
41+
return 1
42+
}
3143

32-
# create a new valid key
33-
KEY_ID=$(gcloud iam service-accounts keys list --iam-account=metrics-writer@k8s-skaffold.iam.gserviceaccount.com --project=k8s-skaffold --managed-by=user --filter="validAfterTime.date('%Y-%m-%d', Z) = `date +%F`" --format="value(name)" --limit=1)
34-
if [ -z "$KEY_ID" ]; then
44+
function upload_new_key() {
45+
echo "Creating new service account key..."
3546
gcloud iam service-accounts keys create ${KEY_FILE} --iam-account=metrics-writer@${PROJECT_ID}.iam.gserviceaccount.com --project=${PROJECT_ID}
3647
retVal=$?
3748
if [ $retVal -ne 0 ]; then
3849
echo "No key created."
39-
exit 1
50+
return 1
4051
fi
52+
echo "New service account key created."
4153
KEY_ID=$(gcloud iam service-accounts keys list --iam-account=metrics-writer@k8s-skaffold.iam.gserviceaccount.com --project=k8s-skaffold --managed-by=user --filter="validAfterTime.date('%Y-%m-%d', Z) = `date +%F`" --format="value(name)" --limit=1)
42-
fi
43-
gsutil cp ${KEY_FILE} gs://${BUCKET_ID}/${KEY_ID}.json
44-
gsutil cp ${KEY_FILE} gs://${BUCKET_ID}/${LATEST_GCS_PATH}
54+
gsutil cp ${KEY_FILE} gs://${BUCKET_ID}/${KEY_ID}.json
55+
gsutil cp ${KEY_FILE} gs://${BUCKET_ID}/${LATEST_GCS_PATH}
56+
echo "New service account key uploaded to GCS."
57+
return 0
58+
}
59+
60+
download_existing_key || upload_new_key

0 commit comments

Comments
 (0)