Skip to content

Commit e46c14f

Browse files
committed
[installer-test] add kubeconfig authentication hack
1 parent ae24fc5 commit e46c14f

File tree

10 files changed

+131
-13
lines changed

10 files changed

+131
-13
lines changed

install/infra/modules/eks/kubernetes.tf

+33
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,36 @@ resource "null_resource" "kubeconfig" {
219219
create_before_destroy = true
220220
}
221221
}
222+
223+
data "aws_iam_policy_document" "eks_policy" {
224+
statement {
225+
actions = [
226+
"eks:DescribeCluster",
227+
"eks:ListClusters"
228+
]
229+
resources = [
230+
"*",
231+
]
232+
effect = "Allow"
233+
}
234+
}
235+
236+
resource "aws_iam_policy" "eks_policy" {
237+
name = "eks-policy-${var.cluster_name}"
238+
description = "Gitpod ${var.cluster_name} EKS cluster access bucket policy"
239+
policy = data.aws_iam_policy_document.eks_policy.json
240+
}
241+
242+
resource "aws_iam_user" "eks_user" {
243+
force_destroy = true
244+
name = "eks-user-${var.cluster_name}"
245+
}
246+
247+
resource "aws_iam_user_policy_attachment" "eks_attachment" {
248+
user = aws_iam_user.eks_user.name
249+
policy_arn = aws_iam_policy.eks_policy.arn
250+
}
251+
252+
resource "aws_iam_access_key" "eks_user_key" {
253+
user = aws_iam_user.eks_user.name
254+
}

install/infra/modules/eks/output.tf

+10
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ output "registry_backend" {
9191
secret_access_key = aws_iam_access_key.bucket_registry_user[0].secret
9292
}, "No s3 bucket created for registry backend.")
9393
}
94+
95+
output "cluster_user" {
96+
sensitive = true
97+
value = {
98+
userarn = aws_iam_user.eks_user.arn
99+
name = aws_iam_user.eks_user.name
100+
access_key_id = aws_iam_access_key.eks_user_key.id
101+
secret_access_key = aws_iam_access_key.eks_user_key.secret
102+
}
103+
}

install/infra/modules/gke/cluster.tf

+15
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,18 @@ resource "local_file" "kubeconfig" {
149149
filename = var.kubeconfig
150150
content = module.gke_auth.kubeconfig_raw
151151
}
152+
153+
resource "google_service_account" "cluster_user_sa" {
154+
account_id = local.gke_user_sa
155+
display_name = "Gitpod Service Account managed by TF for GKE cluster user"
156+
}
157+
158+
resource "google_project_iam_member" "gke-user-sa-iam" {
159+
project = var.project
160+
role = "roles/container.developer"
161+
member = "serviceAccount:${google_service_account.cluster_user_sa.email}"
162+
}
163+
164+
resource "google_service_account_key" "gke_sa_key" {
165+
service_account_id = google_service_account.cluster_user_sa.name
166+
}

install/infra/modules/gke/database.tf

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ resource "random_password" "password" {
4141
count = var.enable_external_database ? 1 : 0
4242

4343
length = 16
44-
special = true
45-
override_special = "!#$%&*()-_=+[]{}<>:?"
44+
special = false
4645
}
4746

4847
resource "google_sql_database" "database" {

install/infra/modules/gke/local.tf

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ locals {
77
"roles/container.admin"
88
])
99

