forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-updater-sa.sh
executable file
·103 lines (86 loc) · 3.44 KB
/
config-updater-sa.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
# This script will create a 'testgrid-config-updater' GCP service account with permissions
# to update the TG config and load a service account key into the cluster's
# test-pods namespace. This should only be done when the Prow instance is using a
# separate build cluster and only trusted jobs are running in the service cluster.
# Setting up this service account is necessary for Prow to update TG config with
# a postsubmit job.
# To use, point your kubeconfig at the correct cluster context and specify gcp
# PROJECT and service account DESCRIPTION environment variables.
# To enable prompts and run in "interactive" mode supply the "-i|--interactive" flag.
# e.g.
# PROJECT="istio-testing" \
# DESCRIPTION="Used to update the TestGrid config in the gs://k8s-testgrid bucket." \
# config-updater-sa.sh --interactive
# Globals:
SERVICE_ACCOUNT="${SERVICE_ACCOUNT:=testgrid-config-updater}"
# PROJECT => "required"
# DESCRIPTION => "required"
# Options:
INTERACTIVE=
function cleanup() {
# For security reasons, delete private key regardless of exit code.
trap 'rm -f "$SERVICE_ACCOUNT-sa-key.json"' EXIT
}
function create_service_account() {
prompt "Create service-account: \"$SERVICE_ACCOUNT\" in Project: \"$PROJECT\""
# Create a service account for performing Prow deployments in a GCP project.
gcloud beta iam service-accounts create "${SERVICE_ACCOUNT}" --project="${PROJECT}" --description="${DESCRIPTION}" --display-name="TestGrid Config Updater SA"
# Add the `roles/storage.objectAdmin` IAM policy binding to the service account.
# https://cloud.google.com/iam/docs/understanding-roles#storage-roles
gcloud projects add-iam-policy-binding "$PROJECT" --member="serviceAccount:${SERVICE_ACCOUNT}@${PROJECT}.iam.gserviceaccount.com" --role "roles/storage.objectAdmin"
# Generate private key and attach to the service account.
gcloud iam service-accounts keys create "${SERVICE_ACCOUNT}-sa-key.json" --project="${PROJECT}" --iam-account="${SERVICE_ACCOUNT}@${PROJECT}.iam.gserviceaccount.com"
}
function create_secret() {
prompt "Create cluster secret for Kube context: \"$(kubectl config current-context)\""
# Deploy the service-account secret to the cluster in the current context.
kubectl create secret generic -n test-pods "${SERVICE_ACCOUNT}-service-account" --from-file="service-account.json=${SERVICE_ACCOUNT}-sa-key.json"
}
function handle_options() {
while [ $# -gt 0 ]; do
case "$1" in
-i | --interactive)
INTERACTIVE=1
shift
;;
*)
echo "Unknown option: $1" >&1
exit 1
;;
esac
done
}
function prompt() {
if [ "$INTERACTIVE" ]; then
echo
read -r -n1 -p "$1 ? [y/n] "
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
fi
}
function main() {
cleanup
handle_options "$@"
create_service_account
create_secret
}
main "$@"