-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathonprem-k8s-connect-hub.sh
More file actions
executable file
·77 lines (57 loc) · 2.55 KB
/
Copy pathonprem-k8s-connect-hub.sh
File metadata and controls
executable file
·77 lines (57 loc) · 2.55 KB
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
#!/usr/bin/env bash
# Copyright 2020 Google LLC
#
# 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.
# Variables
export PROJECT=$(gcloud config get-value project)
export WORK_DIR=${WORK_DIR:="${PWD}/workdir"}
export ONPREM_CLUSTER_NAME_BASE="onprem"
export ONPREM_CLUSTER_NAME=$ONPREM_CLUSTER_NAME_BASE.k8s.local
export ONPREM_KUBECONFIG=$WORK_DIR/${ONPREM_CLUSTER_NAME_BASE}.context
export GKE_CONNECT_SA=anthos-connect
export GKE_SA_CREDS=$WORK_DIR/$GKE_CONNECT_SA-creds.json
echo "### "
echo "### Prepare onprem cluster for Hub"
echo "### "
# Install Tools
### Access hub alpha, gcloud 244+ includes hub commands
# Switch to onprem Context
kubectx $ONPREM_CLUSTER_NAME_BASE
## Provision the GKE Connect Service Account & Access
# Create GKE Connect Service Account
# >> NOTE: This should already exist as it was needed for the whitelisting process
gcloud iam service-accounts create $GKE_CONNECT_SA --project=$PROJECT
# Assign it GKE Connect rights
gcloud projects add-iam-policy-binding $PROJECT \
--member="serviceAccount:$GKE_CONNECT_SA@$PROJECT.iam.gserviceaccount.com" \
--role="roles/gkehub.connect"
FILE=$GKE_SA_CREDS
if ! test -f "$FILE"; then
# Create and download a key
gcloud iam service-accounts keys create $GKE_SA_CREDS --project=$PROJECT \
--iam-account=$GKE_CONNECT_SA@$PROJECT.iam.gserviceaccount.com
fi
### Connect Cluster to Hub
gcloud container hub memberships register $ONPREM_CLUSTER_NAME_BASE\
--context=$ONPREM_CLUSTER_NAME \
--service-account-key-file=$GKE_SA_CREDS \
--kubeconfig=$ONPREM_KUBECONFIG \
--project=$PROJECT
## Create Service Account for use in Login Step
export KSA=onprem-admin-sa
kubectl create serviceaccount $KSA
kubectl create clusterrolebinding ksa-admin-binding --clusterrole cluster-admin --serviceaccount default:$KSA
# Generate Token for login process
echo "###########################"
echo "Use the following token during login at https://console.cloud.google.com/kubernetes/list for cluster $ONPREM_KUBECONFIG"
printf "\n$(kubectl --kubeconfig=$ONPREM_KUBECONFIG describe secret $KSA | sed -ne 's/^token: *//p')\n\n"