10+
gke_user_sa = "user-${var.cluster_name}"
11+
1012
obj_sa = "obj-sa-${var.cluster_name}"
1113
obj_iam_roles = var.enable_external_registry ? toset([
1214
"roles/storage.admin",

install/infra/modules/gke/output.tf

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ output "kubeconfig" {
2222
value = module.gke_auth.kubeconfig_raw
2323
}
2424

25+
output "cluster-sa" {
26+
sensitive = true
27+
value = google_service_account_key.gke_sa_key.private_key
28+
}
29+
2530
output "database" {
2631
sensitive = true
2732
value = try({

install/tests/Makefile

+48-11
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,54 @@ help: Makefile
3333
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
3434
@echo
3535

36+
upload-gcp-cluster-creds:
37+
export GKE_CREDS=$$(terraform output -json gke_user_key) && \
38+
echo $$GKE_CREDS > gcp-creds
39+
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
40+
gsutil cp gcp-creds gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds
41+
42+
download-cluster-creds:
43+
[[ -z $$TF_VAR_sa_creds ]] || gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
44+
gcloud config set project sh-automated-tests
45+
[[ -n $$TF_VAR_sa_creds ]] || gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds gcs-creds
46+
[[ -f gcs-creds ]] && cat gcs-creds | tr -d '"' | base64 -d > ${TF_VAR_TEST_ID}-key.json || echo "No GCP credentials"
47+
rm -f gcs-creds
48+
[[ -f ${TF_VAR_TEST_ID}-key.json ]] || cp ${GOOGLE_APPLICATION_CREDENTIALS} ${TF_VAR_TEST_ID}-key.json
49+
3650
upload-kubeconfig-to-gcp:
3751
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
3852
gsutil cp ${KUBECONFIG} gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-kubeconfig
3953

4054
sync-kubeconfig:
41-
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
55+
gcloud config set project sh-automated-tests
4256
gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-kubeconfig ${KUBECONFIG} || echo "No kubeconfig"
4357

4458
## k3s-kubeconfig: Get the kubeconfig configuration for GCP K3s
4559
k3s-kubeconfig: sync-kubeconfig
4660

4761
## gcp-kubeconfig: Get the kubeconfig configuration for GCP GKE
4862
gcp-kubeconfig:
49-
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
63+
$(MAKE) download-cluster-creds
64+
gcloud auth activate-service-account --key-file=${TF_VAR_TEST_ID}-key.json --project=sh-automated-tests || { echo "Count not authenicate the service account"; exit 1; }
5065
export KUBECONFIG=${KUBECONFIG} && \
51-
gcloud container clusters get-credentials gp-${TF_VAR_TEST_ID} --zone europe-west1-d --project sh-automated-tests || $(MAKE) sync-kubeconfig || echo "No cluster present"
66+
gcloud container clusters get-credentials gp-${TF_VAR_TEST_ID} --zone europe-west1-d --project sh-automated-tests || echo "No cluster present"
67+
rm -f ${TF_VAR_TEST_ID}-key.json
5268

5369
## azure-kubeconfig: Get the kubeconfig configuration for Azure AKS
5470
azure-kubeconfig:
55-
az login --service-principal -u $$ARM_CLIENT_ID -p $$ARM_CLIENT_SECRET --tenant $$ARM_TENANT_ID
71+
[[ -n "$$ARM_CLIENT_SECRET" ]] && az login --service-principal -u $$ARM_CLIENT_ID -p $$ARM_CLIENT_SECRET --tenant $$ARM_TENANT_ID || { echo "Please login to azure using az login command"; exit 1; }
5672
export KUBECONFIG=${KUBECONFIG} && \
5773
az aks get-credentials --name p$$TF_VAR_TEST_ID-cluster --resource-group p$$TF_VAR_TEST_ID --file ${KUBECONFIG} || echo "No cluster present"
5874

5975
## aws-kubeconfig: Get the kubeconfig configuration for AWS EKS
6076
aws-kubeconfig:
61-
export KUBECONFIG=${KUBECONFIG} && \
77+
[[ -z $$TF_VAR_sa_creds ]] || gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
78+
gcloud config set project sh-automated-tests
79+
[[ -n $$TF_VAR_sa_creds ]] || gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds ${TF_VAR_TEST_ID}-creds
80+
[[ -f ${TF_VAR_TEST_ID}-creds ]] || touch ${TF_VAR_TEST_ID}-creds
81+
source ${TF_VAR_TEST_ID}-creds; \
6282
aws eks update-kubeconfig --name ${TF_VAR_TEST_ID} --region eu-west-1 --kubeconfig ${KUBECONFIG} || echo "No cluster present"
6383

64-
6584
.PHONY:
6685
## gke-standard-cluster: Creates a zonal GKE cluster
6786
gke-standard-cluster: check-env-cluster-version
@@ -70,8 +89,22 @@ gke-standard-cluster: check-env-cluster-version
7089
rm -f ${KUBECONFIG} && \
7190
$(MAKE) get-kubeconfig && \
7291
[[ -f ${KUBECONFIG} ]] || terraform apply -target=module.gke -var kubeconfig=${KUBECONFIG} --auto-approve
92+
$(MAKE) upload-gcp-cluster-creds
7393
@echo "Done creating GKE cluster"
7494

95+
upload-eks-user:
96+
export AWS_CLUSTER_USER=$$(terraform output -json aws_cluster_user) && \
97+
export USERARN=$$(echo $$AWS_CLUSTER_USER | yq r - 'userarn') && \
98+
export NAME=$$(echo $$AWS_CLUSTER_USER | yq r - 'name') && \
99+
envsubst < ./manifests/aws-auth.yaml > tmp-aws-auth.yaml && \
100+
echo "export AWS_SECRET_ACCESS_KEY=$$(echo $$AWS_CLUSTER_USER | yq r - 'secret_access_key')" > ${TF_VAR_TEST_ID}-creds && \
101+
echo "export AWS_ACCESS_KEY_ID=$$(echo $$AWS_CLUSTER_USER | yq r - 'access_key_id')" >> ${TF_VAR_TEST_ID}-creds && \
102+
kubectl --kubeconfig=${KUBECONFIG} get configmap -n kube-system aws-auth -o yaml | grep -v "creationTimestamp\|resourceVersion\|selfLink\|uid" | sed '/^ annotations:/,+2 d' > /tmp/aws-auth.yaml
103+
yq m --inplace /tmp/aws-auth.yaml tmp-aws-auth.yaml
104+
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
105+
gsutil cp ${TF_VAR_TEST_ID}-creds gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds
106+
kubectl --kubeconfig=${KUBECONFIG} replace -f /tmp/aws-auth.yaml
107+
75108
ami_id_121 := "ami-060637af2651bc8bb"
76109

77110
ami_id_122 := "ami-0733d755ed2c97a4d"
@@ -87,6 +120,7 @@ eks-standard-cluster: check-env-cluster-version
87120
rm -f ${KUBECONFIG} && \
88121
$(MAKE) get-kubeconfig && \
89122
[[ -f ${KUBECONFIG} ]] || terraform apply -target=module.eks -var kubeconfig=${KUBECONFIG} -var eks_node_image_id=${ami_id} --auto-approve
123+
$(MAKE) upload-eks-user
90124
@echo "Done creating EKS cluster"
91125

92126
.PHONY:
@@ -159,8 +193,10 @@ external-dns: check-env-cloud select-workspace
159193

160194
.PHONY:
161195
## get-kubeconfig: Returns KUBECONFIG of a just created cluster
162-
get-kubeconfig: ${cloud}-kubeconfig
163-
196+
get-kubeconfig:
197+
echo "Getting kubeconfig for $$TF_VAR_TEST_ID terraform state" && \
198+
export provider=$$(echo "$$TF_VAR_TEST_ID" | sed 's/\(.*\)-/\1 /' | xargs | awk '{print $$2}') && \
199+
$(MAKE) $$provider-kubeconfig && echo "kubeconfig written to ${KUBECONFIG}"
164200

165201
get-github-config:
166202
ifneq ($(GITHUB_SCM_OAUTH),)
@@ -212,8 +248,8 @@ registry-config-azure:
212248
yq m -i tmp_config.yml tmp_2_config.yml
213249

214250
storage-config-azure:
215-
export PASSWORD=$$(terraform output -json azure_storage | yq r - 'account_name') && \
216-
export USERNAME=$$(terraform output -json azure_storage | yq r - 'account_key') && \
251+
export USERNAME=$$(terraform output -json azure_storage | yq r - 'account_name') && \
252+
export PASSWORD=$$(terraform output -json azure_storage | yq r - 'account_key') && \
217253
export REGION=$$(terraform output -json azure_storage | yq r - 'storage_region') && \
218254
envsubst < ./manifests/kots-config-azure-storage.yaml > tmp_2_config.yml
219255
yq m -i tmp_config.yml tmp_2_config.yml
@@ -388,7 +424,7 @@ kots-upgrade:
388424
kubectl kots upstream upgrade --kubeconfig=${KUBECONFIG} gitpod -n gitpod --deploy
389425

390426
cloud ?= cluster
391-
cleanup: $(cloud)-kubeconfig destroy-gitpod tf-init destroy-$(cloud) destroy-workspace destroy-kubeconfig
427+
cleanup: get-kubeconfig destroy-gitpod tf-init destroy-$(cloud) destroy-workspace destroy-kubeconfig
392428

393429
cluster-kubeconfig: azure-kubeconfig aws-kubeconfig k3s-kubeconfig gcp-kubeconfig
394430

@@ -400,6 +436,7 @@ destroy-cluster: destroy-gcp destroy-aws destroy-azure
400436
destroy-kubeconfig:
401437
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
402438
gsutil rm gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-kubeconfig || echo "No kubeconfig"
439+
gsutil rm gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds || echo "No credentials file"
403440
rm ${KUBECONFIG} || echo "No kubeconfig"
404441

405442
select-workspace:

install/tests/cleanup.sh

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ for i in $(gsutil ls gs://nightly-tests/tf-state); do
1919
[ -z "$filename" ] && continue
2020

2121
if [[ "$filename" == *-kubeconfig ]]; then continue; fi
22+
if [[ "$filename" == *-creds ]]; then continue; fi
2223

2324
TF_VAR_TEST_ID=$(basename "$filename" .tfstate)
2425

install/tests/manifests/aws-auth.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data:
2+
mapUsers: |
3+
- userarn: ${USERARN}
4+
username: ${NAME}
5+
groups:
6+
- system:masters

install/tests/output.tf

+10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ output "gke_database" {
33
value = try(module.gke.database, null)
44
}
55

6+
output "gke_user_key" {
7+
sensitive = true
8+
value = try(module.gke.cluster-sa, null)
9+
}
10+
611
output "k3s_database" {
712
sensitive = true
813
value = try(module.k3s.database, null)
914
}
1015

16+
output "aws_cluster_user" {
17+
sensitive = true
18+
value = try(module.eks.cluster_user, null)
19+
}
20+
1121
output "aws_storage" {
1222
sensitive = true
1323
value = try(module.eks.storage, null)

0 commit comments

Comments
 (0)