forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcloud-deployer-service-account.sh
executable file
·113 lines (94 loc) · 4 KB
/
gcloud-deployer-service-account.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
104
105
106
107
108
109
110
111
112
113
#!/bin/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 'prow-deployer' GCP service account with permissions
# to deploy to the GKE cluster 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 a deployer service account is necessary for Prow to update itself with
# a postsubmit job.
# To use, point your kubeconfig at the correct cluster context and specify gcp
# PROJECT and service account DESCRIPTION environment variables. Optionally, one can
# supply the PROJECT_BUILD variable to attach the iam policy to the build cluster project.
# To enable prompts and run in "interactive" mode supply the "-i|--interactive" flag.
# e.g.
# PROJECT="istio-testing" \
# PROJECT_BUILD="istio-prow-build" \
# DESCRIPTION="Used to deploy to the clusters in the istio-testing and istio-prow-build projects." \
# gcloud-deployer-service-account.sh --interactive
# Globals:
PROJECT_BUILD="${PROJECT_BUILD:=}"
SERVICE_ACCOUNT="${SERVICE_ACCOUNT:=prow-deployer}"
# 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="Prow Self Deployer SA"
# Add the `roles/container.admin` IAM policy binding to the service account in "service" cluster project.
# https://cloud.google.com/kubernetes-engine/docs/how-to/iam#container.admin
gcloud projects add-iam-policy-binding "$PROJECT" --member="serviceAccount:$SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com" --role "roles/container.admin"
# 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"
if [ "$PROJECT_BUILD" ]; then
prompt "Apply iam policy to build Project: \"$PROJECT_BUILD\""
# Add the `roles/container.admin` IAM policy binding to the service account in "build" cluster project.
gcloud projects add-iam-policy-binding "$PROJECT_BUILD" --member="serviceAccount:$SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com" --role "roles/container.admin"
fi
}
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 "$@"