Skip to content

Commit 6135f47

Browse files
authored
Merge pull request #933 from wongma7/eksctldeleter
Grant EKSCTL_ADMIN_ROLE admin access to eksctl clusters
2 parents ce6a2e1 + 5e9fb10 commit 6135f47

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ test-e2e-external-eks:
113113
CLUSTER_TYPE=eksctl \
114114
K8S_VERSION="1.20" \
115115
HELM_VALUES_FILE="./hack/values_eksctl.yaml" \
116+
EKSCTL_ADMIN_ROLE="Infra-prod-KopsDeleteAllLambdaServiceRoleF1578477-1ELDFIB4KCMXV" \
116117
AWS_REGION=us-west-2 \
117118
AWS_AVAILABILITY_ZONES=us-west-2a,us-west-2b \
118119
TEST_PATH=./tests/e2e-kubernetes/... \

hack/e2e/ecr.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function ecr_build_and_push() {
1111
IMAGE_NAME=${3}
1212
IMAGE_TAG=${4}
1313
set +e
14-
if docker images | grep "${IMAGE_NAME}" | grep "${IMAGE_TAG}"; then
14+
if docker images --format "{{.Repository}}:{{.Tag}}" | grep "${IMAGE_NAME}:${IMAGE_TAG}"; then
1515
set -e
1616
loudecho "Assuming ${IMAGE_NAME}:${IMAGE_TAG} has been built and pushed"
1717
else

hack/e2e/eksctl.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function eksctl_create_cluster() {
2121
CLUSTER_FILE=${7}
2222
KUBECONFIG=${8}
2323
EKSCTL_PATCH_FILE=${9}
24+
EKSCTL_ADMIN_ROLE=${10}
2425

2526
generate_ssh_key "${SSH_KEY_PATH}"
2627

@@ -55,6 +56,14 @@ function eksctl_create_cluster() {
5556

5657
loudecho "Getting cluster ${CLUSTER_NAME}"
5758
${BIN} get cluster "${CLUSTER_NAME}"
59+
60+
if [ -n "$EKSCTL_ADMIN_ROLE" ]; then
61+
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
62+
ADMIN_ARN="arn:aws:iam::${AWS_ACCOUNT_ID}:role/${EKSCTL_ADMIN_ROLE}"
63+
loudecho "Granting ${ADMIN_ARN} admin access to the cluster"
64+
${BIN} create iamidentitymapping --cluster "${CLUSTER_NAME}" --arn "${ADMIN_ARN}" --group system:masters --username admin
65+
fi
66+
5867
return $?
5968
}
6069

hack/e2e/kops.sh

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ function kops_create_cluster() {
2222
CLUSTER_NAME=${2}
2323
BIN=${3}
2424
ZONES=${4}
25-
INSTANCE_TYPE=${5}
26-
K8S_VERSION=${6}
27-
CLUSTER_FILE=${7}
28-
KUBECONFIG=${8}
29-
KOPS_PATCH_FILE=${9}
30-
KOPS_STATE_FILE=${10}
25+
NODE_COUNT=${5}
26+
INSTANCE_TYPE=${6}
27+
K8S_VERSION=${7}
28+
CLUSTER_FILE=${8}
29+
KUBECONFIG=${9}
30+
KOPS_PATCH_FILE=${10}
31+
KOPS_PATCH_NODE_FILE=${11}
32+
KOPS_STATE_FILE=${12}
3133

3234
generate_ssh_key "${SSH_KEY_PATH}"
3335

@@ -39,15 +41,18 @@ function kops_create_cluster() {
3941
${BIN} create cluster --state "${KOPS_STATE_FILE}" \
4042
--ssh-public-key="${SSH_KEY_PATH}".pub \
4143
--zones "${ZONES}" \
42-
--node-count=3 \
44+
--node-count="${NODE_COUNT}" \
4345
--node-size="${INSTANCE_TYPE}" \
4446
--kubernetes-version="${K8S_VERSION}" \
4547
--dry-run \
46-
-o json \
48+
-o yaml \
4749
"${CLUSTER_NAME}" > "${CLUSTER_FILE}"
4850

4951
if test -f "$KOPS_PATCH_FILE"; then
50-
kops_patch_cluster_file "$CLUSTER_FILE" "$KOPS_PATCH_FILE"
52+
kops_patch_cluster_file "$CLUSTER_FILE" "$KOPS_PATCH_FILE" "Cluster" ""
53+
fi
54+
if test -f "$KOPS_PATCH_NODE_FILE"; then
55+
kops_patch_cluster_file "$CLUSTER_FILE" "$KOPS_PATCH_NODE_FILE" "InstanceGroup" "Node"
5156
fi
5257

5358
loudecho "Creating cluster $CLUSTER_NAME with $CLUSTER_FILE"
@@ -88,36 +93,63 @@ function kops_delete_cluster() {
8893
${BIN} delete cluster --name "${CLUSTER_NAME}" --state "${KOPS_STATE_FILE}" --yes
8994
}
9095

91-
# TODO switch this to python or work exclusively with yaml, all this
92-
# hacking with jq stinks!
96+
# TODO switch this to python, work exclusively with yaml, use kops toolbox
97+
# template/kops set?, all this hacking with jq stinks!
9398
function kops_patch_cluster_file() {
94-
CLUSTER_FILE=${1} # input must be json
99+
CLUSTER_FILE=${1} # input must be yaml
95100
KOPS_PATCH_FILE=${2} # input must be yaml
101+
KIND=${3} # must be either Cluster or InstanceGroup
102+
ROLE=${4} # must be either Master or Node
96103

97104
loudecho "Patching cluster $CLUSTER_NAME with $KOPS_PATCH_FILE"
98105

99-
# Temporary intermediate files for patching
106+
# Temporary intermediate files for patching, don't mutate CLUSTER_FILE until
107+
# the end
108+
CLUSTER_FILE_JSON=$CLUSTER_FILE.json
100109
CLUSTER_FILE_0=$CLUSTER_FILE.0
101110
CLUSTER_FILE_1=$CLUSTER_FILE.1
102111

103-
# Output is an array of Cluster and InstanceGroups
104-
jq '.[] | select(.kind=="Cluster")' "$CLUSTER_FILE" > "$CLUSTER_FILE_0"
112+
# HACK convert the multiple yaml documents to an array of json objects
113+
yaml_to_json "$CLUSTER_FILE" "$CLUSTER_FILE_JSON"
114+
115+
# Find the json objects to patch
116+
FILTER=".[] | select(.kind==\"$KIND\")"
117+
if [ -n "$ROLE" ]; then
118+
FILTER="$FILTER | select(.spec.role==\"$ROLE\")"
119+
fi
120+
jq "$FILTER" "$CLUSTER_FILE_JSON" > "$CLUSTER_FILE_0"
105121

106-
# Patch only the Cluster
122+
# Patch only the json objects
107123
kubectl patch -f "$CLUSTER_FILE_0" --local --type merge --patch "$(cat "$KOPS_PATCH_FILE")" -o json > "$CLUSTER_FILE_1"
108124
mv "$CLUSTER_FILE_1" "$CLUSTER_FILE_0"
109125

110-
# Write the patched Cluster back to the array
111-
jq '(.[] | select(.kind=="Cluster")) = $cluster[0]' "$CLUSTER_FILE" --slurpfile cluster "$CLUSTER_FILE_0" > "$CLUSTER_FILE_1"
126+
# Delete the original json objects, add the patched
127+
# TODO Cluster must always be first?
128+
jq "del($FILTER)" "$CLUSTER_FILE_JSON" | jq ". + \$patched | sort" --slurpfile patched "$CLUSTER_FILE_0" > "$CLUSTER_FILE_1"
112129
mv "$CLUSTER_FILE_1" "$CLUSTER_FILE_0"
113130

114-
# HACK convert the json array to multiple yaml documents
115-
for ((i = 0; i < $(jq length "$CLUSTER_FILE_0"); i++)); do
116-
echo "---" >> "$CLUSTER_FILE_1"
117-
jq ".[$i]" "$CLUSTER_FILE_0" | kubectl patch -f - --local -p "{}" --type merge -o yaml >> "$CLUSTER_FILE_1"
118-
done
131+
# HACK convert the array of json objects to multiple yaml documents
132+
json_to_yaml "$CLUSTER_FILE_0" "$CLUSTER_FILE_1"
119133
mv "$CLUSTER_FILE_1" "$CLUSTER_FILE_0"
120134

121-
# Done patching, overwrite original CLUSTER_FILE
135+
# Done patching, overwrite original yaml CLUSTER_FILE
122136
mv "$CLUSTER_FILE_0" "$CLUSTER_FILE" # output is yaml
137+
138+
# Clean up
139+
rm "$CLUSTER_FILE_JSON"
140+
}
141+
142+
function yaml_to_json() {
143+
IN=${1}
144+
OUT=${2}
145+
kubectl patch -f "$IN" --local -p "{}" --type merge -o json | jq '.' -s > "$OUT"
146+
}
147+
148+
function json_to_yaml() {
149+
IN=${1}
150+
OUT=${2}
151+
for ((i = 0; i < $(jq length "$IN"); i++)); do
152+
echo "---" >> "$OUT"
153+
jq ".[$i]" "$IN" | kubectl patch -f - --local -p "{}" --type merge -o yaml >> "$OUT"
154+
done
123155
}

hack/e2e/run.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ CLUSTER_TYPE=${CLUSTER_TYPE:-kops}
3535
TEST_DIR=${BASE_DIR}/csi-test-artifacts
3636
BIN_DIR=${TEST_DIR}/bin
3737
SSH_KEY_PATH=${TEST_DIR}/id_rsa
38-
CLUSTER_FILE=${TEST_DIR}/${CLUSTER_NAME}.${CLUSTER_TYPE}.json
38+
CLUSTER_FILE=${TEST_DIR}/${CLUSTER_NAME}.${CLUSTER_TYPE}.yaml
3939
KUBECONFIG=${KUBECONFIG:-"${TEST_DIR}/${CLUSTER_NAME}.${CLUSTER_TYPE}.kubeconfig"}
4040

4141
REGION=${AWS_REGION:-us-west-2}
4242
ZONES=${AWS_AVAILABILITY_ZONES:-us-west-2a,us-west-2b,us-west-2c}
4343
FIRST_ZONE=$(echo "${ZONES}" | cut -d, -f1)
44+
NODE_COUNT=${NODE_COUNT:-3}
4445
INSTANCE_TYPE=${INSTANCE_TYPE:-c4.large}
4546

4647
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
@@ -54,8 +55,10 @@ K8S_VERSION=${K8S_VERSION:-1.20.6}
5455
KOPS_VERSION=${KOPS_VERSION:-1.20.0}
5556
KOPS_STATE_FILE=${KOPS_STATE_FILE:-s3://k8s-kops-csi-e2e}
5657
KOPS_PATCH_FILE=${KOPS_PATCH_FILE:-./hack/kops-patch.yaml}
58+
KOPS_PATCH_NODE_FILE=${KOPS_PATCH_NODE_FILE:-./hack/kops-patch-node.yaml}
5759

5860
EKSCTL_PATCH_FILE=${EKSCTL_PATCH_FILE:-./hack/eksctl-patch.yaml}
61+
EKSCTL_ADMIN_ROLE=${EKSCTL_ADMIN_ROLE:-}
5962

6063
HELM_VALUES_FILE=${HELM_VALUES_FILE:-./hack/values.yaml}
6164

@@ -111,11 +114,13 @@ if [[ "${CLUSTER_TYPE}" == "kops" ]]; then
111114
"$CLUSTER_NAME" \
112115
"$KOPS_BIN" \
113116
"$ZONES" \
117+
"$NODE_COUNT" \
114118
"$INSTANCE_TYPE" \
115119
"$K8S_VERSION" \
116120
"$CLUSTER_FILE" \
117121
"$KUBECONFIG" \
118122
"$KOPS_PATCH_FILE" \
123+
"$KOPS_PATCH_NODE_FILE" \
119124
"$KOPS_STATE_FILE"
120125
if [[ $? -ne 0 ]]; then
121126
exit 1
@@ -130,7 +135,8 @@ elif [[ "${CLUSTER_TYPE}" == "eksctl" ]]; then
130135
"$K8S_VERSION" \
131136
"$CLUSTER_FILE" \
132137
"$KUBECONFIG" \
133-
"$EKSCTL_PATCH_FILE"
138+
"$EKSCTL_PATCH_FILE" \
139+
"$EKSCTL_ADMIN_ROLE"
134140
if [[ $? -ne 0 ]]; then
135141
exit 1
136142
fi

0 commit comments

Comments
 (0)