Skip to content

Commit dc4aae7

Browse files
committed
removing one level of indirection
1 parent 4d5631a commit dc4aae7

File tree

8 files changed

+97
-29
lines changed

8 files changed

+97
-29
lines changed

install/infra/modules/eks/kubernetes.tf

+34
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ module "eks" {
179179
"k8s.io/cluster-autoscaler/gitpod" = "owned"
180180
}
181181

182+
183+
182184
pre_bootstrap_user_data = <<-EOT
183185
#!/bin/bash
184186
set -ex
@@ -219,3 +221,35 @@ resource "null_resource" "kubeconfig" {
219221
create_before_destroy = true
220222
}
221223
}
224+
225+
data "aws_iam_policy_document" "eks_policy" {
226+
statement {
227+
actions = [
228+
"eks:DescribeCluster",
229+
"eks:ListClusters"
230+
]
231+
resources = [
232+
"*",
233+
]
234+
effect = "Allow"
235+
}
236+
}
237+
238+
resource "aws_iam_policy" "eks_policy" {
239+
name = "eks-policy-${var.cluster_name}"
240+
description = "Gitpod ${var.cluster_name} EKS cluster access bucket policy"
241+
policy = data.aws_iam_policy_document.eks_policy.json
242+
}
243+
244+
resource "aws_iam_user" "eks_user" {
245+
name = "eks-user-${var.cluster_name}"
246+
}
247+
248+
resource "aws_iam_user_policy_attachment" "eks_attachment" {
249+
user = aws_iam_user.eks_user.name
250+
policy_arn = aws_iam_policy.eks_policy.arn
251+
}
252+
253+
resource "aws_iam_access_key" "eks_user_key" {
254+
user = aws_iam_user.eks_user.name
255+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ resource "google_service_account" "cluster_user_sa" {
158158
resource "google_project_iam_member" "gke-user-sa-iam" {
159159
project = var.project
160160
role = "roles/container.developer"
161-
member = "serviceAccount:${google_service_account.cluster_sa.email}"
161+
member = "serviceAccount:${google_service_account.cluster_user_sa.email}"
162162
}
163163

164164
resource "google_service_account_key" "gke_sa_key" {

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/tests/Makefile

+39-26
Original file line numberDiff line numberDiff line change
@@ -35,53 +35,49 @@ help: Makefile
3535

3636
upload-gcp-cluster-creds:
3737
export GKE_CREDS=$$(terraform output -json gke_user_key) && \
38-
echo ${GKE_CREDS} > gcp-creds
38+
echo $$GKE_CREDS > gcp-creds
3939
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}-gcp-creds
40+
gsutil cp gcp-creds gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds
4141

42-
key_file ?= ${GOOGLE_APPLICATION_CREDENTIALS}
43-
download-gcp-cluster-creds:
44-
gcloud auth activate-service-account --key-file=${key_file} --project=sh-automated-tests
45-
gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-gcp-creds gs-creds && cat gs-creds | base64 -d > ${TF_VAR_TEST_ID}-key.json || echo "No GCP credentials"
46-
47-
upload-aws-cluster-creds:
48-
49-
upload-azure-cluster-creds:
42+
download-cluster-creds:
43+
gcloud config set project sh-automated-tests
44+
[[ -n $$TF_VAR_sa_creds ]] || gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds gcs-creds && cat gcs-creds | tr -d '"' | base64 -d > ${TF_VAR_TEST_ID}-key.json || echo "No GCP credentials"
45+
rm -f gcs-creds
46+
[[ -f ${TF_VAR_TEST_ID}-key.json ]] || cp ${GOOGLE_APPLICATION_CREDENTIALS} ${TF_VAR_TEST_ID}-key.json
5047

5148
upload-kubeconfig-to-gcp:
5249
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
5350
gsutil cp ${KUBECONFIG} gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-kubeconfig
5451

55-
key_file ?= ${GOOGLE_APPLICATION_CREDENTIALS}
5652
sync-kubeconfig:
57-
gcloud auth activate-service-account --key-file=key_file --project=sh-automated-tests
53+
gcloud config set project sh-automated-tests
5854
gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-kubeconfig ${KUBECONFIG} || echo "No kubeconfig"
5955

6056
## k3s-kubeconfig: Get the kubeconfig configuration for GCP K3s
6157
k3s-kubeconfig: sync-kubeconfig
6258

6359
## gcp-kubeconfig: Get the kubeconfig configuration for GCP GKE
6460
gcp-kubeconfig:
65-
kubectl get secret -n sh-gcs-secret -o -o jsonpath='{.data.key}' | base64 -d > gcs-key.json || echo "No access to core-dev cluster"
66-
[[ -f gcs-key.json ]] || echo ${GOOGLE_APPLICATION_CREDENTIALS} > gcs-key.json
67-
$(MAKE) download-gcp-cluster-creds key_file=${key_file}
68-
[[ -f ${TF_VAR_TEST_ID}-key.json ]] && gcloud auth activate-service-account --key-file=${TF_VAR_TEST_ID}-key.json --project=sh-automated-tests
61+
$(MAKE) download-cluster-creds
62+
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; }
6963
export KUBECONFIG=${KUBECONFIG} && \
70-
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"
64+
gcloud container clusters get-credentials gp-${TF_VAR_TEST_ID} --zone europe-west1-d --project sh-automated-tests || echo "No cluster present"
7165
rm -f ${TF_VAR_TEST_ID}-key.json
7266

7367
## azure-kubeconfig: Get the kubeconfig configuration for Azure AKS
7468
azure-kubeconfig:
75-
az login --service-principal -u $$ARM_CLIENT_ID -p $$ARM_CLIENT_SECRET --tenant $$ARM_TENANT_ID
69+
[[ -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; }
7670
export KUBECONFIG=${KUBECONFIG} && \
7771
az aks get-credentials --name p$$TF_VAR_TEST_ID-cluster --resource-group p$$TF_VAR_TEST_ID --file ${KUBECONFIG} || echo "No cluster present"
7872

7973
## aws-kubeconfig: Get the kubeconfig configuration for AWS EKS
8074
aws-kubeconfig:
81-
export KUBECONFIG=${KUBECONFIG} && \
75+
gcloud config set project sh-automated-tests
76+
[[ -n $$TF_VAR_sa_creds ]] || gsutil cp gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds ${TF_VAR_TEST_ID}-creds
77+
[[ -f ${TF_VAR_TEST_ID}-creds ]] || touch ${TF_VAR_TEST_ID}-creds
78+
source ${TF_VAR_TEST_ID}-creds; \
8279
aws eks update-kubeconfig --name ${TF_VAR_TEST_ID} --region eu-west-1 --kubeconfig ${KUBECONFIG} || echo "No cluster present"
8380

84-
8581
.PHONY:
8682
## gke-standard-cluster: Creates a zonal GKE cluster
8783
gke-standard-cluster: check-env-cluster-version
@@ -90,8 +86,22 @@ gke-standard-cluster: check-env-cluster-version
9086
rm -f ${KUBECONFIG} && \
9187
$(MAKE) get-kubeconfig && \
9288
[[ -f ${KUBECONFIG} ]] || terraform apply -target=module.gke -var kubeconfig=${KUBECONFIG} --auto-approve
89+
$(MAKE) upload-gcp-cluster-creds
9390
@echo "Done creating GKE cluster"
9491

92+
upload-eks-user:
93+
export AWS_CLUSTER_USER=$$(terraform output -json aws_cluster_user) && \
94+
export USERARN=$$(echo $$AWS_CLUSTER_USER | yq r - 'userarn') && \
95+
export NAME=$$(echo $$AWS_CLUSTER_USER | yq r - 'name') && \
96+
envsubst < ./manifests/aws-auth.yaml > tmp-aws-auth.yaml && \
97+
echo "export AWS_SECRET_ACCESS_KEY=$$(echo $$AWS_CLUSTER_USER | yq r - 'secret_access_key')" > ${TF_VAR_TEST_ID}-creds && \
98+
echo "export AWS_ACCESS_KEY_ID=$$(echo $$AWS_CLUSTER_USER | yq r - 'access_key_id')" >> ${TF_VAR_TEST_ID}-creds && \
99+
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
100+
yq m --inplace /tmp/aws-auth.yaml tmp-aws-auth.yaml
101+
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
102+
gsutil cp ${TF_VAR_TEST_ID}-creds gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds
103+
kubectl --kubeconfig=${KUBECONFIG} replace -f /tmp/aws-auth.yaml
104+
95105
ami_id_121 := "ami-060637af2651bc8bb"
96106

97107
ami_id_122 := "ami-0733d755ed2c97a4d"
@@ -107,6 +117,7 @@ eks-standard-cluster: check-env-cluster-version
107117
rm -f ${KUBECONFIG} && \
108118
$(MAKE) get-kubeconfig && \
109119
[[ -f ${KUBECONFIG} ]] || terraform apply -target=module.eks -var kubeconfig=${KUBECONFIG} -var eks_node_image_id=${ami_id} --auto-approve
120+
$(MAKE) upload-eks-user
110121
@echo "Done creating EKS cluster"
111122

112123
.PHONY:
@@ -156,7 +167,6 @@ k3s-standard-cluster: check-env-cluster-version
156167
$(MAKE) get-kubeconfig && \
157168
[[ -f ${KUBECONFIG} ]] || terraform apply -target=module.k3s -var kubeconfig=${KUBECONFIG} -var k3s_node_image_id=${image_id} --auto-approve && \
158169
$(MAKE) upload-kubeconfig-to-gcp # we upload the file to GCP since we cannot retrieve the file against without SSHing to the master
159-
$(MAKE) upload-gcp-cluster-creds
160170
@echo "Done creating k3s cluster"
161171

162172
.PHONY:
@@ -180,8 +190,10 @@ external-dns: check-env-cloud select-workspace
180190

181191
.PHONY:
182192
## get-kubeconfig: Returns KUBECONFIG of a just created cluster
183-
get-kubeconfig: ${cloud}-kubeconfig
184-
193+
get-kubeconfig:
194+
echo "Getting kubeconfig for $$TF_VAR_TEST_ID terraform state" && \
195+
export provider=$$(echo "$$TF_VAR_TEST_ID" | sed 's/\(.*\)-/\1 /' | xargs | awk '{print $$2}') && \
196+
$(MAKE) $$provider-kubeconfig && echo "kubeconfig written to ${KUBECONFIG}"
185197

186198
get-github-config:
187199
ifneq ($(GITHUB_SCM_OAUTH),)
@@ -233,8 +245,8 @@ registry-config-azure:
233245
yq m -i tmp_config.yml tmp_2_config.yml
234246

235247
storage-config-azure:
236-
export PASSWORD=$$(terraform output -json azure_storage | yq r - 'account_name') && \
237-
export USERNAME=$$(terraform output -json azure_storage | yq r - 'account_key') && \
248+
export USERNAME=$$(terraform output -json azure_storage | yq r - 'account_name') && \
249+
export PASSWORD=$$(terraform output -json azure_storage | yq r - 'account_key') && \
238250
export REGION=$$(terraform output -json azure_storage | yq r - 'storage_region') && \
239251
envsubst < ./manifests/kots-config-azure-storage.yaml > tmp_2_config.yml
240252
yq m -i tmp_config.yml tmp_2_config.yml
@@ -409,7 +421,7 @@ kots-upgrade:
409421
kubectl kots upstream upgrade --kubeconfig=${KUBECONFIG} gitpod -n gitpod --deploy
410422

411423
cloud ?= cluster
412-
cleanup: $(cloud)-kubeconfig destroy-gitpod tf-init destroy-$(cloud) destroy-workspace destroy-kubeconfig
424+
cleanup: get-kubeconfig destroy-gitpod tf-init destroy-$(cloud) destroy-workspace destroy-kubeconfig
413425

414426
cluster-kubeconfig: azure-kubeconfig aws-kubeconfig k3s-kubeconfig gcp-kubeconfig
415427

@@ -421,6 +433,7 @@ destroy-cluster: destroy-gcp destroy-aws destroy-azure
421433
destroy-kubeconfig:
422434
gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS} --project=sh-automated-tests
423435
gsutil rm gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-kubeconfig || echo "No kubeconfig"
436+
gsutil rm gs://nightly-tests/tf-state/${TF_VAR_TEST_ID}-creds || echo "No credentials file"
424437
rm ${KUBECONFIG} || echo "No kubeconfig"
425438

426439
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

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ output "k3s_database" {
1313
value = try(module.k3s.database, null)
1414
}
1515

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

0 commit comments

Comments
 (0)