Skip to content

Commit 4e008fb

Browse files
authored
Merge pull request #57 from kbst/add-support-for-aws-subaccounts
EKS: Add support for cross account roles
2 parents f3667c9 + 6b89a2e commit 4e008fb

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

aws/_modules/eks/cluster_services.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ module "cluster_services" {
88
template_string = "${file("${path.module}/templates/kubeconfig.tpl")}"
99

1010
template_vars = {
11-
cluster_name = "${aws_eks_cluster.current.name}"
12-
cluster_endpoint = "${aws_eks_cluster.current.endpoint}"
13-
cluster_ca = "${aws_eks_cluster.current.certificate_authority.0.data}"
11+
cluster_name = "${aws_eks_cluster.current.name}"
12+
cluster_endpoint = "${aws_eks_cluster.current.endpoint}"
13+
cluster_ca = "${aws_eks_cluster.current.certificate_authority.0.data}"
14+
caller_id_arn = "${local.caller_id_arn}"
15+
caller_id_arn_type = "${local.caller_id_arn_type}"
1416

1517
# hack, because modules can't have depends_on
1618
# prevent a race between kubernetes provider and cluster services/kustomize

aws/_modules/eks/provider.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
data "aws_arn" "current" {
4+
arn = "${data.aws_caller_identity.current.arn}"
5+
}
6+
7+
locals {
8+
resource_split = "${split("/", data.aws_arn.current.resource)}"
9+
caller_id_arn_type = "${replace(element(local.resource_split, 0), "assumed-role", "role")}"
10+
caller_id_name = "${element(local.resource_split, 1)}"
11+
12+
caller_id_arn = "arn:aws:iam::${data.aws_arn.current.account}:${local.caller_id_arn_type}/${local.caller_id_name}"
13+
}
14+
115
data "external" "aws_iam_authenticator" {
216
program = ["sh", "${path.module}/provider_authenticator.sh"]
317

418
query {
5-
cluster_name = "${aws_eks_cluster.current.name}"
19+
cluster_name = "${aws_eks_cluster.current.name}"
20+
caller_id_arn = "${local.caller_id_arn}"
21+
caller_id_arn_type = "${local.caller_id_arn_type}"
622
}
723
}
824

aws/_modules/eks/provider_authenticator.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
set -e
33

44
# Extract cluster name from STDIN
5-
eval "$(jq -r '@sh "CLUSTER_NAME=\(.cluster_name)"')"
5+
eval "$(jq -r '@sh "CLUSTER_NAME=\(.cluster_name) CALLER_ID_ARN=\(.caller_id_arn) CALLER_ID_ARN_TYPE=\(.caller_id_arn_type)"')"
66

77
# Retrieve token with Heptio Authenticator
88
TOKEN=$(aws-iam-authenticator token -i $CLUSTER_NAME | jq -r .status.token)
9+
if [ $CALLER_ID_ARN_TYPE = "role" ]; then
10+
TOKEN=$(aws-iam-authenticator token -i $CLUSTER_NAME --role $CALLER_ID_ARN | jq -r .status.token)
11+
fi
912

1013
# Output token as JSON
1114
jq -n --arg token "$TOKEN" '{"token": $token}'

aws/_modules/eks/templates/kubeconfig.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ users:
2222
- "token"
2323
- "-i"
2424
- "${cluster_name}"
25+
%{ if caller_id_arn_type == "role" }
26+
- "--role"
27+
- "${caller_id_arn}"
28+
%{ endif }

common/cluster_services/kubectl_apply.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
set -e
44

5-
if [ "$(uname -s)" == "Darwin" ]; then
6-
echo "${KUBECONFIG_DATA}" | base64 -D > $KUBECONFIG
7-
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
8-
echo "${KUBECONFIG_DATA}" | base64 -d > $KUBECONFIG
9-
fi
5+
echo "${KUBECONFIG_DATA}" | base64 --decode > $KUBECONFIG
106

117
if [ -s $1 ]
128
then

0 commit comments

Comments
 (0)